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