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