* config/tc-mn10300.c (md_assemble): Copy size to real_size before
[deliverable/binutils-gdb.git] / gdb / coffread.c
CommitLineData
c906108c
SS
1/* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 88, 89, 90, 91, 92, 93, 94, 96, 97, 1998
c5aa993b 3 Free Software Foundation, Inc.
c906108c
SS
4 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "demangle.h"
27#include "breakpoint.h"
28
29#include "bfd.h"
30#include "obstack.h"
31
32#include "gdb_string.h"
33#include <ctype.h>
34
35#include "coff/internal.h" /* Internal format of COFF symbols in BFD */
36#include "libcoff.h" /* FIXME secret internal data from BFD */
37
38#include "symfile.h"
39#include "objfiles.h"
40#include "buildsym.h"
41#include "gdb-stabs.h"
42#include "stabsread.h"
43#include "complaints.h"
44#include "target.h"
45
392a587b
JM
46extern void _initialize_coffread PARAMS ((void));
47
c5aa993b
JM
48struct coff_symfile_info
49 {
50 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
51 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
c906108c 52
c5aa993b
JM
53 CORE_ADDR textaddr; /* Addr of .text section. */
54 unsigned int textsize; /* Size of .text section. */
55 struct stab_section_list *stabsects; /* .stab sections. */
56 asection *stabstrsect; /* Section pointer for .stab section */
57 char *stabstrdata;
58 };
c906108c
SS
59
60/* Translate an external name string into a user-visible name. */
61#define EXTERNAL_NAME(string, abfd) \
62 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
63
64/* To be an sdb debug type, type must have at least a basic or primary
65 derived type. Using this rather than checking against T_NULL is
66 said to prevent core dumps if we try to operate on Michael Bloom
67 dbx-in-coff file. */
68
69#define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
70
71/* Convert from an sdb register number to an internal gdb register number.
72 This should be defined in tm.h, if REGISTER_NAMES is not set up
73 to map one to one onto the sdb register numbers. */
74
75#ifndef SDB_REG_TO_REGNUM
c5aa993b 76#define SDB_REG_TO_REGNUM(value) (value)
c906108c
SS
77#endif
78
79/* Core address of start and end of text of current source file.
80 This comes from a ".text" symbol where x_nlinno > 0. */
81
82static CORE_ADDR current_source_start_addr;
83static CORE_ADDR current_source_end_addr;
84
85/* The addresses of the symbol table stream and number of symbols
86 of the object file we are reading (as copied into core). */
87
88static bfd *nlist_bfd_global;
89static int nlist_nsyms_global;
90
c906108c
SS
91
92/* Pointers to scratch storage, used for reading raw symbols and auxents. */
93
94static char *temp_sym;
95static char *temp_aux;
96
97/* Local variables that hold the shift and mask values for the
98 COFF file that we are currently reading. These come back to us
99 from BFD, and are referenced by their macro names, as well as
100 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
101 macros from include/coff/internal.h . */
102
c5aa993b
JM
103static unsigned local_n_btmask;
104static unsigned local_n_btshft;
105static unsigned local_n_tmask;
106static unsigned local_n_tshift;
c906108c
SS
107
108#define N_BTMASK local_n_btmask
109#define N_BTSHFT local_n_btshft
110#define N_TMASK local_n_tmask
111#define N_TSHIFT local_n_tshift
c5aa993b 112
c906108c
SS
113/* Local variables that hold the sizes in the file of various COFF structures.
114 (We only need to know this to read them from the file -- BFD will then
115 translate the data in them, into `internal_xxx' structs in the right
116 byte order, alignment, etc.) */
117
c5aa993b
JM
118static unsigned local_linesz;
119static unsigned local_symesz;
120static unsigned local_auxesz;
c906108c
SS
121
122/* This is set if this is a PE format file. */
123
124static int pe_file;
125
126/* Chain of typedefs of pointers to empty struct/union types.
127 They are chained thru the SYMBOL_VALUE_CHAIN. */
128
129static struct symbol *opaque_type_chain[HASHSIZE];
130
131/* Complaints about various problems in the file being read */
132
c5aa993b
JM
133struct complaint ef_complaint =
134{"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
c906108c 135
c5aa993b
JM
136struct complaint ef_stack_complaint =
137{"`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d", 0, 0};
c906108c 138
c5aa993b
JM
139struct complaint eb_stack_complaint =
140{"`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d", 0, 0};
c906108c
SS
141
142struct complaint bf_no_aux_complaint =
c5aa993b 143{"`.bf' symbol %d has no aux entry", 0, 0};
c906108c
SS
144
145struct complaint ef_no_aux_complaint =
c5aa993b 146{"`.ef' symbol %d has no aux entry", 0, 0};
c906108c
SS
147
148struct complaint lineno_complaint =
c5aa993b 149{"Line number pointer %d lower than start of line numbers", 0, 0};
c906108c
SS
150
151struct complaint unexpected_type_complaint =
c5aa993b 152{"Unexpected type for symbol %s", 0, 0};
c906108c
SS
153
154struct complaint bad_sclass_complaint =
c5aa993b 155{"Bad n_sclass for symbol %s", 0, 0};
c906108c
SS
156
157struct complaint misordered_blocks_complaint =
c5aa993b 158{"Blocks out of order at address %x", 0, 0};
c906108c
SS
159
160struct complaint tagndx_bad_complaint =
c5aa993b 161{"Symbol table entry for %s has bad tagndx value", 0, 0};
c906108c 162
c5aa993b
JM
163struct complaint eb_complaint =
164{"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
c906108c
SS
165
166/* Simplified internal version of coff symbol table information */
167
c5aa993b
JM
168struct coff_symbol
169 {
170 char *c_name;
171 int c_symnum; /* symbol number of this entry */
172 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
173 long c_value;
174 int c_sclass;
175 int c_secnum;
176 unsigned int c_type;
177 };
c906108c 178
7be570e7
JM
179extern void stabsread_clear_cache PARAMS ((void));
180
c906108c
SS
181static struct type *coff_read_struct_type PARAMS ((int, int, int));
182
183static struct type *decode_base_type PARAMS ((struct coff_symbol *,
184 unsigned int,
185 union internal_auxent *));
186
187static struct type *decode_type PARAMS ((struct coff_symbol *, unsigned int,
188 union internal_auxent *));
189
190static struct type *decode_function_type PARAMS ((struct coff_symbol *,
191 unsigned int,
192 union internal_auxent *));
193
194static struct type *coff_read_enum_type PARAMS ((int, int, int));
195
196static struct symbol *process_coff_symbol PARAMS ((struct coff_symbol *,
197 union internal_auxent *,
c906108c
SS
198 struct objfile *));
199
200static void patch_opaque_types PARAMS ((struct symtab *));
201
202static void patch_type PARAMS ((struct type *, struct type *));
203
d4f3574e 204static void enter_linenos PARAMS ((long, int, int, struct objfile *));
c906108c
SS
205
206static void free_linetab PARAMS ((void));
207
74b7792f
AC
208static void free_linetab_cleanup (void *ignore);
209
c906108c
SS
210static int init_lineno PARAMS ((bfd *, long, int));
211
212static char *getsymname PARAMS ((struct internal_syment *));
213
214static char *coff_getfilename PARAMS ((union internal_auxent *));
215
216static void free_stringtab PARAMS ((void));
217
74b7792f
AC
218static void free_stringtab_cleanup (void *ignore);
219
c906108c
SS
220static int init_stringtab PARAMS ((bfd *, long));
221
222static void read_one_sym PARAMS ((struct coff_symbol *,
223 struct internal_syment *,
224 union internal_auxent *));
225
745b8ca0 226static void coff_symtab_read PARAMS ((long, unsigned int, struct objfile *));
c906108c
SS
227
228static void find_linenos PARAMS ((bfd *, sec_ptr, PTR));
229
230static void coff_symfile_init PARAMS ((struct objfile *));
231
232static void coff_new_init PARAMS ((struct objfile *));
233
96baa820 234static void coff_symfile_read PARAMS ((struct objfile *, int));
c906108c
SS
235
236static void coff_symfile_finish PARAMS ((struct objfile *));
237
238static void record_minimal_symbol PARAMS ((char *, CORE_ADDR,
239 enum minimal_symbol_type,
240 struct objfile *));
241
242static void coff_end_symtab PARAMS ((struct objfile *));
243
244static void complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
245
246static void coff_start_symtab PARAMS ((char *));
247
c906108c
SS
248static struct type *coff_alloc_type PARAMS ((int));
249
250static struct type **coff_lookup_type PARAMS ((int));
251
252static void coff_locate_sections PARAMS ((bfd *, asection *, PTR));
253\f
254/* We are called once per section from coff_symfile_read. We
255 need to examine each section we are passed, check to see
256 if it is something we are interested in processing, and
257 if so, stash away some access information for the section.
258
259 FIXME: The section names should not be hardwired strings (what
260 should they be? I don't think most object file formats have enough
261 section flags to specify what kind of debug section it is
262 -kingdon). */
263
264static void
265coff_locate_sections (abfd, sectp, csip)
266 bfd *abfd;
267 asection *sectp;
268 PTR csip;
269{
270 register struct coff_symfile_info *csi;
271 const char *name;
272
273 csi = (struct coff_symfile_info *) csip;
274 name = bfd_get_section_name (abfd, sectp);
275 if (STREQ (name, ".text"))
276 {
277 csi->textaddr = bfd_section_vma (abfd, sectp);
278 csi->textsize += bfd_section_size (abfd, sectp);
279 }
280 else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
281 {
282 csi->textsize += bfd_section_size (abfd, sectp);
283 }
284 else if (STREQ (name, ".stabstr"))
285 {
286 csi->stabstrsect = sectp;
287 }
288 else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
289 {
290 const char *s;
291
292 /* We can have multiple .stab sections if linked with
293 --split-by-reloc. */
294 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
c5aa993b 295 if (!isdigit (*s))
c906108c
SS
296 break;
297 if (*s == '\0')
298 {
299 struct stab_section_list *n, **pn;
300
301 n = ((struct stab_section_list *)
302 xmalloc (sizeof (struct stab_section_list)));
303 n->section = sectp;
304 n->next = NULL;
305 for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
306 ;
307 *pn = n;
308
309 /* This will be run after coffstab_build_psymtabs is called
c5aa993b
JM
310 in coff_symfile_read, at which point we no longer need
311 the information. */
c906108c
SS
312 make_cleanup (free, n);
313 }
314 }
315}
316
317/* Return the section_offsets* that CS points to. */
318static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
319
c5aa993b
JM
320struct find_targ_sec_arg
321 {
322 int targ_index;
323 asection **resultp;
324 };
c906108c
SS
325
326static void find_targ_sec PARAMS ((bfd *, asection *, void *));
327
c5aa993b
JM
328static void
329find_targ_sec (abfd, sect, obj)
c906108c
SS
330 bfd *abfd;
331 asection *sect;
332 PTR obj;
333{
c5aa993b 334 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
c906108c
SS
335 if (sect->target_index == args->targ_index)
336 *args->resultp = sect;
337}
338
339/* Return the section number (SECT_OFF_*) that CS points to. */
340static int
341cs_to_section (cs, objfile)
342 struct coff_symbol *cs;
343 struct objfile *objfile;
344{
345 asection *sect = NULL;
346 struct find_targ_sec_arg args;
b8fbeb18 347 int off = SECT_OFF_TEXT (objfile);
c906108c
SS
348
349 args.targ_index = cs->c_secnum;
350 args.resultp = &sect;
351 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
352 if (sect != NULL)
353 {
354 /* This is the section. Figure out what SECT_OFF_* code it is. */
355 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
b8fbeb18 356 off = SECT_OFF_TEXT (objfile);
c906108c 357 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
b8fbeb18 358 off = SECT_OFF_DATA (objfile);
c906108c 359 else
b8fbeb18 360 off = SECT_OFF_BSS (objfile);
c906108c
SS
361 }
362 return off;
363}
364
365/* Return the address of the section of a COFF symbol. */
366
367static CORE_ADDR cs_section_address PARAMS ((struct coff_symbol *, bfd *));
368
369static CORE_ADDR
370cs_section_address (cs, abfd)
371 struct coff_symbol *cs;
372 bfd *abfd;
373{
374 asection *sect = NULL;
375 struct find_targ_sec_arg args;
376 CORE_ADDR addr = 0;
377
378 args.targ_index = cs->c_secnum;
379 args.resultp = &sect;
380 bfd_map_over_sections (abfd, find_targ_sec, &args);
381 if (sect != NULL)
382 addr = bfd_get_section_vma (objfile->obfd, sect);
383 return addr;
384}
385
386/* Look up a coff type-number index. Return the address of the slot
387 where the type for that index is stored.
388 The type-number is in INDEX.
389
390 This can be used for finding the type associated with that index
391 or for associating a new type with the index. */
392
393static struct type **
394coff_lookup_type (index)
395 register int index;
396{
397 if (index >= type_vector_length)
398 {
399 int old_vector_length = type_vector_length;
400
401 type_vector_length *= 2;
c5aa993b 402 if (index /* is still */ >= type_vector_length)
c906108c
SS
403 type_vector_length = index * 2;
404
405 type_vector = (struct type **)
406 xrealloc ((char *) type_vector,
407 type_vector_length * sizeof (struct type *));
408 memset (&type_vector[old_vector_length], 0,
c5aa993b 409 (type_vector_length - old_vector_length) * sizeof (struct type *));
c906108c
SS
410 }
411 return &type_vector[index];
412}
413
414/* Make sure there is a type allocated for type number index
415 and return the type object.
416 This can create an empty (zeroed) type object. */
417
418static struct type *
419coff_alloc_type (index)
420 int index;
421{
422 register struct type **type_addr = coff_lookup_type (index);
423 register struct type *type = *type_addr;
424
425 /* If we are referring to a type not known at all yet,
426 allocate an empty type for it.
427 We will fill it in later if we find out how. */
428 if (type == NULL)
429 {
430 type = alloc_type (current_objfile);
431 *type_addr = type;
432 }
433 return type;
434}
435\f
c906108c
SS
436/* Start a new symtab for a new source file.
437 This is called when a COFF ".file" symbol is seen;
438 it indicates the start of data for one original source file. */
439
440static void
441coff_start_symtab (name)
c5aa993b 442 char *name;
c906108c
SS
443{
444 start_symtab (
c5aa993b
JM
445 /* We fill in the filename later. start_symtab puts
446 this pointer into last_source_file and we put it in
447 subfiles->name, which end_symtab frees; that's why
448 it must be malloc'd. */
449 savestring (name, strlen (name)),
450 /* We never know the directory name for COFF. */
451 NULL,
452 /* The start address is irrelevant, since we set
453 last_source_start_addr in coff_end_symtab. */
454 0);
c906108c 455 record_debugformat ("COFF");
c906108c
SS
456}
457
458/* Save the vital information from when starting to read a file,
459 for use when closing off the current file.
460 NAME is the file name the symbols came from, START_ADDR is the first
461 text address for the file, and SIZE is the number of bytes of text. */
462
463static void
464complete_symtab (name, start_addr, size)
c5aa993b
JM
465 char *name;
466 CORE_ADDR start_addr;
467 unsigned int size;
c906108c
SS
468{
469 if (last_source_file != NULL)
470 free (last_source_file);
471 last_source_file = savestring (name, strlen (name));
472 current_source_start_addr = start_addr;
473 current_source_end_addr = start_addr + size;
474
c5aa993b
JM
475 if (current_objfile->ei.entry_point >= current_source_start_addr &&
476 current_objfile->ei.entry_point < current_source_end_addr)
c906108c 477 {
c5aa993b
JM
478 current_objfile->ei.entry_file_lowpc = current_source_start_addr;
479 current_objfile->ei.entry_file_highpc = current_source_end_addr;
c906108c
SS
480 }
481}
482
483/* Finish the symbol definitions for one main source file,
484 close off all the lexical contexts for that file
485 (creating struct block's for them), then make the
486 struct symtab for that file and put it in the list of all such. */
487
488static void
489coff_end_symtab (objfile)
490 struct objfile *objfile;
491{
492 struct symtab *symtab;
493
494 last_source_start_addr = current_source_start_addr;
495
c906108c
SS
496 symtab = end_symtab (current_source_end_addr, objfile, 0);
497
498 if (symtab != NULL)
499 free_named_symtabs (symtab->filename);
500
501 /* Reinitialize for beginning of new file. */
c906108c
SS
502 last_source_file = NULL;
503}
504\f
505static void
506record_minimal_symbol (name, address, type, objfile)
507 char *name;
508 CORE_ADDR address;
509 enum minimal_symbol_type type;
510 struct objfile *objfile;
511{
512 /* We don't want TDESC entry points in the minimal symbol table */
c5aa993b
JM
513 if (name[0] == '@')
514 return;
c906108c
SS
515
516 prim_record_minimal_symbol (name, address, type, objfile);
517}
518\f
519/* coff_symfile_init ()
520 is the coff-specific initialization routine for reading symbols.
521 It is passed a struct objfile which contains, among other things,
522 the BFD for the file whose symbols are being read, and a slot for
523 a pointer to "private data" which we fill with cookies and other
524 treats for coff_symfile_read ().
525
526 We will only be called if this is a COFF or COFF-like file.
527 BFD handles figuring out the format of the file, and code in symtab.c
528 uses BFD's determination to vector to us.
529
530 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
531
532static void
533coff_symfile_init (objfile)
534 struct objfile *objfile;
535{
536 /* Allocate struct to keep track of stab reading. */
537 objfile->sym_stab_info = (struct dbx_symfile_info *)
538 xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
539
540 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
541
542 /* Allocate struct to keep track of the symfile */
543 objfile->sym_private = xmmalloc (objfile->md,
544 sizeof (struct coff_symfile_info));
545
546 memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
547
548 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
549 find this causes a significant slowdown in gdb then we could
550 set it in the debug symbol readers only when necessary. */
551 objfile->flags |= OBJF_REORDERED;
552
553 init_entry_point_info (objfile);
554}
555
556/* This function is called for every section; it finds the outer limits
557 of the line table (minimum and maximum file offset) so that the
558 mainline code can read the whole thing for efficiency. */
559
560/* ARGSUSED */
561static void
562find_linenos (abfd, asect, vpinfo)
563 bfd *abfd;
564 sec_ptr asect;
565 PTR vpinfo;
566{
567 struct coff_symfile_info *info;
568 int size, count;
569 file_ptr offset, maxoff;
570
571/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
572 count = asect->lineno_count;
573/* End of warning */
574
575 if (count == 0)
576 return;
577 size = count * local_linesz;
578
c5aa993b 579 info = (struct coff_symfile_info *) vpinfo;
c906108c
SS
580/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
581 offset = asect->line_filepos;
582/* End of warning */
583
584 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
585 info->min_lineno_offset = offset;
586
587 maxoff = offset + size;
588 if (maxoff > info->max_lineno_offset)
589 info->max_lineno_offset = maxoff;
590}
591
592
593/* The BFD for this file -- only good while we're actively reading
594 symbols into a psymtab or a symtab. */
595
596static bfd *symfile_bfd;
597
598/* Read a symbol file, after initialization by coff_symfile_init. */
599
600/* ARGSUSED */
601static void
96baa820 602coff_symfile_read (objfile, mainline)
c906108c 603 struct objfile *objfile;
c906108c
SS
604 int mainline;
605{
606 struct coff_symfile_info *info;
607 struct dbx_symfile_info *dbxinfo;
608 bfd *abfd = objfile->obfd;
609 coff_data_type *cdata = coff_data (abfd);
610 char *name = bfd_get_filename (abfd);
611 register int val;
745b8ca0 612 unsigned int num_symbols;
c906108c
SS
613 int symtab_offset;
614 int stringtab_offset;
615 struct cleanup *back_to;
616 int stabstrsize;
c2d11a7d
JM
617 int len;
618 char * target;
619
c5aa993b 620 info = (struct coff_symfile_info *) objfile->sym_private;
c906108c 621 dbxinfo = objfile->sym_stab_info;
c5aa993b 622 symfile_bfd = abfd; /* Kludge for swap routines */
c906108c
SS
623
624/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
c5aa993b
JM
625 num_symbols = bfd_get_symcount (abfd); /* How many syms */
626 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
627 stringtab_offset = symtab_offset + /* String table file offset */
628 num_symbols * cdata->local_symesz;
c906108c
SS
629
630 /* Set a few file-statics that give us specific information about
631 the particular COFF file format we're reading. */
c906108c
SS
632 local_n_btmask = cdata->local_n_btmask;
633 local_n_btshft = cdata->local_n_btshft;
c5aa993b 634 local_n_tmask = cdata->local_n_tmask;
c906108c 635 local_n_tshift = cdata->local_n_tshift;
c5aa993b
JM
636 local_linesz = cdata->local_linesz;
637 local_symesz = cdata->local_symesz;
638 local_auxesz = cdata->local_auxesz;
c906108c
SS
639
640 /* Allocate space for raw symbol and aux entries, based on their
641 space requirements as reported by BFD. */
642 temp_sym = (char *) xmalloc
c5aa993b 643 (cdata->local_symesz + cdata->local_auxesz);
c906108c 644 temp_aux = temp_sym + cdata->local_symesz;
c13c43fd 645 back_to = make_cleanup (free_current_contents, &temp_sym);
c906108c
SS
646
647 /* We need to know whether this is a PE file, because in PE files,
648 unlike standard COFF files, symbol values are stored as offsets
649 from the section address, rather than as absolute addresses.
650 FIXME: We should use BFD to read the symbol table, and thus avoid
651 this problem. */
0d06e24b
JM
652 pe_file =
653 strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0
654 || strncmp (bfd_get_target (objfile->obfd), "epoc-pe", 7) == 0;
c906108c
SS
655
656/* End of warning */
657
658 /* Read the line number table, all at once. */
659 info->min_lineno_offset = 0;
660 info->max_lineno_offset = 0;
661 bfd_map_over_sections (abfd, find_linenos, (PTR) info);
662
74b7792f 663 make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
c5aa993b 664 val = init_lineno (abfd, info->min_lineno_offset,
c906108c
SS
665 info->max_lineno_offset - info->min_lineno_offset);
666 if (val < 0)
667 error ("\"%s\": error reading line numbers\n", name);
668
669 /* Now read the string table, all at once. */
670
74b7792f 671 make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
c906108c
SS
672 val = init_stringtab (abfd, stringtab_offset);
673 if (val < 0)
674 error ("\"%s\": can't get string table", name);
675
676 init_minimal_symbol_collection ();
56e290f4 677 make_cleanup_discard_minimal_symbols ();
c906108c
SS
678
679 /* Now that the executable file is positioned at symbol table,
680 process it and define symbols accordingly. */
681
96baa820 682 coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
c906108c
SS
683
684 /* Sort symbols alphabetically within each block. */
685
686 {
687 struct symtab *s;
688
c5aa993b 689 for (s = objfile->symtabs; s != NULL; s = s->next)
c906108c
SS
690 sort_symtab_syms (s);
691 }
692
693 /* Install any minimal symbols that have been collected as the current
694 minimal symbols for this objfile. */
695
696 install_minimal_symbols (objfile);
697
698 bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
699
700 if (info->stabsects)
701 {
c5aa993b 702 if (!info->stabstrsect)
b83266a0
SS
703 {
704 error_begin ();
705 fprintf_filtered
706 (gdb_stderr,
707 ("The debugging information in `%s' is corrupted.\n"
c5aa993b 708 "The file has a `.stabs' section, but no `.stabstr' section.\n"),
b83266a0
SS
709 name);
710 return_to_top_level (RETURN_ERROR);
711 }
712
c906108c 713 /* FIXME: dubious. Why can't we use something normal like
c5aa993b 714 bfd_get_section_contents? */
c906108c
SS
715 bfd_seek (abfd, abfd->where, 0);
716
717 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
718
719 coffstab_build_psymtabs (objfile,
c906108c
SS
720 mainline,
721 info->textaddr, info->textsize,
722 info->stabsects,
723 info->stabstrsect->filepos, stabstrsize);
724 }
725
726 do_cleanups (back_to);
727}
728
729static void
730coff_new_init (ignore)
731 struct objfile *ignore;
732{
733}
734
735/* Perform any local cleanups required when we are done with a particular
736 objfile. I.E, we are in the process of discarding all symbol information
737 for an objfile, freeing up all memory held for it, and unlinking the
738 objfile struct from the global list of known objfiles. */
739
740static void
741coff_symfile_finish (objfile)
742 struct objfile *objfile;
743{
c5aa993b 744 if (objfile->sym_private != NULL)
c906108c 745 {
c5aa993b 746 mfree (objfile->md, objfile->sym_private);
c906108c 747 }
7be570e7
JM
748
749 /* Let stabs reader clean up */
750 stabsread_clear_cache ();
c906108c 751}
c906108c 752\f
c5aa993b 753
c906108c
SS
754/* Given pointers to a symbol table in coff style exec file,
755 analyze them and create struct symtab's describing the symbols.
756 NSYMS is the number of symbols in the symbol table.
757 We read them one at a time using read_one_sym (). */
758
759static void
96baa820 760coff_symtab_read (symtab_offset, nsyms, objfile)
c906108c 761 long symtab_offset;
745b8ca0 762 unsigned int nsyms;
c906108c
SS
763 struct objfile *objfile;
764{
765 register struct context_stack *new;
766 struct coff_symbol coff_symbol;
767 register struct coff_symbol *cs = &coff_symbol;
768 static struct internal_syment main_sym;
769 static union internal_auxent main_aux;
770 struct coff_symbol fcn_cs_saved;
771 static struct internal_syment fcn_sym_saved;
772 static union internal_auxent fcn_aux_saved;
773 struct symtab *s;
774 /* A .file is open. */
775 int in_source_file = 0;
776 int next_file_symnum = -1;
777 /* Name of the current file. */
778 char *filestring = "";
779 int depth = 0;
780 int fcn_first_line = 0;
cd0fc7c3 781 CORE_ADDR fcn_first_line_addr;
c906108c
SS
782 int fcn_last_line = 0;
783 int fcn_start_addr = 0;
784 long fcn_line_ptr = 0;
785 int val;
786 CORE_ADDR tmpaddr;
787
788 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
789 it's hard to know I've really worked around it. The fix should be
790 harmless, anyway). The symptom of the bug is that the first
791 fread (in read_one_sym), will (in my example) actually get data
792 from file offset 268, when the fseek was to 264 (and ftell shows
793 264). This causes all hell to break loose. I was unable to
794 reproduce this on a short test program which operated on the same
795 file, performing (I think) the same sequence of operations.
796
797 It stopped happening when I put in this (former) rewind().
798
799 FIXME: Find out if this has been reported to Sun, whether it has
800 been fixed in a later release, etc. */
801
802 bfd_seek (objfile->obfd, 0, 0);
803
804 /* Position to read the symbol table. */
805 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
806 if (val < 0)
807 perror_with_name (objfile->name);
808
809 current_objfile = objfile;
810 nlist_bfd_global = objfile->obfd;
811 nlist_nsyms_global = nsyms;
812 last_source_file = NULL;
813 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
814
c5aa993b 815 if (type_vector) /* Get rid of previous one */
c906108c
SS
816 free ((PTR) type_vector);
817 type_vector_length = 160;
818 type_vector = (struct type **)
819 xmalloc (type_vector_length * sizeof (struct type *));
820 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
821
822 coff_start_symtab ("");
823
824 symnum = 0;
825 while (symnum < nsyms)
826 {
827 QUIT; /* Make this command interruptable. */
828
829 read_one_sym (cs, &main_sym, &main_aux);
830
831 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
832 {
833 if (last_source_file)
834 coff_end_symtab (objfile);
835
836 coff_start_symtab ("_globals_");
837 complete_symtab ("_globals_", 0, 0);
838 /* done with all files, everything from here on out is globals */
839 }
840
841 /* Special case for file with type declarations only, no text. */
842 if (!last_source_file && SDB_TYPE (cs->c_type)
843 && cs->c_secnum == N_DEBUG)
844 complete_symtab (filestring, 0, 0);
845
846 /* Typedefs should not be treated as symbol definitions. */
847 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
848 {
849 /* Record all functions -- external and static -- in minsyms. */
b8fbeb18 850 tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
851 record_minimal_symbol (cs->c_name, tmpaddr, mst_text, objfile);
852
853 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
854 fcn_start_addr = tmpaddr;
855 fcn_cs_saved = *cs;
856 fcn_sym_saved = main_sym;
857 fcn_aux_saved = main_aux;
858 continue;
859 }
860
861 switch (cs->c_sclass)
862 {
c5aa993b
JM
863 case C_EFCN:
864 case C_EXTDEF:
865 case C_ULABEL:
866 case C_USTATIC:
867 case C_LINE:
868 case C_ALIAS:
869 case C_HIDDEN:
870 complain (&bad_sclass_complaint, cs->c_name);
871 break;
c906108c 872
c5aa993b
JM
873 case C_FILE:
874 /* c_value field contains symnum of next .file entry in table
875 or symnum of first global after last .file. */
876 next_file_symnum = cs->c_value;
877 if (cs->c_naux > 0)
878 filestring = coff_getfilename (&main_aux);
879 else
880 filestring = "";
881
882 /* Complete symbol table for last object file
883 containing debugging information. */
884 if (last_source_file)
885 {
886 coff_end_symtab (objfile);
887 coff_start_symtab (filestring);
888 }
889 in_source_file = 1;
890 break;
c906108c
SS
891
892 /* C_LABEL is used for labels and static functions. Including
893 it here allows gdb to see static functions when no debug
894 info is available. */
c5aa993b
JM
895 case C_LABEL:
896 /* However, labels within a function can make weird backtraces,
897 so filter them out (from phdm@macqel.be). */
898 if (within_function)
899 break;
900 case C_STAT:
901 case C_THUMBLABEL:
902 case C_THUMBSTAT:
903 case C_THUMBSTATFUNC:
904 if (cs->c_name[0] == '.')
905 {
906 if (STREQ (cs->c_name, ".text"))
907 {
c906108c
SS
908 /* FIXME: don't wire in ".text" as section name
909 or symbol name! */
910 /* Check for in_source_file deals with case of
911 a file with debugging symbols
912 followed by a later file with no symbols. */
913 if (in_source_file)
914 complete_symtab (filestring,
b8fbeb18 915 cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)),
c906108c
SS
916 main_aux.x_scn.x_scnlen);
917 in_source_file = 0;
918 }
c5aa993b 919 /* flush rest of '.' symbols */
c906108c 920 break;
c5aa993b
JM
921 }
922 else if (!SDB_TYPE (cs->c_type)
923 && cs->c_name[0] == 'L'
924 && (strncmp (cs->c_name, "LI%", 3) == 0
925 || strncmp (cs->c_name, "LF%", 3) == 0
926 || strncmp (cs->c_name, "LC%", 3) == 0
927 || strncmp (cs->c_name, "LP%", 3) == 0
928 || strncmp (cs->c_name, "LPB%", 4) == 0
929 || strncmp (cs->c_name, "LBB%", 4) == 0
930 || strncmp (cs->c_name, "LBE%", 4) == 0
931 || strncmp (cs->c_name, "LPBX%", 5) == 0))
932 /* At least on a 3b1, gcc generates swbeg and string labels
933 that look like this. Ignore them. */
934 break;
935 /* fall in for static symbols that don't start with '.' */
936 case C_THUMBEXT:
937 case C_THUMBEXTFUNC:
938 case C_EXT:
939 {
940 /* Record it in the minimal symbols regardless of
941 SDB_TYPE. This parallels what we do for other debug
942 formats, and probably is needed to make
943 print_address_symbolic work right without the (now
944 gone) "set fast-symbolic-addr off" kludge. */
c906108c 945
c5aa993b
JM
946 /* FIXME: should use mst_abs, and not relocate, if absolute. */
947 enum minimal_symbol_type ms_type;
948 int sec;
c906108c 949
c5aa993b
JM
950 if (cs->c_secnum == N_UNDEF)
951 {
952 /* This is a common symbol. See if the target
953 environment knows where it has been relocated to. */
954 CORE_ADDR reladdr;
955 if (target_lookup_symbol (cs->c_name, &reladdr))
956 {
957 /* Error in lookup; ignore symbol. */
958 break;
959 }
960 tmpaddr = reladdr;
961 /* The address has already been relocated; make sure that
962 objfile_relocate doesn't relocate it again. */
963 sec = -2;
964 ms_type = cs->c_sclass == C_EXT
965 || cs->c_sclass == C_THUMBEXT ?
966 mst_bss : mst_file_bss;
967 }
968 else
969 {
970 sec = cs_to_section (cs, objfile);
971 tmpaddr = cs->c_value;
972 if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
973 || cs->c_sclass == C_THUMBEXT)
96baa820 974 tmpaddr += ANOFFSET (objfile->section_offsets, sec);
c906108c 975
2ec466f9 976 if (sec == SECT_OFF_TEXT (objfile))
c5aa993b 977 {
c5aa993b
JM
978 ms_type =
979 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
980 || cs->c_sclass == C_THUMBEXT ?
981 mst_text : mst_file_text;
c906108c 982#ifdef SMASH_TEXT_ADDRESS
c5aa993b
JM
983 if (tmpaddr & 1) /* FIXME: delete this line */
984 SMASH_TEXT_ADDRESS (tmpaddr);
c906108c 985#endif
b8fbeb18
EZ
986 }
987 else
988 if (sec == SECT_OFF_DATA (objfile))
989 {
c5aa993b
JM
990 ms_type =
991 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
992 mst_data : mst_file_data;
b8fbeb18
EZ
993 }
994 else
995 if (sec == SECT_OFF_BSS (objfile))
996 {
c5aa993b
JM
997 ms_type =
998 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
999 mst_data : mst_file_data;
b8fbeb18
EZ
1000 }
1001 else
1002 ms_type = mst_unknown;
c5aa993b 1003 }
c906108c 1004
c5aa993b
JM
1005 if (cs->c_name[0] != '@' /* Skip tdesc symbols */ )
1006 {
1007 struct minimal_symbol *msym;
c906108c 1008
c5aa993b
JM
1009 msym = prim_record_minimal_symbol_and_info
1010 (cs->c_name, tmpaddr, ms_type, (char *) cs->c_sclass, sec,
c906108c
SS
1011 NULL, objfile);
1012#ifdef COFF_MAKE_MSYMBOL_SPECIAL
c5aa993b
JM
1013 if (msym)
1014 COFF_MAKE_MSYMBOL_SPECIAL (cs->c_sclass, msym);
c906108c 1015#endif
c5aa993b
JM
1016 }
1017 if (SDB_TYPE (cs->c_type))
1018 {
1019 struct symbol *sym;
1020 sym = process_coff_symbol
96baa820 1021 (cs, &main_aux, objfile);
c5aa993b
JM
1022 SYMBOL_VALUE (sym) = tmpaddr;
1023 SYMBOL_SECTION (sym) = sec;
1024 }
1025 }
1026 break;
1027
1028 case C_FCN:
1029 if (STREQ (cs->c_name, ".bf"))
1030 {
1031 within_function = 1;
1032
1033 /* value contains address of first non-init type code */
1034 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1035 contains line number of '{' } */
1036 if (cs->c_naux != 1)
1037 complain (&bf_no_aux_complaint, cs->c_symnum);
1038 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1039 fcn_first_line_addr = cs->c_value;
1040
1041 /* Might want to check that locals are 0 and
1042 context_stack_depth is zero, and complain if not. */
1043
1044 depth = 0;
1045 new = push_context (depth, fcn_start_addr);
1046 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1047 new->name =
96baa820 1048 process_coff_symbol (&fcn_cs_saved, &fcn_aux_saved, objfile);
c5aa993b
JM
1049 }
1050 else if (STREQ (cs->c_name, ".ef"))
1051 {
1052 /* the value of .ef is the address of epilogue code;
1053 not useful for gdb. */
1054 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1055 contains number of lines to '}' */
1056
1057 if (context_stack_depth <= 0)
1058 { /* We attempted to pop an empty context stack */
1059 complain (&ef_stack_complaint, cs->c_symnum);
1060 within_function = 0;
1061 break;
c906108c 1062 }
c5aa993b
JM
1063
1064 new = pop_context ();
1065 /* Stack must be empty now. */
1066 if (context_stack_depth > 0 || new == NULL)
c906108c 1067 {
c5aa993b
JM
1068 complain (&ef_complaint, cs->c_symnum);
1069 within_function = 0;
1070 break;
c906108c 1071 }
c5aa993b
JM
1072 if (cs->c_naux != 1)
1073 {
1074 complain (&ef_no_aux_complaint, cs->c_symnum);
1075 fcn_last_line = 0x7FFFFFFF;
1076 }
1077 else
1078 {
1079 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1080 }
1081 /* fcn_first_line is the line number of the opening '{'.
1082 Do not record it - because it would affect gdb's idea
1083 of the line number of the first statement of the function -
1084 except for one-line functions, for which it is also the line
1085 number of all the statements and of the closing '}', and
1086 for which we do not have any other statement-line-number. */
1087 if (fcn_last_line == 1)
1088 record_line (current_subfile, fcn_first_line,
1089 fcn_first_line_addr);
1090 else
1091 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line,
d4f3574e 1092 objfile);
c906108c 1093
c5aa993b
JM
1094 finish_block (new->name, &local_symbols, new->old_blocks,
1095 new->start_addr,
c906108c 1096#if defined (FUNCTION_EPILOGUE_SIZE)
c5aa993b
JM
1097 /* This macro should be defined only on
1098 machines where the
1099 fcn_aux_saved.x_sym.x_misc.x_fsize
1100 field is always zero.
1101 So use the .bf record information that
1102 points to the epilogue and add the size
1103 of the epilogue. */
1104 cs->c_value
1105 + FUNCTION_EPILOGUE_SIZE
b8fbeb18 1106 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)),
c906108c 1107#else
c5aa993b
JM
1108 fcn_cs_saved.c_value
1109 + fcn_aux_saved.x_sym.x_misc.x_fsize
b8fbeb18 1110 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)),
c906108c 1111#endif
c5aa993b
JM
1112 objfile
1113 );
1114 within_function = 0;
1115 }
1116 break;
c906108c 1117
c5aa993b
JM
1118 case C_BLOCK:
1119 if (STREQ (cs->c_name, ".bb"))
1120 {
1121 tmpaddr = cs->c_value;
b8fbeb18 1122 tmpaddr += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c5aa993b
JM
1123 push_context (++depth, tmpaddr);
1124 }
1125 else if (STREQ (cs->c_name, ".eb"))
1126 {
1127 if (context_stack_depth <= 0)
1128 { /* We attempted to pop an empty context stack */
1129 complain (&eb_stack_complaint, cs->c_symnum);
1130 break;
1131 }
c906108c 1132
c5aa993b
JM
1133 new = pop_context ();
1134 if (depth-- != new->depth)
1135 {
1136 complain (&eb_complaint, symnum);
1137 break;
1138 }
1139 if (local_symbols && context_stack_depth > 0)
1140 {
1141 tmpaddr =
b8fbeb18 1142 cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c5aa993b
JM
1143 /* Make a block for the local symbols within. */
1144 finish_block (0, &local_symbols, new->old_blocks,
1145 new->start_addr, tmpaddr, objfile);
1146 }
1147 /* Now pop locals of block just finished. */
1148 local_symbols = new->locals;
1149 }
1150 break;
c906108c 1151
c5aa993b 1152 default:
96baa820 1153 process_coff_symbol (cs, &main_aux, objfile);
c5aa993b 1154 break;
c906108c
SS
1155 }
1156 }
1157
1158 if (last_source_file)
1159 coff_end_symtab (objfile);
1160
1161 /* Patch up any opaque types (references to types that are not defined
1162 in the file where they are referenced, e.g. "struct foo *bar"). */
1163 ALL_OBJFILE_SYMTABS (objfile, s)
1164 patch_opaque_types (s);
1165
1166 current_objfile = NULL;
1167}
1168\f
1169/* Routines for reading headers and symbols from executable. */
1170
1171/* Read the next symbol, swap it, and return it in both internal_syment
1172 form, and coff_symbol form. Also return its first auxent, if any,
1173 in internal_auxent form, and skip any other auxents. */
1174
1175static void
1176read_one_sym (cs, sym, aux)
c5aa993b
JM
1177 register struct coff_symbol *cs;
1178 register struct internal_syment *sym;
1179 register union internal_auxent *aux;
c906108c
SS
1180{
1181 int i;
1182
1183 cs->c_symnum = symnum;
1184 bfd_read (temp_sym, local_symesz, 1, nlist_bfd_global);
c5aa993b 1185 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
c906108c
SS
1186 cs->c_naux = sym->n_numaux & 0xff;
1187 if (cs->c_naux >= 1)
1188 {
c906108c 1189 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
c5aa993b
JM
1190 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1191 0, cs->c_naux, (char *) aux);
1192 /* If more than one aux entry, read past it (only the first aux
1193 is important). */
1194 for (i = 1; i < cs->c_naux; i++)
1195 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
c906108c
SS
1196 }
1197 cs->c_name = getsymname (sym);
1198 cs->c_value = sym->n_value;
1199 cs->c_sclass = (sym->n_sclass & 0xff);
1200 cs->c_secnum = sym->n_scnum;
1201 cs->c_type = (unsigned) sym->n_type;
1202 if (!SDB_TYPE (cs->c_type))
1203 cs->c_type = 0;
1204
1205#if 0
1206 if (cs->c_sclass & 128)
c5aa993b 1207 printf ("thumb symbol %s, class 0x%x\n", cs->c_name, cs->c_sclass);
c906108c
SS
1208#endif
1209
1210 symnum += 1 + cs->c_naux;
1211
1212 /* The PE file format stores symbol values as offsets within the
1213 section, rather than as absolute addresses. We correct that
1214 here, if the symbol has an appropriate storage class. FIXME: We
1215 should use BFD to read the symbols, rather than duplicating the
1216 work here. */
1217 if (pe_file)
1218 {
1219 switch (cs->c_sclass)
1220 {
1221 case C_EXT:
1222 case C_THUMBEXT:
1223 case C_THUMBEXTFUNC:
1224 case C_SECTION:
1225 case C_NT_WEAK:
1226 case C_STAT:
1227 case C_THUMBSTAT:
1228 case C_THUMBSTATFUNC:
1229 case C_LABEL:
1230 case C_THUMBLABEL:
1231 case C_BLOCK:
1232 case C_FCN:
1233 case C_EFCN:
1234 if (cs->c_secnum != 0)
1235 cs->c_value += cs_section_address (cs, symfile_bfd);
1236 break;
1237 }
1238 }
1239}
1240\f
1241/* Support for string table handling */
1242
1243static char *stringtab = NULL;
1244
1245static int
1246init_stringtab (abfd, offset)
c5aa993b
JM
1247 bfd *abfd;
1248 long offset;
c906108c
SS
1249{
1250 long length;
1251 int val;
1252 unsigned char lengthbuf[4];
1253
1254 free_stringtab ();
1255
1256 /* If the file is stripped, the offset might be zero, indicating no
1257 string table. Just return with `stringtab' set to null. */
1258 if (offset == 0)
1259 return 0;
1260
1261 if (bfd_seek (abfd, offset, 0) < 0)
1262 return -1;
1263
c5aa993b 1264 val = bfd_read ((char *) lengthbuf, sizeof lengthbuf, 1, abfd);
c906108c 1265 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
c5aa993b 1266
c906108c
SS
1267 /* If no string table is needed, then the file may end immediately
1268 after the symbols. Just return with `stringtab' set to null. */
1269 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1270 return 0;
1271
1272 stringtab = (char *) xmalloc (length);
1273 /* This is in target format (probably not very useful, and not currently
1274 used), not host format. */
1275 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
c5aa993b 1276 if (length == sizeof length) /* Empty table -- just the count */
c906108c
SS
1277 return 0;
1278
1279 val = bfd_read (stringtab + sizeof lengthbuf, length - sizeof lengthbuf, 1, abfd);
1280 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1281 return -1;
1282
1283 return 0;
1284}
1285
1286static void
1287free_stringtab ()
1288{
1289 if (stringtab)
1290 free (stringtab);
1291 stringtab = NULL;
1292}
1293
74b7792f
AC
1294static void
1295free_stringtab_cleanup (void *ignore)
1296{
1297 free_stringtab ();
1298}
1299
c906108c
SS
1300static char *
1301getsymname (symbol_entry)
c5aa993b 1302 struct internal_syment *symbol_entry;
c906108c 1303{
c5aa993b 1304 static char buffer[SYMNMLEN + 1];
c906108c
SS
1305 char *result;
1306
1307 if (symbol_entry->_n._n_n._n_zeroes == 0)
1308 {
1309 /* FIXME: Probably should be detecting corrupt symbol files by
c5aa993b 1310 seeing whether offset points to within the stringtab. */
c906108c
SS
1311 result = stringtab + symbol_entry->_n._n_n._n_offset;
1312 }
1313 else
1314 {
1315 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1316 buffer[SYMNMLEN] = '\0';
1317 result = buffer;
1318 }
1319 return result;
1320}
1321
1322/* Extract the file name from the aux entry of a C_FILE symbol. Return
1323 only the last component of the name. Result is in static storage and
1324 is only good for temporary use. */
1325
1326static char *
1327coff_getfilename (aux_entry)
c5aa993b 1328 union internal_auxent *aux_entry;
c906108c
SS
1329{
1330 static char buffer[BUFSIZ];
1331 register char *temp;
1332 char *result;
1333
1334 if (aux_entry->x_file.x_n.x_zeroes == 0)
1335 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1336 else
1337 {
1338 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1339 buffer[FILNMLEN] = '\0';
1340 }
1341 result = buffer;
1342
1343 /* FIXME: We should not be throwing away the information about what
1344 directory. It should go into dirname of the symtab, or some such
1345 place. */
1346 if ((temp = strrchr (result, '/')) != NULL)
1347 result = temp + 1;
1348 return (result);
1349}
1350\f
1351/* Support for line number handling. */
1352
1353static char *linetab = NULL;
1354static long linetab_offset;
1355static unsigned long linetab_size;
1356
1357/* Read in all the line numbers for fast lookups later. Leave them in
1358 external (unswapped) format in memory; we'll swap them as we enter
1359 them into GDB's data structures. */
c5aa993b 1360
c906108c
SS
1361static int
1362init_lineno (abfd, offset, size)
c5aa993b
JM
1363 bfd *abfd;
1364 long offset;
1365 int size;
c906108c
SS
1366{
1367 int val;
1368
1369 linetab_offset = offset;
1370 linetab_size = size;
1371
c5aa993b 1372 free_linetab ();
c906108c
SS
1373
1374 if (size == 0)
1375 return 0;
1376
1377 if (bfd_seek (abfd, offset, 0) < 0)
1378 return -1;
c5aa993b 1379
c906108c
SS
1380 /* Allocate the desired table, plus a sentinel */
1381 linetab = (char *) xmalloc (size + local_linesz);
1382
1383 val = bfd_read (linetab, size, 1, abfd);
1384 if (val != size)
1385 return -1;
1386
1387 /* Terminate it with an all-zero sentinel record */
1388 memset (linetab + size, 0, local_linesz);
1389
1390 return 0;
1391}
1392
1393static void
1394free_linetab ()
1395{
1396 if (linetab)
1397 free (linetab);
1398 linetab = NULL;
1399}
1400
74b7792f
AC
1401static void
1402free_linetab_cleanup (void *ignore)
1403{
1404 free_linetab ();
1405}
1406
c906108c
SS
1407#if !defined (L_LNNO32)
1408#define L_LNNO32(lp) ((lp)->l_lnno)
1409#endif
1410
1411static void
d4f3574e 1412enter_linenos (file_offset, first_line, last_line, objfile)
c906108c
SS
1413 long file_offset;
1414 register int first_line;
1415 register int last_line;
d4f3574e 1416 struct objfile *objfile;
c906108c
SS
1417{
1418 register char *rawptr;
1419 struct internal_lineno lptr;
1420
1421 if (!linetab)
c5aa993b 1422 return;
c906108c
SS
1423 if (file_offset < linetab_offset)
1424 {
1425 complain (&lineno_complaint, file_offset);
1426 if (file_offset > linetab_size) /* Too big to be an offset? */
1427 return;
c5aa993b 1428 file_offset += linetab_offset; /* Try reading at that linetab offset */
c906108c 1429 }
c5aa993b 1430
c906108c
SS
1431 rawptr = &linetab[file_offset - linetab_offset];
1432
1433 /* skip first line entry for each function */
1434 rawptr += local_linesz;
1435 /* line numbers start at one for the first line of the function */
1436 first_line--;
1437
c5aa993b
JM
1438 for (;;)
1439 {
1440 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1441 rawptr += local_linesz;
1442 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1443 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1444 record_line (current_subfile, first_line + L_LNNO32 (&lptr),
1445 lptr.l_addr.l_paddr
b8fbeb18 1446 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)));
c5aa993b
JM
1447 else
1448 break;
1449 }
c906108c
SS
1450}
1451\f
1452static void
1453patch_type (type, real_type)
c5aa993b
JM
1454 struct type *type;
1455 struct type *real_type;
c906108c
SS
1456{
1457 register struct type *target = TYPE_TARGET_TYPE (type);
1458 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1459 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1460
1461 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1462 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1463 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1464
1465 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1466
1467 if (TYPE_NAME (real_target))
1468 {
1469 if (TYPE_NAME (target))
1470 free (TYPE_NAME (target));
1471 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1472 }
1473}
1474
1475/* Patch up all appropriate typedef symbols in the opaque_type_chains
1476 so that they can be used to print out opaque data structures properly. */
1477
1478static void
1479patch_opaque_types (s)
1480 struct symtab *s;
1481{
1482 register struct block *b;
1483 register int i;
1484 register struct symbol *real_sym;
c5aa993b 1485
c906108c
SS
1486 /* Go through the per-file symbols only */
1487 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1488 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1489 {
1490 /* Find completed typedefs to use to fix opaque ones.
c5aa993b
JM
1491 Remove syms from the chain when their types are stored,
1492 but search the whole chain, as there may be several syms
1493 from different files with the same name. */
c906108c
SS
1494 real_sym = BLOCK_SYM (b, i);
1495 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1496 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1497 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1498 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1499 {
1500 register char *name = SYMBOL_NAME (real_sym);
1501 register int hash = hashname (name);
1502 register struct symbol *sym, *prev;
c5aa993b 1503
c906108c
SS
1504 prev = 0;
1505 for (sym = opaque_type_chain[hash]; sym;)
1506 {
1507 if (name[0] == SYMBOL_NAME (sym)[0] &&
1508 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1509 {
1510 if (prev)
1511 {
1512 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1513 }
1514 else
1515 {
1516 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1517 }
c5aa993b 1518
c906108c 1519 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
c5aa993b 1520
c906108c
SS
1521 if (prev)
1522 {
1523 sym = SYMBOL_VALUE_CHAIN (prev);
1524 }
1525 else
1526 {
1527 sym = opaque_type_chain[hash];
1528 }
1529 }
1530 else
1531 {
1532 prev = sym;
1533 sym = SYMBOL_VALUE_CHAIN (sym);
1534 }
1535 }
1536 }
1537 }
1538}
1539\f
1540static struct symbol *
96baa820 1541process_coff_symbol (cs, aux, objfile)
c906108c
SS
1542 register struct coff_symbol *cs;
1543 register union internal_auxent *aux;
c906108c
SS
1544 struct objfile *objfile;
1545{
1546 register struct symbol *sym
c5aa993b
JM
1547 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1548 sizeof (struct symbol));
c906108c
SS
1549 char *name;
1550
1551 memset (sym, 0, sizeof (struct symbol));
1552 name = cs->c_name;
1553 name = EXTERNAL_NAME (name, objfile->obfd);
1554 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
1555 &objfile->symbol_obstack);
1556 SYMBOL_LANGUAGE (sym) = language_auto;
1557 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1558
1559 /* default assumptions */
1560 SYMBOL_VALUE (sym) = cs->c_value;
1561 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1562 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1563
1564 if (ISFCN (cs->c_type))
1565 {
b8fbeb18 1566 SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c5aa993b
JM
1567 SYMBOL_TYPE (sym) =
1568 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
c906108c
SS
1569
1570 SYMBOL_CLASS (sym) = LOC_BLOCK;
1571 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1572 || cs->c_sclass == C_THUMBSTATFUNC)
1573 add_symbol_to_list (sym, &file_symbols);
1574 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1575 || cs->c_sclass == C_THUMBEXTFUNC)
1576 add_symbol_to_list (sym, &global_symbols);
1577 }
1578 else
1579 {
1580 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1581 switch (cs->c_sclass)
1582 {
c5aa993b
JM
1583 case C_NULL:
1584 break;
c906108c 1585
c5aa993b
JM
1586 case C_AUTO:
1587 SYMBOL_CLASS (sym) = LOC_LOCAL;
1588 add_symbol_to_list (sym, &local_symbols);
1589 break;
c906108c 1590
c5aa993b
JM
1591 case C_THUMBEXT:
1592 case C_THUMBEXTFUNC:
1593 case C_EXT:
1594 SYMBOL_CLASS (sym) = LOC_STATIC;
1595 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
b8fbeb18 1596 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c5aa993b
JM
1597 add_symbol_to_list (sym, &global_symbols);
1598 break;
c906108c 1599
c5aa993b
JM
1600 case C_THUMBSTAT:
1601 case C_THUMBSTATFUNC:
1602 case C_STAT:
1603 SYMBOL_CLASS (sym) = LOC_STATIC;
1604 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
b8fbeb18 1605 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c5aa993b
JM
1606 if (within_function)
1607 {
c906108c
SS
1608 /* Static symbol of local scope */
1609 add_symbol_to_list (sym, &local_symbols);
1610 }
c5aa993b
JM
1611 else
1612 {
c906108c
SS
1613 /* Static symbol at top level of file */
1614 add_symbol_to_list (sym, &file_symbols);
1615 }
c5aa993b 1616 break;
c906108c
SS
1617
1618#ifdef C_GLBLREG /* AMD coff */
c5aa993b 1619 case C_GLBLREG:
c906108c 1620#endif
c5aa993b
JM
1621 case C_REG:
1622 SYMBOL_CLASS (sym) = LOC_REGISTER;
1623 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1624 add_symbol_to_list (sym, &local_symbols);
1625 break;
c906108c 1626
c5aa993b
JM
1627 case C_THUMBLABEL:
1628 case C_LABEL:
1629 break;
c906108c 1630
c5aa993b
JM
1631 case C_ARG:
1632 SYMBOL_CLASS (sym) = LOC_ARG;
1633 add_symbol_to_list (sym, &local_symbols);
c906108c 1634#if !defined (BELIEVE_PCC_PROMOTION)
c5aa993b
JM
1635 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1636 {
1637 /* If PCC says a parameter is a short or a char,
1638 aligned on an int boundary, realign it to the
1639 "little end" of the int. */
1640 struct type *temptype;
1641 temptype = lookup_fundamental_type (current_objfile,
1642 FT_INTEGER);
1643 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1644 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1645 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1646 {
1647 SYMBOL_VALUE (sym) +=
1648 TYPE_LENGTH (temptype)
1649 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1650 }
1651 }
c906108c 1652#endif
c5aa993b 1653 break;
c906108c 1654
c5aa993b
JM
1655 case C_REGPARM:
1656 SYMBOL_CLASS (sym) = LOC_REGPARM;
1657 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1658 add_symbol_to_list (sym, &local_symbols);
c906108c 1659#if !defined (BELIEVE_PCC_PROMOTION)
c5aa993b
JM
1660 /* FIXME: This should retain the current type, since it's just
1661 a register value. gnu@adobe, 26Feb93 */
1662 {
1663 /* If PCC says a parameter is a short or a char,
1664 it is really an int. */
1665 struct type *temptype;
1666 temptype =
1667 lookup_fundamental_type (current_objfile, FT_INTEGER);
1668 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1669 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
c906108c 1670 {
c5aa993b
JM
1671 SYMBOL_TYPE (sym) =
1672 (TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1673 ? lookup_fundamental_type (current_objfile,
1674 FT_UNSIGNED_INTEGER)
1675 : temptype);
c906108c 1676 }
c5aa993b 1677 }
c906108c 1678#endif
c5aa993b 1679 break;
c906108c 1680
c5aa993b
JM
1681 case C_TPDEF:
1682 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1683 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1684
1685 /* If type has no name, give it one */
1686 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1687 {
1688 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1689 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1690 {
1691 /* If we are giving a name to a type such as "pointer to
1692 foo" or "function returning foo", we better not set
1693 the TYPE_NAME. If the program contains "typedef char
1694 *caddr_t;", we don't want all variables of type char
1695 * to print as caddr_t. This is not just a
1696 consequence of GDB's type management; CC and GCC (at
1697 least through version 2.4) both output variables of
1698 either type char * or caddr_t with the type
1699 refering to the C_TPDEF symbol for caddr_t. If a future
1700 compiler cleans this up it GDB is not ready for it
1701 yet, but if it becomes ready we somehow need to
1702 disable this check (without breaking the PCC/GCC2.4
1703 case).
1704
1705 Sigh.
1706
1707 Fortunately, this check seems not to be necessary
1708 for anything except pointers or functions. */
1709 ;
1710 }
1711 else
1712 TYPE_NAME (SYMBOL_TYPE (sym)) =
1713 concat (SYMBOL_NAME (sym), NULL);
1714 }
c906108c 1715#ifdef CXUX_TARGET
c5aa993b
JM
1716 /* Ignore vendor section for Harris CX/UX targets. */
1717 else if (cs->c_name[0] == '$')
1718 break;
c906108c
SS
1719#endif /* CXUX_TARGET */
1720
c5aa993b
JM
1721 /* Keep track of any type which points to empty structured type,
1722 so it can be filled from a definition from another file. A
1723 simple forward reference (TYPE_CODE_UNDEF) is not an
1724 empty structured type, though; the forward references
1725 work themselves out via the magic of coff_lookup_type. */
1726 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1727 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1728 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1729 TYPE_CODE_UNDEF)
1730 {
1731 register int i = hashname (SYMBOL_NAME (sym));
c906108c 1732
c5aa993b
JM
1733 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1734 opaque_type_chain[i] = sym;
1735 }
1736 add_symbol_to_list (sym, &file_symbols);
1737 break;
c906108c 1738
c5aa993b
JM
1739 case C_STRTAG:
1740 case C_UNTAG:
1741 case C_ENTAG:
1742 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1743 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1744
1745 /* Some compilers try to be helpful by inventing "fake"
1746 names for anonymous enums, structures, and unions, like
1747 "~0fake" or ".0fake". Thanks, but no thanks... */
1748 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1749 if (SYMBOL_NAME (sym) != NULL
1750 && *SYMBOL_NAME (sym) != '~'
1751 && *SYMBOL_NAME (sym) != '.')
1752 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1753 concat (SYMBOL_NAME (sym), NULL);
1754
1755 add_symbol_to_list (sym, &file_symbols);
1756 break;
c906108c 1757
c5aa993b
JM
1758 default:
1759 break;
c906108c
SS
1760 }
1761 }
1762 return sym;
1763}
1764\f
1765/* Decode a coff type specifier; return the type that is meant. */
1766
1767static struct type *
1768decode_type (cs, c_type, aux)
1769 register struct coff_symbol *cs;
1770 unsigned int c_type;
1771 register union internal_auxent *aux;
1772{
1773 register struct type *type = 0;
1774 unsigned int new_c_type;
1775
1776 if (c_type & ~N_BTMASK)
1777 {
1778 new_c_type = DECREF (c_type);
1779 if (ISPTR (c_type))
1780 {
1781 type = decode_type (cs, new_c_type, aux);
1782 type = lookup_pointer_type (type);
1783 }
1784 else if (ISFCN (c_type))
1785 {
1786 type = decode_type (cs, new_c_type, aux);
1787 type = lookup_function_type (type);
1788 }
1789 else if (ISARY (c_type))
1790 {
1791 int i, n;
1792 register unsigned short *dim;
1793 struct type *base_type, *index_type, *range_type;
1794
1795 /* Define an array type. */
1796 /* auxent refers to array, not base type */
1797 if (aux->x_sym.x_tagndx.l == 0)
1798 cs->c_naux = 0;
1799
1800 /* shift the indices down */
1801 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1802 i = 1;
1803 n = dim[0];
1804 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1805 *dim = *(dim + 1);
1806 *dim = 0;
1807
1808 base_type = decode_type (cs, new_c_type, aux);
1809 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1810 range_type =
1811 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1812 type =
1813 create_array_type ((struct type *) NULL, base_type, range_type);
1814 }
1815 return type;
1816 }
1817
1818 /* Reference to existing type. This only occurs with the
1819 struct, union, and enum types. EPI a29k coff
1820 fakes us out by producing aux entries with a nonzero
1821 x_tagndx for definitions of structs, unions, and enums, so we
1822 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1823 with pointers to pointers to defined structs, and generates
1824 negative x_tagndx fields. */
1825 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1826 {
1827 if (cs->c_sclass != C_STRTAG
1828 && cs->c_sclass != C_UNTAG
1829 && cs->c_sclass != C_ENTAG
1830 && aux->x_sym.x_tagndx.l >= 0)
1831 {
1832 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1833 return type;
1834 }
1835 else
1836 {
1837 complain (&tagndx_bad_complaint, cs->c_name);
1838 /* And fall through to decode_base_type... */
1839 }
1840 }
1841
1842 return decode_base_type (cs, BTYPE (c_type), aux);
1843}
1844
1845/* Decode a coff type specifier for function definition;
1846 return the type that the function returns. */
1847
1848static struct type *
1849decode_function_type (cs, c_type, aux)
1850 register struct coff_symbol *cs;
1851 unsigned int c_type;
1852 register union internal_auxent *aux;
1853{
1854 if (aux->x_sym.x_tagndx.l == 0)
c5aa993b 1855 cs->c_naux = 0; /* auxent refers to function, not base type */
c906108c
SS
1856
1857 return decode_type (cs, DECREF (c_type), aux);
1858}
1859\f
1860/* basic C types */
1861
1862static struct type *
1863decode_base_type (cs, c_type, aux)
1864 register struct coff_symbol *cs;
1865 unsigned int c_type;
1866 register union internal_auxent *aux;
1867{
1868 struct type *type;
1869
1870 switch (c_type)
1871 {
c5aa993b
JM
1872 case T_NULL:
1873 /* shows up with "void (*foo)();" structure members */
1874 return lookup_fundamental_type (current_objfile, FT_VOID);
c906108c
SS
1875
1876#if 0
1877/* DGUX actually defines both T_ARG and T_VOID to the same value. */
1878#ifdef T_ARG
c5aa993b
JM
1879 case T_ARG:
1880 /* Shows up in DGUX, I think. Not sure where. */
1881 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
c906108c
SS
1882#endif
1883#endif /* 0 */
1884
1885#ifdef T_VOID
c5aa993b
JM
1886 case T_VOID:
1887 /* Intel 960 COFF has this symbol and meaning. */
1888 return lookup_fundamental_type (current_objfile, FT_VOID);
c906108c
SS
1889#endif
1890
c5aa993b
JM
1891 case T_CHAR:
1892 return lookup_fundamental_type (current_objfile, FT_CHAR);
c906108c 1893
c5aa993b
JM
1894 case T_SHORT:
1895 return lookup_fundamental_type (current_objfile, FT_SHORT);
c906108c 1896
c5aa993b
JM
1897 case T_INT:
1898 return lookup_fundamental_type (current_objfile, FT_INTEGER);
c906108c 1899
c5aa993b
JM
1900 case T_LONG:
1901 if (cs->c_sclass == C_FIELD
1902 && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
1903 return lookup_fundamental_type (current_objfile, FT_LONG_LONG);
1904 else
1905 return lookup_fundamental_type (current_objfile, FT_LONG);
c906108c 1906
c5aa993b
JM
1907 case T_FLOAT:
1908 return lookup_fundamental_type (current_objfile, FT_FLOAT);
c906108c 1909
c5aa993b
JM
1910 case T_DOUBLE:
1911 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
c906108c 1912
c5aa993b
JM
1913 case T_LNGDBL:
1914 return lookup_fundamental_type (current_objfile, FT_EXT_PREC_FLOAT);
c906108c 1915
c5aa993b
JM
1916 case T_STRUCT:
1917 if (cs->c_naux != 1)
1918 {
1919 /* anonymous structure type */
1920 type = coff_alloc_type (cs->c_symnum);
1921 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1922 TYPE_NAME (type) = NULL;
1923 /* This used to set the tag to "<opaque>". But I think setting it
1924 to NULL is right, and the printing code can print it as
1925 "struct {...}". */
1926 TYPE_TAG_NAME (type) = NULL;
1927 INIT_CPLUS_SPECIFIC (type);
1928 TYPE_LENGTH (type) = 0;
1929 TYPE_FIELDS (type) = 0;
1930 TYPE_NFIELDS (type) = 0;
1931 }
1932 else
1933 {
1934 type = coff_read_struct_type (cs->c_symnum,
1935 aux->x_sym.x_misc.x_lnsz.x_size,
1936 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1937 }
1938 return type;
c906108c 1939
c5aa993b
JM
1940 case T_UNION:
1941 if (cs->c_naux != 1)
1942 {
1943 /* anonymous union type */
1944 type = coff_alloc_type (cs->c_symnum);
1945 TYPE_NAME (type) = NULL;
1946 /* This used to set the tag to "<opaque>". But I think setting it
1947 to NULL is right, and the printing code can print it as
1948 "union {...}". */
1949 TYPE_TAG_NAME (type) = NULL;
1950 INIT_CPLUS_SPECIFIC (type);
1951 TYPE_LENGTH (type) = 0;
1952 TYPE_FIELDS (type) = 0;
1953 TYPE_NFIELDS (type) = 0;
1954 }
1955 else
1956 {
1957 type = coff_read_struct_type (cs->c_symnum,
c906108c 1958 aux->x_sym.x_misc.x_lnsz.x_size,
c5aa993b
JM
1959 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1960 }
1961 TYPE_CODE (type) = TYPE_CODE_UNION;
1962 return type;
c906108c 1963
c5aa993b
JM
1964 case T_ENUM:
1965 if (cs->c_naux != 1)
1966 {
1967 /* anonymous enum type */
1968 type = coff_alloc_type (cs->c_symnum);
1969 TYPE_CODE (type) = TYPE_CODE_ENUM;
1970 TYPE_NAME (type) = NULL;
1971 /* This used to set the tag to "<opaque>". But I think setting it
1972 to NULL is right, and the printing code can print it as
1973 "enum {...}". */
1974 TYPE_TAG_NAME (type) = NULL;
1975 TYPE_LENGTH (type) = 0;
1976 TYPE_FIELDS (type) = 0;
1977 TYPE_NFIELDS (type) = 0;
1978 }
1979 else
1980 {
1981 type = coff_read_enum_type (cs->c_symnum,
1982 aux->x_sym.x_misc.x_lnsz.x_size,
1983 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1984 }
1985 return type;
1986
1987 case T_MOE:
1988 /* shouldn't show up here */
1989 break;
c906108c 1990
c5aa993b
JM
1991 case T_UCHAR:
1992 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
c906108c 1993
c5aa993b
JM
1994 case T_USHORT:
1995 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
c906108c 1996
c5aa993b
JM
1997 case T_UINT:
1998 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
c906108c 1999
c5aa993b
JM
2000 case T_ULONG:
2001 if (cs->c_sclass == C_FIELD
2002 && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
2003 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG_LONG);
2004 else
2005 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
c906108c
SS
2006 }
2007 complain (&unexpected_type_complaint, cs->c_name);
2008 return lookup_fundamental_type (current_objfile, FT_VOID);
2009}
2010\f
2011/* This page contains subroutines of read_type. */
2012
2013/* Read the description of a structure (or union type) and return an
2014 object describing the type. */
2015
2016static struct type *
2017coff_read_struct_type (index, length, lastsym)
2018 int index;
2019 int length;
2020 int lastsym;
2021{
2022 struct nextfield
2023 {
2024 struct nextfield *next;
2025 struct field field;
2026 };
2027
2028 register struct type *type;
2029 register struct nextfield *list = 0;
2030 struct nextfield *new;
2031 int nfields = 0;
2032 register int n;
2033 char *name;
2034 struct coff_symbol member_sym;
2035 register struct coff_symbol *ms = &member_sym;
2036 struct internal_syment sub_sym;
2037 union internal_auxent sub_aux;
2038 int done = 0;
2039
2040 type = coff_alloc_type (index);
2041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c5aa993b 2042 INIT_CPLUS_SPECIFIC (type);
c906108c
SS
2043 TYPE_LENGTH (type) = length;
2044
2045 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2046 {
2047 read_one_sym (ms, &sub_sym, &sub_aux);
2048 name = ms->c_name;
2049 name = EXTERNAL_NAME (name, current_objfile->obfd);
2050
2051 switch (ms->c_sclass)
2052 {
c5aa993b
JM
2053 case C_MOS:
2054 case C_MOU:
2055
2056 /* Get space to record the next field's data. */
2057 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2058 new->next = list;
2059 list = new;
2060
2061 /* Save the data. */
2062 list->field.name =
2063 obsavestring (name,
2064 strlen (name),
2065 &current_objfile->symbol_obstack);
2066 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2067 FIELD_BITPOS (list->field) = 8 * ms->c_value;
2068 FIELD_BITSIZE (list->field) = 0;
2069 nfields++;
2070 break;
c906108c 2071
c5aa993b
JM
2072 case C_FIELD:
2073
2074 /* Get space to record the next field's data. */
2075 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2076 new->next = list;
2077 list = new;
2078
2079 /* Save the data. */
2080 list->field.name =
2081 obsavestring (name,
2082 strlen (name),
2083 &current_objfile->symbol_obstack);
2084 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2085 FIELD_BITPOS (list->field) = ms->c_value;
2086 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2087 nfields++;
2088 break;
c906108c 2089
c5aa993b
JM
2090 case C_EOS:
2091 done = 1;
2092 break;
c906108c
SS
2093 }
2094 }
2095 /* Now create the vector of fields, and record how big it is. */
2096
2097 TYPE_NFIELDS (type) = nfields;
2098 TYPE_FIELDS (type) = (struct field *)
2099 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2100
2101 /* Copy the saved-up fields into the field vector. */
2102
2103 for (n = nfields; list; list = list->next)
2104 TYPE_FIELD (type, --n) = list->field;
2105
2106 return type;
2107}
2108\f
2109/* Read a definition of an enumeration type,
2110 and create and return a suitable type object.
2111 Also defines the symbols that represent the values of the type. */
2112
2113/* ARGSUSED */
2114static struct type *
2115coff_read_enum_type (index, length, lastsym)
2116 int index;
2117 int length;
2118 int lastsym;
2119{
2120 register struct symbol *sym;
2121 register struct type *type;
2122 int nsyms = 0;
2123 int done = 0;
2124 struct pending **symlist;
2125 struct coff_symbol member_sym;
2126 register struct coff_symbol *ms = &member_sym;
2127 struct internal_syment sub_sym;
2128 union internal_auxent sub_aux;
2129 struct pending *osyms, *syms;
2130 int o_nsyms;
2131 register int n;
2132 char *name;
2133 int unsigned_enum = 1;
2134
2135 type = coff_alloc_type (index);
2136 if (within_function)
2137 symlist = &local_symbols;
2138 else
2139 symlist = &file_symbols;
2140 osyms = *symlist;
2141 o_nsyms = osyms ? osyms->nsyms : 0;
2142
2143 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2144 {
2145 read_one_sym (ms, &sub_sym, &sub_aux);
2146 name = ms->c_name;
2147 name = EXTERNAL_NAME (name, current_objfile->obfd);
2148
2149 switch (ms->c_sclass)
2150 {
c5aa993b
JM
2151 case C_MOE:
2152 sym = (struct symbol *) obstack_alloc
2153 (&current_objfile->symbol_obstack,
2154 sizeof (struct symbol));
2155 memset (sym, 0, sizeof (struct symbol));
2156
2157 SYMBOL_NAME (sym) =
2158 obsavestring (name, strlen (name),
2159 &current_objfile->symbol_obstack);
2160 SYMBOL_CLASS (sym) = LOC_CONST;
2161 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2162 SYMBOL_VALUE (sym) = ms->c_value;
2163 add_symbol_to_list (sym, symlist);
2164 nsyms++;
2165 break;
c906108c 2166
c5aa993b
JM
2167 case C_EOS:
2168 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2169 up the count of how many symbols to read. So stop
2170 on .eos. */
2171 done = 1;
2172 break;
c906108c
SS
2173 }
2174 }
2175
2176 /* Now fill in the fields of the type-structure. */
2177
2178 if (length > 0)
2179 TYPE_LENGTH (type) = length;
2180 else
c5aa993b 2181 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
c906108c
SS
2182 TYPE_CODE (type) = TYPE_CODE_ENUM;
2183 TYPE_NFIELDS (type) = nsyms;
2184 TYPE_FIELDS (type) = (struct field *)
2185 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2186
2187 /* Find the symbols for the values and put them into the type.
2188 The symbols can be found in the symlist that we put them on
2189 to cause them to be defined. osyms contains the old value
2190 of that symlist; everything up to there was defined by us. */
2191 /* Note that we preserve the order of the enum constants, so
2192 that in something like "enum {FOO, LAST_THING=FOO}" we print
2193 FOO, not LAST_THING. */
2194
2195 for (syms = *symlist, n = 0; syms; syms = syms->next)
2196 {
2197 int j = 0;
2198
2199 if (syms == osyms)
2200 j = o_nsyms;
c5aa993b 2201 for (; j < syms->nsyms; j++, n++)
c906108c
SS
2202 {
2203 struct symbol *xsym = syms->symbol[j];
2204 SYMBOL_TYPE (xsym) = type;
2205 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2206 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2207 if (SYMBOL_VALUE (xsym) < 0)
2208 unsigned_enum = 0;
2209 TYPE_FIELD_BITSIZE (type, n) = 0;
2210 }
2211 if (syms == osyms)
2212 break;
2213 }
2214
2215 if (unsigned_enum)
2216 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2217
2218 return type;
2219}
2220
2221/* Register our ability to parse symbols for coff BFD files. */
2222
2223static struct sym_fns coff_sym_fns =
2224{
2225 bfd_target_coff_flavour,
c5aa993b
JM
2226 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2227 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2228 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2229 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
96baa820 2230 default_symfile_offsets, /* sym_offsets: xlate external to internal form */
c5aa993b 2231 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
2232};
2233
2234void
2235_initialize_coffread ()
2236{
2237 add_symtab_fns (&coff_sym_fns);
2238}
This page took 0.146755 seconds and 4 git commands to generate.