GDB (API)
/home/stan/gdb/src/gdb/coffread.c
Go to the documentation of this file.
00001 /* Read coff symbol tables and convert to internal format, for GDB.
00002    Copyright (C) 1987-2013 Free Software Foundation, Inc.
00003    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "demangle.h"
00024 #include "breakpoint.h"
00025 
00026 #include "bfd.h"
00027 #include "gdb_obstack.h"
00028 
00029 #include "gdb_string.h"
00030 #include <ctype.h>
00031 
00032 #include "coff/internal.h"      /* Internal format of COFF symbols in BFD */
00033 #include "libcoff.h"            /* FIXME secret internal data from BFD */
00034 #include "objfiles.h"
00035 #include "buildsym.h"
00036 #include "gdb-stabs.h"
00037 #include "stabsread.h"
00038 #include "complaints.h"
00039 #include "target.h"
00040 #include "gdb_assert.h"
00041 #include "block.h"
00042 #include "dictionary.h"
00043 
00044 #include "coff-pe-read.h"
00045 
00046 #include "psymtab.h"
00047 
00048 extern void _initialize_coffread (void);
00049 
00050 /* Key for COFF-associated data.  */
00051 
00052 static const struct objfile_data *coff_objfile_data_key;
00053 
00054 /* The objfile we are currently reading.  */
00055 
00056 static struct objfile *coffread_objfile;
00057 
00058 struct coff_symfile_info
00059   {
00060     file_ptr min_lineno_offset; /* Where in file lowest line#s are.  */
00061     file_ptr max_lineno_offset; /* 1+last byte of line#s in file.  */
00062 
00063     CORE_ADDR textaddr;         /* Addr of .text section.  */
00064     unsigned int textsize;      /* Size of .text section.  */
00065     struct stab_section_list *stabsects;        /* .stab sections.  */
00066     asection *stabstrsect;      /* Section pointer for .stab section.  */
00067     char *stabstrdata;
00068   };
00069 
00070 /* Translate an external name string into a user-visible name.  */
00071 #define EXTERNAL_NAME(string, abfd) \
00072         (string[0] == bfd_get_symbol_leading_char (abfd) \
00073         ? string + 1 : string)
00074 
00075 /* To be an sdb debug type, type must have at least a basic or primary
00076    derived type.  Using this rather than checking against T_NULL is
00077    said to prevent core dumps if we try to operate on Michael Bloom
00078    dbx-in-coff file.  */
00079 
00080 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
00081 
00082 /* Core address of start and end of text of current source file.
00083    This comes from a ".text" symbol where x_nlinno > 0.  */
00084 
00085 static CORE_ADDR current_source_start_addr;
00086 static CORE_ADDR current_source_end_addr;
00087 
00088 /* The addresses of the symbol table stream and number of symbols
00089    of the object file we are reading (as copied into core).  */
00090 
00091 static bfd *nlist_bfd_global;
00092 static int nlist_nsyms_global;
00093 
00094 
00095 /* Pointers to scratch storage, used for reading raw symbols and
00096    auxents.  */
00097 
00098 static char *temp_sym;
00099 static char *temp_aux;
00100 
00101 /* Local variables that hold the shift and mask values for the
00102    COFF file that we are currently reading.  These come back to us
00103    from BFD, and are referenced by their macro names, as well as
00104    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
00105    macros from include/coff/internal.h .  */
00106 
00107 static unsigned local_n_btmask;
00108 static unsigned local_n_btshft;
00109 static unsigned local_n_tmask;
00110 static unsigned local_n_tshift;
00111 
00112 #define N_BTMASK        local_n_btmask
00113 #define N_BTSHFT        local_n_btshft
00114 #define N_TMASK         local_n_tmask
00115 #define N_TSHIFT        local_n_tshift
00116 
00117 /* Local variables that hold the sizes in the file of various COFF
00118    structures.  (We only need to know this to read them from the file
00119    -- BFD will then translate the data in them, into `internal_xxx'
00120    structs in the right byte order, alignment, etc.)  */
00121 
00122 static unsigned local_linesz;
00123 static unsigned local_symesz;
00124 static unsigned local_auxesz;
00125 
00126 /* This is set if this is a PE format file.  */
00127 
00128 static int pe_file;
00129 
00130 /* Chain of typedefs of pointers to empty struct/union types.
00131    They are chained thru the SYMBOL_VALUE_CHAIN.  */
00132 
00133 static struct symbol *opaque_type_chain[HASHSIZE];
00134 
00135 /* Simplified internal version of coff symbol table information.  */
00136 
00137 struct coff_symbol
00138   {
00139     char *c_name;
00140     int c_symnum;               /* Symbol number of this entry.  */
00141     int c_naux;                 /* 0 if syment only, 1 if syment +
00142                                    auxent, etc.  */
00143     CORE_ADDR c_value;
00144     int c_sclass;
00145     int c_secnum;
00146     unsigned int c_type;
00147   };
00148 
00149 /* Vector of types defined so far, indexed by their type numbers.  */
00150 
00151 static struct type **type_vector;
00152 
00153 /* Number of elements allocated for type_vector currently.  */
00154 
00155 static int type_vector_length;
00156 
00157 /* Initial size of type vector.  Is realloc'd larger if needed, and
00158    realloc'd down to the size actually used, when completed.  */
00159 
00160 #define INITIAL_TYPE_VECTOR_LENGTH 160
00161 
00162 extern void stabsread_clear_cache (void);
00163 
00164 static struct type *coff_read_struct_type (int, int, int,
00165                                            struct objfile *);
00166 
00167 static struct type *decode_base_type (struct coff_symbol *,
00168                                       unsigned int,
00169                                       union internal_auxent *,
00170                                       struct objfile *);
00171 
00172 static struct type *decode_type (struct coff_symbol *, unsigned int,
00173                                  union internal_auxent *,
00174                                  struct objfile *);
00175 
00176 static struct type *decode_function_type (struct coff_symbol *,
00177                                           unsigned int,
00178                                           union internal_auxent *,
00179                                           struct objfile *);
00180 
00181 static struct type *coff_read_enum_type (int, int, int,
00182                                          struct objfile *);
00183 
00184 static struct symbol *process_coff_symbol (struct coff_symbol *,
00185                                            union internal_auxent *,
00186                                            struct objfile *);
00187 
00188 static void patch_opaque_types (struct symtab *);
00189 
00190 static void enter_linenos (long, int, int, struct objfile *);
00191 
00192 static void free_linetab (void);
00193 
00194 static void free_linetab_cleanup (void *ignore);
00195 
00196 static int init_lineno (bfd *, long, int);
00197 
00198 static char *getsymname (struct internal_syment *);
00199 
00200 static const char *coff_getfilename (union internal_auxent *);
00201 
00202 static void free_stringtab (void);
00203 
00204 static void free_stringtab_cleanup (void *ignore);
00205 
00206 static int init_stringtab (bfd *, long);
00207 
00208 static void read_one_sym (struct coff_symbol *,
00209                           struct internal_syment *,
00210                           union internal_auxent *);
00211 
00212 static void coff_symtab_read (long, unsigned int, struct objfile *);
00213 
00214 /* We are called once per section from coff_symfile_read.  We
00215    need to examine each section we are passed, check to see
00216    if it is something we are interested in processing, and
00217    if so, stash away some access information for the section.
00218 
00219    FIXME: The section names should not be hardwired strings (what
00220    should they be?  I don't think most object file formats have enough
00221    section flags to specify what kind of debug section it is
00222    -kingdon).  */
00223 
00224 static void
00225 coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
00226 {
00227   struct coff_symfile_info *csi;
00228   const char *name;
00229 
00230   csi = (struct coff_symfile_info *) csip;
00231   name = bfd_get_section_name (abfd, sectp);
00232   if (strcmp (name, ".text") == 0)
00233     {
00234       csi->textaddr = bfd_section_vma (abfd, sectp);
00235       csi->textsize += bfd_section_size (abfd, sectp);
00236     }
00237   else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
00238     {
00239       csi->textsize += bfd_section_size (abfd, sectp);
00240     }
00241   else if (strcmp (name, ".stabstr") == 0)
00242     {
00243       csi->stabstrsect = sectp;
00244     }
00245   else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
00246     {
00247       const char *s;
00248 
00249       /* We can have multiple .stab sections if linked with
00250          --split-by-reloc.  */
00251       for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
00252         if (!isdigit (*s))
00253           break;
00254       if (*s == '\0')
00255         {
00256           struct stab_section_list *n, **pn;
00257 
00258           n = ((struct stab_section_list *)
00259                xmalloc (sizeof (struct stab_section_list)));
00260           n->section = sectp;
00261           n->next = NULL;
00262           for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
00263             ;
00264           *pn = n;
00265 
00266           /* This will be run after coffstab_build_psymtabs is called
00267              in coff_symfile_read, at which point we no longer need
00268              the information.  */
00269           make_cleanup (xfree, n);
00270         }
00271     }
00272 }
00273 
00274 /* Return the section_offsets* that CS points to.  */
00275 static int cs_to_section (struct coff_symbol *, struct objfile *);
00276 
00277 struct find_targ_sec_arg
00278   {
00279     int targ_index;
00280     asection **resultp;
00281   };
00282 
00283 static void
00284 find_targ_sec (bfd *abfd, asection *sect, void *obj)
00285 {
00286   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
00287 
00288   if (sect->target_index == args->targ_index)
00289     *args->resultp = sect;
00290 }
00291 
00292 /* Return the bfd_section that CS points to.  */
00293 static struct bfd_section*
00294 cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
00295 {
00296   asection *sect = NULL;
00297   struct find_targ_sec_arg args;
00298 
00299   args.targ_index = cs->c_secnum;
00300   args.resultp = &sect;
00301   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
00302   return sect;
00303 }
00304 
00305 /* Return the section number (SECT_OFF_*) that CS points to.  */
00306 static int
00307 cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
00308 {
00309   asection *sect = cs_to_bfd_section (cs, objfile);
00310 
00311   if (sect == NULL)
00312     return SECT_OFF_TEXT (objfile);
00313   return gdb_bfd_section_index (objfile->obfd, sect);
00314 }
00315 
00316 /* Return the address of the section of a COFF symbol.  */
00317 
00318 static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
00319 
00320 static CORE_ADDR
00321 cs_section_address (struct coff_symbol *cs, bfd *abfd)
00322 {
00323   asection *sect = NULL;
00324   struct find_targ_sec_arg args;
00325   CORE_ADDR addr = 0;
00326 
00327   args.targ_index = cs->c_secnum;
00328   args.resultp = &sect;
00329   bfd_map_over_sections (abfd, find_targ_sec, &args);
00330   if (sect != NULL)
00331     addr = bfd_get_section_vma (abfd, sect);
00332   return addr;
00333 }
00334 
00335 /* Look up a coff type-number index.  Return the address of the slot
00336    where the type for that index is stored.
00337    The type-number is in INDEX. 
00338 
00339    This can be used for finding the type associated with that index
00340    or for associating a new type with the index.  */
00341 
00342 static struct type **
00343 coff_lookup_type (int index)
00344 {
00345   if (index >= type_vector_length)
00346     {
00347       int old_vector_length = type_vector_length;
00348 
00349       type_vector_length *= 2;
00350       if (index /* is still */  >= type_vector_length)
00351         type_vector_length = index * 2;
00352 
00353       type_vector = (struct type **)
00354         xrealloc ((char *) type_vector,
00355                   type_vector_length * sizeof (struct type *));
00356       memset (&type_vector[old_vector_length], 0,
00357          (type_vector_length - old_vector_length) * sizeof (struct type *));
00358     }
00359   return &type_vector[index];
00360 }
00361 
00362 /* Make sure there is a type allocated for type number index
00363    and return the type object.
00364    This can create an empty (zeroed) type object.  */
00365 
00366 static struct type *
00367 coff_alloc_type (int index)
00368 {
00369   struct type **type_addr = coff_lookup_type (index);
00370   struct type *type = *type_addr;
00371 
00372   /* If we are referring to a type not known at all yet,
00373      allocate an empty type for it.
00374      We will fill it in later if we find out how.  */
00375   if (type == NULL)
00376     {
00377       type = alloc_type (coffread_objfile);
00378       *type_addr = type;
00379     }
00380   return type;
00381 }
00382 
00383 /* Start a new symtab for a new source file.
00384    This is called when a COFF ".file" symbol is seen;
00385    it indicates the start of data for one original source file.  */
00386 
00387 static void
00388 coff_start_symtab (const char *name)
00389 {
00390   start_symtab (
00391   /* We fill in the filename later.  start_symtab puts this pointer
00392      into last_source_file and we put it in subfiles->name, which
00393      end_symtab frees; that's why it must be malloc'd.  */
00394                  xstrdup (name),
00395   /* We never know the directory name for COFF.  */
00396                  NULL,
00397   /* The start address is irrelevant, since we set
00398      last_source_start_addr in coff_end_symtab.  */
00399                  0);
00400   record_debugformat ("COFF");
00401 }
00402 
00403 /* Save the vital information from when starting to read a file,
00404    for use when closing off the current file.
00405    NAME is the file name the symbols came from, START_ADDR is the
00406    first text address for the file, and SIZE is the number of bytes of
00407    text.  */
00408 
00409 static void
00410 complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
00411 {
00412   set_last_source_file (name);
00413   current_source_start_addr = start_addr;
00414   current_source_end_addr = start_addr + size;
00415 }
00416 
00417 /* Finish the symbol definitions for one main source file, close off
00418    all the lexical contexts for that file (creating struct block's for
00419    them), then make the struct symtab for that file and put it in the
00420    list of all such.  */
00421 
00422 static void
00423 coff_end_symtab (struct objfile *objfile)
00424 {
00425   last_source_start_addr = current_source_start_addr;
00426 
00427   end_symtab (current_source_end_addr, objfile,
00428               SECT_OFF_TEXT (objfile));
00429 
00430   /* Reinitialize for beginning of new file.  */
00431   set_last_source_file (NULL);
00432 }
00433 
00434 /* The linker sometimes generates some non-function symbols inside
00435    functions referencing variables imported from another DLL.
00436    Return nonzero if the given symbol corresponds to one of them.  */
00437 
00438 static int
00439 is_import_fixup_symbol (struct coff_symbol *cs,
00440                         enum minimal_symbol_type type)
00441 {
00442   /* The following is a bit of a heuristic using the characterictics
00443      of these fixup symbols, but should work well in practice...  */
00444   int i;
00445 
00446   /* Must be a non-static text symbol.  */
00447   if (type != mst_text)
00448     return 0;
00449 
00450   /* Must be a non-function symbol.  */
00451   if (ISFCN (cs->c_type))
00452     return 0;
00453 
00454   /* The name must start with "__fu<digits>__".  */
00455   if (strncmp (cs->c_name, "__fu", 4) != 0)
00456     return 0;
00457   if (! isdigit (cs->c_name[4]))
00458     return 0;
00459   for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
00460     /* Nothing, just incrementing index past all digits.  */;
00461   if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
00462     return 0;
00463 
00464   return 1;
00465 }
00466 
00467 static struct minimal_symbol *
00468 record_minimal_symbol (struct coff_symbol *cs, CORE_ADDR address,
00469                        enum minimal_symbol_type type, int section, 
00470                        struct objfile *objfile)
00471 {
00472   /* We don't want TDESC entry points in the minimal symbol table.  */
00473   if (cs->c_name[0] == '@')
00474     return NULL;
00475 
00476   if (is_import_fixup_symbol (cs, type))
00477     {
00478       /* Because the value of these symbols is within a function code
00479          range, these symbols interfere with the symbol-from-address
00480          reverse lookup; this manifests itselfs in backtraces, or any
00481          other commands that prints symbolic addresses.  Just pretend
00482          these symbols do not exist.  */
00483       return NULL;
00484     }
00485 
00486   return prim_record_minimal_symbol_and_info (cs->c_name, address,
00487                                               type, section, objfile);
00488 }
00489 
00490 /* coff_symfile_init ()
00491    is the coff-specific initialization routine for reading symbols.
00492    It is passed a struct objfile which contains, among other things,
00493    the BFD for the file whose symbols are being read, and a slot for
00494    a pointer to "private data" which we fill with cookies and other
00495    treats for coff_symfile_read ().
00496 
00497    We will only be called if this is a COFF or COFF-like file.  BFD
00498    handles figuring out the format of the file, and code in symtab.c
00499    uses BFD's determination to vector to us.
00500 
00501    The ultimate result is a new symtab (or, FIXME, eventually a
00502    psymtab).  */
00503 
00504 static void
00505 coff_symfile_init (struct objfile *objfile)
00506 {
00507   struct dbx_symfile_info *dbx;
00508   struct coff_symfile_info *coff;
00509 
00510   /* Allocate struct to keep track of stab reading.  */
00511   dbx = XCNEW (struct dbx_symfile_info);
00512   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
00513 
00514   /* Allocate struct to keep track of the symfile.  */
00515   coff = XCNEW (struct coff_symfile_info);
00516   set_objfile_data (objfile, coff_objfile_data_key, coff);
00517 
00518   /* COFF objects may be reordered, so set OBJF_REORDERED.  If we
00519      find this causes a significant slowdown in gdb then we could
00520      set it in the debug symbol readers only when necessary.  */
00521   objfile->flags |= OBJF_REORDERED;
00522 }
00523 
00524 /* This function is called for every section; it finds the outer
00525    limits of the line table (minimum and maximum file offset) so that
00526    the mainline code can read the whole thing for efficiency.  */
00527 
00528 static void
00529 find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
00530 {
00531   struct coff_symfile_info *info;
00532   int size, count;
00533   file_ptr offset, maxoff;
00534 
00535   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
00536   count = asect->lineno_count;
00537   /* End of warning.  */
00538 
00539   if (count == 0)
00540     return;
00541   size = count * local_linesz;
00542 
00543   info = (struct coff_symfile_info *) vpinfo;
00544   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
00545   offset = asect->line_filepos;
00546   /* End of warning.  */
00547 
00548   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
00549     info->min_lineno_offset = offset;
00550 
00551   maxoff = offset + size;
00552   if (maxoff > info->max_lineno_offset)
00553     info->max_lineno_offset = maxoff;
00554 }
00555 
00556 
00557 /* The BFD for this file -- only good while we're actively reading
00558    symbols into a psymtab or a symtab.  */
00559 
00560 static bfd *symfile_bfd;
00561 
00562 /* Read a symbol file, after initialization by coff_symfile_init.  */
00563 
00564 static void
00565 coff_symfile_read (struct objfile *objfile, int symfile_flags)
00566 {
00567   struct coff_symfile_info *info;
00568   struct dbx_symfile_info *dbxinfo;
00569   bfd *abfd = objfile->obfd;
00570   coff_data_type *cdata = coff_data (abfd);
00571   char *name = bfd_get_filename (abfd);
00572   int val;
00573   unsigned int num_symbols;
00574   int symtab_offset;
00575   int stringtab_offset;
00576   struct cleanup *back_to, *cleanup_minimal_symbols;
00577   int stabstrsize;
00578   
00579   info = objfile_data (objfile, coff_objfile_data_key);
00580   dbxinfo = DBX_SYMFILE_INFO (objfile);
00581   symfile_bfd = abfd;           /* Kludge for swap routines.  */
00582 
00583 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
00584   num_symbols = bfd_get_symcount (abfd);        /* How many syms */
00585   symtab_offset = cdata->sym_filepos;   /* Symbol table file offset */
00586   stringtab_offset = symtab_offset +    /* String table file offset */
00587     num_symbols * cdata->local_symesz;
00588 
00589   /* Set a few file-statics that give us specific information about
00590      the particular COFF file format we're reading.  */
00591   local_n_btmask = cdata->local_n_btmask;
00592   local_n_btshft = cdata->local_n_btshft;
00593   local_n_tmask = cdata->local_n_tmask;
00594   local_n_tshift = cdata->local_n_tshift;
00595   local_linesz = cdata->local_linesz;
00596   local_symesz = cdata->local_symesz;
00597   local_auxesz = cdata->local_auxesz;
00598 
00599   /* Allocate space for raw symbol and aux entries, based on their
00600      space requirements as reported by BFD.  */
00601   temp_sym = (char *) xmalloc
00602     (cdata->local_symesz + cdata->local_auxesz);
00603   temp_aux = temp_sym + cdata->local_symesz;
00604   back_to = make_cleanup (free_current_contents, &temp_sym);
00605 
00606   /* We need to know whether this is a PE file, because in PE files,
00607      unlike standard COFF files, symbol values are stored as offsets
00608      from the section address, rather than as absolute addresses.
00609      FIXME: We should use BFD to read the symbol table, and thus avoid
00610      this problem.  */
00611   pe_file =
00612     strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0
00613     || strncmp (bfd_get_target (objfile->obfd), "epoc-pe", 7) == 0;
00614 
00615   /* End of warning.  */
00616 
00617   info->min_lineno_offset = 0;
00618   info->max_lineno_offset = 0;
00619 
00620   /* Only read line number information if we have symbols.
00621 
00622      On Windows NT, some of the system's DLL's have sections with
00623      PointerToLinenumbers fields that are non-zero, but point at
00624      random places within the image file.  (In the case I found,
00625      KERNEL32.DLL's .text section has a line number info pointer that
00626      points into the middle of the string `lib\\i386\kernel32.dll'.)
00627 
00628      However, these DLL's also have no symbols.  The line number
00629      tables are meaningless without symbols.  And in fact, GDB never
00630      uses the line number information unless there are symbols.  So we
00631      can avoid spurious error messages (and maybe run a little
00632      faster!) by not even reading the line number table unless we have
00633      symbols.  */
00634   if (num_symbols > 0)
00635     {
00636       /* Read the line number table, all at once.  */
00637       bfd_map_over_sections (abfd, find_linenos, (void *) info);
00638 
00639       make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
00640       val = init_lineno (abfd, info->min_lineno_offset,
00641                          info->max_lineno_offset - info->min_lineno_offset);
00642       if (val < 0)
00643         error (_("\"%s\": error reading line numbers."), name);
00644     }
00645 
00646   /* Now read the string table, all at once.  */
00647 
00648   make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
00649   val = init_stringtab (abfd, stringtab_offset);
00650   if (val < 0)
00651     error (_("\"%s\": can't get string table"), name);
00652 
00653   init_minimal_symbol_collection ();
00654   cleanup_minimal_symbols = make_cleanup_discard_minimal_symbols ();
00655 
00656   /* Now that the executable file is positioned at symbol table,
00657      process it and define symbols accordingly.  */
00658 
00659   coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
00660 
00661   /* Install any minimal symbols that have been collected as the
00662      current minimal symbols for this objfile.  */
00663 
00664   install_minimal_symbols (objfile);
00665 
00666   if (pe_file)
00667     {
00668       struct minimal_symbol *msym;
00669 
00670       ALL_OBJFILE_MSYMBOLS (objfile, msym)
00671         {
00672           const char *name = SYMBOL_LINKAGE_NAME (msym);
00673 
00674           /* If the minimal symbols whose name are prefixed by "__imp_"
00675              or "_imp_", get rid of the prefix, and search the minimal
00676              symbol in OBJFILE.  Note that 'maintenance print msymbols'
00677              shows that type of these "_imp_XXXX" symbols is mst_data.  */
00678           if (MSYMBOL_TYPE (msym) == mst_data
00679               && (strncmp (name, "__imp_", 6) == 0
00680                   || strncmp (name, "_imp_", 5) == 0))
00681             {
00682               const char *name1 = (name[1] == '_' ? &name[7] : &name[6]);
00683               struct minimal_symbol *found;
00684 
00685               found = lookup_minimal_symbol (name1, NULL, objfile);
00686               /* If found, there are symbols named "_imp_foo" and "foo"
00687                  respectively in OBJFILE.  Set the type of symbol "foo"
00688                  as 'mst_solib_trampoline'.  */
00689               if (found != NULL && MSYMBOL_TYPE (found) == mst_text)
00690                 MSYMBOL_TYPE (found) = mst_solib_trampoline;
00691             }
00692         }
00693     }
00694 
00695   /* Free the installed minimal symbol data.  */
00696   do_cleanups (cleanup_minimal_symbols);
00697 
00698   bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
00699 
00700   if (info->stabsects)
00701     {
00702       if (!info->stabstrsect)
00703         {
00704           error (_("The debugging information in `%s' is corrupted.\nThe "
00705                    "file has a `.stabs' section, but no `.stabstr' section."),
00706                  name);
00707         }
00708 
00709       /* FIXME: dubious.  Why can't we use something normal like
00710          bfd_get_section_contents?  */
00711       bfd_seek (abfd, abfd->where, 0);
00712 
00713       stabstrsize = bfd_section_size (abfd, info->stabstrsect);
00714 
00715       coffstab_build_psymtabs (objfile,
00716                                info->textaddr, info->textsize,
00717                                info->stabsects,
00718                                info->stabstrsect->filepos, stabstrsize);
00719     }
00720   if (dwarf2_has_info (objfile, NULL))
00721     {
00722       /* DWARF2 sections.  */
00723       dwarf2_build_psymtabs (objfile);
00724     }
00725 
00726   dwarf2_build_frame_info (objfile);
00727 
00728   /* Try to add separate debug file if no symbols table found.   */
00729   if (!objfile_has_partial_symbols (objfile))
00730     {
00731       char *debugfile;
00732 
00733       debugfile = find_separate_debug_file_by_debuglink (objfile);
00734       make_cleanup (xfree, debugfile);
00735 
00736       if (debugfile)
00737         {
00738           bfd *abfd = symfile_bfd_open (debugfile);
00739 
00740           make_cleanup_bfd_unref (abfd);
00741           symbol_file_add_separate (abfd, debugfile, symfile_flags, objfile);
00742         }
00743     }
00744 
00745   do_cleanups (back_to);
00746 }
00747 
00748 static void
00749 coff_new_init (struct objfile *ignore)
00750 {
00751 }
00752 
00753 /* Perform any local cleanups required when we are done with a
00754    particular objfile.  I.E, we are in the process of discarding all
00755    symbol information for an objfile, freeing up all memory held for
00756    it, and unlinking the objfile struct from the global list of known
00757    objfiles.  */
00758 
00759 static void
00760 coff_symfile_finish (struct objfile *objfile)
00761 {
00762   /* Let stabs reader clean up.  */
00763   stabsread_clear_cache ();
00764 
00765   dwarf2_free_objfile (objfile);
00766 }
00767 
00768 
00769 /* Given pointers to a symbol table in coff style exec file,
00770    analyze them and create struct symtab's describing the symbols.
00771    NSYMS is the number of symbols in the symbol table.
00772    We read them one at a time using read_one_sym ().  */
00773 
00774 static void
00775 coff_symtab_read (long symtab_offset, unsigned int nsyms,
00776                   struct objfile *objfile)
00777 {
00778   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00779   struct context_stack *new;
00780   struct coff_symbol coff_symbol;
00781   struct coff_symbol *cs = &coff_symbol;
00782   static struct internal_syment main_sym;
00783   static union internal_auxent main_aux;
00784   struct coff_symbol fcn_cs_saved;
00785   static struct internal_syment fcn_sym_saved;
00786   static union internal_auxent fcn_aux_saved;
00787   struct symtab *s;
00788   /* A .file is open.  */
00789   int in_source_file = 0;
00790   int next_file_symnum = -1;
00791   /* Name of the current file.  */
00792   const char *filestring = "";
00793   int depth = 0;
00794   int fcn_first_line = 0;
00795   CORE_ADDR fcn_first_line_addr = 0;
00796   int fcn_last_line = 0;
00797   int fcn_start_addr = 0;
00798   long fcn_line_ptr = 0;
00799   int val;
00800   CORE_ADDR tmpaddr;
00801   struct minimal_symbol *msym;
00802 
00803   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
00804      it's hard to know I've really worked around it.  The fix should
00805      be harmless, anyway).  The symptom of the bug is that the first
00806      fread (in read_one_sym), will (in my example) actually get data
00807      from file offset 268, when the fseek was to 264 (and ftell shows
00808      264).  This causes all hell to break loose.  I was unable to
00809      reproduce this on a short test program which operated on the same
00810      file, performing (I think) the same sequence of operations.
00811 
00812      It stopped happening when I put in this (former) rewind().
00813 
00814      FIXME: Find out if this has been reported to Sun, whether it has
00815      been fixed in a later release, etc.  */
00816 
00817   bfd_seek (objfile->obfd, 0, 0);
00818 
00819   /* Position to read the symbol table.  */
00820   val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
00821   if (val < 0)
00822     perror_with_name (objfile_name (objfile));
00823 
00824   coffread_objfile = objfile;
00825   nlist_bfd_global = objfile->obfd;
00826   nlist_nsyms_global = nsyms;
00827   set_last_source_file (NULL);
00828   memset (opaque_type_chain, 0, sizeof opaque_type_chain);
00829 
00830   if (type_vector)              /* Get rid of previous one.  */
00831     xfree (type_vector);
00832   type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
00833   type_vector = (struct type **)
00834     xmalloc (type_vector_length * sizeof (struct type *));
00835   memset (type_vector, 0, type_vector_length * sizeof (struct type *));
00836 
00837   coff_start_symtab ("");
00838 
00839   symnum = 0;
00840   while (symnum < nsyms)
00841     {
00842       QUIT;                     /* Make this command interruptable.  */
00843 
00844       read_one_sym (cs, &main_sym, &main_aux);
00845 
00846       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
00847         {
00848           if (get_last_source_file ())
00849             coff_end_symtab (objfile);
00850 
00851           coff_start_symtab ("_globals_");
00852           /* coff_start_symtab will set the language of this symtab to
00853              language_unknown, since such a ``file name'' is not
00854              recognized.  Override that with the minimal language to
00855              allow printing values in this symtab.  */
00856           current_subfile->language = language_minimal;
00857           complete_symtab ("_globals_", 0, 0);
00858           /* Done with all files, everything from here on out is
00859              globals.  */
00860         }
00861 
00862       /* Special case for file with type declarations only, no
00863          text.  */
00864       if (!get_last_source_file () && SDB_TYPE (cs->c_type)
00865           && cs->c_secnum == N_DEBUG)
00866         complete_symtab (filestring, 0, 0);
00867 
00868       /* Typedefs should not be treated as symbol definitions.  */
00869       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
00870         {
00871           /* Record all functions -- external and static -- in
00872              minsyms.  */
00873           int section = cs_to_section (cs, objfile);
00874 
00875           tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets,
00876                                             SECT_OFF_TEXT (objfile));
00877           record_minimal_symbol (cs, tmpaddr, mst_text,
00878                                  section, objfile);
00879 
00880           fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
00881           fcn_start_addr = tmpaddr;
00882           fcn_cs_saved = *cs;
00883           fcn_sym_saved = main_sym;
00884           fcn_aux_saved = main_aux;
00885           continue;
00886         }
00887 
00888       switch (cs->c_sclass)
00889         {
00890         case C_EFCN:
00891         case C_EXTDEF:
00892         case C_ULABEL:
00893         case C_USTATIC:
00894         case C_LINE:
00895         case C_ALIAS:
00896         case C_HIDDEN:
00897           complaint (&symfile_complaints,
00898                      _("Bad n_sclass for symbol %s"),
00899                      cs->c_name);
00900           break;
00901 
00902         case C_FILE:
00903           /* c_value field contains symnum of next .file entry in
00904              table or symnum of first global after last .file.  */
00905           next_file_symnum = cs->c_value;
00906           if (cs->c_naux > 0)
00907             filestring = coff_getfilename (&main_aux);
00908           else
00909             filestring = "";
00910 
00911           /* Complete symbol table for last object file
00912              containing debugging information.  */
00913           if (get_last_source_file ())
00914             {
00915               coff_end_symtab (objfile);
00916               coff_start_symtab (filestring);
00917             }
00918           in_source_file = 1;
00919           break;
00920 
00921           /* C_LABEL is used for labels and static functions.
00922              Including it here allows gdb to see static functions when
00923              no debug info is available.  */
00924         case C_LABEL:
00925           /* However, labels within a function can make weird
00926              backtraces, so filter them out (from phdm@macqel.be).  */
00927           if (within_function)
00928             break;
00929         case C_STAT:
00930         case C_THUMBLABEL:
00931         case C_THUMBSTAT:
00932         case C_THUMBSTATFUNC:
00933           if (cs->c_name[0] == '.')
00934             {
00935               if (strcmp (cs->c_name, ".text") == 0)
00936                 {
00937                   /* FIXME: don't wire in ".text" as section name or
00938                      symbol name!  */
00939                   /* Check for in_source_file deals with case of a
00940                      file with debugging symbols followed by a later
00941                      file with no symbols.  */
00942                   if (in_source_file)
00943                     complete_symtab (filestring,
00944                     cs->c_value + ANOFFSET (objfile->section_offsets,
00945                                             SECT_OFF_TEXT (objfile)),
00946                                      main_aux.x_scn.x_scnlen);
00947                   in_source_file = 0;
00948                 }
00949               /* Flush rest of '.' symbols.  */
00950               break;
00951             }
00952           else if (!SDB_TYPE (cs->c_type)
00953                    && cs->c_name[0] == 'L'
00954                    && (strncmp (cs->c_name, "LI%", 3) == 0
00955                        || strncmp (cs->c_name, "LF%", 3) == 0
00956                        || strncmp (cs->c_name, "LC%", 3) == 0
00957                        || strncmp (cs->c_name, "LP%", 3) == 0
00958                        || strncmp (cs->c_name, "LPB%", 4) == 0
00959                        || strncmp (cs->c_name, "LBB%", 4) == 0
00960                        || strncmp (cs->c_name, "LBE%", 4) == 0
00961                        || strncmp (cs->c_name, "LPBX%", 5) == 0))
00962             /* At least on a 3b1, gcc generates swbeg and string labels
00963                that look like this.  Ignore them.  */
00964             break;
00965           /* Fall in for static symbols that don't start with '.'  */
00966         case C_THUMBEXT:
00967         case C_THUMBEXTFUNC:
00968         case C_EXT:
00969           {
00970             /* Record it in the minimal symbols regardless of
00971                SDB_TYPE.  This parallels what we do for other debug
00972                formats, and probably is needed to make
00973                print_address_symbolic work right without the (now
00974                gone) "set fast-symbolic-addr off" kludge.  */
00975 
00976             enum minimal_symbol_type ms_type;
00977             int sec;
00978 
00979             if (cs->c_secnum == N_UNDEF)
00980               {
00981                 /* This is a common symbol.  We used to rely on
00982                    the target to tell us whether it knows where
00983                    the symbol has been relocated to, but none of
00984                    the target implementations actually provided
00985                    that operation.  So we just ignore the symbol,
00986                    the same way we would do if we had a target-side
00987                    symbol lookup which returned no match.  */
00988                 break;
00989               }
00990             else if (cs->c_secnum == N_ABS)
00991               {
00992                 /* Use the correct minimal symbol type (and don't
00993                    relocate) for absolute values.  */
00994                 ms_type = mst_abs;
00995                 sec = cs_to_section (cs, objfile);
00996                 tmpaddr = cs->c_value;
00997               }
00998             else
00999               {
01000                 asection *bfd_section = cs_to_bfd_section (cs, objfile);
01001 
01002                 sec = cs_to_section (cs, objfile);
01003                 tmpaddr = cs->c_value;
01004                 /* Statics in a PE file also get relocated.  */
01005                 if (cs->c_sclass == C_EXT
01006                     || cs->c_sclass == C_THUMBEXTFUNC
01007                     || cs->c_sclass == C_THUMBEXT
01008                     || (pe_file && (cs->c_sclass == C_STAT)))
01009                   tmpaddr += ANOFFSET (objfile->section_offsets, sec);
01010 
01011                 if (bfd_section->flags & SEC_CODE)
01012                   {
01013                     ms_type =
01014                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
01015                       || cs->c_sclass == C_THUMBEXT ?
01016                       mst_text : mst_file_text;
01017                     tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
01018                   }
01019                 else if (bfd_section->flags & SEC_ALLOC
01020                          && bfd_section->flags & SEC_LOAD)
01021                   {
01022                     ms_type =
01023                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
01024                       ? mst_data : mst_file_data;
01025                   }
01026                 else if (bfd_section->flags & SEC_ALLOC)
01027                   {
01028                     ms_type =
01029                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
01030                       ? mst_bss : mst_file_bss;
01031                   }
01032                 else
01033                   ms_type = mst_unknown;
01034               }
01035 
01036             msym = record_minimal_symbol (cs, tmpaddr, ms_type,
01037                                           sec, objfile);
01038             if (msym)
01039               gdbarch_coff_make_msymbol_special (gdbarch,
01040                                                  cs->c_sclass, msym);
01041 
01042             if (SDB_TYPE (cs->c_type))
01043               {
01044                 struct symbol *sym;
01045 
01046                 sym = process_coff_symbol
01047                   (cs, &main_aux, objfile);
01048                 SYMBOL_VALUE (sym) = tmpaddr;
01049                 SYMBOL_SECTION (sym) = sec;
01050               }
01051           }
01052           break;
01053 
01054         case C_FCN:
01055           if (strcmp (cs->c_name, ".bf") == 0)
01056             {
01057               within_function = 1;
01058 
01059               /* Value contains address of first non-init type
01060                  code.  */
01061               /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
01062                  contains line number of '{' }.  */
01063               if (cs->c_naux != 1)
01064                 complaint (&symfile_complaints,
01065                            _("`.bf' symbol %d has no aux entry"),
01066                            cs->c_symnum);
01067               fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
01068               fcn_first_line_addr = cs->c_value;
01069 
01070               /* Might want to check that locals are 0 and
01071                  context_stack_depth is zero, and complain if not.  */
01072 
01073               depth = 0;
01074               new = push_context (depth, fcn_start_addr);
01075               fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
01076               new->name =
01077                 process_coff_symbol (&fcn_cs_saved, 
01078                                      &fcn_aux_saved, objfile);
01079             }
01080           else if (strcmp (cs->c_name, ".ef") == 0)
01081             {
01082               if (!within_function)
01083                 error (_("Bad coff function information."));
01084               /* The value of .ef is the address of epilogue code;
01085                  not useful for gdb.  */
01086               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
01087                  contains number of lines to '}' */
01088 
01089               if (context_stack_depth <= 0)
01090                 {       /* We attempted to pop an empty context stack.  */
01091                   complaint (&symfile_complaints,
01092                              _("`.ef' symbol without matching `.bf' "
01093                                "symbol ignored starting at symnum %d"),
01094                              cs->c_symnum);
01095                   within_function = 0;
01096                   break;
01097                 }
01098 
01099               new = pop_context ();
01100               /* Stack must be empty now.  */
01101               if (context_stack_depth > 0 || new == NULL)
01102                 {
01103                   complaint (&symfile_complaints,
01104                              _("Unmatched .ef symbol(s) ignored "
01105                                "starting at symnum %d"),
01106                              cs->c_symnum);
01107                   within_function = 0;
01108                   break;
01109                 }
01110               if (cs->c_naux != 1)
01111                 {
01112                   complaint (&symfile_complaints,
01113                              _("`.ef' symbol %d has no aux entry"),
01114                              cs->c_symnum);
01115                   fcn_last_line = 0x7FFFFFFF;
01116                 }
01117               else
01118                 {
01119                   fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
01120                 }
01121               /* fcn_first_line is the line number of the opening '{'.
01122                  Do not record it - because it would affect gdb's idea
01123                  of the line number of the first statement of the
01124                  function - except for one-line functions, for which
01125                  it is also the line number of all the statements and
01126                  of the closing '}', and for which we do not have any
01127                  other statement-line-number.  */
01128               if (fcn_last_line == 1)
01129                 record_line (current_subfile, fcn_first_line,
01130                              gdbarch_addr_bits_remove (gdbarch,
01131                                                        fcn_first_line_addr));
01132               else
01133                 enter_linenos (fcn_line_ptr, fcn_first_line,
01134                                fcn_last_line, objfile);
01135 
01136               finish_block (new->name, &local_symbols,
01137                             new->old_blocks, new->start_addr,
01138                             fcn_cs_saved.c_value
01139                             + fcn_aux_saved.x_sym.x_misc.x_fsize
01140                             + ANOFFSET (objfile->section_offsets,
01141                                         SECT_OFF_TEXT (objfile)),
01142                             objfile
01143                 );
01144               within_function = 0;
01145             }
01146           break;
01147 
01148         case C_BLOCK:
01149           if (strcmp (cs->c_name, ".bb") == 0)
01150             {
01151               tmpaddr = cs->c_value;
01152               tmpaddr += ANOFFSET (objfile->section_offsets,
01153                                    SECT_OFF_TEXT (objfile));
01154               push_context (++depth, tmpaddr);
01155             }
01156           else if (strcmp (cs->c_name, ".eb") == 0)
01157             {
01158               if (context_stack_depth <= 0)
01159                 {       /* We attempted to pop an empty context stack.  */
01160                   complaint (&symfile_complaints,
01161                              _("`.eb' symbol without matching `.bb' "
01162                                "symbol ignored starting at symnum %d"),
01163                              cs->c_symnum);
01164                   break;
01165                 }
01166 
01167               new = pop_context ();
01168               if (depth-- != new->depth)
01169                 {
01170                   complaint (&symfile_complaints,
01171                              _("Mismatched .eb symbol ignored "
01172                                "starting at symnum %d"),
01173                              symnum);
01174                   break;
01175                 }
01176               if (local_symbols && context_stack_depth > 0)
01177                 {
01178                   tmpaddr =
01179                     cs->c_value + ANOFFSET (objfile->section_offsets,
01180                                             SECT_OFF_TEXT (objfile));
01181                   /* Make a block for the local symbols within.  */
01182                   finish_block (0, &local_symbols, new->old_blocks,
01183                                 new->start_addr, tmpaddr, objfile);
01184                 }
01185               /* Now pop locals of block just finished.  */
01186               local_symbols = new->locals;
01187             }
01188           break;
01189 
01190         default:
01191           process_coff_symbol (cs, &main_aux, objfile);
01192           break;
01193         }
01194     }
01195 
01196   if ((nsyms == 0) && (pe_file))
01197     {
01198       /* We've got no debugging symbols, but it's a portable
01199          executable, so try to read the export table.  */
01200       read_pe_exported_syms (objfile);
01201     }
01202 
01203   if (get_last_source_file ())
01204     coff_end_symtab (objfile);
01205 
01206   /* Patch up any opaque types (references to types that are not defined
01207      in the file where they are referenced, e.g. "struct foo *bar").  */
01208   ALL_OBJFILE_SYMTABS (objfile, s)
01209     patch_opaque_types (s);
01210 
01211   coffread_objfile = NULL;
01212 }
01213 
01214 /* Routines for reading headers and symbols from executable.  */
01215 
01216 /* Read the next symbol, swap it, and return it in both
01217    internal_syment form, and coff_symbol form.  Also return its first
01218    auxent, if any, in internal_auxent form, and skip any other
01219    auxents.  */
01220 
01221 static void
01222 read_one_sym (struct coff_symbol *cs,
01223               struct internal_syment *sym,
01224               union internal_auxent *aux)
01225 {
01226   int i;
01227   bfd_size_type bytes;
01228 
01229   cs->c_symnum = symnum;
01230   bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
01231   if (bytes != local_symesz)
01232     error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
01233   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
01234   cs->c_naux = sym->n_numaux & 0xff;
01235   if (cs->c_naux >= 1)
01236     {
01237       bytes  = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
01238       if (bytes != local_auxesz)
01239         error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
01240       bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
01241                             sym->n_type, sym->n_sclass,
01242                             0, cs->c_naux, (char *) aux);
01243       /* If more than one aux entry, read past it (only the first aux
01244          is important).  */
01245       for (i = 1; i < cs->c_naux; i++)
01246         {
01247           bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
01248           if (bytes != local_auxesz)
01249             error (_("%s: error reading symbols"),
01250                    objfile_name (coffread_objfile));
01251         }
01252     }
01253   cs->c_name = getsymname (sym);
01254   cs->c_value = sym->n_value;
01255   cs->c_sclass = (sym->n_sclass & 0xff);
01256   cs->c_secnum = sym->n_scnum;
01257   cs->c_type = (unsigned) sym->n_type;
01258   if (!SDB_TYPE (cs->c_type))
01259     cs->c_type = 0;
01260 
01261 #if 0
01262   if (cs->c_sclass & 128)
01263     printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
01264 #endif
01265 
01266   symnum += 1 + cs->c_naux;
01267 
01268   /* The PE file format stores symbol values as offsets within the
01269      section, rather than as absolute addresses.  We correct that
01270      here, if the symbol has an appropriate storage class.  FIXME: We
01271      should use BFD to read the symbols, rather than duplicating the
01272      work here.  */
01273   if (pe_file)
01274     {
01275       switch (cs->c_sclass)
01276         {
01277         case C_EXT:
01278         case C_THUMBEXT:
01279         case C_THUMBEXTFUNC:
01280         case C_SECTION:
01281         case C_NT_WEAK:
01282         case C_STAT:
01283         case C_THUMBSTAT:
01284         case C_THUMBSTATFUNC:
01285         case C_LABEL:
01286         case C_THUMBLABEL:
01287         case C_BLOCK:
01288         case C_FCN:
01289         case C_EFCN:
01290           if (cs->c_secnum != 0)
01291             cs->c_value += cs_section_address (cs, symfile_bfd);
01292           break;
01293         }
01294     }
01295 }
01296 
01297 /* Support for string table handling.  */
01298 
01299 static char *stringtab = NULL;
01300 
01301 static int
01302 init_stringtab (bfd *abfd, long offset)
01303 {
01304   long length;
01305   int val;
01306   unsigned char lengthbuf[4];
01307 
01308   free_stringtab ();
01309 
01310   /* If the file is stripped, the offset might be zero, indicating no
01311      string table.  Just return with `stringtab' set to null.  */
01312   if (offset == 0)
01313     return 0;
01314 
01315   if (bfd_seek (abfd, offset, 0) < 0)
01316     return -1;
01317 
01318   val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
01319   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
01320 
01321   /* If no string table is needed, then the file may end immediately
01322      after the symbols.  Just return with `stringtab' set to null.  */
01323   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
01324     return 0;
01325 
01326   stringtab = (char *) xmalloc (length);
01327   /* This is in target format (probably not very useful, and not
01328      currently used), not host format.  */
01329   memcpy (stringtab, lengthbuf, sizeof lengthbuf);
01330   if (length == sizeof length)  /* Empty table -- just the count.  */
01331     return 0;
01332 
01333   val = bfd_bread (stringtab + sizeof lengthbuf, 
01334                    length - sizeof lengthbuf, abfd);
01335   if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
01336     return -1;
01337 
01338   return 0;
01339 }
01340 
01341 static void
01342 free_stringtab (void)
01343 {
01344   if (stringtab)
01345     xfree (stringtab);
01346   stringtab = NULL;
01347 }
01348 
01349 static void
01350 free_stringtab_cleanup (void *ignore)
01351 {
01352   free_stringtab ();
01353 }
01354 
01355 static char *
01356 getsymname (struct internal_syment *symbol_entry)
01357 {
01358   static char buffer[SYMNMLEN + 1];
01359   char *result;
01360 
01361   if (symbol_entry->_n._n_n._n_zeroes == 0)
01362     {
01363       /* FIXME: Probably should be detecting corrupt symbol files by
01364          seeing whether offset points to within the stringtab.  */
01365       result = stringtab + symbol_entry->_n._n_n._n_offset;
01366     }
01367   else
01368     {
01369       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
01370       buffer[SYMNMLEN] = '\0';
01371       result = buffer;
01372     }
01373   return result;
01374 }
01375 
01376 /* Extract the file name from the aux entry of a C_FILE symbol.
01377    Return only the last component of the name.  Result is in static
01378    storage and is only good for temporary use.  */
01379 
01380 static const char *
01381 coff_getfilename (union internal_auxent *aux_entry)
01382 {
01383   static char buffer[BUFSIZ];
01384   const char *result;
01385 
01386   if (aux_entry->x_file.x_n.x_zeroes == 0)
01387     {
01388       if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
01389         internal_error (__FILE__, __LINE__, _("coff file name too long"));
01390       strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
01391     }
01392   else
01393     {
01394       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
01395       buffer[FILNMLEN] = '\0';
01396     }
01397   result = buffer;
01398 
01399   /* FIXME: We should not be throwing away the information about what
01400      directory.  It should go into dirname of the symtab, or some such
01401      place.  */
01402   result = lbasename (result);
01403   return (result);
01404 }
01405 
01406 /* Support for line number handling.  */
01407 
01408 static char *linetab = NULL;
01409 static long linetab_offset;
01410 static unsigned long linetab_size;
01411 
01412 /* Read in all the line numbers for fast lookups later.  Leave them in
01413    external (unswapped) format in memory; we'll swap them as we enter
01414    them into GDB's data structures.  */
01415 
01416 static int
01417 init_lineno (bfd *abfd, long offset, int size)
01418 {
01419   int val;
01420 
01421   linetab_offset = offset;
01422   linetab_size = size;
01423 
01424   free_linetab ();
01425 
01426   if (size == 0)
01427     return 0;
01428 
01429   if (bfd_seek (abfd, offset, 0) < 0)
01430     return -1;
01431 
01432   /* Allocate the desired table, plus a sentinel.  */
01433   linetab = (char *) xmalloc (size + local_linesz);
01434 
01435   val = bfd_bread (linetab, size, abfd);
01436   if (val != size)
01437     return -1;
01438 
01439   /* Terminate it with an all-zero sentinel record.  */
01440   memset (linetab + size, 0, local_linesz);
01441 
01442   return 0;
01443 }
01444 
01445 static void
01446 free_linetab (void)
01447 {
01448   if (linetab)
01449     xfree (linetab);
01450   linetab = NULL;
01451 }
01452 
01453 static void
01454 free_linetab_cleanup (void *ignore)
01455 {
01456   free_linetab ();
01457 }
01458 
01459 #if !defined (L_LNNO32)
01460 #define L_LNNO32(lp) ((lp)->l_lnno)
01461 #endif
01462 
01463 static void
01464 enter_linenos (long file_offset, int first_line,
01465                int last_line, struct objfile *objfile)
01466 {
01467   struct gdbarch *gdbarch = get_objfile_arch (objfile);
01468   char *rawptr;
01469   struct internal_lineno lptr;
01470 
01471   if (!linetab)
01472     return;
01473   if (file_offset < linetab_offset)
01474     {
01475       complaint (&symfile_complaints,
01476                  _("Line number pointer %ld lower than start of line numbers"),
01477                  file_offset);
01478       if (file_offset > linetab_size)   /* Too big to be an offset?  */
01479         return;
01480       file_offset += linetab_offset;    /* Try reading at that linetab
01481                                            offset.  */
01482     }
01483 
01484   rawptr = &linetab[file_offset - linetab_offset];
01485 
01486   /* Skip first line entry for each function.  */
01487   rawptr += local_linesz;
01488   /* Line numbers start at one for the first line of the function.  */
01489   first_line--;
01490 
01491   /* If the line number table is full (e.g. 64K lines in COFF debug
01492      info), the next function's L_LNNO32 might not be zero, so don't
01493      overstep the table's end in any case.  */
01494   while (rawptr <= &linetab[0] + linetab_size)
01495     {
01496       bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
01497       rawptr += local_linesz;
01498       /* The next function, or the sentinel, will have L_LNNO32 zero;
01499          we exit.  */
01500       if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
01501         {
01502           CORE_ADDR addr = lptr.l_addr.l_paddr;
01503           addr += ANOFFSET (objfile->section_offsets,
01504                             SECT_OFF_TEXT (objfile));
01505           record_line (current_subfile,
01506                        first_line + L_LNNO32 (&lptr),
01507                        gdbarch_addr_bits_remove (gdbarch, addr));
01508         }
01509       else
01510         break;
01511     }
01512 }
01513 
01514 static void
01515 patch_type (struct type *type, struct type *real_type)
01516 {
01517   struct type *target = TYPE_TARGET_TYPE (type);
01518   struct type *real_target = TYPE_TARGET_TYPE (real_type);
01519   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
01520 
01521   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
01522   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
01523   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
01524                                                       field_size);
01525 
01526   memcpy (TYPE_FIELDS (target), 
01527           TYPE_FIELDS (real_target), 
01528           field_size);
01529 
01530   if (TYPE_NAME (real_target))
01531     {
01532       /* The previous copy of TYPE_NAME is allocated by
01533          process_coff_symbol.  */
01534       if (TYPE_NAME (target))
01535         xfree ((char*) TYPE_NAME (target));
01536       TYPE_NAME (target) = xstrdup (TYPE_NAME (real_target));
01537     }
01538 }
01539 
01540 /* Patch up all appropriate typedef symbols in the opaque_type_chains
01541    so that they can be used to print out opaque data structures
01542    properly.  */
01543 
01544 static void
01545 patch_opaque_types (struct symtab *s)
01546 {
01547   struct block *b;
01548   struct block_iterator iter;
01549   struct symbol *real_sym;
01550 
01551   /* Go through the per-file symbols only.  */
01552   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
01553   ALL_BLOCK_SYMBOLS (b, iter, real_sym)
01554     {
01555       /* Find completed typedefs to use to fix opaque ones.
01556          Remove syms from the chain when their types are stored,
01557          but search the whole chain, as there may be several syms
01558          from different files with the same name.  */
01559       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
01560           && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
01561           && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
01562           && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
01563         {
01564           const char *name = SYMBOL_LINKAGE_NAME (real_sym);
01565           int hash = hashname (name);
01566           struct symbol *sym, *prev;
01567 
01568           prev = 0;
01569           for (sym = opaque_type_chain[hash]; sym;)
01570             {
01571               if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
01572                   && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
01573                 {
01574                   if (prev)
01575                     {
01576                       SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
01577                     }
01578                   else
01579                     {
01580                       opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
01581                     }
01582 
01583                   patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
01584 
01585                   if (prev)
01586                     {
01587                       sym = SYMBOL_VALUE_CHAIN (prev);
01588                     }
01589                   else
01590                     {
01591                       sym = opaque_type_chain[hash];
01592                     }
01593                 }
01594               else
01595                 {
01596                   prev = sym;
01597                   sym = SYMBOL_VALUE_CHAIN (sym);
01598                 }
01599             }
01600         }
01601     }
01602 }
01603 
01604 static int
01605 coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
01606 {
01607   return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
01608 }
01609 
01610 static const struct symbol_register_ops coff_register_funcs = {
01611   coff_reg_to_regnum
01612 };
01613 
01614 /* The "aclass" index for computed COFF symbols.  */
01615 
01616 static int coff_register_index;
01617 
01618 static struct symbol *
01619 process_coff_symbol (struct coff_symbol *cs,
01620                      union internal_auxent *aux,
01621                      struct objfile *objfile)
01622 {
01623   struct symbol *sym = allocate_symbol (objfile);
01624   char *name;
01625 
01626   name = cs->c_name;
01627   name = EXTERNAL_NAME (name, objfile->obfd);
01628   SYMBOL_SET_LANGUAGE (sym, current_subfile->language,
01629                        &objfile->objfile_obstack);
01630   SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);
01631 
01632   /* default assumptions */
01633   SYMBOL_VALUE (sym) = cs->c_value;
01634   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
01635   SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
01636 
01637   if (ISFCN (cs->c_type))
01638     {
01639       SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
01640                                       SECT_OFF_TEXT (objfile));
01641       SYMBOL_TYPE (sym) =
01642         lookup_function_type (decode_function_type (cs, cs->c_type,
01643                                                     aux, objfile));
01644 
01645       SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
01646       if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
01647           || cs->c_sclass == C_THUMBSTATFUNC)
01648         add_symbol_to_list (sym, &file_symbols);
01649       else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
01650                || cs->c_sclass == C_THUMBEXTFUNC)
01651         add_symbol_to_list (sym, &global_symbols);
01652     }
01653   else
01654     {
01655       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
01656       switch (cs->c_sclass)
01657         {
01658         case C_NULL:
01659           break;
01660 
01661         case C_AUTO:
01662           SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
01663           add_symbol_to_list (sym, &local_symbols);
01664           break;
01665 
01666         case C_THUMBEXT:
01667         case C_THUMBEXTFUNC:
01668         case C_EXT:
01669           SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
01670           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
01671           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
01672                                                   SECT_OFF_TEXT (objfile));
01673           add_symbol_to_list (sym, &global_symbols);
01674           break;
01675 
01676         case C_THUMBSTAT:
01677         case C_THUMBSTATFUNC:
01678         case C_STAT:
01679           SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
01680           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
01681           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
01682                                                   SECT_OFF_TEXT (objfile));
01683           if (within_function)
01684             {
01685               /* Static symbol of local scope.  */
01686               add_symbol_to_list (sym, &local_symbols);
01687             }
01688           else
01689             {
01690               /* Static symbol at top level of file.  */
01691               add_symbol_to_list (sym, &file_symbols);
01692             }
01693           break;
01694 
01695 #ifdef C_GLBLREG                /* AMD coff */
01696         case C_GLBLREG:
01697 #endif
01698         case C_REG:
01699           SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
01700           SYMBOL_VALUE (sym) = cs->c_value;
01701           add_symbol_to_list (sym, &local_symbols);
01702           break;
01703 
01704         case C_THUMBLABEL:
01705         case C_LABEL:
01706           break;
01707 
01708         case C_ARG:
01709           SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
01710           SYMBOL_IS_ARGUMENT (sym) = 1;
01711           add_symbol_to_list (sym, &local_symbols);
01712           break;
01713 
01714         case C_REGPARM:
01715           SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
01716           SYMBOL_IS_ARGUMENT (sym) = 1;
01717           SYMBOL_VALUE (sym) = cs->c_value;
01718           add_symbol_to_list (sym, &local_symbols);
01719           break;
01720 
01721         case C_TPDEF:
01722           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
01723           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
01724 
01725           /* If type has no name, give it one.  */
01726           if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
01727             {
01728               if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
01729                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
01730                 {
01731                   /* If we are giving a name to a type such as
01732                      "pointer to foo" or "function returning foo", we
01733                      better not set the TYPE_NAME.  If the program
01734                      contains "typedef char *caddr_t;", we don't want 
01735                      all variables of type char * to print as caddr_t.
01736                      This is not just a consequence of GDB's type
01737                      management; CC and GCC (at least through version
01738                      2.4) both output variables of either type char *
01739                      or caddr_t with the type refering to the C_TPDEF
01740                      symbol for caddr_t.  If a future compiler cleans
01741                      this up it GDB is not ready for it yet, but if it
01742                      becomes ready we somehow need to disable this
01743                      check (without breaking the PCC/GCC2.4 case).
01744 
01745                      Sigh.
01746 
01747                      Fortunately, this check seems not to be necessary
01748                      for anything except pointers or functions.  */
01749                   ;
01750                 }
01751               else
01752                 TYPE_NAME (SYMBOL_TYPE (sym)) =
01753                   xstrdup (SYMBOL_LINKAGE_NAME (sym));
01754             }
01755 
01756           /* Keep track of any type which points to empty structured
01757              type, so it can be filled from a definition from another
01758              file.  A simple forward reference (TYPE_CODE_UNDEF) is
01759              not an empty structured type, though; the forward
01760              references work themselves out via the magic of
01761              coff_lookup_type.  */
01762           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
01763               && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
01764               && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
01765                  != TYPE_CODE_UNDEF)
01766             {
01767               int i = hashname (SYMBOL_LINKAGE_NAME (sym));
01768 
01769               SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
01770               opaque_type_chain[i] = sym;
01771             }
01772           add_symbol_to_list (sym, &file_symbols);
01773           break;
01774 
01775         case C_STRTAG:
01776         case C_UNTAG:
01777         case C_ENTAG:
01778           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
01779           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
01780 
01781           /* Some compilers try to be helpful by inventing "fake"
01782              names for anonymous enums, structures, and unions, like
01783              "~0fake" or ".0fake".  Thanks, but no thanks...  */
01784           if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
01785             if (SYMBOL_LINKAGE_NAME (sym) != NULL
01786                 && *SYMBOL_LINKAGE_NAME (sym) != '~'
01787                 && *SYMBOL_LINKAGE_NAME (sym) != '.')
01788               TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
01789                 concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);
01790 
01791           add_symbol_to_list (sym, &file_symbols);
01792           break;
01793 
01794         default:
01795           break;
01796         }
01797     }
01798   return sym;
01799 }
01800 
01801 /* Decode a coff type specifier;  return the type that is meant.  */
01802 
01803 static struct type *
01804 decode_type (struct coff_symbol *cs, unsigned int c_type,
01805              union internal_auxent *aux, struct objfile *objfile)
01806 {
01807   struct type *type = 0;
01808   unsigned int new_c_type;
01809 
01810   if (c_type & ~N_BTMASK)
01811     {
01812       new_c_type = DECREF (c_type);
01813       if (ISPTR (c_type))
01814         {
01815           type = decode_type (cs, new_c_type, aux, objfile);
01816           type = lookup_pointer_type (type);
01817         }
01818       else if (ISFCN (c_type))
01819         {
01820           type = decode_type (cs, new_c_type, aux, objfile);
01821           type = lookup_function_type (type);
01822         }
01823       else if (ISARY (c_type))
01824         {
01825           int i, n;
01826           unsigned short *dim;
01827           struct type *base_type, *index_type, *range_type;
01828 
01829           /* Define an array type.  */
01830           /* auxent refers to array, not base type.  */
01831           if (aux->x_sym.x_tagndx.l == 0)
01832             cs->c_naux = 0;
01833 
01834           /* Shift the indices down.  */
01835           dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
01836           i = 1;
01837           n = dim[0];
01838           for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
01839             *dim = *(dim + 1);
01840           *dim = 0;
01841 
01842           base_type = decode_type (cs, new_c_type, aux, objfile);
01843           index_type = objfile_type (objfile)->builtin_int;
01844           range_type =
01845             create_range_type ((struct type *) NULL, 
01846                                index_type, 0, n - 1);
01847           type =
01848             create_array_type ((struct type *) NULL, 
01849                                base_type, range_type);
01850         }
01851       return type;
01852     }
01853 
01854   /* Reference to existing type.  This only occurs with the struct,
01855      union, and enum types.  EPI a29k coff fakes us out by producing
01856      aux entries with a nonzero x_tagndx for definitions of structs,
01857      unions, and enums, so we have to check the c_sclass field.  SCO
01858      3.2v4 cc gets confused with pointers to pointers to defined
01859      structs, and generates negative x_tagndx fields.  */
01860   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
01861     {
01862       if (cs->c_sclass != C_STRTAG
01863           && cs->c_sclass != C_UNTAG
01864           && cs->c_sclass != C_ENTAG
01865           && aux->x_sym.x_tagndx.l >= 0)
01866         {
01867           type = coff_alloc_type (aux->x_sym.x_tagndx.l);
01868           return type;
01869         }
01870       else
01871         {
01872           complaint (&symfile_complaints,
01873                      _("Symbol table entry for %s has bad tagndx value"),
01874                      cs->c_name);
01875           /* And fall through to decode_base_type...  */
01876         }
01877     }
01878 
01879   return decode_base_type (cs, BTYPE (c_type), aux, objfile);
01880 }
01881 
01882 /* Decode a coff type specifier for function definition;
01883    return the type that the function returns.  */
01884 
01885 static struct type *
01886 decode_function_type (struct coff_symbol *cs, 
01887                       unsigned int c_type,
01888                       union internal_auxent *aux, 
01889                       struct objfile *objfile)
01890 {
01891   if (aux->x_sym.x_tagndx.l == 0)
01892     cs->c_naux = 0;     /* auxent refers to function, not base
01893                            type.  */
01894 
01895   return decode_type (cs, DECREF (c_type), aux, objfile);
01896 }
01897 
01898 /* Basic C types.  */
01899 
01900 static struct type *
01901 decode_base_type (struct coff_symbol *cs, 
01902                   unsigned int c_type,
01903                   union internal_auxent *aux, 
01904                   struct objfile *objfile)
01905 {
01906   struct gdbarch *gdbarch = get_objfile_arch (objfile);
01907   struct type *type;
01908 
01909   switch (c_type)
01910     {
01911     case T_NULL:
01912       /* Shows up with "void (*foo)();" structure members.  */
01913       return objfile_type (objfile)->builtin_void;
01914 
01915 #ifdef T_VOID
01916     case T_VOID:
01917       /* Intel 960 COFF has this symbol and meaning.  */
01918       return objfile_type (objfile)->builtin_void;
01919 #endif
01920 
01921     case T_CHAR:
01922       return objfile_type (objfile)->builtin_char;
01923 
01924     case T_SHORT:
01925       return objfile_type (objfile)->builtin_short;
01926 
01927     case T_INT:
01928       return objfile_type (objfile)->builtin_int;
01929 
01930     case T_LONG:
01931       if (cs->c_sclass == C_FIELD
01932           && aux->x_sym.x_misc.x_lnsz.x_size
01933              > gdbarch_long_bit (gdbarch))
01934         return objfile_type (objfile)->builtin_long_long;
01935       else
01936         return objfile_type (objfile)->builtin_long;
01937 
01938     case T_FLOAT:
01939       return objfile_type (objfile)->builtin_float;
01940 
01941     case T_DOUBLE:
01942       return objfile_type (objfile)->builtin_double;
01943 
01944     case T_LNGDBL:
01945       return objfile_type (objfile)->builtin_long_double;
01946 
01947     case T_STRUCT:
01948       if (cs->c_naux != 1)
01949         {
01950           /* Anonymous structure type.  */
01951           type = coff_alloc_type (cs->c_symnum);
01952           TYPE_CODE (type) = TYPE_CODE_STRUCT;
01953           TYPE_NAME (type) = NULL;
01954           /* This used to set the tag to "<opaque>".  But I think
01955              setting it to NULL is right, and the printing code can
01956              print it as "struct {...}".  */
01957           TYPE_TAG_NAME (type) = NULL;
01958           INIT_CPLUS_SPECIFIC (type);
01959           TYPE_LENGTH (type) = 0;
01960           TYPE_FIELDS (type) = 0;
01961           TYPE_NFIELDS (type) = 0;
01962         }
01963       else
01964         {
01965           type = coff_read_struct_type (cs->c_symnum,
01966                                         aux->x_sym.x_misc.x_lnsz.x_size,
01967                                         aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
01968                                         objfile);
01969         }
01970       return type;
01971 
01972     case T_UNION:
01973       if (cs->c_naux != 1)
01974         {
01975           /* Anonymous union type.  */
01976           type = coff_alloc_type (cs->c_symnum);
01977           TYPE_NAME (type) = NULL;
01978           /* This used to set the tag to "<opaque>".  But I think
01979              setting it to NULL is right, and the printing code can
01980              print it as "union {...}".  */
01981           TYPE_TAG_NAME (type) = NULL;
01982           INIT_CPLUS_SPECIFIC (type);
01983           TYPE_LENGTH (type) = 0;
01984           TYPE_FIELDS (type) = 0;
01985           TYPE_NFIELDS (type) = 0;
01986         }
01987       else
01988         {
01989           type = coff_read_struct_type (cs->c_symnum,
01990                                         aux->x_sym.x_misc.x_lnsz.x_size,
01991                                         aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
01992                                         objfile);
01993         }
01994       TYPE_CODE (type) = TYPE_CODE_UNION;
01995       return type;
01996 
01997     case T_ENUM:
01998       if (cs->c_naux != 1)
01999         {
02000           /* Anonymous enum type.  */
02001           type = coff_alloc_type (cs->c_symnum);
02002           TYPE_CODE (type) = TYPE_CODE_ENUM;
02003           TYPE_NAME (type) = NULL;
02004           /* This used to set the tag to "<opaque>".  But I think
02005              setting it to NULL is right, and the printing code can
02006              print it as "enum {...}".  */
02007           TYPE_TAG_NAME (type) = NULL;
02008           TYPE_LENGTH (type) = 0;
02009           TYPE_FIELDS (type) = 0;
02010           TYPE_NFIELDS (type) = 0;
02011         }
02012       else
02013         {
02014           type = coff_read_enum_type (cs->c_symnum,
02015                                       aux->x_sym.x_misc.x_lnsz.x_size,
02016                                       aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
02017                                       objfile);
02018         }
02019       return type;
02020 
02021     case T_MOE:
02022       /* Shouldn't show up here.  */
02023       break;
02024 
02025     case T_UCHAR:
02026       return objfile_type (objfile)->builtin_unsigned_char;
02027 
02028     case T_USHORT:
02029       return objfile_type (objfile)->builtin_unsigned_short;
02030 
02031     case T_UINT:
02032       return objfile_type (objfile)->builtin_unsigned_int;
02033 
02034     case T_ULONG:
02035       if (cs->c_sclass == C_FIELD
02036           && aux->x_sym.x_misc.x_lnsz.x_size
02037              > gdbarch_long_bit (gdbarch))
02038         return objfile_type (objfile)->builtin_unsigned_long_long;
02039       else
02040         return objfile_type (objfile)->builtin_unsigned_long;
02041     }
02042   complaint (&symfile_complaints, 
02043              _("Unexpected type for symbol %s"), cs->c_name);
02044   return objfile_type (objfile)->builtin_void;
02045 }
02046 
02047 /* This page contains subroutines of read_type.  */
02048 
02049 /* Read the description of a structure (or union type) and return an
02050    object describing the type.  */
02051 
02052 static struct type *
02053 coff_read_struct_type (int index, int length, int lastsym,
02054                        struct objfile *objfile)
02055 {
02056   struct nextfield
02057     {
02058       struct nextfield *next;
02059       struct field field;
02060     };
02061 
02062   struct type *type;
02063   struct nextfield *list = 0;
02064   struct nextfield *new;
02065   int nfields = 0;
02066   int n;
02067   char *name;
02068   struct coff_symbol member_sym;
02069   struct coff_symbol *ms = &member_sym;
02070   struct internal_syment sub_sym;
02071   union internal_auxent sub_aux;
02072   int done = 0;
02073 
02074   type = coff_alloc_type (index);
02075   TYPE_CODE (type) = TYPE_CODE_STRUCT;
02076   INIT_CPLUS_SPECIFIC (type);
02077   TYPE_LENGTH (type) = length;
02078 
02079   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
02080     {
02081       read_one_sym (ms, &sub_sym, &sub_aux);
02082       name = ms->c_name;
02083       name = EXTERNAL_NAME (name, objfile->obfd);
02084 
02085       switch (ms->c_sclass)
02086         {
02087         case C_MOS:
02088         case C_MOU:
02089 
02090           /* Get space to record the next field's data.  */
02091           new = (struct nextfield *) alloca (sizeof (struct nextfield));
02092           new->next = list;
02093           list = new;
02094 
02095           /* Save the data.  */
02096           list->field.name = obstack_copy0 (&objfile->objfile_obstack,
02097                                             name, strlen (name));
02098           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
02099                                                   &sub_aux, objfile);
02100           SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
02101           FIELD_BITSIZE (list->field) = 0;
02102           nfields++;
02103           break;
02104 
02105         case C_FIELD:
02106 
02107           /* Get space to record the next field's data.  */
02108           new = (struct nextfield *) alloca (sizeof (struct nextfield));
02109           new->next = list;
02110           list = new;
02111 
02112           /* Save the data.  */
02113           list->field.name = obstack_copy0 (&objfile->objfile_obstack,
02114                                             name, strlen (name));
02115           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
02116                                                   &sub_aux, objfile);
02117           SET_FIELD_BITPOS (list->field, ms->c_value);
02118           FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
02119           nfields++;
02120           break;
02121 
02122         case C_EOS:
02123           done = 1;
02124           break;
02125         }
02126     }
02127   /* Now create the vector of fields, and record how big it is.  */
02128 
02129   TYPE_NFIELDS (type) = nfields;
02130   TYPE_FIELDS (type) = (struct field *)
02131     TYPE_ALLOC (type, sizeof (struct field) * nfields);
02132 
02133   /* Copy the saved-up fields into the field vector.  */
02134 
02135   for (n = nfields; list; list = list->next)
02136     TYPE_FIELD (type, --n) = list->field;
02137 
02138   return type;
02139 }
02140 
02141 /* Read a definition of an enumeration type,
02142    and create and return a suitable type object.
02143    Also defines the symbols that represent the values of the type.  */
02144 
02145 static struct type *
02146 coff_read_enum_type (int index, int length, int lastsym,
02147                      struct objfile *objfile)
02148 {
02149   struct gdbarch *gdbarch = get_objfile_arch (objfile);
02150   struct symbol *sym;
02151   struct type *type;
02152   int nsyms = 0;
02153   int done = 0;
02154   struct pending **symlist;
02155   struct coff_symbol member_sym;
02156   struct coff_symbol *ms = &member_sym;
02157   struct internal_syment sub_sym;
02158   union internal_auxent sub_aux;
02159   struct pending *osyms, *syms;
02160   int o_nsyms;
02161   int n;
02162   char *name;
02163   int unsigned_enum = 1;
02164 
02165   type = coff_alloc_type (index);
02166   if (within_function)
02167     symlist = &local_symbols;
02168   else
02169     symlist = &file_symbols;
02170   osyms = *symlist;
02171   o_nsyms = osyms ? osyms->nsyms : 0;
02172 
02173   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
02174     {
02175       read_one_sym (ms, &sub_sym, &sub_aux);
02176       name = ms->c_name;
02177       name = EXTERNAL_NAME (name, objfile->obfd);
02178 
02179       switch (ms->c_sclass)
02180         {
02181         case C_MOE:
02182           sym = allocate_symbol (objfile);
02183 
02184           SYMBOL_SET_LINKAGE_NAME (sym,
02185                                    obstack_copy0 (&objfile->objfile_obstack,
02186                                                   name, strlen (name)));
02187           SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
02188           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
02189           SYMBOL_VALUE (sym) = ms->c_value;
02190           add_symbol_to_list (sym, symlist);
02191           nsyms++;
02192           break;
02193 
02194         case C_EOS:
02195           /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
02196              up the count of how many symbols to read.  So stop
02197              on .eos.  */
02198           done = 1;
02199           break;
02200         }
02201     }
02202 
02203   /* Now fill in the fields of the type-structure.  */
02204 
02205   if (length > 0)
02206     TYPE_LENGTH (type) = length;
02207   else /* Assume ints.  */
02208     TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
02209   TYPE_CODE (type) = TYPE_CODE_ENUM;
02210   TYPE_NFIELDS (type) = nsyms;
02211   TYPE_FIELDS (type) = (struct field *)
02212     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
02213 
02214   /* Find the symbols for the values and put them into the type.
02215      The symbols can be found in the symlist that we put them on
02216      to cause them to be defined.  osyms contains the old value
02217      of that symlist; everything up to there was defined by us.  */
02218   /* Note that we preserve the order of the enum constants, so
02219      that in something like "enum {FOO, LAST_THING=FOO}" we print
02220      FOO, not LAST_THING.  */
02221 
02222   for (syms = *symlist, n = 0; syms; syms = syms->next)
02223     {
02224       int j = 0;
02225 
02226       if (syms == osyms)
02227         j = o_nsyms;
02228       for (; j < syms->nsyms; j++, n++)
02229         {
02230           struct symbol *xsym = syms->symbol[j];
02231 
02232           SYMBOL_TYPE (xsym) = type;
02233           TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
02234           SET_FIELD_ENUMVAL (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
02235           if (SYMBOL_VALUE (xsym) < 0)
02236             unsigned_enum = 0;
02237           TYPE_FIELD_BITSIZE (type, n) = 0;
02238         }
02239       if (syms == osyms)
02240         break;
02241     }
02242 
02243   if (unsigned_enum)
02244     TYPE_UNSIGNED (type) = 1;
02245 
02246   return type;
02247 }
02248 
02249 /* Register our ability to parse symbols for coff BFD files.  */
02250 
02251 static const struct sym_fns coff_sym_fns =
02252 {
02253   coff_new_init,                /* sym_new_init: init anything gbl to
02254                                    entire symtab */
02255   coff_symfile_init,            /* sym_init: read initial info, setup
02256                                    for sym_read() */
02257   coff_symfile_read,            /* sym_read: read a symbol file into
02258                                    symtab */
02259   NULL,                         /* sym_read_psymbols */
02260   coff_symfile_finish,          /* sym_finish: finished with file,
02261                                    cleanup */
02262   default_symfile_offsets,      /* sym_offsets: xlate external to
02263                                    internal form */
02264   default_symfile_segments,     /* sym_segments: Get segment
02265                                    information from a file */
02266   NULL,                         /* sym_read_linetable  */
02267 
02268   default_symfile_relocate,     /* sym_relocate: Relocate a debug
02269                                    section.  */
02270   NULL,                         /* sym_probe_fns */
02271   &psym_functions
02272 };
02273 
02274 /* Free the per-objfile COFF data.  */
02275 
02276 static void
02277 coff_free_info (struct objfile *objfile, void *arg)
02278 {
02279   xfree (arg);
02280 }
02281 
02282 void
02283 _initialize_coffread (void)
02284 {
02285   add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);
02286 
02287   coff_objfile_data_key = register_objfile_data_with_cleanup (NULL,
02288                                                               coff_free_info);
02289 
02290   coff_register_index
02291     = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);
02292 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines