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