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