b98e8ce24c51ecf981b0a1a723faea9d246f8a14
[external/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, 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   int num_symbols;
609   int symtab_offset;
610   int stringtab_offset;
611   struct cleanup *back_to;
612   int stabstrsize;
613
614   info = (struct coff_symfile_info *) objfile->sym_private;
615   dbxinfo = objfile->sym_stab_info;
616   symfile_bfd = abfd;           /* Kludge for swap routines */
617
618 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
619   num_symbols = bfd_get_symcount (abfd);        /* How many syms */
620   symtab_offset = cdata->sym_filepos;   /* Symbol table file offset */
621   stringtab_offset = symtab_offset +    /* String table file offset */
622     num_symbols * cdata->local_symesz;
623
624   /* Set a few file-statics that give us specific information about
625      the particular COFF file format we're reading.  */
626   local_n_btmask = cdata->local_n_btmask;
627   local_n_btshft = cdata->local_n_btshft;
628   local_n_tmask = cdata->local_n_tmask;
629   local_n_tshift = cdata->local_n_tshift;
630   local_linesz = cdata->local_linesz;
631   local_symesz = cdata->local_symesz;
632   local_auxesz = cdata->local_auxesz;
633
634   /* Allocate space for raw symbol and aux entries, based on their
635      space requirements as reported by BFD.  */
636   temp_sym = (char *) xmalloc
637     (cdata->local_symesz + cdata->local_auxesz);
638   temp_aux = temp_sym + cdata->local_symesz;
639   back_to = make_cleanup ((make_cleanup_func) free_current_contents, &temp_sym);
640
641   /* We need to know whether this is a PE file, because in PE files,
642      unlike standard COFF files, symbol values are stored as offsets
643      from the section address, rather than as absolute addresses.
644      FIXME: We should use BFD to read the symbol table, and thus avoid
645      this problem.  */
646   pe_file = strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0;
647
648 /* End of warning */
649
650   /* Read the line number table, all at once.  */
651   info->min_lineno_offset = 0;
652   info->max_lineno_offset = 0;
653   bfd_map_over_sections (abfd, find_linenos, (PTR) info);
654
655   make_cleanup ((make_cleanup_func) free_linetab, 0);
656   val = init_lineno (abfd, info->min_lineno_offset,
657                      info->max_lineno_offset - info->min_lineno_offset);
658   if (val < 0)
659     error ("\"%s\": error reading line numbers\n", name);
660
661   /* Now read the string table, all at once.  */
662
663   make_cleanup ((make_cleanup_func) free_stringtab, 0);
664   val = init_stringtab (abfd, stringtab_offset);
665   if (val < 0)
666     error ("\"%s\": can't get string table", name);
667
668   init_minimal_symbol_collection ();
669   make_cleanup ((make_cleanup_func) discard_minimal_symbols, 0);
670
671   /* Now that the executable file is positioned at symbol table,
672      process it and define symbols accordingly.  */
673
674   coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
675
676   /* Sort symbols alphabetically within each block.  */
677
678   {
679     struct symtab *s;
680
681     for (s = objfile->symtabs; s != NULL; s = s->next)
682       sort_symtab_syms (s);
683   }
684
685   /* Install any minimal symbols that have been collected as the current
686      minimal symbols for this objfile.  */
687
688   install_minimal_symbols (objfile);
689
690   bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
691
692   if (info->stabsects)
693     {
694       if (!info->stabstrsect)
695         {
696           error_begin ();
697           fprintf_filtered
698             (gdb_stderr,
699              ("The debugging information in `%s' is corrupted.\n"
700            "The file has a `.stabs' section, but no `.stabstr' section.\n"),
701              name);
702           return_to_top_level (RETURN_ERROR);
703         }
704
705       /* FIXME: dubious.  Why can't we use something normal like
706          bfd_get_section_contents?  */
707       bfd_seek (abfd, abfd->where, 0);
708
709       stabstrsize = bfd_section_size (abfd, info->stabstrsect);
710
711       coffstab_build_psymtabs (objfile,
712                                mainline,
713                                info->textaddr, info->textsize,
714                                info->stabsects,
715                                info->stabstrsect->filepos, stabstrsize);
716     }
717
718   do_cleanups (back_to);
719 }
720
721 static void
722 coff_new_init (ignore)
723      struct objfile *ignore;
724 {
725 }
726
727 /* Perform any local cleanups required when we are done with a particular
728    objfile.  I.E, we are in the process of discarding all symbol information
729    for an objfile, freeing up all memory held for it, and unlinking the
730    objfile struct from the global list of known objfiles. */
731
732 static void
733 coff_symfile_finish (objfile)
734      struct objfile *objfile;
735 {
736   if (objfile->sym_private != NULL)
737     {
738       mfree (objfile->md, objfile->sym_private);
739     }
740
741   /* Let stabs reader clean up */
742   stabsread_clear_cache ();
743 }
744 \f
745
746 /* Given pointers to a symbol table in coff style exec file,
747    analyze them and create struct symtab's describing the symbols.
748    NSYMS is the number of symbols in the symbol table.
749    We read them one at a time using read_one_sym ().  */
750
751 static void
752 coff_symtab_read (symtab_offset, nsyms, objfile)
753      long symtab_offset;
754      int nsyms;
755      struct objfile *objfile;
756 {
757   register struct context_stack *new;
758   struct coff_symbol coff_symbol;
759   register struct coff_symbol *cs = &coff_symbol;
760   static struct internal_syment main_sym;
761   static union internal_auxent main_aux;
762   struct coff_symbol fcn_cs_saved;
763   static struct internal_syment fcn_sym_saved;
764   static union internal_auxent fcn_aux_saved;
765   struct symtab *s;
766   /* A .file is open.  */
767   int in_source_file = 0;
768   int next_file_symnum = -1;
769   /* Name of the current file.  */
770   char *filestring = "";
771   int depth = 0;
772   int fcn_first_line = 0;
773   CORE_ADDR fcn_first_line_addr;
774   int fcn_last_line = 0;
775   int fcn_start_addr = 0;
776   long fcn_line_ptr = 0;
777   int val;
778   CORE_ADDR tmpaddr;
779
780   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
781      it's hard to know I've really worked around it.  The fix should be
782      harmless, anyway).  The symptom of the bug is that the first
783      fread (in read_one_sym), will (in my example) actually get data
784      from file offset 268, when the fseek was to 264 (and ftell shows
785      264).  This causes all hell to break loose.  I was unable to
786      reproduce this on a short test program which operated on the same
787      file, performing (I think) the same sequence of operations.
788
789      It stopped happening when I put in this (former) rewind().
790
791      FIXME: Find out if this has been reported to Sun, whether it has
792      been fixed in a later release, etc.  */
793
794   bfd_seek (objfile->obfd, 0, 0);
795
796   /* Position to read the symbol table. */
797   val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
798   if (val < 0)
799     perror_with_name (objfile->name);
800
801   current_objfile = objfile;
802   nlist_bfd_global = objfile->obfd;
803   nlist_nsyms_global = nsyms;
804   last_source_file = NULL;
805   memset (opaque_type_chain, 0, sizeof opaque_type_chain);
806
807   if (type_vector)              /* Get rid of previous one */
808     free ((PTR) type_vector);
809   type_vector_length = 160;
810   type_vector = (struct type **)
811     xmalloc (type_vector_length * sizeof (struct type *));
812   memset (type_vector, 0, type_vector_length * sizeof (struct type *));
813
814   coff_start_symtab ("");
815
816   symnum = 0;
817   while (symnum < nsyms)
818     {
819       QUIT;                     /* Make this command interruptable.  */
820
821       read_one_sym (cs, &main_sym, &main_aux);
822
823       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
824         {
825           if (last_source_file)
826             coff_end_symtab (objfile);
827
828           coff_start_symtab ("_globals_");
829           complete_symtab ("_globals_", 0, 0);
830           /* done with all files, everything from here on out is globals */
831         }
832
833       /* Special case for file with type declarations only, no text.  */
834       if (!last_source_file && SDB_TYPE (cs->c_type)
835           && cs->c_secnum == N_DEBUG)
836         complete_symtab (filestring, 0, 0);
837
838       /* Typedefs should not be treated as symbol definitions.  */
839       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
840         {
841           /* Record all functions -- external and static -- in minsyms. */
842           tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
843           record_minimal_symbol (cs->c_name, tmpaddr, mst_text, objfile);
844
845           fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
846           fcn_start_addr = tmpaddr;
847           fcn_cs_saved = *cs;
848           fcn_sym_saved = main_sym;
849           fcn_aux_saved = main_aux;
850           continue;
851         }
852
853       switch (cs->c_sclass)
854         {
855         case C_EFCN:
856         case C_EXTDEF:
857         case C_ULABEL:
858         case C_USTATIC:
859         case C_LINE:
860         case C_ALIAS:
861         case C_HIDDEN:
862           complain (&bad_sclass_complaint, cs->c_name);
863           break;
864
865         case C_FILE:
866           /* c_value field contains symnum of next .file entry in table
867              or symnum of first global after last .file.  */
868           next_file_symnum = cs->c_value;
869           if (cs->c_naux > 0)
870             filestring = coff_getfilename (&main_aux);
871           else
872             filestring = "";
873
874           /* Complete symbol table for last object file
875              containing debugging information.  */
876           if (last_source_file)
877             {
878               coff_end_symtab (objfile);
879               coff_start_symtab (filestring);
880             }
881           in_source_file = 1;
882           break;
883
884           /* C_LABEL is used for labels and static functions.  Including
885              it here allows gdb to see static functions when no debug
886              info is available.  */
887         case C_LABEL:
888           /* However, labels within a function can make weird backtraces,
889              so filter them out (from phdm@macqel.be). */
890           if (within_function)
891             break;
892         case C_STAT:
893         case C_THUMBLABEL:
894         case C_THUMBSTAT:
895         case C_THUMBSTATFUNC:
896           if (cs->c_name[0] == '.')
897             {
898               if (STREQ (cs->c_name, ".text"))
899                 {
900                   /* FIXME:  don't wire in ".text" as section name
901                      or symbol name! */
902                   /* Check for in_source_file deals with case of
903                      a file with debugging symbols
904                      followed by a later file with no symbols.  */
905                   if (in_source_file)
906                     complete_symtab (filestring,
907                     cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
908                                      main_aux.x_scn.x_scnlen);
909                   in_source_file = 0;
910                 }
911               /* flush rest of '.' symbols */
912               break;
913             }
914           else if (!SDB_TYPE (cs->c_type)
915                    && cs->c_name[0] == 'L'
916                    && (strncmp (cs->c_name, "LI%", 3) == 0
917                        || strncmp (cs->c_name, "LF%", 3) == 0
918                        || strncmp (cs->c_name, "LC%", 3) == 0
919                        || strncmp (cs->c_name, "LP%", 3) == 0
920                        || strncmp (cs->c_name, "LPB%", 4) == 0
921                        || strncmp (cs->c_name, "LBB%", 4) == 0
922                        || strncmp (cs->c_name, "LBE%", 4) == 0
923                        || strncmp (cs->c_name, "LPBX%", 5) == 0))
924             /* At least on a 3b1, gcc generates swbeg and string labels
925                that look like this.  Ignore them.  */
926             break;
927           /* fall in for static symbols that don't start with '.' */
928         case C_THUMBEXT:
929         case C_THUMBEXTFUNC:
930         case C_EXT:
931           {
932             /* Record it in the minimal symbols regardless of
933                SDB_TYPE.  This parallels what we do for other debug
934                formats, and probably is needed to make
935                print_address_symbolic work right without the (now
936                gone) "set fast-symbolic-addr off" kludge.  */
937
938             /* FIXME: should use mst_abs, and not relocate, if absolute.  */
939             enum minimal_symbol_type ms_type;
940             int sec;
941
942             if (cs->c_secnum == N_UNDEF)
943               {
944                 /* This is a common symbol.  See if the target
945                    environment knows where it has been relocated to.  */
946                 CORE_ADDR reladdr;
947                 if (target_lookup_symbol (cs->c_name, &reladdr))
948                   {
949                     /* Error in lookup; ignore symbol.  */
950                     break;
951                   }
952                 tmpaddr = reladdr;
953                 /* The address has already been relocated; make sure that
954                    objfile_relocate doesn't relocate it again.  */
955                 sec = -2;
956                 ms_type = cs->c_sclass == C_EXT
957                   || cs->c_sclass == C_THUMBEXT ?
958                   mst_bss : mst_file_bss;
959               }
960             else
961               {
962                 sec = cs_to_section (cs, objfile);
963                 tmpaddr = cs->c_value;
964                 if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
965                     || cs->c_sclass == C_THUMBEXT)
966                   tmpaddr += ANOFFSET (objfile->section_offsets, sec);
967
968                 switch (sec)
969                   {
970                   case SECT_OFF_TEXT:
971                   case SECT_OFF_RODATA:
972                     ms_type =
973                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
974                       || cs->c_sclass == C_THUMBEXT ?
975                       mst_text : mst_file_text;
976 #ifdef SMASH_TEXT_ADDRESS
977                     if (tmpaddr & 1)    /* FIXME: delete this line */
978                       SMASH_TEXT_ADDRESS (tmpaddr);
979 #endif
980                     break;
981                   case SECT_OFF_DATA:
982                     ms_type =
983                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
984                       mst_data : mst_file_data;
985                     break;
986                   case SECT_OFF_BSS:
987                     ms_type =
988                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
989                       mst_data : mst_file_data;
990                     break;
991                   default:
992                     ms_type = mst_unknown;
993                     break;
994                   }
995               }
996
997             if (cs->c_name[0] != '@' /* Skip tdesc symbols */ )
998               {
999                 struct minimal_symbol *msym;
1000
1001                 msym = prim_record_minimal_symbol_and_info
1002                   (cs->c_name, tmpaddr, ms_type, (char *) cs->c_sclass, sec,
1003                    NULL, objfile);
1004 #ifdef COFF_MAKE_MSYMBOL_SPECIAL
1005                 if (msym)
1006                   COFF_MAKE_MSYMBOL_SPECIAL (cs->c_sclass, msym);
1007 #endif
1008               }
1009             if (SDB_TYPE (cs->c_type))
1010               {
1011                 struct symbol *sym;
1012                 sym = process_coff_symbol
1013                   (cs, &main_aux, objfile);
1014                 SYMBOL_VALUE (sym) = tmpaddr;
1015                 SYMBOL_SECTION (sym) = sec;
1016               }
1017           }
1018           break;
1019
1020         case C_FCN:
1021           if (STREQ (cs->c_name, ".bf"))
1022             {
1023               within_function = 1;
1024
1025               /* value contains address of first non-init type code */
1026               /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1027                  contains line number of '{' } */
1028               if (cs->c_naux != 1)
1029                 complain (&bf_no_aux_complaint, cs->c_symnum);
1030               fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1031               fcn_first_line_addr = cs->c_value;
1032
1033               /* Might want to check that locals are 0 and
1034                  context_stack_depth is zero, and complain if not.  */
1035
1036               depth = 0;
1037               new = push_context (depth, fcn_start_addr);
1038               fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1039               new->name =
1040                 process_coff_symbol (&fcn_cs_saved, &fcn_aux_saved, objfile);
1041             }
1042           else if (STREQ (cs->c_name, ".ef"))
1043             {
1044               /* the value of .ef is the address of epilogue code;
1045                  not useful for gdb.  */
1046               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1047                  contains number of lines to '}' */
1048
1049               if (context_stack_depth <= 0)
1050                 {               /* We attempted to pop an empty context stack */
1051                   complain (&ef_stack_complaint, cs->c_symnum);
1052                   within_function = 0;
1053                   break;
1054                 }
1055
1056               new = pop_context ();
1057               /* Stack must be empty now.  */
1058               if (context_stack_depth > 0 || new == NULL)
1059                 {
1060                   complain (&ef_complaint, cs->c_symnum);
1061                   within_function = 0;
1062                   break;
1063                 }
1064               if (cs->c_naux != 1)
1065                 {
1066                   complain (&ef_no_aux_complaint, cs->c_symnum);
1067                   fcn_last_line = 0x7FFFFFFF;
1068                 }
1069               else
1070                 {
1071                   fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1072                 }
1073               /* fcn_first_line is the line number of the opening '{'.
1074                  Do not record it - because it would affect gdb's idea
1075                  of the line number of the first statement of the function -
1076                  except for one-line functions, for which it is also the line
1077                  number of all the statements and of the closing '}', and
1078                  for which we do not have any other statement-line-number. */
1079               if (fcn_last_line == 1)
1080                 record_line (current_subfile, fcn_first_line,
1081                              fcn_first_line_addr);
1082               else
1083                 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line,
1084                                objfile);
1085
1086               finish_block (new->name, &local_symbols, new->old_blocks,
1087                             new->start_addr,
1088 #if defined (FUNCTION_EPILOGUE_SIZE)
1089               /* This macro should be defined only on
1090                  machines where the
1091                  fcn_aux_saved.x_sym.x_misc.x_fsize
1092                  field is always zero.
1093                  So use the .bf record information that
1094                  points to the epilogue and add the size
1095                  of the epilogue.  */
1096                             cs->c_value
1097                             + FUNCTION_EPILOGUE_SIZE
1098                             + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
1099 #else
1100                             fcn_cs_saved.c_value
1101                             + fcn_aux_saved.x_sym.x_misc.x_fsize
1102                             + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
1103 #endif
1104                             objfile
1105                 );
1106               within_function = 0;
1107             }
1108           break;
1109
1110         case C_BLOCK:
1111           if (STREQ (cs->c_name, ".bb"))
1112             {
1113               tmpaddr = cs->c_value;
1114               tmpaddr += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1115               push_context (++depth, tmpaddr);
1116             }
1117           else if (STREQ (cs->c_name, ".eb"))
1118             {
1119               if (context_stack_depth <= 0)
1120                 {               /* We attempted to pop an empty context stack */
1121                   complain (&eb_stack_complaint, cs->c_symnum);
1122                   break;
1123                 }
1124
1125               new = pop_context ();
1126               if (depth-- != new->depth)
1127                 {
1128                   complain (&eb_complaint, symnum);
1129                   break;
1130                 }
1131               if (local_symbols && context_stack_depth > 0)
1132                 {
1133                   tmpaddr =
1134                     cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1135                   /* Make a block for the local symbols within.  */
1136                   finish_block (0, &local_symbols, new->old_blocks,
1137                                 new->start_addr, tmpaddr, objfile);
1138                 }
1139               /* Now pop locals of block just finished.  */
1140               local_symbols = new->locals;
1141             }
1142           break;
1143
1144         default:
1145           process_coff_symbol (cs, &main_aux, objfile);
1146           break;
1147         }
1148     }
1149
1150   if (last_source_file)
1151     coff_end_symtab (objfile);
1152
1153   /* Patch up any opaque types (references to types that are not defined
1154      in the file where they are referenced, e.g. "struct foo *bar").  */
1155   ALL_OBJFILE_SYMTABS (objfile, s)
1156     patch_opaque_types (s);
1157
1158   current_objfile = NULL;
1159 }
1160 \f
1161 /* Routines for reading headers and symbols from executable.  */
1162
1163 /* Read the next symbol, swap it, and return it in both internal_syment
1164    form, and coff_symbol form.  Also return its first auxent, if any,
1165    in internal_auxent form, and skip any other auxents.  */
1166
1167 static void
1168 read_one_sym (cs, sym, aux)
1169      register struct coff_symbol *cs;
1170      register struct internal_syment *sym;
1171      register union internal_auxent *aux;
1172 {
1173   int i;
1174
1175   cs->c_symnum = symnum;
1176   bfd_read (temp_sym, local_symesz, 1, nlist_bfd_global);
1177   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1178   cs->c_naux = sym->n_numaux & 0xff;
1179   if (cs->c_naux >= 1)
1180     {
1181       bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1182       bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1183                             0, cs->c_naux, (char *) aux);
1184       /* If more than one aux entry, read past it (only the first aux
1185          is important). */
1186       for (i = 1; i < cs->c_naux; i++)
1187         bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1188     }
1189   cs->c_name = getsymname (sym);
1190   cs->c_value = sym->n_value;
1191   cs->c_sclass = (sym->n_sclass & 0xff);
1192   cs->c_secnum = sym->n_scnum;
1193   cs->c_type = (unsigned) sym->n_type;
1194   if (!SDB_TYPE (cs->c_type))
1195     cs->c_type = 0;
1196
1197 #if 0
1198   if (cs->c_sclass & 128)
1199     printf ("thumb symbol %s, class 0x%x\n", cs->c_name, cs->c_sclass);
1200 #endif
1201
1202   symnum += 1 + cs->c_naux;
1203
1204   /* The PE file format stores symbol values as offsets within the
1205      section, rather than as absolute addresses.  We correct that
1206      here, if the symbol has an appropriate storage class.  FIXME: We
1207      should use BFD to read the symbols, rather than duplicating the
1208      work here.  */
1209   if (pe_file)
1210     {
1211       switch (cs->c_sclass)
1212         {
1213         case C_EXT:
1214         case C_THUMBEXT:
1215         case C_THUMBEXTFUNC:
1216         case C_SECTION:
1217         case C_NT_WEAK:
1218         case C_STAT:
1219         case C_THUMBSTAT:
1220         case C_THUMBSTATFUNC:
1221         case C_LABEL:
1222         case C_THUMBLABEL:
1223         case C_BLOCK:
1224         case C_FCN:
1225         case C_EFCN:
1226           if (cs->c_secnum != 0)
1227             cs->c_value += cs_section_address (cs, symfile_bfd);
1228           break;
1229         }
1230     }
1231 }
1232 \f
1233 /* Support for string table handling */
1234
1235 static char *stringtab = NULL;
1236
1237 static int
1238 init_stringtab (abfd, offset)
1239      bfd *abfd;
1240      long offset;
1241 {
1242   long length;
1243   int val;
1244   unsigned char lengthbuf[4];
1245
1246   free_stringtab ();
1247
1248   /* If the file is stripped, the offset might be zero, indicating no
1249      string table.  Just return with `stringtab' set to null. */
1250   if (offset == 0)
1251     return 0;
1252
1253   if (bfd_seek (abfd, offset, 0) < 0)
1254     return -1;
1255
1256   val = bfd_read ((char *) lengthbuf, sizeof lengthbuf, 1, abfd);
1257   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1258
1259   /* If no string table is needed, then the file may end immediately
1260      after the symbols.  Just return with `stringtab' set to null. */
1261   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1262     return 0;
1263
1264   stringtab = (char *) xmalloc (length);
1265   /* This is in target format (probably not very useful, and not currently
1266      used), not host format.  */
1267   memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1268   if (length == sizeof length)  /* Empty table -- just the count */
1269     return 0;
1270
1271   val = bfd_read (stringtab + sizeof lengthbuf, length - sizeof lengthbuf, 1, abfd);
1272   if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1273     return -1;
1274
1275   return 0;
1276 }
1277
1278 static void
1279 free_stringtab ()
1280 {
1281   if (stringtab)
1282     free (stringtab);
1283   stringtab = NULL;
1284 }
1285
1286 static char *
1287 getsymname (symbol_entry)
1288      struct internal_syment *symbol_entry;
1289 {
1290   static char buffer[SYMNMLEN + 1];
1291   char *result;
1292
1293   if (symbol_entry->_n._n_n._n_zeroes == 0)
1294     {
1295       /* FIXME: Probably should be detecting corrupt symbol files by
1296          seeing whether offset points to within the stringtab.  */
1297       result = stringtab + symbol_entry->_n._n_n._n_offset;
1298     }
1299   else
1300     {
1301       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1302       buffer[SYMNMLEN] = '\0';
1303       result = buffer;
1304     }
1305   return result;
1306 }
1307
1308 /* Extract the file name from the aux entry of a C_FILE symbol.  Return
1309    only the last component of the name.  Result is in static storage and
1310    is only good for temporary use.  */
1311
1312 static char *
1313 coff_getfilename (aux_entry)
1314      union internal_auxent *aux_entry;
1315 {
1316   static char buffer[BUFSIZ];
1317   register char *temp;
1318   char *result;
1319
1320   if (aux_entry->x_file.x_n.x_zeroes == 0)
1321     strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1322   else
1323     {
1324       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1325       buffer[FILNMLEN] = '\0';
1326     }
1327   result = buffer;
1328
1329   /* FIXME: We should not be throwing away the information about what
1330      directory.  It should go into dirname of the symtab, or some such
1331      place.  */
1332   if ((temp = strrchr (result, '/')) != NULL)
1333     result = temp + 1;
1334   return (result);
1335 }
1336 \f
1337 /* Support for line number handling.  */
1338
1339 static char *linetab = NULL;
1340 static long linetab_offset;
1341 static unsigned long linetab_size;
1342
1343 /* Read in all the line numbers for fast lookups later.  Leave them in
1344    external (unswapped) format in memory; we'll swap them as we enter
1345    them into GDB's data structures.  */
1346
1347 static int
1348 init_lineno (abfd, offset, size)
1349      bfd *abfd;
1350      long offset;
1351      int size;
1352 {
1353   int val;
1354
1355   linetab_offset = offset;
1356   linetab_size = size;
1357
1358   free_linetab ();
1359
1360   if (size == 0)
1361     return 0;
1362
1363   if (bfd_seek (abfd, offset, 0) < 0)
1364     return -1;
1365
1366   /* Allocate the desired table, plus a sentinel */
1367   linetab = (char *) xmalloc (size + local_linesz);
1368
1369   val = bfd_read (linetab, size, 1, abfd);
1370   if (val != size)
1371     return -1;
1372
1373   /* Terminate it with an all-zero sentinel record */
1374   memset (linetab + size, 0, local_linesz);
1375
1376   return 0;
1377 }
1378
1379 static void
1380 free_linetab ()
1381 {
1382   if (linetab)
1383     free (linetab);
1384   linetab = NULL;
1385 }
1386
1387 #if !defined (L_LNNO32)
1388 #define L_LNNO32(lp) ((lp)->l_lnno)
1389 #endif
1390
1391 static void
1392 enter_linenos (file_offset, first_line, last_line, objfile)
1393      long file_offset;
1394      register int first_line;
1395      register int last_line;
1396      struct objfile *objfile;
1397 {
1398   register char *rawptr;
1399   struct internal_lineno lptr;
1400
1401   if (!linetab)
1402     return;
1403   if (file_offset < linetab_offset)
1404     {
1405       complain (&lineno_complaint, file_offset);
1406       if (file_offset > linetab_size)   /* Too big to be an offset? */
1407         return;
1408       file_offset += linetab_offset;    /* Try reading at that linetab offset */
1409     }
1410
1411   rawptr = &linetab[file_offset - linetab_offset];
1412
1413   /* skip first line entry for each function */
1414   rawptr += local_linesz;
1415   /* line numbers start at one for the first line of the function */
1416   first_line--;
1417
1418   for (;;)
1419     {
1420       bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1421       rawptr += local_linesz;
1422       /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1423       if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1424         record_line (current_subfile, first_line + L_LNNO32 (&lptr),
1425                      lptr.l_addr.l_paddr
1426                      + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT));
1427       else
1428         break;
1429     }
1430 }
1431 \f
1432 static void
1433 patch_type (type, real_type)
1434      struct type *type;
1435      struct type *real_type;
1436 {
1437   register struct type *target = TYPE_TARGET_TYPE (type);
1438   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1439   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1440
1441   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1442   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1443   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1444
1445   memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1446
1447   if (TYPE_NAME (real_target))
1448     {
1449       if (TYPE_NAME (target))
1450         free (TYPE_NAME (target));
1451       TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1452     }
1453 }
1454
1455 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1456    so that they can be used to print out opaque data structures properly.  */
1457
1458 static void
1459 patch_opaque_types (s)
1460      struct symtab *s;
1461 {
1462   register struct block *b;
1463   register int i;
1464   register struct symbol *real_sym;
1465
1466   /* Go through the per-file symbols only */
1467   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1468   for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1469     {
1470       /* Find completed typedefs to use to fix opaque ones.
1471          Remove syms from the chain when their types are stored,
1472          but search the whole chain, as there may be several syms
1473          from different files with the same name.  */
1474       real_sym = BLOCK_SYM (b, i);
1475       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1476           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1477           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1478           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1479         {
1480           register char *name = SYMBOL_NAME (real_sym);
1481           register int hash = hashname (name);
1482           register struct symbol *sym, *prev;
1483
1484           prev = 0;
1485           for (sym = opaque_type_chain[hash]; sym;)
1486             {
1487               if (name[0] == SYMBOL_NAME (sym)[0] &&
1488                   STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1489                 {
1490                   if (prev)
1491                     {
1492                       SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1493                     }
1494                   else
1495                     {
1496                       opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1497                     }
1498
1499                   patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1500
1501                   if (prev)
1502                     {
1503                       sym = SYMBOL_VALUE_CHAIN (prev);
1504                     }
1505                   else
1506                     {
1507                       sym = opaque_type_chain[hash];
1508                     }
1509                 }
1510               else
1511                 {
1512                   prev = sym;
1513                   sym = SYMBOL_VALUE_CHAIN (sym);
1514                 }
1515             }
1516         }
1517     }
1518 }
1519 \f
1520 static struct symbol *
1521 process_coff_symbol (cs, aux, objfile)
1522      register struct coff_symbol *cs;
1523      register union internal_auxent *aux;
1524      struct objfile *objfile;
1525 {
1526   register struct symbol *sym
1527   = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1528                                      sizeof (struct symbol));
1529   char *name;
1530
1531   memset (sym, 0, sizeof (struct symbol));
1532   name = cs->c_name;
1533   name = EXTERNAL_NAME (name, objfile->obfd);
1534   SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
1535                                     &objfile->symbol_obstack);
1536   SYMBOL_LANGUAGE (sym) = language_auto;
1537   SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1538
1539   /* default assumptions */
1540   SYMBOL_VALUE (sym) = cs->c_value;
1541   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1542   SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1543
1544   if (ISFCN (cs->c_type))
1545     {
1546       SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1547       SYMBOL_TYPE (sym) =
1548         lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1549
1550       SYMBOL_CLASS (sym) = LOC_BLOCK;
1551       if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1552           || cs->c_sclass == C_THUMBSTATFUNC)
1553         add_symbol_to_list (sym, &file_symbols);
1554       else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1555                || cs->c_sclass == C_THUMBEXTFUNC)
1556         add_symbol_to_list (sym, &global_symbols);
1557     }
1558   else
1559     {
1560       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1561       switch (cs->c_sclass)
1562         {
1563         case C_NULL:
1564           break;
1565
1566         case C_AUTO:
1567           SYMBOL_CLASS (sym) = LOC_LOCAL;
1568           add_symbol_to_list (sym, &local_symbols);
1569           break;
1570
1571         case C_THUMBEXT:
1572         case C_THUMBEXTFUNC:
1573         case C_EXT:
1574           SYMBOL_CLASS (sym) = LOC_STATIC;
1575           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1576           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1577           add_symbol_to_list (sym, &global_symbols);
1578           break;
1579
1580         case C_THUMBSTAT:
1581         case C_THUMBSTATFUNC:
1582         case C_STAT:
1583           SYMBOL_CLASS (sym) = LOC_STATIC;
1584           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1585           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1586           if (within_function)
1587             {
1588               /* Static symbol of local scope */
1589               add_symbol_to_list (sym, &local_symbols);
1590             }
1591           else
1592             {
1593               /* Static symbol at top level of file */
1594               add_symbol_to_list (sym, &file_symbols);
1595             }
1596           break;
1597
1598 #ifdef C_GLBLREG                /* AMD coff */
1599         case C_GLBLREG:
1600 #endif
1601         case C_REG:
1602           SYMBOL_CLASS (sym) = LOC_REGISTER;
1603           SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1604           add_symbol_to_list (sym, &local_symbols);
1605           break;
1606
1607         case C_THUMBLABEL:
1608         case C_LABEL:
1609           break;
1610
1611         case C_ARG:
1612           SYMBOL_CLASS (sym) = LOC_ARG;
1613           add_symbol_to_list (sym, &local_symbols);
1614 #if !defined (BELIEVE_PCC_PROMOTION)
1615           if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1616             {
1617               /* If PCC says a parameter is a short or a char,
1618                  aligned on an int boundary, realign it to the
1619                  "little end" of the int.  */
1620               struct type *temptype;
1621               temptype = lookup_fundamental_type (current_objfile,
1622                                                   FT_INTEGER);
1623               if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1624                   && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1625                   && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1626                 {
1627                   SYMBOL_VALUE (sym) +=
1628                     TYPE_LENGTH (temptype)
1629                     - TYPE_LENGTH (SYMBOL_TYPE (sym));
1630                 }
1631             }
1632 #endif
1633           break;
1634
1635         case C_REGPARM:
1636           SYMBOL_CLASS (sym) = LOC_REGPARM;
1637           SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1638           add_symbol_to_list (sym, &local_symbols);
1639 #if !defined (BELIEVE_PCC_PROMOTION)
1640           /* FIXME:  This should retain the current type, since it's just
1641              a register value.  gnu@adobe, 26Feb93 */
1642           {
1643             /* If PCC says a parameter is a short or a char,
1644                it is really an int.  */
1645             struct type *temptype;
1646             temptype =
1647               lookup_fundamental_type (current_objfile, FT_INTEGER);
1648             if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1649                 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1650               {
1651                 SYMBOL_TYPE (sym) =
1652                   (TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1653                    ? lookup_fundamental_type (current_objfile,
1654                                               FT_UNSIGNED_INTEGER)
1655                    : temptype);
1656               }
1657           }
1658 #endif
1659           break;
1660
1661         case C_TPDEF:
1662           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1663           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1664
1665           /* If type has no name, give it one */
1666           if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1667             {
1668               if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1669                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1670                 {
1671                   /* If we are giving a name to a type such as "pointer to
1672                      foo" or "function returning foo", we better not set
1673                      the TYPE_NAME.  If the program contains "typedef char
1674                      *caddr_t;", we don't want all variables of type char
1675                      * to print as caddr_t.  This is not just a
1676                      consequence of GDB's type management; CC and GCC (at
1677                      least through version 2.4) both output variables of
1678                      either type char * or caddr_t with the type
1679                      refering to the C_TPDEF symbol for caddr_t.  If a future
1680                      compiler cleans this up it GDB is not ready for it
1681                      yet, but if it becomes ready we somehow need to
1682                      disable this check (without breaking the PCC/GCC2.4
1683                      case).
1684
1685                      Sigh.
1686
1687                      Fortunately, this check seems not to be necessary
1688                      for anything except pointers or functions.  */
1689                   ;
1690                 }
1691               else
1692                 TYPE_NAME (SYMBOL_TYPE (sym)) =
1693                   concat (SYMBOL_NAME (sym), NULL);
1694             }
1695 #ifdef CXUX_TARGET
1696           /* Ignore vendor section for Harris CX/UX targets. */
1697           else if (cs->c_name[0] == '$')
1698             break;
1699 #endif /* CXUX_TARGET */
1700
1701           /* Keep track of any type which points to empty structured type,
1702              so it can be filled from a definition from another file.  A
1703              simple forward reference (TYPE_CODE_UNDEF) is not an
1704              empty structured type, though; the forward references
1705              work themselves out via the magic of coff_lookup_type.  */
1706           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1707               TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1708               TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1709               TYPE_CODE_UNDEF)
1710             {
1711               register int i = hashname (SYMBOL_NAME (sym));
1712
1713               SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1714               opaque_type_chain[i] = sym;
1715             }
1716           add_symbol_to_list (sym, &file_symbols);
1717           break;
1718
1719         case C_STRTAG:
1720         case C_UNTAG:
1721         case C_ENTAG:
1722           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1723           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1724
1725           /* Some compilers try to be helpful by inventing "fake"
1726              names for anonymous enums, structures, and unions, like
1727              "~0fake" or ".0fake".  Thanks, but no thanks... */
1728           if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1729             if (SYMBOL_NAME (sym) != NULL
1730                 && *SYMBOL_NAME (sym) != '~'
1731                 && *SYMBOL_NAME (sym) != '.')
1732               TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1733                 concat (SYMBOL_NAME (sym), NULL);
1734
1735           add_symbol_to_list (sym, &file_symbols);
1736           break;
1737
1738         default:
1739           break;
1740         }
1741     }
1742   return sym;
1743 }
1744 \f
1745 /* Decode a coff type specifier;  return the type that is meant.  */
1746
1747 static struct type *
1748 decode_type (cs, c_type, aux)
1749      register struct coff_symbol *cs;
1750      unsigned int c_type;
1751      register union internal_auxent *aux;
1752 {
1753   register struct type *type = 0;
1754   unsigned int new_c_type;
1755
1756   if (c_type & ~N_BTMASK)
1757     {
1758       new_c_type = DECREF (c_type);
1759       if (ISPTR (c_type))
1760         {
1761           type = decode_type (cs, new_c_type, aux);
1762           type = lookup_pointer_type (type);
1763         }
1764       else if (ISFCN (c_type))
1765         {
1766           type = decode_type (cs, new_c_type, aux);
1767           type = lookup_function_type (type);
1768         }
1769       else if (ISARY (c_type))
1770         {
1771           int i, n;
1772           register unsigned short *dim;
1773           struct type *base_type, *index_type, *range_type;
1774
1775           /* Define an array type.  */
1776           /* auxent refers to array, not base type */
1777           if (aux->x_sym.x_tagndx.l == 0)
1778             cs->c_naux = 0;
1779
1780           /* shift the indices down */
1781           dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1782           i = 1;
1783           n = dim[0];
1784           for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1785             *dim = *(dim + 1);
1786           *dim = 0;
1787
1788           base_type = decode_type (cs, new_c_type, aux);
1789           index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1790           range_type =
1791             create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1792           type =
1793             create_array_type ((struct type *) NULL, base_type, range_type);
1794         }
1795       return type;
1796     }
1797
1798   /* Reference to existing type.  This only occurs with the
1799      struct, union, and enum types.  EPI a29k coff
1800      fakes us out by producing aux entries with a nonzero
1801      x_tagndx for definitions of structs, unions, and enums, so we
1802      have to check the c_sclass field.  SCO 3.2v4 cc gets confused
1803      with pointers to pointers to defined structs, and generates
1804      negative x_tagndx fields.  */
1805   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1806     {
1807       if (cs->c_sclass != C_STRTAG
1808           && cs->c_sclass != C_UNTAG
1809           && cs->c_sclass != C_ENTAG
1810           && aux->x_sym.x_tagndx.l >= 0)
1811         {
1812           type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1813           return type;
1814         }
1815       else
1816         {
1817           complain (&tagndx_bad_complaint, cs->c_name);
1818           /* And fall through to decode_base_type... */
1819         }
1820     }
1821
1822   return decode_base_type (cs, BTYPE (c_type), aux);
1823 }
1824
1825 /* Decode a coff type specifier for function definition;
1826    return the type that the function returns.  */
1827
1828 static struct type *
1829 decode_function_type (cs, c_type, aux)
1830      register struct coff_symbol *cs;
1831      unsigned int c_type;
1832      register union internal_auxent *aux;
1833 {
1834   if (aux->x_sym.x_tagndx.l == 0)
1835     cs->c_naux = 0;             /* auxent refers to function, not base type */
1836
1837   return decode_type (cs, DECREF (c_type), aux);
1838 }
1839 \f
1840 /* basic C types */
1841
1842 static struct type *
1843 decode_base_type (cs, c_type, aux)
1844      register struct coff_symbol *cs;
1845      unsigned int c_type;
1846      register union internal_auxent *aux;
1847 {
1848   struct type *type;
1849
1850   switch (c_type)
1851     {
1852     case T_NULL:
1853       /* shows up with "void (*foo)();" structure members */
1854       return lookup_fundamental_type (current_objfile, FT_VOID);
1855
1856 #if 0
1857 /* DGUX actually defines both T_ARG and T_VOID to the same value.  */
1858 #ifdef T_ARG
1859     case T_ARG:
1860       /* Shows up in DGUX, I think.  Not sure where.  */
1861       return lookup_fundamental_type (current_objfile, FT_VOID);        /* shouldn't show up here */
1862 #endif
1863 #endif /* 0 */
1864
1865 #ifdef T_VOID
1866     case T_VOID:
1867       /* Intel 960 COFF has this symbol and meaning.  */
1868       return lookup_fundamental_type (current_objfile, FT_VOID);
1869 #endif
1870
1871     case T_CHAR:
1872       return lookup_fundamental_type (current_objfile, FT_CHAR);
1873
1874     case T_SHORT:
1875       return lookup_fundamental_type (current_objfile, FT_SHORT);
1876
1877     case T_INT:
1878       return lookup_fundamental_type (current_objfile, FT_INTEGER);
1879
1880     case T_LONG:
1881       if (cs->c_sclass == C_FIELD
1882           && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
1883         return lookup_fundamental_type (current_objfile, FT_LONG_LONG);
1884       else
1885         return lookup_fundamental_type (current_objfile, FT_LONG);
1886
1887     case T_FLOAT:
1888       return lookup_fundamental_type (current_objfile, FT_FLOAT);
1889
1890     case T_DOUBLE:
1891       return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
1892
1893     case T_LNGDBL:
1894       return lookup_fundamental_type (current_objfile, FT_EXT_PREC_FLOAT);
1895
1896     case T_STRUCT:
1897       if (cs->c_naux != 1)
1898         {
1899           /* anonymous structure type */
1900           type = coff_alloc_type (cs->c_symnum);
1901           TYPE_CODE (type) = TYPE_CODE_STRUCT;
1902           TYPE_NAME (type) = NULL;
1903           /* This used to set the tag to "<opaque>".  But I think setting it
1904              to NULL is right, and the printing code can print it as
1905              "struct {...}".  */
1906           TYPE_TAG_NAME (type) = NULL;
1907           INIT_CPLUS_SPECIFIC (type);
1908           TYPE_LENGTH (type) = 0;
1909           TYPE_FIELDS (type) = 0;
1910           TYPE_NFIELDS (type) = 0;
1911         }
1912       else
1913         {
1914           type = coff_read_struct_type (cs->c_symnum,
1915                                         aux->x_sym.x_misc.x_lnsz.x_size,
1916                                       aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1917         }
1918       return type;
1919
1920     case T_UNION:
1921       if (cs->c_naux != 1)
1922         {
1923           /* anonymous union type */
1924           type = coff_alloc_type (cs->c_symnum);
1925           TYPE_NAME (type) = NULL;
1926           /* This used to set the tag to "<opaque>".  But I think setting it
1927              to NULL is right, and the printing code can print it as
1928              "union {...}".  */
1929           TYPE_TAG_NAME (type) = NULL;
1930           INIT_CPLUS_SPECIFIC (type);
1931           TYPE_LENGTH (type) = 0;
1932           TYPE_FIELDS (type) = 0;
1933           TYPE_NFIELDS (type) = 0;
1934         }
1935       else
1936         {
1937           type = coff_read_struct_type (cs->c_symnum,
1938                                         aux->x_sym.x_misc.x_lnsz.x_size,
1939                                       aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1940         }
1941       TYPE_CODE (type) = TYPE_CODE_UNION;
1942       return type;
1943
1944     case T_ENUM:
1945       if (cs->c_naux != 1)
1946         {
1947           /* anonymous enum type */
1948           type = coff_alloc_type (cs->c_symnum);
1949           TYPE_CODE (type) = TYPE_CODE_ENUM;
1950           TYPE_NAME (type) = NULL;
1951           /* This used to set the tag to "<opaque>".  But I think setting it
1952              to NULL is right, and the printing code can print it as
1953              "enum {...}".  */
1954           TYPE_TAG_NAME (type) = NULL;
1955           TYPE_LENGTH (type) = 0;
1956           TYPE_FIELDS (type) = 0;
1957           TYPE_NFIELDS (type) = 0;
1958         }
1959       else
1960         {
1961           type = coff_read_enum_type (cs->c_symnum,
1962                                       aux->x_sym.x_misc.x_lnsz.x_size,
1963                                       aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1964         }
1965       return type;
1966
1967     case T_MOE:
1968       /* shouldn't show up here */
1969       break;
1970
1971     case T_UCHAR:
1972       return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
1973
1974     case T_USHORT:
1975       return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
1976
1977     case T_UINT:
1978       return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
1979
1980     case T_ULONG:
1981       if (cs->c_sclass == C_FIELD
1982           && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
1983         return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG_LONG);
1984       else
1985         return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
1986     }
1987   complain (&unexpected_type_complaint, cs->c_name);
1988   return lookup_fundamental_type (current_objfile, FT_VOID);
1989 }
1990 \f
1991 /* This page contains subroutines of read_type.  */
1992
1993 /* Read the description of a structure (or union type) and return an
1994    object describing the type.  */
1995
1996 static struct type *
1997 coff_read_struct_type (index, length, lastsym)
1998      int index;
1999      int length;
2000      int lastsym;
2001 {
2002   struct nextfield
2003     {
2004       struct nextfield *next;
2005       struct field field;
2006     };
2007
2008   register struct type *type;
2009   register struct nextfield *list = 0;
2010   struct nextfield *new;
2011   int nfields = 0;
2012   register int n;
2013   char *name;
2014   struct coff_symbol member_sym;
2015   register struct coff_symbol *ms = &member_sym;
2016   struct internal_syment sub_sym;
2017   union internal_auxent sub_aux;
2018   int done = 0;
2019
2020   type = coff_alloc_type (index);
2021   TYPE_CODE (type) = TYPE_CODE_STRUCT;
2022   INIT_CPLUS_SPECIFIC (type);
2023   TYPE_LENGTH (type) = length;
2024
2025   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2026     {
2027       read_one_sym (ms, &sub_sym, &sub_aux);
2028       name = ms->c_name;
2029       name = EXTERNAL_NAME (name, current_objfile->obfd);
2030
2031       switch (ms->c_sclass)
2032         {
2033         case C_MOS:
2034         case C_MOU:
2035
2036           /* Get space to record the next field's data.  */
2037           new = (struct nextfield *) alloca (sizeof (struct nextfield));
2038           new->next = list;
2039           list = new;
2040
2041           /* Save the data.  */
2042           list->field.name =
2043             obsavestring (name,
2044                           strlen (name),
2045                           &current_objfile->symbol_obstack);
2046           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2047           FIELD_BITPOS (list->field) = 8 * ms->c_value;
2048           FIELD_BITSIZE (list->field) = 0;
2049           nfields++;
2050           break;
2051
2052         case C_FIELD:
2053
2054           /* Get space to record the next field's data.  */
2055           new = (struct nextfield *) alloca (sizeof (struct nextfield));
2056           new->next = list;
2057           list = new;
2058
2059           /* Save the data.  */
2060           list->field.name =
2061             obsavestring (name,
2062                           strlen (name),
2063                           &current_objfile->symbol_obstack);
2064           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2065           FIELD_BITPOS (list->field) = ms->c_value;
2066           FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2067           nfields++;
2068           break;
2069
2070         case C_EOS:
2071           done = 1;
2072           break;
2073         }
2074     }
2075   /* Now create the vector of fields, and record how big it is.  */
2076
2077   TYPE_NFIELDS (type) = nfields;
2078   TYPE_FIELDS (type) = (struct field *)
2079     TYPE_ALLOC (type, sizeof (struct field) * nfields);
2080
2081   /* Copy the saved-up fields into the field vector.  */
2082
2083   for (n = nfields; list; list = list->next)
2084     TYPE_FIELD (type, --n) = list->field;
2085
2086   return type;
2087 }
2088 \f
2089 /* Read a definition of an enumeration type,
2090    and create and return a suitable type object.
2091    Also defines the symbols that represent the values of the type.  */
2092
2093 /* ARGSUSED */
2094 static struct type *
2095 coff_read_enum_type (index, length, lastsym)
2096      int index;
2097      int length;
2098      int lastsym;
2099 {
2100   register struct symbol *sym;
2101   register struct type *type;
2102   int nsyms = 0;
2103   int done = 0;
2104   struct pending **symlist;
2105   struct coff_symbol member_sym;
2106   register struct coff_symbol *ms = &member_sym;
2107   struct internal_syment sub_sym;
2108   union internal_auxent sub_aux;
2109   struct pending *osyms, *syms;
2110   int o_nsyms;
2111   register int n;
2112   char *name;
2113   int unsigned_enum = 1;
2114
2115   type = coff_alloc_type (index);
2116   if (within_function)
2117     symlist = &local_symbols;
2118   else
2119     symlist = &file_symbols;
2120   osyms = *symlist;
2121   o_nsyms = osyms ? osyms->nsyms : 0;
2122
2123   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2124     {
2125       read_one_sym (ms, &sub_sym, &sub_aux);
2126       name = ms->c_name;
2127       name = EXTERNAL_NAME (name, current_objfile->obfd);
2128
2129       switch (ms->c_sclass)
2130         {
2131         case C_MOE:
2132           sym = (struct symbol *) obstack_alloc
2133             (&current_objfile->symbol_obstack,
2134              sizeof (struct symbol));
2135           memset (sym, 0, sizeof (struct symbol));
2136
2137           SYMBOL_NAME (sym) =
2138             obsavestring (name, strlen (name),
2139                           &current_objfile->symbol_obstack);
2140           SYMBOL_CLASS (sym) = LOC_CONST;
2141           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2142           SYMBOL_VALUE (sym) = ms->c_value;
2143           add_symbol_to_list (sym, symlist);
2144           nsyms++;
2145           break;
2146
2147         case C_EOS:
2148           /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2149              up the count of how many symbols to read.  So stop
2150              on .eos.  */
2151           done = 1;
2152           break;
2153         }
2154     }
2155
2156   /* Now fill in the fields of the type-structure.  */
2157
2158   if (length > 0)
2159     TYPE_LENGTH (type) = length;
2160   else
2161     TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT;      /* Assume ints */
2162   TYPE_CODE (type) = TYPE_CODE_ENUM;
2163   TYPE_NFIELDS (type) = nsyms;
2164   TYPE_FIELDS (type) = (struct field *)
2165     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2166
2167   /* Find the symbols for the values and put them into the type.
2168      The symbols can be found in the symlist that we put them on
2169      to cause them to be defined.  osyms contains the old value
2170      of that symlist; everything up to there was defined by us.  */
2171   /* Note that we preserve the order of the enum constants, so
2172      that in something like "enum {FOO, LAST_THING=FOO}" we print
2173      FOO, not LAST_THING.  */
2174
2175   for (syms = *symlist, n = 0; syms; syms = syms->next)
2176     {
2177       int j = 0;
2178
2179       if (syms == osyms)
2180         j = o_nsyms;
2181       for (; j < syms->nsyms; j++, n++)
2182         {
2183           struct symbol *xsym = syms->symbol[j];
2184           SYMBOL_TYPE (xsym) = type;
2185           TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2186           TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2187           if (SYMBOL_VALUE (xsym) < 0)
2188             unsigned_enum = 0;
2189           TYPE_FIELD_BITSIZE (type, n) = 0;
2190         }
2191       if (syms == osyms)
2192         break;
2193     }
2194
2195   if (unsigned_enum)
2196     TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2197
2198   return type;
2199 }
2200
2201 /* Register our ability to parse symbols for coff BFD files. */
2202
2203 static struct sym_fns coff_sym_fns =
2204 {
2205   bfd_target_coff_flavour,
2206   coff_new_init,                /* sym_new_init: init anything gbl to entire symtab */
2207   coff_symfile_init,            /* sym_init: read initial info, setup for sym_read() */
2208   coff_symfile_read,            /* sym_read: read a symbol file into symtab */
2209   coff_symfile_finish,          /* sym_finish: finished with file, cleanup */
2210   default_symfile_offsets,      /* sym_offsets:  xlate external to internal form */
2211   NULL                          /* next: pointer to next struct sym_fns */
2212 };
2213
2214 void
2215 _initialize_coffread ()
2216 {
2217   add_symtab_fns (&coff_sym_fns);
2218 }