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