gdb/coffread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3.    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "demangle.h"
  19. #include "breakpoint.h"

  20. #include "bfd.h"
  21. #include "gdb_obstack.h"
  22. #include <ctype.h>

  23. #include "coff/internal.h"        /* Internal format of COFF symbols in BFD */
  24. #include "libcoff.h"                /* FIXME secret internal data from BFD */
  25. #include "objfiles.h"
  26. #include "buildsym.h"
  27. #include "gdb-stabs.h"
  28. #include "stabsread.h"
  29. #include "complaints.h"
  30. #include "target.h"
  31. #include "block.h"
  32. #include "dictionary.h"

  33. #include "coff-pe-read.h"

  34. #include "psymtab.h"

  35. extern void _initialize_coffread (void);

  36. /* Key for COFF-associated data.  */

  37. static const struct objfile_data *coff_objfile_data_key;

  38. /* The objfile we are currently reading.  */

  39. static struct objfile *coffread_objfile;

  40. struct coff_symfile_info
  41.   {
  42.     file_ptr min_lineno_offset;        /* Where in file lowest line#s are.  */
  43.     file_ptr max_lineno_offset;        /* 1+last byte of line#s in file.  */

  44.     CORE_ADDR textaddr;                /* Addr of .text section.  */
  45.     unsigned int textsize;        /* Size of .text section.  */
  46.     struct stab_section_list *stabsects;        /* .stab sections.  */
  47.     asection *stabstrsect;        /* Section pointer for .stab section.  */
  48.     char *stabstrdata;
  49.   };

  50. /* Translate an external name string into a user-visible name.  */
  51. #define        EXTERNAL_NAME(string, abfd) \
  52.         (string[0] == bfd_get_symbol_leading_char (abfd) \
  53.         ? string + 1 : string)

  54. /* To be an sdb debug type, type must have at least a basic or primary
  55.    derived type.  Using this rather than checking against T_NULL is
  56.    said to prevent core dumps if we try to operate on Michael Bloom
  57.    dbx-in-coff file.  */

  58. #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))

  59. /* Core address of start and end of text of current source file.
  60.    This comes from a ".text" symbol where x_nlinno > 0.  */

  61. static CORE_ADDR current_source_start_addr;
  62. static CORE_ADDR current_source_end_addr;

  63. /* The addresses of the symbol table stream and number of symbols
  64.    of the object file we are reading (as copied into core).  */

  65. static bfd *nlist_bfd_global;
  66. static int nlist_nsyms_global;


  67. /* Pointers to scratch storage, used for reading raw symbols and
  68.    auxents.  */

  69. static char *temp_sym;
  70. static char *temp_aux;

  71. /* Local variables that hold the shift and mask values for the
  72.    COFF file that we are currently reading.  These come back to us
  73.    from BFD, and are referenced by their macro names, as well as
  74.    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
  75.    macros from include/coff/internal.h .  */

  76. static unsigned local_n_btmask;
  77. static unsigned local_n_btshft;
  78. static unsigned local_n_tmask;
  79. static unsigned local_n_tshift;

  80. #define        N_BTMASK        local_n_btmask
  81. #define        N_BTSHFT        local_n_btshft
  82. #define        N_TMASK                local_n_tmask
  83. #define        N_TSHIFT        local_n_tshift

  84. /* Local variables that hold the sizes in the file of various COFF
  85.    structures.  (We only need to know this to read them from the file
  86.    -- BFD will then translate the data in them, into `internal_xxx'
  87.    structs in the right byte order, alignment, etc.)  */

  88. static unsigned local_linesz;
  89. static unsigned local_symesz;
  90. static unsigned local_auxesz;

  91. /* This is set if this is a PE format file.  */

  92. static int pe_file;

  93. /* Chain of typedefs of pointers to empty struct/union types.
  94.    They are chained thru the SYMBOL_VALUE_CHAIN.  */

  95. static struct symbol *opaque_type_chain[HASHSIZE];

  96. /* Simplified internal version of coff symbol table information.  */

  97. struct coff_symbol
  98.   {
  99.     char *c_name;
  100.     int c_symnum;                /* Symbol number of this entry.  */
  101.     int c_naux;                        /* 0 if syment only, 1 if syment +
  102.                                    auxent, etc.  */
  103.     CORE_ADDR c_value;
  104.     int c_sclass;
  105.     int c_secnum;
  106.     unsigned int c_type;
  107.   };

  108. /* Vector of types defined so far, indexed by their type numbers.  */

  109. static struct type **type_vector;

  110. /* Number of elements allocated for type_vector currently.  */

  111. static int type_vector_length;

  112. /* Initial size of type vector.  Is realloc'd larger if needed, and
  113.    realloc'd down to the size actually used, when completed.  */

  114. #define INITIAL_TYPE_VECTOR_LENGTH 160

  115. extern void stabsread_clear_cache (void);

  116. static struct type *coff_read_struct_type (int, int, int,
  117.                                            struct objfile *);

  118. static struct type *decode_base_type (struct coff_symbol *,
  119.                                       unsigned int,
  120.                                       union internal_auxent *,
  121.                                       struct objfile *);

  122. static struct type *decode_type (struct coff_symbol *, unsigned int,
  123.                                  union internal_auxent *,
  124.                                  struct objfile *);

  125. static struct type *decode_function_type (struct coff_symbol *,
  126.                                           unsigned int,
  127.                                           union internal_auxent *,
  128.                                           struct objfile *);

  129. static struct type *coff_read_enum_type (int, int, int,
  130.                                          struct objfile *);

  131. static struct symbol *process_coff_symbol (struct coff_symbol *,
  132.                                            union internal_auxent *,
  133.                                            struct objfile *);

  134. static void patch_opaque_types (struct symtab *);

  135. static void enter_linenos (long, int, int, struct objfile *);

  136. static void free_linetab (void);

  137. static void free_linetab_cleanup (void *ignore);

  138. static int init_lineno (bfd *, long, int);

  139. static char *getsymname (struct internal_syment *);

  140. static const char *coff_getfilename (union internal_auxent *);

  141. static void free_stringtab (void);

  142. static void free_stringtab_cleanup (void *ignore);

  143. static int init_stringtab (bfd *, long);

  144. static void read_one_sym (struct coff_symbol *,
  145.                           struct internal_syment *,
  146.                           union internal_auxent *);

  147. static void coff_symtab_read (long, unsigned int, struct objfile *);

  148. /* We are called once per section from coff_symfile_read.  We
  149.    need to examine each section we are passed, check to see
  150.    if it is something we are interested in processing, and
  151.    if so, stash away some access information for the section.

  152.    FIXME: The section names should not be hardwired strings (what
  153.    should they be?  I don't think most object file formats have enough
  154.    section flags to specify what kind of debug section it is
  155.    -kingdon).  */

  156. static void
  157. coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
  158. {
  159.   struct coff_symfile_info *csi;
  160.   const char *name;

  161.   csi = (struct coff_symfile_info *) csip;
  162.   name = bfd_get_section_name (abfd, sectp);
  163.   if (strcmp (name, ".text") == 0)
  164.     {
  165.       csi->textaddr = bfd_section_vma (abfd, sectp);
  166.       csi->textsize += bfd_section_size (abfd, sectp);
  167.     }
  168.   else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
  169.     {
  170.       csi->textsize += bfd_section_size (abfd, sectp);
  171.     }
  172.   else if (strcmp (name, ".stabstr") == 0)
  173.     {
  174.       csi->stabstrsect = sectp;
  175.     }
  176.   else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
  177.     {
  178.       const char *s;

  179.       /* We can have multiple .stab sections if linked with
  180.          --split-by-reloc.  */
  181.       for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
  182.         if (!isdigit (*s))
  183.           break;
  184.       if (*s == '\0')
  185.         {
  186.           struct stab_section_list *n, **pn;

  187.           n = ((struct stab_section_list *)
  188.                xmalloc (sizeof (struct stab_section_list)));
  189.           n->section = sectp;
  190.           n->next = NULL;
  191.           for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
  192.             ;
  193.           *pn = n;

  194.           /* This will be run after coffstab_build_psymtabs is called
  195.              in coff_symfile_read, at which point we no longer need
  196.              the information.  */
  197.           make_cleanup (xfree, n);
  198.         }
  199.     }
  200. }

  201. /* Return the section_offsets* that CS points to.  */
  202. static int cs_to_section (struct coff_symbol *, struct objfile *);

  203. struct find_targ_sec_arg
  204.   {
  205.     int targ_index;
  206.     asection **resultp;
  207.   };

  208. static void
  209. find_targ_sec (bfd *abfd, asection *sect, void *obj)
  210. {
  211.   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;

  212.   if (sect->target_index == args->targ_index)
  213.     *args->resultp = sect;
  214. }

  215. /* Return the bfd_section that CS points to.  */
  216. static struct bfd_section*
  217. cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
  218. {
  219.   asection *sect = NULL;
  220.   struct find_targ_sec_arg args;

  221.   args.targ_index = cs->c_secnum;
  222.   args.resultp = &sect;
  223.   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
  224.   return sect;
  225. }

  226. /* Return the section number (SECT_OFF_*) that CS points to.  */
  227. static int
  228. cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
  229. {
  230.   asection *sect = cs_to_bfd_section (cs, objfile);

  231.   if (sect == NULL)
  232.     return SECT_OFF_TEXT (objfile);
  233.   return gdb_bfd_section_index (objfile->obfd, sect);
  234. }

  235. /* Return the address of the section of a COFF symbol.  */

  236. static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);

  237. static CORE_ADDR
  238. cs_section_address (struct coff_symbol *cs, bfd *abfd)
  239. {
  240.   asection *sect = NULL;
  241.   struct find_targ_sec_arg args;
  242.   CORE_ADDR addr = 0;

  243.   args.targ_index = cs->c_secnum;
  244.   args.resultp = &sect;
  245.   bfd_map_over_sections (abfd, find_targ_sec, &args);
  246.   if (sect != NULL)
  247.     addr = bfd_get_section_vma (abfd, sect);
  248.   return addr;
  249. }

  250. /* Look up a coff type-number index.  Return the address of the slot
  251.    where the type for that index is stored.
  252.    The type-number is in INDEX.

  253.    This can be used for finding the type associated with that index
  254.    or for associating a new type with the index.  */

  255. static struct type **
  256. coff_lookup_type (int index)
  257. {
  258.   if (index >= type_vector_length)
  259.     {
  260.       int old_vector_length = type_vector_length;

  261.       type_vector_length *= 2;
  262.       if (index /* is still */  >= type_vector_length)
  263.         type_vector_length = index * 2;

  264.       type_vector = (struct type **)
  265.         xrealloc ((char *) type_vector,
  266.                   type_vector_length * sizeof (struct type *));
  267.       memset (&type_vector[old_vector_length], 0,
  268.          (type_vector_length - old_vector_length) * sizeof (struct type *));
  269.     }
  270.   return &type_vector[index];
  271. }

  272. /* Make sure there is a type allocated for type number index
  273.    and return the type object.
  274.    This can create an empty (zeroed) type object.  */

  275. static struct type *
  276. coff_alloc_type (int index)
  277. {
  278.   struct type **type_addr = coff_lookup_type (index);
  279.   struct type *type = *type_addr;

  280.   /* If we are referring to a type not known at all yet,
  281.      allocate an empty type for it.
  282.      We will fill it in later if we find out how.  */
  283.   if (type == NULL)
  284.     {
  285.       type = alloc_type (coffread_objfile);
  286.       *type_addr = type;
  287.     }
  288.   return type;
  289. }

  290. /* Start a new symtab for a new source file.
  291.    This is called when a COFF ".file" symbol is seen;
  292.    it indicates the start of data for one original source file.  */

  293. static void
  294. coff_start_symtab (struct objfile *objfile, const char *name)
  295. {
  296.   start_symtab (objfile,
  297.   /* We fill in the filename later.  start_symtab puts this pointer
  298.      into last_source_file and we put it in subfiles->name, which
  299.      end_symtab frees; that's why it must be malloc'd.  */
  300.                  xstrdup (name),
  301.   /* We never know the directory name for COFF.  */
  302.                  NULL,
  303.   /* The start address is irrelevant, since we set
  304.      last_source_start_addr in coff_end_symtab.  */
  305.                  0);
  306.   record_debugformat ("COFF");
  307. }

  308. /* Save the vital information from when starting to read a file,
  309.    for use when closing off the current file.
  310.    NAME is the file name the symbols came from, START_ADDR is the
  311.    first text address for the file, and SIZE is the number of bytes of
  312.    text.  */

  313. static void
  314. complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
  315. {
  316.   set_last_source_file (name);
  317.   current_source_start_addr = start_addr;
  318.   current_source_end_addr = start_addr + size;
  319. }

  320. /* Finish the symbol definitions for one main source file, close off
  321.    all the lexical contexts for that file (creating struct block's for
  322.    them), then make the struct symtab for that file and put it in the
  323.    list of all such.  */

  324. static void
  325. coff_end_symtab (struct objfile *objfile)
  326. {
  327.   last_source_start_addr = current_source_start_addr;

  328.   end_symtab (current_source_end_addr, SECT_OFF_TEXT (objfile));

  329.   /* Reinitialize for beginning of new file.  */
  330.   set_last_source_file (NULL);
  331. }

  332. /* The linker sometimes generates some non-function symbols inside
  333.    functions referencing variables imported from another DLL.
  334.    Return nonzero if the given symbol corresponds to one of them.  */

  335. static int
  336. is_import_fixup_symbol (struct coff_symbol *cs,
  337.                         enum minimal_symbol_type type)
  338. {
  339.   /* The following is a bit of a heuristic using the characterictics
  340.      of these fixup symbols, but should work well in practice...  */
  341.   int i;

  342.   /* Must be a non-static text symbol.  */
  343.   if (type != mst_text)
  344.     return 0;

  345.   /* Must be a non-function symbol.  */
  346.   if (ISFCN (cs->c_type))
  347.     return 0;

  348.   /* The name must start with "__fu<digits>__".  */
  349.   if (strncmp (cs->c_name, "__fu", 4) != 0)
  350.     return 0;
  351.   if (! isdigit (cs->c_name[4]))
  352.     return 0;
  353.   for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
  354.     /* Nothing, just incrementing index past all digits.  */;
  355.   if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
  356.     return 0;

  357.   return 1;
  358. }

  359. static struct minimal_symbol *
  360. record_minimal_symbol (struct coff_symbol *cs, CORE_ADDR address,
  361.                        enum minimal_symbol_type type, int section,
  362.                        struct objfile *objfile)
  363. {
  364.   /* We don't want TDESC entry points in the minimal symbol table.  */
  365.   if (cs->c_name[0] == '@')
  366.     return NULL;

  367.   if (is_import_fixup_symbol (cs, type))
  368.     {
  369.       /* Because the value of these symbols is within a function code
  370.          range, these symbols interfere with the symbol-from-address
  371.          reverse lookup; this manifests itselfs in backtraces, or any
  372.          other commands that prints symbolic addresses.  Just pretend
  373.          these symbols do not exist.  */
  374.       return NULL;
  375.     }

  376.   return prim_record_minimal_symbol_and_info (cs->c_name, address,
  377.                                               type, section, objfile);
  378. }

  379. /* coff_symfile_init ()
  380.    is the coff-specific initialization routine for reading symbols.
  381.    It is passed a struct objfile which contains, among other things,
  382.    the BFD for the file whose symbols are being read, and a slot for
  383.    a pointer to "private data" which we fill with cookies and other
  384.    treats for coff_symfile_read ().

  385.    We will only be called if this is a COFF or COFF-like file.  BFD
  386.    handles figuring out the format of the file, and code in symtab.c
  387.    uses BFD's determination to vector to us.

  388.    The ultimate result is a new symtab (or, FIXME, eventually a
  389.    psymtab).  */

  390. static void
  391. coff_symfile_init (struct objfile *objfile)
  392. {
  393.   struct dbx_symfile_info *dbx;
  394.   struct coff_symfile_info *coff;

  395.   /* Allocate struct to keep track of stab reading.  */
  396.   dbx = XCNEW (struct dbx_symfile_info);
  397.   set_objfile_data (objfile, dbx_objfile_data_key, dbx);

  398.   /* Allocate struct to keep track of the symfile.  */
  399.   coff = XCNEW (struct coff_symfile_info);
  400.   set_objfile_data (objfile, coff_objfile_data_key, coff);

  401.   /* COFF objects may be reordered, so set OBJF_REORDERED.  If we
  402.      find this causes a significant slowdown in gdb then we could
  403.      set it in the debug symbol readers only when necessary.  */
  404.   objfile->flags |= OBJF_REORDERED;
  405. }

  406. /* This function is called for every section; it finds the outer
  407.    limits of the line table (minimum and maximum file offset) so that
  408.    the mainline code can read the whole thing for efficiency.  */

  409. static void
  410. find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
  411. {
  412.   struct coff_symfile_info *info;
  413.   int size, count;
  414.   file_ptr offset, maxoff;

  415.   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  416.   count = asect->lineno_count;
  417.   /* End of warning.  */

  418.   if (count == 0)
  419.     return;
  420.   size = count * local_linesz;

  421.   info = (struct coff_symfile_info *) vpinfo;
  422.   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  423.   offset = asect->line_filepos;
  424.   /* End of warning.  */

  425.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  426.     info->min_lineno_offset = offset;

  427.   maxoff = offset + size;
  428.   if (maxoff > info->max_lineno_offset)
  429.     info->max_lineno_offset = maxoff;
  430. }


  431. /* The BFD for this file -- only good while we're actively reading
  432.    symbols into a psymtab or a symtab.  */

  433. static bfd *symfile_bfd;

  434. /* Read a symbol file, after initialization by coff_symfile_init.  */

  435. static void
  436. coff_symfile_read (struct objfile *objfile, int symfile_flags)
  437. {
  438.   struct coff_symfile_info *info;
  439.   struct dbx_symfile_info *dbxinfo;
  440.   bfd *abfd = objfile->obfd;
  441.   coff_data_type *cdata = coff_data (abfd);
  442.   char *name = bfd_get_filename (abfd);
  443.   int val;
  444.   unsigned int num_symbols;
  445.   int symtab_offset;
  446.   int stringtab_offset;
  447.   struct cleanup *back_to, *cleanup_minimal_symbols;
  448.   int stabstrsize;

  449.   info = objfile_data (objfile, coff_objfile_data_key);
  450.   dbxinfo = DBX_SYMFILE_INFO (objfile);
  451.   symfile_bfd = abfd;                /* Kludge for swap routines.  */

  452. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  453.   num_symbols = bfd_get_symcount (abfd);        /* How many syms */
  454.   symtab_offset = cdata->sym_filepos;        /* Symbol table file offset */
  455.   stringtab_offset = symtab_offset +        /* String table file offset */
  456.     num_symbols * cdata->local_symesz;

  457.   /* Set a few file-statics that give us specific information about
  458.      the particular COFF file format we're reading.  */
  459.   local_n_btmask = cdata->local_n_btmask;
  460.   local_n_btshft = cdata->local_n_btshft;
  461.   local_n_tmask = cdata->local_n_tmask;
  462.   local_n_tshift = cdata->local_n_tshift;
  463.   local_linesz = cdata->local_linesz;
  464.   local_symesz = cdata->local_symesz;
  465.   local_auxesz = cdata->local_auxesz;

  466.   /* Allocate space for raw symbol and aux entries, based on their
  467.      space requirements as reported by BFD.  */
  468.   temp_sym = (char *) xmalloc
  469.     (cdata->local_symesz + cdata->local_auxesz);
  470.   temp_aux = temp_sym + cdata->local_symesz;
  471.   back_to = make_cleanup (free_current_contents, &temp_sym);

  472.   /* We need to know whether this is a PE file, because in PE files,
  473.      unlike standard COFF files, symbol values are stored as offsets
  474.      from the section address, rather than as absolute addresses.
  475.      FIXME: We should use BFD to read the symbol table, and thus avoid
  476.      this problem.  */
  477.   pe_file =
  478.     strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0
  479.     || strncmp (bfd_get_target (objfile->obfd), "epoc-pe", 7) == 0;

  480.   /* End of warning.  */

  481.   info->min_lineno_offset = 0;
  482.   info->max_lineno_offset = 0;

  483.   /* Only read line number information if we have symbols.

  484.      On Windows NT, some of the system's DLL's have sections with
  485.      PointerToLinenumbers fields that are non-zero, but point at
  486.      random places within the image file.  (In the case I found,
  487.      KERNEL32.DLL's .text section has a line number info pointer that
  488.      points into the middle of the string `lib\\i386\kernel32.dll'.)

  489.      However, these DLL's also have no symbols.  The line number
  490.      tables are meaningless without symbols.  And in fact, GDB never
  491.      uses the line number information unless there are symbols.  So we
  492.      can avoid spurious error messages (and maybe run a little
  493.      faster!) by not even reading the line number table unless we have
  494.      symbols.  */
  495.   if (num_symbols > 0)
  496.     {
  497.       /* Read the line number table, all at once.  */
  498.       bfd_map_over_sections (abfd, find_linenos, (void *) info);

  499.       make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
  500.       val = init_lineno (abfd, info->min_lineno_offset,
  501.                          info->max_lineno_offset - info->min_lineno_offset);
  502.       if (val < 0)
  503.         error (_("\"%s\": error reading line numbers."), name);
  504.     }

  505.   /* Now read the string table, all at once.  */

  506.   make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
  507.   val = init_stringtab (abfd, stringtab_offset);
  508.   if (val < 0)
  509.     error (_("\"%s\": can't get string table"), name);

  510.   init_minimal_symbol_collection ();
  511.   cleanup_minimal_symbols = make_cleanup_discard_minimal_symbols ();

  512.   /* Now that the executable file is positioned at symbol table,
  513.      process it and define symbols accordingly.  */

  514.   coff_symtab_read ((long) symtab_offset, num_symbols, objfile);

  515.   /* Install any minimal symbols that have been collected as the
  516.      current minimal symbols for this objfile.  */

  517.   install_minimal_symbols (objfile);

  518.   if (pe_file)
  519.     {
  520.       struct minimal_symbol *msym;

  521.       ALL_OBJFILE_MSYMBOLS (objfile, msym)
  522.         {
  523.           const char *name = MSYMBOL_LINKAGE_NAME (msym);

  524.           /* If the minimal symbols whose name are prefixed by "__imp_"
  525.              or "_imp_", get rid of the prefix, and search the minimal
  526.              symbol in OBJFILE.  Note that 'maintenance print msymbols'
  527.              shows that type of these "_imp_XXXX" symbols is mst_data.  */
  528.           if (MSYMBOL_TYPE (msym) == mst_data
  529.               && (strncmp (name, "__imp_", 6) == 0
  530.                   || strncmp (name, "_imp_", 5) == 0))
  531.             {
  532.               const char *name1 = (name[1] == '_' ? &name[7] : &name[6]);
  533.               struct bound_minimal_symbol found;

  534.               found = lookup_minimal_symbol (name1, NULL, objfile);
  535.               /* If found, there are symbols named "_imp_foo" and "foo"
  536.                  respectively in OBJFILE.  Set the type of symbol "foo"
  537.                  as 'mst_solib_trampoline'.  */
  538.               if (found.minsym != NULL
  539.                   && MSYMBOL_TYPE (found.minsym) == mst_text)
  540.                 MSYMBOL_TYPE (found.minsym) = mst_solib_trampoline;
  541.             }
  542.         }
  543.     }

  544.   /* Free the installed minimal symbol data.  */
  545.   do_cleanups (cleanup_minimal_symbols);

  546.   bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);

  547.   if (info->stabsects)
  548.     {
  549.       if (!info->stabstrsect)
  550.         {
  551.           error (_("The debugging information in `%s' is corrupted.\nThe "
  552.                    "file has a `.stabs' section, but no `.stabstr' section."),
  553.                  name);
  554.         }

  555.       /* FIXME: dubious.  Why can't we use something normal like
  556.          bfd_get_section_contents?  */
  557.       bfd_seek (abfd, abfd->where, 0);

  558.       stabstrsize = bfd_section_size (abfd, info->stabstrsect);

  559.       coffstab_build_psymtabs (objfile,
  560.                                info->textaddr, info->textsize,
  561.                                info->stabsects,
  562.                                info->stabstrsect->filepos, stabstrsize);
  563.     }
  564.   if (dwarf2_has_info (objfile, NULL))
  565.     {
  566.       /* DWARF2 sections.  */
  567.       dwarf2_build_psymtabs (objfile);
  568.     }

  569.   dwarf2_build_frame_info (objfile);

  570.   /* Try to add separate debug file if no symbols table found.   */
  571.   if (!objfile_has_partial_symbols (objfile))
  572.     {
  573.       char *debugfile;

  574.       debugfile = find_separate_debug_file_by_debuglink (objfile);
  575.       make_cleanup (xfree, debugfile);

  576.       if (debugfile)
  577.         {
  578.           bfd *abfd = symfile_bfd_open (debugfile);

  579.           make_cleanup_bfd_unref (abfd);
  580.           symbol_file_add_separate (abfd, debugfile, symfile_flags, objfile);
  581.         }
  582.     }

  583.   do_cleanups (back_to);
  584. }

  585. static void
  586. coff_new_init (struct objfile *ignore)
  587. {
  588. }

  589. /* Perform any local cleanups required when we are done with a
  590.    particular objfileI.E, we are in the process of discarding all
  591.    symbol information for an objfile, freeing up all memory held for
  592.    it, and unlinking the objfile struct from the global list of known
  593.    objfiles.  */

  594. static void
  595. coff_symfile_finish (struct objfile *objfile)
  596. {
  597.   /* Let stabs reader clean up.  */
  598.   stabsread_clear_cache ();

  599.   dwarf2_free_objfile (objfile);
  600. }


  601. /* Given pointers to a symbol table in coff style exec file,
  602.    analyze them and create struct symtab's describing the symbols.
  603.    NSYMS is the number of symbols in the symbol table.
  604.    We read them one at a time using read_one_sym ().  */

  605. static void
  606. coff_symtab_read (long symtab_offset, unsigned int nsyms,
  607.                   struct objfile *objfile)
  608. {
  609.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  610.   struct context_stack *new;
  611.   struct coff_symbol coff_symbol;
  612.   struct coff_symbol *cs = &coff_symbol;
  613.   static struct internal_syment main_sym;
  614.   static union internal_auxent main_aux;
  615.   struct coff_symbol fcn_cs_saved;
  616.   static struct internal_syment fcn_sym_saved;
  617.   static union internal_auxent fcn_aux_saved;
  618.   /* A .file is open.  */
  619.   int in_source_file = 0;
  620.   int next_file_symnum = -1;
  621.   /* Name of the current file.  */
  622.   const char *filestring = "";
  623.   int depth = 0;
  624.   int fcn_first_line = 0;
  625.   CORE_ADDR fcn_first_line_addr = 0;
  626.   int fcn_last_line = 0;
  627.   int fcn_start_addr = 0;
  628.   long fcn_line_ptr = 0;
  629.   int val;
  630.   CORE_ADDR tmpaddr;
  631.   struct minimal_symbol *msym;

  632.   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
  633.      it's hard to know I've really worked around it.  The fix should
  634.      be harmless, anyway).  The symptom of the bug is that the first
  635.      fread (in read_one_sym), will (in my example) actually get data
  636.      from file offset 268, when the fseek was to 264 (and ftell shows
  637.      264).  This causes all hell to break loose.  I was unable to
  638.      reproduce this on a short test program which operated on the same
  639.      file, performing (I think) the same sequence of operations.

  640.      It stopped happening when I put in this (former) rewind().

  641.      FIXME: Find out if this has been reported to Sun, whether it has
  642.      been fixed in a later release, etc.  */

  643.   bfd_seek (objfile->obfd, 0, 0);

  644.   /* Position to read the symbol table.  */
  645.   val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
  646.   if (val < 0)
  647.     perror_with_name (objfile_name (objfile));

  648.   coffread_objfile = objfile;
  649.   nlist_bfd_global = objfile->obfd;
  650.   nlist_nsyms_global = nsyms;
  651.   set_last_source_file (NULL);
  652.   memset (opaque_type_chain, 0, sizeof opaque_type_chain);

  653.   if (type_vector)                /* Get rid of previous one.  */
  654.     xfree (type_vector);
  655.   type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
  656.   type_vector = (struct type **)
  657.     xmalloc (type_vector_length * sizeof (struct type *));
  658.   memset (type_vector, 0, type_vector_length * sizeof (struct type *));

  659.   coff_start_symtab (objfile, "");

  660.   symnum = 0;
  661.   while (symnum < nsyms)
  662.     {
  663.       QUIT;                        /* Make this command interruptable.  */

  664.       read_one_sym (cs, &main_sym, &main_aux);

  665.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  666.         {
  667.           if (get_last_source_file ())
  668.             coff_end_symtab (objfile);

  669.           coff_start_symtab (objfile, "_globals_");
  670.           /* coff_start_symtab will set the language of this symtab to
  671.              language_unknown, since such a ``file name'' is not
  672.              recognized.  Override that with the minimal language to
  673.              allow printing values in this symtab.  */
  674.           current_subfile->language = language_minimal;
  675.           complete_symtab ("_globals_", 0, 0);
  676.           /* Done with all files, everything from here on out is
  677.              globals.  */
  678.         }

  679.       /* Special case for file with type declarations only, no
  680.          text.  */
  681.       if (!get_last_source_file () && SDB_TYPE (cs->c_type)
  682.           && cs->c_secnum == N_DEBUG)
  683.         complete_symtab (filestring, 0, 0);

  684.       /* Typedefs should not be treated as symbol definitions.  */
  685.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  686.         {
  687.           /* Record all functions -- external and static -- in
  688.              minsyms.  */
  689.           int section = cs_to_section (cs, objfile);

  690.           tmpaddr = cs->c_value;
  691.           record_minimal_symbol (cs, tmpaddr, mst_text,
  692.                                  section, objfile);

  693.           fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  694.           fcn_start_addr = tmpaddr;
  695.           fcn_cs_saved = *cs;
  696.           fcn_sym_saved = main_sym;
  697.           fcn_aux_saved = main_aux;
  698.           continue;
  699.         }

  700.       switch (cs->c_sclass)
  701.         {
  702.         case C_EFCN:
  703.         case C_EXTDEF:
  704.         case C_ULABEL:
  705.         case C_USTATIC:
  706.         case C_LINE:
  707.         case C_ALIAS:
  708.         case C_HIDDEN:
  709.           complaint (&symfile_complaints,
  710.                      _("Bad n_sclass for symbol %s"),
  711.                      cs->c_name);
  712.           break;

  713.         case C_FILE:
  714.           /* c_value field contains symnum of next .file entry in
  715.              table or symnum of first global after last .file.  */
  716.           next_file_symnum = cs->c_value;
  717.           if (cs->c_naux > 0)
  718.             filestring = coff_getfilename (&main_aux);
  719.           else
  720.             filestring = "";

  721.           /* Complete symbol table for last object file
  722.              containing debugging information.  */
  723.           if (get_last_source_file ())
  724.             {
  725.               coff_end_symtab (objfile);
  726.               coff_start_symtab (objfile, filestring);
  727.             }
  728.           in_source_file = 1;
  729.           break;

  730.           /* C_LABEL is used for labels and static functions.
  731.              Including it here allows gdb to see static functions when
  732.              no debug info is available.  */
  733.         case C_LABEL:
  734.           /* However, labels within a function can make weird
  735.              backtraces, so filter them out (from phdm@macqel.be).  */
  736.           if (within_function)
  737.             break;
  738.         case C_STAT:
  739.         case C_THUMBLABEL:
  740.         case C_THUMBSTAT:
  741.         case C_THUMBSTATFUNC:
  742.           if (cs->c_name[0] == '.')
  743.             {
  744.               if (strcmp (cs->c_name, ".text") == 0)
  745.                 {
  746.                   /* FIXME: don't wire in ".text" as section name or
  747.                      symbol name!  */
  748.                   /* Check for in_source_file deals with case of a
  749.                      file with debugging symbols followed by a later
  750.                      file with no symbols.  */
  751.                   if (in_source_file)
  752.                     complete_symtab (filestring,
  753.                     cs->c_value + ANOFFSET (objfile->section_offsets,
  754.                                             SECT_OFF_TEXT (objfile)),
  755.                                      main_aux.x_scn.x_scnlen);
  756.                   in_source_file = 0;
  757.                 }
  758.               /* Flush rest of '.' symbols.  */
  759.               break;
  760.             }
  761.           else if (!SDB_TYPE (cs->c_type)
  762.                    && cs->c_name[0] == 'L'
  763.                    && (strncmp (cs->c_name, "LI%", 3) == 0
  764.                        || strncmp (cs->c_name, "LF%", 3) == 0
  765.                        || strncmp (cs->c_name, "LC%", 3) == 0
  766.                        || strncmp (cs->c_name, "LP%", 3) == 0
  767.                        || strncmp (cs->c_name, "LPB%", 4) == 0
  768.                        || strncmp (cs->c_name, "LBB%", 4) == 0
  769.                        || strncmp (cs->c_name, "LBE%", 4) == 0
  770.                        || strncmp (cs->c_name, "LPBX%", 5) == 0))
  771.             /* At least on a 3b1, gcc generates swbeg and string labels
  772.                that look like this.  Ignore them.  */
  773.             break;
  774.           /* Fall in for static symbols that don't start with '.'  */
  775.         case C_THUMBEXT:
  776.         case C_THUMBEXTFUNC:
  777.         case C_EXT:
  778.           {
  779.             /* Record it in the minimal symbols regardless of
  780.                SDB_TYPE.  This parallels what we do for other debug
  781.                formats, and probably is needed to make
  782.                print_address_symbolic work right without the (now
  783.                gone) "set fast-symbolic-addr off" kludge.  */

  784.             enum minimal_symbol_type ms_type;
  785.             int sec;
  786.             CORE_ADDR offset = 0;

  787.             if (cs->c_secnum == N_UNDEF)
  788.               {
  789.                 /* This is a common symbol.  We used to rely on
  790.                    the target to tell us whether it knows where
  791.                    the symbol has been relocated to, but none of
  792.                    the target implementations actually provided
  793.                    that operation.  So we just ignore the symbol,
  794.                    the same way we would do if we had a target-side
  795.                    symbol lookup which returned no match.  */
  796.                 break;
  797.               }
  798.              else if (cs->c_secnum == N_ABS)
  799.                {
  800.                  /* Use the correct minimal symbol type (and don't
  801.                     relocate) for absolute values.  */
  802.                  ms_type = mst_abs;
  803.                  sec = cs_to_section (cs, objfile);
  804.                  tmpaddr = cs->c_value;
  805.                }
  806.             else
  807.               {
  808.                 asection *bfd_section = cs_to_bfd_section (cs, objfile);

  809.                 sec = cs_to_section (cs, objfile);
  810.                 tmpaddr = cs->c_value;
  811.                  /* Statics in a PE file also get relocated.  */
  812.                  if (cs->c_sclass == C_EXT
  813.                      || cs->c_sclass == C_THUMBEXTFUNC
  814.                      || cs->c_sclass == C_THUMBEXT
  815.                      || (pe_file && (cs->c_sclass == C_STAT)))
  816.                   offset = ANOFFSET (objfile->section_offsets, sec);

  817.                 if (bfd_section->flags & SEC_CODE)
  818.                   {
  819.                     ms_type =
  820.                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
  821.                       || cs->c_sclass == C_THUMBEXT ?
  822.                       mst_text : mst_file_text;
  823.                     tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
  824.                   }
  825.                 else if (bfd_section->flags & SEC_ALLOC
  826.                          && bfd_section->flags & SEC_LOAD)
  827.                   {
  828.                     ms_type =
  829.                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
  830.                       ? mst_data : mst_file_data;
  831.                   }
  832.                 else if (bfd_section->flags & SEC_ALLOC)
  833.                   {
  834.                     ms_type =
  835.                       cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
  836.                       ? mst_bss : mst_file_bss;
  837.                   }
  838.                 else
  839.                   ms_type = mst_unknown;
  840.               }

  841.             msym = record_minimal_symbol (cs, tmpaddr, ms_type,
  842.                                           sec, objfile);
  843.             if (msym)
  844.               gdbarch_coff_make_msymbol_special (gdbarch,
  845.                                                  cs->c_sclass, msym);

  846.             if (SDB_TYPE (cs->c_type))
  847.               {
  848.                 struct symbol *sym;

  849.                 sym = process_coff_symbol
  850.                   (cs, &main_aux, objfile);
  851.                 SYMBOL_VALUE (sym) = tmpaddr + offset;
  852.                 SYMBOL_SECTION (sym) = sec;
  853.               }
  854.           }
  855.           break;

  856.         case C_FCN:
  857.           if (strcmp (cs->c_name, ".bf") == 0)
  858.             {
  859.               within_function = 1;

  860.               /* Value contains address of first non-init type
  861.                  code.  */
  862.               /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
  863.                  contains line number of '{' }.  */
  864.               if (cs->c_naux != 1)
  865.                 complaint (&symfile_complaints,
  866.                            _("`.bf' symbol %d has no aux entry"),
  867.                            cs->c_symnum);
  868.               fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  869.               fcn_first_line_addr = cs->c_value;

  870.               /* Might want to check that locals are 0 and
  871.                  context_stack_depth is zero, and complain if not.  */

  872.               depth = 0;
  873.               new = push_context (depth, fcn_start_addr);
  874.               fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
  875.               new->name =
  876.                 process_coff_symbol (&fcn_cs_saved,
  877.                                      &fcn_aux_saved, objfile);
  878.             }
  879.           else if (strcmp (cs->c_name, ".ef") == 0)
  880.             {
  881.               if (!within_function)
  882.                 error (_("Bad coff function information."));
  883.               /* The value of .ef is the address of epilogue code;
  884.                  not useful for gdb.  */
  885.               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  886.                  contains number of lines to '}' */

  887.               if (context_stack_depth <= 0)
  888.                 {        /* We attempted to pop an empty context stack.  */
  889.                   complaint (&symfile_complaints,
  890.                              _("`.ef' symbol without matching `.bf' "
  891.                                "symbol ignored starting at symnum %d"),
  892.                              cs->c_symnum);
  893.                   within_function = 0;
  894.                   break;
  895.                 }

  896.               new = pop_context ();
  897.               /* Stack must be empty now.  */
  898.               if (context_stack_depth > 0 || new == NULL)
  899.                 {
  900.                   complaint (&symfile_complaints,
  901.                              _("Unmatched .ef symbol(s) ignored "
  902.                                "starting at symnum %d"),
  903.                              cs->c_symnum);
  904.                   within_function = 0;
  905.                   break;
  906.                 }
  907.               if (cs->c_naux != 1)
  908.                 {
  909.                   complaint (&symfile_complaints,
  910.                              _("`.ef' symbol %d has no aux entry"),
  911.                              cs->c_symnum);
  912.                   fcn_last_line = 0x7FFFFFFF;
  913.                 }
  914.               else
  915.                 {
  916.                   fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  917.                 }
  918.               /* fcn_first_line is the line number of the opening '{'.
  919.                  Do not record it - because it would affect gdb's idea
  920.                  of the line number of the first statement of the
  921.                  function - except for one-line functions, for which
  922.                  it is also the line number of all the statements and
  923.                  of the closing '}', and for which we do not have any
  924.                  other statement-line-number.  */
  925.               if (fcn_last_line == 1)
  926.                 record_line (current_subfile, fcn_first_line,
  927.                              gdbarch_addr_bits_remove (gdbarch,
  928.                                                        fcn_first_line_addr));
  929.               else
  930.                 enter_linenos (fcn_line_ptr, fcn_first_line,
  931.                                fcn_last_line, objfile);

  932.               finish_block (new->name, &local_symbols,
  933.                             new->old_blocks, new->start_addr,
  934.                             fcn_cs_saved.c_value
  935.                             + fcn_aux_saved.x_sym.x_misc.x_fsize
  936.                             + ANOFFSET (objfile->section_offsets,
  937.                                         SECT_OFF_TEXT (objfile)));
  938.               within_function = 0;
  939.             }
  940.           break;

  941.         case C_BLOCK:
  942.           if (strcmp (cs->c_name, ".bb") == 0)
  943.             {
  944.               tmpaddr = cs->c_value;
  945.               tmpaddr += ANOFFSET (objfile->section_offsets,
  946.                                    SECT_OFF_TEXT (objfile));
  947.               push_context (++depth, tmpaddr);
  948.             }
  949.           else if (strcmp (cs->c_name, ".eb") == 0)
  950.             {
  951.               if (context_stack_depth <= 0)
  952.                 {        /* We attempted to pop an empty context stack.  */
  953.                   complaint (&symfile_complaints,
  954.                              _("`.eb' symbol without matching `.bb' "
  955.                                "symbol ignored starting at symnum %d"),
  956.                              cs->c_symnum);
  957.                   break;
  958.                 }

  959.               new = pop_context ();
  960.               if (depth-- != new->depth)
  961.                 {
  962.                   complaint (&symfile_complaints,
  963.                              _("Mismatched .eb symbol ignored "
  964.                                "starting at symnum %d"),
  965.                              symnum);
  966.                   break;
  967.                 }
  968.               if (local_symbols && context_stack_depth > 0)
  969.                 {
  970.                   tmpaddr =
  971.                     cs->c_value + ANOFFSET (objfile->section_offsets,
  972.                                             SECT_OFF_TEXT (objfile));
  973.                   /* Make a block for the local symbols within.  */
  974.                   finish_block (0, &local_symbols, new->old_blocks,
  975.                                 new->start_addr, tmpaddr);
  976.                 }
  977.               /* Now pop locals of block just finished.  */
  978.               local_symbols = new->locals;
  979.             }
  980.           break;

  981.         default:
  982.           process_coff_symbol (cs, &main_aux, objfile);
  983.           break;
  984.         }
  985.     }

  986.   if ((nsyms == 0) && (pe_file))
  987.     {
  988.       /* We've got no debugging symbols, but it's a portable
  989.          executable, so try to read the export table.  */
  990.       read_pe_exported_syms (objfile);
  991.     }

  992.   if (get_last_source_file ())
  993.     coff_end_symtab (objfile);

  994.   /* Patch up any opaque types (references to types that are not defined
  995.      in the file where they are referenced, e.g. "struct foo *bar").  */
  996.   {
  997.     struct compunit_symtab *cu;
  998.     struct symtab *s;

  999.     ALL_OBJFILE_FILETABS (objfile, cu, s)
  1000.       patch_opaque_types (s);
  1001.   }

  1002.   coffread_objfile = NULL;
  1003. }

  1004. /* Routines for reading headers and symbols from executable.  */

  1005. /* Read the next symbol, swap it, and return it in both
  1006.    internal_syment form, and coff_symbol form.  Also return its first
  1007.    auxent, if any, in internal_auxent form, and skip any other
  1008.    auxents.  */

  1009. static void
  1010. read_one_sym (struct coff_symbol *cs,
  1011.               struct internal_syment *sym,
  1012.               union internal_auxent *aux)
  1013. {
  1014.   int i;
  1015.   bfd_size_type bytes;

  1016.   cs->c_symnum = symnum;
  1017.   bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
  1018.   if (bytes != local_symesz)
  1019.     error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
  1020.   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
  1021.   cs->c_naux = sym->n_numaux & 0xff;
  1022.   if (cs->c_naux >= 1)
  1023.     {
  1024.       bytes  = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
  1025.       if (bytes != local_auxesz)
  1026.         error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
  1027.       bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
  1028.                             sym->n_type, sym->n_sclass,
  1029.                             0, cs->c_naux, (char *) aux);
  1030.       /* If more than one aux entry, read past it (only the first aux
  1031.          is important).  */
  1032.       for (i = 1; i < cs->c_naux; i++)
  1033.         {
  1034.           bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
  1035.           if (bytes != local_auxesz)
  1036.             error (_("%s: error reading symbols"),
  1037.                    objfile_name (coffread_objfile));
  1038.         }
  1039.     }
  1040.   cs->c_name = getsymname (sym);
  1041.   cs->c_value = sym->n_value;
  1042.   cs->c_sclass = (sym->n_sclass & 0xff);
  1043.   cs->c_secnum = sym->n_scnum;
  1044.   cs->c_type = (unsigned) sym->n_type;
  1045.   if (!SDB_TYPE (cs->c_type))
  1046.     cs->c_type = 0;

  1047. #if 0
  1048.   if (cs->c_sclass & 128)
  1049.     printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
  1050. #endif

  1051.   symnum += 1 + cs->c_naux;

  1052.   /* The PE file format stores symbol values as offsets within the
  1053.      section, rather than as absolute addresses.  We correct that
  1054.      here, if the symbol has an appropriate storage class.  FIXME: We
  1055.      should use BFD to read the symbols, rather than duplicating the
  1056.      work here.  */
  1057.   if (pe_file)
  1058.     {
  1059.       switch (cs->c_sclass)
  1060.         {
  1061.         case C_EXT:
  1062.         case C_THUMBEXT:
  1063.         case C_THUMBEXTFUNC:
  1064.         case C_SECTION:
  1065.         case C_NT_WEAK:
  1066.         case C_STAT:
  1067.         case C_THUMBSTAT:
  1068.         case C_THUMBSTATFUNC:
  1069.         case C_LABEL:
  1070.         case C_THUMBLABEL:
  1071.         case C_BLOCK:
  1072.         case C_FCN:
  1073.         case C_EFCN:
  1074.           if (cs->c_secnum != 0)
  1075.             cs->c_value += cs_section_address (cs, symfile_bfd);
  1076.           break;
  1077.         }
  1078.     }
  1079. }

  1080. /* Support for string table handling.  */

  1081. static char *stringtab = NULL;

  1082. static int
  1083. init_stringtab (bfd *abfd, long offset)
  1084. {
  1085.   long length;
  1086.   int val;
  1087.   unsigned char lengthbuf[4];

  1088.   free_stringtab ();

  1089.   /* If the file is stripped, the offset might be zero, indicating no
  1090.      string table.  Just return with `stringtab' set to null.  */
  1091.   if (offset == 0)
  1092.     return 0;

  1093.   if (bfd_seek (abfd, offset, 0) < 0)
  1094.     return -1;

  1095.   val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
  1096.   length = bfd_h_get_32 (symfile_bfd, lengthbuf);

  1097.   /* If no string table is needed, then the file may end immediately
  1098.      after the symbols.  Just return with `stringtab' set to null.  */
  1099.   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
  1100.     return 0;

  1101.   stringtab = (char *) xmalloc (length);
  1102.   /* This is in target format (probably not very useful, and not
  1103.      currently used), not host format.  */
  1104.   memcpy (stringtab, lengthbuf, sizeof lengthbuf);
  1105.   if (length == sizeof length)        /* Empty table -- just the count.  */
  1106.     return 0;

  1107.   val = bfd_bread (stringtab + sizeof lengthbuf,
  1108.                    length - sizeof lengthbuf, abfd);
  1109.   if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
  1110.     return -1;

  1111.   return 0;
  1112. }

  1113. static void
  1114. free_stringtab (void)
  1115. {
  1116.   if (stringtab)
  1117.     xfree (stringtab);
  1118.   stringtab = NULL;
  1119. }

  1120. static void
  1121. free_stringtab_cleanup (void *ignore)
  1122. {
  1123.   free_stringtab ();
  1124. }

  1125. static char *
  1126. getsymname (struct internal_syment *symbol_entry)
  1127. {
  1128.   static char buffer[SYMNMLEN + 1];
  1129.   char *result;

  1130.   if (symbol_entry->_n._n_n._n_zeroes == 0)
  1131.     {
  1132.       /* FIXME: Probably should be detecting corrupt symbol files by
  1133.          seeing whether offset points to within the stringtab.  */
  1134.       result = stringtab + symbol_entry->_n._n_n._n_offset;
  1135.     }
  1136.   else
  1137.     {
  1138.       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
  1139.       buffer[SYMNMLEN] = '\0';
  1140.       result = buffer;
  1141.     }
  1142.   return result;
  1143. }

  1144. /* Extract the file name from the aux entry of a C_FILE symbol.
  1145.    Return only the last component of the name.  Result is in static
  1146.    storage and is only good for temporary use.  */

  1147. static const char *
  1148. coff_getfilename (union internal_auxent *aux_entry)
  1149. {
  1150.   static char buffer[BUFSIZ];
  1151.   const char *result;

  1152.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1153.     {
  1154.       if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
  1155.         internal_error (__FILE__, __LINE__, _("coff file name too long"));
  1156.       strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
  1157.     }
  1158.   else
  1159.     {
  1160.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1161.       buffer[FILNMLEN] = '\0';
  1162.     }
  1163.   result = buffer;

  1164.   /* FIXME: We should not be throwing away the information about what
  1165.      directory.  It should go into dirname of the symtab, or some such
  1166.      place.  */
  1167.   result = lbasename (result);
  1168.   return (result);
  1169. }

  1170. /* Support for line number handling.  */

  1171. static char *linetab = NULL;
  1172. static long linetab_offset;
  1173. static unsigned long linetab_size;

  1174. /* Read in all the line numbers for fast lookups later.  Leave them in
  1175.    external (unswapped) format in memory; we'll swap them as we enter
  1176.    them into GDB's data structures.  */

  1177. static int
  1178. init_lineno (bfd *abfd, long offset, int size)
  1179. {
  1180.   int val;

  1181.   linetab_offset = offset;
  1182.   linetab_size = size;

  1183.   free_linetab ();

  1184.   if (size == 0)
  1185.     return 0;

  1186.   if (bfd_seek (abfd, offset, 0) < 0)
  1187.     return -1;

  1188.   /* Allocate the desired table, plus a sentinel.  */
  1189.   linetab = (char *) xmalloc (size + local_linesz);

  1190.   val = bfd_bread (linetab, size, abfd);
  1191.   if (val != size)
  1192.     return -1;

  1193.   /* Terminate it with an all-zero sentinel record.  */
  1194.   memset (linetab + size, 0, local_linesz);

  1195.   return 0;
  1196. }

  1197. static void
  1198. free_linetab (void)
  1199. {
  1200.   if (linetab)
  1201.     xfree (linetab);
  1202.   linetab = NULL;
  1203. }

  1204. static void
  1205. free_linetab_cleanup (void *ignore)
  1206. {
  1207.   free_linetab ();
  1208. }

  1209. #if !defined (L_LNNO32)
  1210. #define L_LNNO32(lp) ((lp)->l_lnno)
  1211. #endif

  1212. static void
  1213. enter_linenos (long file_offset, int first_line,
  1214.                int last_line, struct objfile *objfile)
  1215. {
  1216.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1217.   char *rawptr;
  1218.   struct internal_lineno lptr;

  1219.   if (!linetab)
  1220.     return;
  1221.   if (file_offset < linetab_offset)
  1222.     {
  1223.       complaint (&symfile_complaints,
  1224.                  _("Line number pointer %ld lower than start of line numbers"),
  1225.                  file_offset);
  1226.       if (file_offset > linetab_size)        /* Too big to be an offset?  */
  1227.         return;
  1228.       file_offset += linetab_offset;        /* Try reading at that linetab
  1229.                                            offset.  */
  1230.     }

  1231.   rawptr = &linetab[file_offset - linetab_offset];

  1232.   /* Skip first line entry for each function.  */
  1233.   rawptr += local_linesz;
  1234.   /* Line numbers start at one for the first line of the function.  */
  1235.   first_line--;

  1236.   /* If the line number table is full (e.g. 64K lines in COFF debug
  1237.      info), the next function's L_LNNO32 might not be zero, so don't
  1238.      overstep the table's end in any case.  */
  1239.   while (rawptr <= &linetab[0] + linetab_size)
  1240.     {
  1241.       bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
  1242.       rawptr += local_linesz;
  1243.       /* The next function, or the sentinel, will have L_LNNO32 zero;
  1244.          we exit.  */
  1245.       if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
  1246.         {
  1247.           CORE_ADDR addr = lptr.l_addr.l_paddr;
  1248.           addr += ANOFFSET (objfile->section_offsets,
  1249.                             SECT_OFF_TEXT (objfile));
  1250.           record_line (current_subfile,
  1251.                        first_line + L_LNNO32 (&lptr),
  1252.                        gdbarch_addr_bits_remove (gdbarch, addr));
  1253.         }
  1254.       else
  1255.         break;
  1256.     }
  1257. }

  1258. static void
  1259. patch_type (struct type *type, struct type *real_type)
  1260. {
  1261.   struct type *target = TYPE_TARGET_TYPE (type);
  1262.   struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1263.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);

  1264.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1265.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1266.   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
  1267.                                                       field_size);

  1268.   memcpy (TYPE_FIELDS (target),
  1269.           TYPE_FIELDS (real_target),
  1270.           field_size);

  1271.   if (TYPE_NAME (real_target))
  1272.     {
  1273.       /* The previous copy of TYPE_NAME is allocated by
  1274.          process_coff_symbol.  */
  1275.       if (TYPE_NAME (target))
  1276.         xfree ((char*) TYPE_NAME (target));
  1277.       TYPE_NAME (target) = xstrdup (TYPE_NAME (real_target));
  1278.     }
  1279. }

  1280. /* Patch up all appropriate typedef symbols in the opaque_type_chains
  1281.    so that they can be used to print out opaque data structures
  1282.    properly.  */

  1283. static void
  1284. patch_opaque_types (struct symtab *s)
  1285. {
  1286.   struct block *b;
  1287.   struct block_iterator iter;
  1288.   struct symbol *real_sym;

  1289.   /* Go through the per-file symbols only.  */
  1290.   b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), STATIC_BLOCK);
  1291.   ALL_BLOCK_SYMBOLS (b, iter, real_sym)
  1292.     {
  1293.       /* Find completed typedefs to use to fix opaque ones.
  1294.          Remove syms from the chain when their types are stored,
  1295.          but search the whole chain, as there may be several syms
  1296.          from different files with the same name.  */
  1297.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
  1298.           && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
  1299.           && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
  1300.           && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1301.         {
  1302.           const char *name = SYMBOL_LINKAGE_NAME (real_sym);
  1303.           int hash = hashname (name);
  1304.           struct symbol *sym, *prev;

  1305.           prev = 0;
  1306.           for (sym = opaque_type_chain[hash]; sym;)
  1307.             {
  1308.               if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
  1309.                   && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
  1310.                 {
  1311.                   if (prev)
  1312.                     {
  1313.                       SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
  1314.                     }
  1315.                   else
  1316.                     {
  1317.                       opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
  1318.                     }

  1319.                   patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));

  1320.                   if (prev)
  1321.                     {
  1322.                       sym = SYMBOL_VALUE_CHAIN (prev);
  1323.                     }
  1324.                   else
  1325.                     {
  1326.                       sym = opaque_type_chain[hash];
  1327.                     }
  1328.                 }
  1329.               else
  1330.                 {
  1331.                   prev = sym;
  1332.                   sym = SYMBOL_VALUE_CHAIN (sym);
  1333.                 }
  1334.             }
  1335.         }
  1336.     }
  1337. }

  1338. static int
  1339. coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
  1340. {
  1341.   return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
  1342. }

  1343. static const struct symbol_register_ops coff_register_funcs = {
  1344.   coff_reg_to_regnum
  1345. };

  1346. /* The "aclass" index for computed COFF symbols.  */

  1347. static int coff_register_index;

  1348. static struct symbol *
  1349. process_coff_symbol (struct coff_symbol *cs,
  1350.                      union internal_auxent *aux,
  1351.                      struct objfile *objfile)
  1352. {
  1353.   struct symbol *sym = allocate_symbol (objfile);
  1354.   char *name;

  1355.   name = cs->c_name;
  1356.   name = EXTERNAL_NAME (name, objfile->obfd);
  1357.   SYMBOL_SET_LANGUAGE (sym, current_subfile->language,
  1358.                        &objfile->objfile_obstack);
  1359.   SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);

  1360.   /* default assumptions */
  1361.   SYMBOL_VALUE (sym) = cs->c_value;
  1362.   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  1363.   SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);

  1364.   if (ISFCN (cs->c_type))
  1365.     {
  1366.       SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
  1367.                                       SECT_OFF_TEXT (objfile));
  1368.       SYMBOL_TYPE (sym) =
  1369.         lookup_function_type (decode_function_type (cs, cs->c_type,
  1370.                                                     aux, objfile));

  1371.       SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
  1372.       if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
  1373.           || cs->c_sclass == C_THUMBSTATFUNC)
  1374.         add_symbol_to_list (sym, &file_symbols);
  1375.       else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
  1376.                || cs->c_sclass == C_THUMBEXTFUNC)
  1377.         add_symbol_to_list (sym, &global_symbols);
  1378.     }
  1379.   else
  1380.     {
  1381.       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
  1382.       switch (cs->c_sclass)
  1383.         {
  1384.         case C_NULL:
  1385.           break;

  1386.         case C_AUTO:
  1387.           SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
  1388.           add_symbol_to_list (sym, &local_symbols);
  1389.           break;

  1390.         case C_THUMBEXT:
  1391.         case C_THUMBEXTFUNC:
  1392.         case C_EXT:
  1393.           SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
  1394.           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1395.           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
  1396.                                                   SECT_OFF_TEXT (objfile));
  1397.           add_symbol_to_list (sym, &global_symbols);
  1398.           break;

  1399.         case C_THUMBSTAT:
  1400.         case C_THUMBSTATFUNC:
  1401.         case C_STAT:
  1402.           SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
  1403.           SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1404.           SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
  1405.                                                   SECT_OFF_TEXT (objfile));
  1406.           if (within_function)
  1407.             {
  1408.               /* Static symbol of local scope.  */
  1409.               add_symbol_to_list (sym, &local_symbols);
  1410.             }
  1411.           else
  1412.             {
  1413.               /* Static symbol at top level of file.  */
  1414.               add_symbol_to_list (sym, &file_symbols);
  1415.             }
  1416.           break;

  1417. #ifdef C_GLBLREG                /* AMD coff */
  1418.         case C_GLBLREG:
  1419. #endif
  1420.         case C_REG:
  1421.           SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
  1422.           SYMBOL_VALUE (sym) = cs->c_value;
  1423.           add_symbol_to_list (sym, &local_symbols);
  1424.           break;

  1425.         case C_THUMBLABEL:
  1426.         case C_LABEL:
  1427.           break;

  1428.         case C_ARG:
  1429.           SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
  1430.           SYMBOL_IS_ARGUMENT (sym) = 1;
  1431.           add_symbol_to_list (sym, &local_symbols);
  1432.           break;

  1433.         case C_REGPARM:
  1434.           SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
  1435.           SYMBOL_IS_ARGUMENT (sym) = 1;
  1436.           SYMBOL_VALUE (sym) = cs->c_value;
  1437.           add_symbol_to_list (sym, &local_symbols);
  1438.           break;

  1439.         case C_TPDEF:
  1440.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  1441.           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;

  1442.           /* If type has no name, give it one.  */
  1443.           if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
  1444.             {
  1445.               if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
  1446.                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
  1447.                 {
  1448.                   /* If we are giving a name to a type such as
  1449.                      "pointer to foo" or "function returning foo", we
  1450.                      better not set the TYPE_NAME.  If the program
  1451.                      contains "typedef char *caddr_t;", we don't want
  1452.                      all variables of type char * to print as caddr_t.
  1453.                      This is not just a consequence of GDB's type
  1454.                      management; CC and GCC (at least through version
  1455.                      2.4) both output variables of either type char *
  1456.                      or caddr_t with the type refering to the C_TPDEF
  1457.                      symbol for caddr_t.  If a future compiler cleans
  1458.                      this up it GDB is not ready for it yet, but if it
  1459.                      becomes ready we somehow need to disable this
  1460.                      check (without breaking the PCC/GCC2.4 case).

  1461.                      Sigh.

  1462.                      Fortunately, this check seems not to be necessary
  1463.                      for anything except pointers or functions.  */
  1464.                   ;
  1465.                 }
  1466.               else
  1467.                 TYPE_NAME (SYMBOL_TYPE (sym)) =
  1468.                   xstrdup (SYMBOL_LINKAGE_NAME (sym));
  1469.             }

  1470.           /* Keep track of any type which points to empty structured
  1471.              type, so it can be filled from a definition from another
  1472.              file.  A simple forward reference (TYPE_CODE_UNDEF) is
  1473.              not an empty structured type, though; the forward
  1474.              references work themselves out via the magic of
  1475.              coff_lookup_type.  */
  1476.           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
  1477.               && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
  1478.               && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
  1479.                  != TYPE_CODE_UNDEF)
  1480.             {
  1481.               int i = hashname (SYMBOL_LINKAGE_NAME (sym));

  1482.               SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
  1483.               opaque_type_chain[i] = sym;
  1484.             }
  1485.           add_symbol_to_list (sym, &file_symbols);
  1486.           break;

  1487.         case C_STRTAG:
  1488.         case C_UNTAG:
  1489.         case C_ENTAG:
  1490.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  1491.           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;

  1492.           /* Some compilers try to be helpful by inventing "fake"
  1493.              names for anonymous enums, structures, and unions, like
  1494.              "~0fake" or ".0fake".  Thanks, but no thanks...  */
  1495.           if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
  1496.             if (SYMBOL_LINKAGE_NAME (sym) != NULL
  1497.                 && *SYMBOL_LINKAGE_NAME (sym) != '~'
  1498.                 && *SYMBOL_LINKAGE_NAME (sym) != '.')
  1499.               TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
  1500.                 concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);

  1501.           add_symbol_to_list (sym, &file_symbols);
  1502.           break;

  1503.         default:
  1504.           break;
  1505.         }
  1506.     }
  1507.   return sym;
  1508. }

  1509. /* Decode a coff type specifier;  return the type that is meant.  */

  1510. static struct type *
  1511. decode_type (struct coff_symbol *cs, unsigned int c_type,
  1512.              union internal_auxent *aux, struct objfile *objfile)
  1513. {
  1514.   struct type *type = 0;
  1515.   unsigned int new_c_type;

  1516.   if (c_type & ~N_BTMASK)
  1517.     {
  1518.       new_c_type = DECREF (c_type);
  1519.       if (ISPTR (c_type))
  1520.         {
  1521.           type = decode_type (cs, new_c_type, aux, objfile);
  1522.           type = lookup_pointer_type (type);
  1523.         }
  1524.       else if (ISFCN (c_type))
  1525.         {
  1526.           type = decode_type (cs, new_c_type, aux, objfile);
  1527.           type = lookup_function_type (type);
  1528.         }
  1529.       else if (ISARY (c_type))
  1530.         {
  1531.           int i, n;
  1532.           unsigned short *dim;
  1533.           struct type *base_type, *index_type, *range_type;

  1534.           /* Define an array type.  */
  1535.           /* auxent refers to array, not base type.  */
  1536.           if (aux->x_sym.x_tagndx.l == 0)
  1537.             cs->c_naux = 0;

  1538.           /* Shift the indices down.  */
  1539.           dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
  1540.           i = 1;
  1541.           n = dim[0];
  1542.           for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
  1543.             *dim = *(dim + 1);
  1544.           *dim = 0;

  1545.           base_type = decode_type (cs, new_c_type, aux, objfile);
  1546.           index_type = objfile_type (objfile)->builtin_int;
  1547.           range_type
  1548.             = create_static_range_type ((struct type *) NULL,
  1549.                                         index_type, 0, n - 1);
  1550.           type =
  1551.             create_array_type ((struct type *) NULL,
  1552.                                base_type, range_type);
  1553.         }
  1554.       return type;
  1555.     }

  1556.   /* Reference to existing type.  This only occurs with the struct,
  1557.      union, and enum types.  EPI a29k coff fakes us out by producing
  1558.      aux entries with a nonzero x_tagndx for definitions of structs,
  1559.      unions, and enums, so we have to check the c_sclass field.  SCO
  1560.      3.2v4 cc gets confused with pointers to pointers to defined
  1561.      structs, and generates negative x_tagndx fields.  */
  1562.   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
  1563.     {
  1564.       if (cs->c_sclass != C_STRTAG
  1565.           && cs->c_sclass != C_UNTAG
  1566.           && cs->c_sclass != C_ENTAG
  1567.           && aux->x_sym.x_tagndx.l >= 0)
  1568.         {
  1569.           type = coff_alloc_type (aux->x_sym.x_tagndx.l);
  1570.           return type;
  1571.         }
  1572.       else
  1573.         {
  1574.           complaint (&symfile_complaints,
  1575.                      _("Symbol table entry for %s has bad tagndx value"),
  1576.                      cs->c_name);
  1577.           /* And fall through to decode_base_type...  */
  1578.         }
  1579.     }

  1580.   return decode_base_type (cs, BTYPE (c_type), aux, objfile);
  1581. }

  1582. /* Decode a coff type specifier for function definition;
  1583.    return the type that the function returns.  */

  1584. static struct type *
  1585. decode_function_type (struct coff_symbol *cs,
  1586.                       unsigned int c_type,
  1587.                       union internal_auxent *aux,
  1588.                       struct objfile *objfile)
  1589. {
  1590.   if (aux->x_sym.x_tagndx.l == 0)
  1591.     cs->c_naux = 0;        /* auxent refers to function, not base
  1592.                            type.  */

  1593.   return decode_type (cs, DECREF (c_type), aux, objfile);
  1594. }

  1595. /* Basic C types.  */

  1596. static struct type *
  1597. decode_base_type (struct coff_symbol *cs,
  1598.                   unsigned int c_type,
  1599.                   union internal_auxent *aux,
  1600.                   struct objfile *objfile)
  1601. {
  1602.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1603.   struct type *type;

  1604.   switch (c_type)
  1605.     {
  1606.     case T_NULL:
  1607.       /* Shows up with "void (*foo)();" structure members.  */
  1608.       return objfile_type (objfile)->builtin_void;

  1609. #ifdef T_VOID
  1610.     case T_VOID:
  1611.       /* Intel 960 COFF has this symbol and meaning.  */
  1612.       return objfile_type (objfile)->builtin_void;
  1613. #endif

  1614.     case T_CHAR:
  1615.       return objfile_type (objfile)->builtin_char;

  1616.     case T_SHORT:
  1617.       return objfile_type (objfile)->builtin_short;

  1618.     case T_INT:
  1619.       return objfile_type (objfile)->builtin_int;

  1620.     case T_LONG:
  1621.       if (cs->c_sclass == C_FIELD
  1622.           && aux->x_sym.x_misc.x_lnsz.x_size
  1623.              > gdbarch_long_bit (gdbarch))
  1624.         return objfile_type (objfile)->builtin_long_long;
  1625.       else
  1626.         return objfile_type (objfile)->builtin_long;

  1627.     case T_FLOAT:
  1628.       return objfile_type (objfile)->builtin_float;

  1629.     case T_DOUBLE:
  1630.       return objfile_type (objfile)->builtin_double;

  1631.     case T_LNGDBL:
  1632.       return objfile_type (objfile)->builtin_long_double;

  1633.     case T_STRUCT:
  1634.       if (cs->c_naux != 1)
  1635.         {
  1636.           /* Anonymous structure type.  */
  1637.           type = coff_alloc_type (cs->c_symnum);
  1638.           TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1639.           TYPE_NAME (type) = NULL;
  1640.           /* This used to set the tag to "<opaque>".  But I think
  1641.              setting it to NULL is right, and the printing code can
  1642.              print it as "struct {...}".  */
  1643.           TYPE_TAG_NAME (type) = NULL;
  1644.           INIT_CPLUS_SPECIFIC (type);
  1645.           TYPE_LENGTH (type) = 0;
  1646.           TYPE_FIELDS (type) = 0;
  1647.           TYPE_NFIELDS (type) = 0;
  1648.         }
  1649.       else
  1650.         {
  1651.           type = coff_read_struct_type (cs->c_symnum,
  1652.                                         aux->x_sym.x_misc.x_lnsz.x_size,
  1653.                                         aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
  1654.                                         objfile);
  1655.         }
  1656.       return type;

  1657.     case T_UNION:
  1658.       if (cs->c_naux != 1)
  1659.         {
  1660.           /* Anonymous union type.  */
  1661.           type = coff_alloc_type (cs->c_symnum);
  1662.           TYPE_NAME (type) = NULL;
  1663.           /* This used to set the tag to "<opaque>".  But I think
  1664.              setting it to NULL is right, and the printing code can
  1665.              print it as "union {...}".  */
  1666.           TYPE_TAG_NAME (type) = NULL;
  1667.           INIT_CPLUS_SPECIFIC (type);
  1668.           TYPE_LENGTH (type) = 0;
  1669.           TYPE_FIELDS (type) = 0;
  1670.           TYPE_NFIELDS (type) = 0;
  1671.         }
  1672.       else
  1673.         {
  1674.           type = coff_read_struct_type (cs->c_symnum,
  1675.                                         aux->x_sym.x_misc.x_lnsz.x_size,
  1676.                                         aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
  1677.                                         objfile);
  1678.         }
  1679.       TYPE_CODE (type) = TYPE_CODE_UNION;
  1680.       return type;

  1681.     case T_ENUM:
  1682.       if (cs->c_naux != 1)
  1683.         {
  1684.           /* Anonymous enum type.  */
  1685.           type = coff_alloc_type (cs->c_symnum);
  1686.           TYPE_CODE (type) = TYPE_CODE_ENUM;
  1687.           TYPE_NAME (type) = NULL;
  1688.           /* This used to set the tag to "<opaque>".  But I think
  1689.              setting it to NULL is right, and the printing code can
  1690.              print it as "enum {...}".  */
  1691.           TYPE_TAG_NAME (type) = NULL;
  1692.           TYPE_LENGTH (type) = 0;
  1693.           TYPE_FIELDS (type) = 0;
  1694.           TYPE_NFIELDS (type) = 0;
  1695.         }
  1696.       else
  1697.         {
  1698.           type = coff_read_enum_type (cs->c_symnum,
  1699.                                       aux->x_sym.x_misc.x_lnsz.x_size,
  1700.                                       aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
  1701.                                       objfile);
  1702.         }
  1703.       return type;

  1704.     case T_MOE:
  1705.       /* Shouldn't show up here.  */
  1706.       break;

  1707.     case T_UCHAR:
  1708.       return objfile_type (objfile)->builtin_unsigned_char;

  1709.     case T_USHORT:
  1710.       return objfile_type (objfile)->builtin_unsigned_short;

  1711.     case T_UINT:
  1712.       return objfile_type (objfile)->builtin_unsigned_int;

  1713.     case T_ULONG:
  1714.       if (cs->c_sclass == C_FIELD
  1715.           && aux->x_sym.x_misc.x_lnsz.x_size
  1716.              > gdbarch_long_bit (gdbarch))
  1717.         return objfile_type (objfile)->builtin_unsigned_long_long;
  1718.       else
  1719.         return objfile_type (objfile)->builtin_unsigned_long;
  1720.     }
  1721.   complaint (&symfile_complaints,
  1722.              _("Unexpected type for symbol %s"), cs->c_name);
  1723.   return objfile_type (objfile)->builtin_void;
  1724. }

  1725. /* This page contains subroutines of read_type.  */

  1726. /* Read the description of a structure (or union type) and return an
  1727.    object describing the type.  */

  1728. static struct type *
  1729. coff_read_struct_type (int index, int length, int lastsym,
  1730.                        struct objfile *objfile)
  1731. {
  1732.   struct nextfield
  1733.     {
  1734.       struct nextfield *next;
  1735.       struct field field;
  1736.     };

  1737.   struct type *type;
  1738.   struct nextfield *list = 0;
  1739.   struct nextfield *new;
  1740.   int nfields = 0;
  1741.   int n;
  1742.   char *name;
  1743.   struct coff_symbol member_sym;
  1744.   struct coff_symbol *ms = &member_sym;
  1745.   struct internal_syment sub_sym;
  1746.   union internal_auxent sub_aux;
  1747.   int done = 0;

  1748.   type = coff_alloc_type (index);
  1749.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1750.   INIT_CPLUS_SPECIFIC (type);
  1751.   TYPE_LENGTH (type) = length;

  1752.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1753.     {
  1754.       read_one_sym (ms, &sub_sym, &sub_aux);
  1755.       name = ms->c_name;
  1756.       name = EXTERNAL_NAME (name, objfile->obfd);

  1757.       switch (ms->c_sclass)
  1758.         {
  1759.         case C_MOS:
  1760.         case C_MOU:

  1761.           /* Get space to record the next field's data.  */
  1762.           new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1763.           new->next = list;
  1764.           list = new;

  1765.           /* Save the data.  */
  1766.           list->field.name = obstack_copy0 (&objfile->objfile_obstack,
  1767.                                             name, strlen (name));
  1768.           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
  1769.                                                   &sub_aux, objfile);
  1770.           SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
  1771.           FIELD_BITSIZE (list->field) = 0;
  1772.           nfields++;
  1773.           break;

  1774.         case C_FIELD:

  1775.           /* Get space to record the next field's data.  */
  1776.           new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1777.           new->next = list;
  1778.           list = new;

  1779.           /* Save the data.  */
  1780.           list->field.name = obstack_copy0 (&objfile->objfile_obstack,
  1781.                                             name, strlen (name));
  1782.           FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
  1783.                                                   &sub_aux, objfile);
  1784.           SET_FIELD_BITPOS (list->field, ms->c_value);
  1785.           FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
  1786.           nfields++;
  1787.           break;

  1788.         case C_EOS:
  1789.           done = 1;
  1790.           break;
  1791.         }
  1792.     }
  1793.   /* Now create the vector of fields, and record how big it is.  */

  1794.   TYPE_NFIELDS (type) = nfields;
  1795.   TYPE_FIELDS (type) = (struct field *)
  1796.     TYPE_ALLOC (type, sizeof (struct field) * nfields);

  1797.   /* Copy the saved-up fields into the field vector.  */

  1798.   for (n = nfields; list; list = list->next)
  1799.     TYPE_FIELD (type, --n) = list->field;

  1800.   return type;
  1801. }

  1802. /* Read a definition of an enumeration type,
  1803.    and create and return a suitable type object.
  1804.    Also defines the symbols that represent the values of the type.  */

  1805. static struct type *
  1806. coff_read_enum_type (int index, int length, int lastsym,
  1807.                      struct objfile *objfile)
  1808. {
  1809.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1810.   struct symbol *sym;
  1811.   struct type *type;
  1812.   int nsyms = 0;
  1813.   int done = 0;
  1814.   struct pending **symlist;
  1815.   struct coff_symbol member_sym;
  1816.   struct coff_symbol *ms = &member_sym;
  1817.   struct internal_syment sub_sym;
  1818.   union internal_auxent sub_aux;
  1819.   struct pending *osyms, *syms;
  1820.   int o_nsyms;
  1821.   int n;
  1822.   char *name;
  1823.   int unsigned_enum = 1;

  1824.   type = coff_alloc_type (index);
  1825.   if (within_function)
  1826.     symlist = &local_symbols;
  1827.   else
  1828.     symlist = &file_symbols;
  1829.   osyms = *symlist;
  1830.   o_nsyms = osyms ? osyms->nsyms : 0;

  1831.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1832.     {
  1833.       read_one_sym (ms, &sub_sym, &sub_aux);
  1834.       name = ms->c_name;
  1835.       name = EXTERNAL_NAME (name, objfile->obfd);

  1836.       switch (ms->c_sclass)
  1837.         {
  1838.         case C_MOE:
  1839.           sym = allocate_symbol (objfile);

  1840.           SYMBOL_SET_LINKAGE_NAME (sym,
  1841.                                    obstack_copy0 (&objfile->objfile_obstack,
  1842.                                                   name, strlen (name)));
  1843.           SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
  1844.           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  1845.           SYMBOL_VALUE (sym) = ms->c_value;
  1846.           add_symbol_to_list (sym, symlist);
  1847.           nsyms++;
  1848.           break;

  1849.         case C_EOS:
  1850.           /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
  1851.              up the count of how many symbols to read.  So stop
  1852.              on .eos.  */
  1853.           done = 1;
  1854.           break;
  1855.         }
  1856.     }

  1857.   /* Now fill in the fields of the type-structure.  */

  1858.   if (length > 0)
  1859.     TYPE_LENGTH (type) = length;
  1860.   else /* Assume ints.  */
  1861.     TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
  1862.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  1863.   TYPE_NFIELDS (type) = nsyms;
  1864.   TYPE_FIELDS (type) = (struct field *)
  1865.     TYPE_ALLOC (type, sizeof (struct field) * nsyms);

  1866.   /* Find the symbols for the values and put them into the type.
  1867.      The symbols can be found in the symlist that we put them on
  1868.      to cause them to be defined.  osyms contains the old value
  1869.      of that symlist; everything up to there was defined by us.  */
  1870.   /* Note that we preserve the order of the enum constants, so
  1871.      that in something like "enum {FOO, LAST_THING=FOO}" we print
  1872.      FOO, not LAST_THING.  */

  1873.   for (syms = *symlist, n = 0; syms; syms = syms->next)
  1874.     {
  1875.       int j = 0;

  1876.       if (syms == osyms)
  1877.         j = o_nsyms;
  1878.       for (; j < syms->nsyms; j++, n++)
  1879.         {
  1880.           struct symbol *xsym = syms->symbol[j];

  1881.           SYMBOL_TYPE (xsym) = type;
  1882.           TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
  1883.           SET_FIELD_ENUMVAL (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
  1884.           if (SYMBOL_VALUE (xsym) < 0)
  1885.             unsigned_enum = 0;
  1886.           TYPE_FIELD_BITSIZE (type, n) = 0;
  1887.         }
  1888.       if (syms == osyms)
  1889.         break;
  1890.     }

  1891.   if (unsigned_enum)
  1892.     TYPE_UNSIGNED (type) = 1;

  1893.   return type;
  1894. }

  1895. /* Register our ability to parse symbols for coff BFD files.  */

  1896. static const struct sym_fns coff_sym_fns =
  1897. {
  1898.   coff_new_init,                /* sym_new_init: init anything gbl to
  1899.                                    entire symtab */
  1900.   coff_symfile_init,                /* sym_init: read initial info, setup
  1901.                                    for sym_read() */
  1902.   coff_symfile_read,                /* sym_read: read a symbol file into
  1903.                                    symtab */
  1904.   NULL,                                /* sym_read_psymbols */
  1905.   coff_symfile_finish,                /* sym_finish: finished with file,
  1906.                                    cleanup */
  1907.   default_symfile_offsets,        /* sym_offsets: xlate external to
  1908.                                    internal form */
  1909.   default_symfile_segments,        /* sym_segments: Get segment
  1910.                                    information from a file */
  1911.   NULL,                         /* sym_read_linetable  */

  1912.   default_symfile_relocate,        /* sym_relocate: Relocate a debug
  1913.                                    section.  */
  1914.   NULL,                                /* sym_probe_fns */
  1915.   &psym_functions
  1916. };

  1917. /* Free the per-objfile COFF data.  */

  1918. static void
  1919. coff_free_info (struct objfile *objfile, void *arg)
  1920. {
  1921.   xfree (arg);
  1922. }

  1923. void
  1924. _initialize_coffread (void)
  1925. {
  1926.   add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);

  1927.   coff_objfile_data_key = register_objfile_data_with_cleanup (NULL,
  1928.                                                               coff_free_info);

  1929.   coff_register_index
  1930.     = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);
  1931. }