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