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