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