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