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