home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / coffread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-26  |  61.1 KB  |  2,131 lines

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