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