home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / coffread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  58.6 KB  |  2,102 lines

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