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