home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gdb35src.zoo / dist-gdb / dbxread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  153.9 KB  |  5,482 lines

  1. /* Read dbx symbol tables and convert to internal format, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Symbol read-in occurs in two phases:
  21.    1.  A scan (read_dbx_symtab()) of the entire executable, whose sole
  22.        purpose is to make a list of symbols (partial symbol table)
  23.        which will cause symbols
  24.        to be read in if referenced.  This scan happens when the
  25.        "symbol-file" command is given (symbol_file_command()).
  26.    2.  Full read-in of symbols.  (psymtab_to_symtab()).  This happens
  27.        when a symbol in a file for which symbols have not yet been
  28.        read in is referenced.
  29.    2a.  The "add-file" command.  Similar to #2.  */
  30.  
  31. #include <stdio.h>
  32. #include "param.h"
  33.  
  34. #ifdef READ_DBX_FORMAT
  35.  
  36. #ifdef USG
  37. #include <sys/types.h>
  38. #include <fcntl.h>
  39. #define L_SET 0
  40. #define L_INCR 1
  41. #endif
  42.  
  43. #ifdef atarist
  44. #define L_SET 0
  45. #define L_INCR 1
  46. #endif
  47.  
  48. #ifdef COFF_ENCAPSULATE
  49. #include "a.out.encap.h"
  50. #include "stab.gnu.h"
  51. #else
  52. #include <a.out.h>
  53. #include <stab.h>
  54. #endif
  55. #include <ctype.h>
  56.  
  57. #ifndef NO_GNU_STABS
  58. /*
  59.  * Define specifically gnu symbols here.
  60.  */
  61.  
  62. /* The following type indicates the definition of a symbol as being
  63.    an indirect reference to another symbol.  The other symbol
  64.    appears as an undefined reference, immediately following this symbol.
  65.  
  66.    Indirection is asymmetrical.  The other symbol's value will be used
  67.    to satisfy requests for the indirect symbol, but not vice versa.
  68.    If the other symbol does not have a definition, libraries will
  69.    be searched to find a definition.  */
  70. #ifndef N_INDR
  71. #define N_INDR 0xa
  72. #endif
  73.  
  74. /* The following symbols refer to set elements.
  75.    All the N_SET[ATDB] symbols with the same name form one set.
  76.    Space is allocated for the set in the text section, and each set
  77.    element's value is stored into one word of the space.
  78.    The first word of the space is the length of the set (number of elements).
  79.  
  80.    The address of the set is made into an N_SETV symbol
  81.    whose name is the same as the name of the set.
  82.    This symbol acts like a N_DATA global symbol
  83.    in that it can satisfy undefined external references.  */
  84.  
  85. #ifndef N_SETA
  86. #define    N_SETA    0x14        /* Absolute set element symbol */
  87. #endif                /* This is input to LD, in a .o file.  */
  88.  
  89. #ifndef N_SETT
  90. #define    N_SETT    0x16        /* Text set element symbol */
  91. #endif                /* This is input to LD, in a .o file.  */
  92.  
  93. #ifndef N_SETD
  94. #define    N_SETD    0x18        /* Data set element symbol */
  95. #endif                /* This is input to LD, in a .o file.  */
  96.  
  97. #ifndef N_SETB
  98. #define    N_SETB    0x1A        /* Bss set element symbol */
  99. #endif                /* This is input to LD, in a .o file.  */
  100.  
  101. /* Macros dealing with the set element symbols defined in a.out.h */
  102. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  103. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  104.  
  105. #ifndef N_SETV
  106. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  107. #endif                /* This is output from LD.  */
  108.  
  109. #ifndef N_WARNING
  110. #define N_WARNING 0x1E        /* Warning message to print if file included */
  111. #endif                /* This is input to ld */
  112.  
  113. #ifndef __GNU_STAB__
  114.  
  115. /* Line number for the data section.  This is to be used to describe
  116.    the source location of a variable declaration.  */
  117. #ifndef N_DSLINE
  118. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  119. #endif
  120.  
  121. /* Line number for the bss section.  This is to be used to describe
  122.    the source location of a variable declaration.  */
  123. #ifndef N_BSLINE
  124. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  125. #endif
  126.  
  127. #endif /* not __GNU_STAB__ */
  128. #endif /* NO_GNU_STABS */
  129.  
  130. #include <obstack.h>
  131. #include <sys/param.h>
  132. #include <sys/file.h>
  133. #include <sys/stat.h>
  134. #include "defs.h"
  135. #include "symtab.h"
  136.  
  137. #ifndef COFF_FORMAT
  138. #ifndef AOUTHDR
  139. #define AOUTHDR        struct exec
  140. #endif
  141. #endif
  142.  
  143. static void add_symbol_to_list ();
  144. static void read_dbx_symtab ();
  145. static void process_one_symbol ();
  146. static void free_all_psymbols ();
  147. static struct type *read_type ();
  148. static struct type *read_range_type ();
  149. static struct type *read_enum_type ();
  150. static struct type *read_struct_type ();
  151. static struct type *read_array_type ();
  152. static long read_number ();
  153. static void finish_block ();
  154. static struct blockvector *make_blockvector ();
  155. static struct symbol *define_symbol ();
  156. static void start_subfile ();
  157. static int hashname ();
  158. static void hash_symsegs ();
  159. static struct pending *copy_pending ();
  160. static void fix_common_block ();
  161.  
  162. static void add_undefined_type ();
  163. static void cleanup_undefined_types ();
  164.  
  165. extern char *index();
  166.  
  167. extern struct symtab *read_symsegs ();
  168. extern void free_all_symtabs ();
  169. extern void free_all_psymtabs ();
  170. extern void free_inclink_symtabs ();
  171.  
  172. /* C++ */
  173. static struct type **read_args ();
  174.  
  175. /* Macro to determine which symbols to ignore when reading the first symbol
  176.    of a file.  Some machines override this definition. */
  177. #ifdef N_NSYMS
  178. #ifndef IGNORE_SYMBOL
  179. /* This code is used on Ultrix systems.  Ignore it */
  180. #define IGNORE_SYMBOL(type)  (type == N_NSYMS)
  181. #endif
  182. #else
  183. #ifndef IGNORE_SYMBOL
  184. /* Don't ignore any symbols. */
  185. #define IGNORE_SYMBOL(type) (0)
  186. #endif
  187. #endif /* not N_NSYMS */
  188.  
  189. /* Macro for number of symbol table entries (in usual a.out format).
  190.    Some machines override this definition.  */
  191. #ifndef NUMBER_OF_SYMBOLS
  192. #ifdef COFF_HEADER
  193. #define NUMBER_OF_SYMBOLS \
  194.   ((COFF_HEADER(hdr) ? hdr.coffhdr.filehdr.f_nsyms : hdr.a_syms) /    \
  195.    sizeof (struct nlist))
  196. #else
  197. #define NUMBER_OF_SYMBOLS (hdr.a_syms / sizeof (struct nlist))
  198. #endif
  199. #endif
  200.  
  201. /* Macro for file-offset of symbol table (in usual a.out format).  */
  202. #ifndef SYMBOL_TABLE_OFFSET
  203. #define SYMBOL_TABLE_OFFSET N_SYMOFF (hdr)
  204. #endif
  205.  
  206. /* Macro for file-offset of string table (in usual a.out format).  */
  207. #ifndef STRING_TABLE_OFFSET
  208. #define STRING_TABLE_OFFSET (N_SYMOFF (hdr) + hdr.a_syms)
  209. #endif
  210.  
  211. /* Macro to store the length of the string table data in INTO.  */
  212. #ifndef READ_STRING_TABLE_SIZE
  213. #define READ_STRING_TABLE_SIZE(INTO)        \
  214. { val = myread (desc, &INTO, sizeof INTO);    \
  215.   if (val < 0) perror_with_name (name); }
  216. #endif
  217.  
  218. /* Macro to declare variables to hold the file's header data.  */
  219. #ifndef DECLARE_FILE_HEADERS
  220. #define DECLARE_FILE_HEADERS  AOUTHDR hdr
  221. #endif
  222.  
  223. /* Macro to read the header data from descriptor DESC and validate it.
  224.    NAME is the file name, for error messages.  */
  225. #ifndef READ_FILE_HEADERS
  226. #ifdef HEADER_SEEK_FD
  227. #define READ_FILE_HEADERS(DESC, NAME)        \
  228. { HEADER_SEEK_FD (DESC);            \
  229.   val = myread (DESC, &hdr, sizeof hdr);    \
  230.   if (val < 0) perror_with_name (NAME);        \
  231.   if (N_BADMAG (hdr))                \
  232.     error ("File \"%s\" not in executable format.", NAME); }
  233. #else
  234. #define READ_FILE_HEADERS(DESC, NAME)        \
  235. { val = myread (DESC, &hdr, sizeof hdr);    \
  236.   if (val < 0) perror_with_name (NAME);        \
  237.   if (N_BADMAG (hdr))                \
  238.     error ("File \"%s\" not in executable format.", NAME); }
  239. #endif
  240. #endif
  241.  
  242. /* Non-zero if this is an object (.o) file, rather than an executable.
  243.    Distinguishing between the two is rarely necessary (and seems like
  244.    a hack, but there is no other way to do ADDR_OF_TEXT_SEGMENT
  245.    right for SunOS).  */
  246. #if !defined (IS_OBJECT_FILE)
  247. /* This will not work
  248.    if someone decides to make ld preserve relocation info.  */
  249. #define IS_OBJECT_FILE (hdr.a_trsize != 0)
  250. #endif
  251.  
  252. /* Macro for size of text segment */
  253. #ifndef SIZE_OF_TEXT_SEGMENT
  254. #define SIZE_OF_TEXT_SEGMENT hdr.a_text
  255. #endif
  256.  
  257. /* Get the address in debugged memory of the start
  258.    of the text segment.  */
  259. #if !defined (ADDR_OF_TEXT_SEGMENT)
  260. #if defined (N_TXTADDR)
  261. #define ADDR_OF_TEXT_SEGMENT (IS_OBJECT_FILE ? 0 : N_TXTADDR (hdr))
  262. #else /* no N_TXTADDR */
  263. #define ADDR_OF_TEXT_SEGMENT 0
  264. #endif /* no N_TXTADDR */
  265. #endif /* no ADDR_OF_TEXT_SEGMENT */
  266.  
  267. /* Macro to get entry point from headers.  */
  268. #ifndef ENTRY_POINT
  269. #define ENTRY_POINT hdr.a_entry
  270. #endif
  271.  
  272. /* Macro for name of symbol to indicate a file compiled with gcc. */
  273. #ifndef GCC_COMPILED_FLAG_SYMBOL
  274. #define GCC_COMPILED_FLAG_SYMBOL "gcc_compiled."
  275. #endif
  276.  
  277. /* Convert stab register number (from `r' declaration) to a gdb REGNUM.  */
  278.  
  279. #ifndef STAB_REG_TO_REGNUM
  280. #define STAB_REG_TO_REGNUM(VALUE) (VALUE)
  281. #endif
  282.  
  283. /* Define this as 1 if a pcc declaration of a char or short argument
  284.    gives the correct address.  Otherwise assume pcc gives the
  285.    address of the corresponding int, which is not the same on a
  286.    big-endian machine.  */
  287.  
  288. #ifndef BELIEVE_PCC_PROMOTION
  289. #define BELIEVE_PCC_PROMOTION 0
  290. #endif
  291.  
  292. /* Nonzero means give verbose info on gdb action.  From main.c.  */
  293. extern int info_verbose;
  294.  
  295. /* Chain of symtabs made from reading the file's symsegs.
  296.    These symtabs do not go into symtab_list themselves,
  297.    but the information is copied from them when appropriate
  298.    to make the symtabs that will exist permanently.  */
  299.  
  300. static struct symtab *symseg_chain;
  301.  
  302. /* Symseg symbol table for the file whose data we are now processing.
  303.    It is one of those in symseg_chain.  Or 0, for a compilation that
  304.    has no symseg.  */
  305.  
  306. static struct symtab *current_symseg;
  307.  
  308. /* Name of source file whose symbol data we are now processing.
  309.    This comes from a symbol of type N_SO.  */
  310.  
  311. static char *last_source_file;
  312.  
  313. /* Core address of start of text of current source file.
  314.    This too comes from the N_SO symbol.  */
  315.  
  316. static CORE_ADDR last_source_start_addr;
  317.  
  318. /* End of the text segment of the executable file,
  319.    as found in the symbol _etext.  */
  320.  
  321. static CORE_ADDR end_of_text_addr;
  322.  
  323. /* The list of sub-source-files within the current individual compilation.
  324.    Each file gets its own symtab with its own linetable and associated info,
  325.    but they all share one blockvector.  */
  326.  
  327. struct subfile
  328. {
  329.   struct subfile *next;
  330.   char *name;
  331.   struct linetable *line_vector;
  332.   int line_vector_length;
  333.   int line_vector_index;
  334.   int prev_line_number;
  335. };
  336.  
  337. static struct subfile *subfiles;
  338.  
  339. static struct subfile *current_subfile;
  340.  
  341. /* Count symbols as they are processed, for error messages.  */
  342.  
  343. static int symnum;
  344.  
  345. /* Vector of types defined so far, indexed by their dbx type numbers.
  346.    (In newer sun systems, dbx uses a pair of numbers in parens,
  347.     as in "(SUBFILENUM,NUMWITHINSUBFILE)".  Then these numbers must be
  348.     translated through the type_translations hash table to get
  349.     the index into the type vector.)  */
  350.  
  351. static struct typevector *type_vector;
  352.  
  353. /* Number of elements allocated for type_vector currently.  */
  354.  
  355. static int type_vector_length;
  356.  
  357. /* Vector of line number information.  */
  358.  
  359. static struct linetable *line_vector;
  360.  
  361. /* Index of next entry to go in line_vector_index.  */
  362.  
  363. static int line_vector_index;
  364.  
  365. /* Last line number recorded in the line vector.  */
  366.  
  367. static int prev_line_number;
  368.  
  369. /* Number of elements allocated for line_vector currently.  */
  370.  
  371. static int line_vector_length;
  372.  
  373. /* Hash table of global symbols whose values are not known yet.
  374.    They are chained thru the SYMBOL_VALUE, since we don't
  375.    have the correct data for that slot yet.  */
  376. /* The use of the LOC_BLOCK code in this chain is nonstandard--
  377.    it refers to a FORTRAN common block rather than the usual meaning.  */
  378.  
  379. #define HASHSIZE 127
  380. static struct symbol *global_sym_chain[HASHSIZE];
  381.  
  382. /* Record the symbols defined for each context in a list.
  383.    We don't create a struct block for the context until we
  384.    know how long to make it.  */
  385.  
  386. #define PENDINGSIZE 100
  387.  
  388. struct pending
  389. {
  390.   struct pending *next;
  391.   int nsyms;
  392.   struct symbol *symbol[PENDINGSIZE];
  393. };
  394.  
  395. /* List of free `struct pending' structures for reuse.  */
  396. struct pending *free_pendings;
  397.  
  398. /* Here are the three lists that symbols are put on.  */
  399.  
  400. struct pending *file_symbols;    /* static at top level, and types */
  401.  
  402. struct pending *global_symbols;    /* global functions and variables */
  403.  
  404. struct pending *local_symbols;    /* everything local to lexical context */
  405.  
  406. /* List of symbols declared since the last BCOMM.  This list is a tail
  407.    of local_symbols.  When ECOMM is seen, the symbols on the list
  408.    are noted so their proper addresses can be filled in later,
  409.    using the common block base address gotten from the assembler
  410.    stabs.  */
  411.  
  412. struct pending *common_block;
  413. int common_block_i;
  414.  
  415. /* Stack representing unclosed lexical contexts
  416.    (that will become blocks, eventually).  */
  417.  
  418. struct context_stack
  419. {
  420.   struct pending *locals;
  421.   struct pending_block *old_blocks;
  422.   struct symbol *name;
  423.   CORE_ADDR start_addr;
  424.   int depth;
  425. };
  426.  
  427. struct context_stack *context_stack;
  428.  
  429. /* Index of first unused entry in context stack.  */
  430. int context_stack_depth;
  431.  
  432. /* Currently allocated size of context stack.  */
  433.  
  434. int context_stack_size;
  435.  
  436. /* Nonzero if within a function (so symbols should be local,
  437.    if nothing says specifically).  */
  438.  
  439. int within_function;
  440.  
  441. /* List of blocks already made (lexical contexts already closed).
  442.    This is used at the end to make the blockvector.  */
  443.  
  444. struct pending_block
  445. {
  446.   struct pending_block *next;
  447.   struct block *block;
  448. };
  449.  
  450. struct pending_block *pending_blocks;
  451.  
  452. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  453. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  454.  
  455. /* File name symbols were loaded from.  */
  456.  
  457. static char *symfile;
  458.  
  459. /* Low and high symbol values (inclusive) for the global variable
  460.    entries in the symbol file. */
  461.  
  462. static int first_global_sym, last_global_sym;
  463.  
  464. /* Structures with which to manage partial symbol allocation.  */
  465.  
  466. struct psymbol_allocation_list global_psymbols, static_psymbols;
  467.  
  468. /* Global variable which, when set, indicates that we are processing a
  469.    .o file compiled with gcc */
  470.  
  471. static unsigned char processing_gcc_compilation;
  472.  
  473. /* Make a list of forward references which haven't been defined.  */
  474. static struct type **undef_types;
  475. static int undef_types_allocated, undef_types_length;
  476.  
  477.   /* Setup a define to deal cleanly with the underscore problem */
  478.  
  479. #ifdef NAMES_HAVE_UNDERSCORE
  480. #define HASH_OFFSET 1
  481. #else
  482. #define HASH_OFFSET 0
  483. #endif
  484.  
  485. #if 0
  486. /* I'm not sure why this is here.  To debug bugs which cause
  487.    an infinite loop of allocations, I suppose.  In any event,
  488.    dumping core when out of memory isn't usually right.  */
  489. static int
  490. xxmalloc (n)
  491. {
  492.   int v = malloc (n);
  493.   if (v == 0)
  494.     {
  495.       fprintf (stderr, "Virtual memory exhausted.\n");
  496.       abort ();
  497.     }
  498.   return v;
  499. }
  500. #else /* not 0 */
  501. #define xxmalloc xmalloc
  502. #endif /* not 0 */
  503.  
  504. /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
  505.    (and add a null character at the end in the copy).
  506.    Returns the address of the copy.  */
  507.  
  508. static char *
  509. obsavestring (ptr, size)
  510.      char *ptr;
  511.      int size;
  512. {
  513.   register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
  514.   /* Open-coded bcopy--saves function call time.
  515.      These strings are usually short.  */
  516.   {
  517.     register char *p1 = ptr;
  518.     register char *p2 = p;
  519.     char *end = ptr + size;
  520.     while (p1 != end)
  521.       *p2++ = *p1++;
  522.   }
  523.   p[size] = 0;
  524.   return p;
  525. }
  526.  
  527. /* Concatenate strings S1, S2 and S3; return the new string.
  528.    Space is found in the symbol_obstack.  */
  529.  
  530. static char *
  531. obconcat (s1, s2, s3)
  532.      char *s1, *s2, *s3;
  533. {
  534.   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  535.   register char *val = (char *) obstack_alloc (symbol_obstack, len);
  536.   strcpy (val, s1);
  537.   strcat (val, s2);
  538.   strcat (val, s3);
  539.   return val;
  540. }
  541.  
  542. /* Support for Sun changes to dbx symbol format */
  543.  
  544. /* For each identified header file, we have a table of types defined
  545.    in that header file.
  546.  
  547.    header_files maps header file names to their type tables.
  548.    It is a vector of n_header_files elements.
  549.    Each element describes one header file.
  550.    It contains a vector of types.
  551.  
  552.    Sometimes it can happen that the same header file produces
  553.    different results when included in different places.
  554.    This can result from conditionals or from different
  555.    things done before including the file.
  556.    When this happens, there are multiple entries for the file in this table,
  557.    one entry for each distinct set of results.
  558.    The entries are distinguished by the INSTANCE field.
  559.    The INSTANCE field appears in the N_BINCL and N_EXCL symbol table and is
  560.    used to match header-file references to their corresponding data.  */
  561.  
  562. struct header_file
  563. {
  564.   char *name;            /* Name of header file */
  565.   int instance;            /* Numeric code distinguishing instances
  566.                    of one header file that produced
  567.                    different results when included.
  568.                    It comes from the N_BINCL or N_EXCL.  */
  569.   struct type **vector;        /* Pointer to vector of types */
  570.   int length;            /* Allocated length (# elts) of that vector */
  571. };
  572.  
  573. static struct header_file *header_files;
  574.  
  575. static int n_header_files;
  576.  
  577. static int n_allocated_header_files;
  578.  
  579. /* During initial symbol readin, we need to have a structure to keep
  580.    track of which psymtabs have which bincls in them.  This structure
  581.    is used during readin to setup the list of dependencies within each
  582.    partial symbol table. */
  583.  
  584. struct header_file_location
  585. {
  586.   char *name;            /* Name of header file */
  587.   int instance;            /* See above */
  588.   struct partial_symtab *pst;    /* Partial symtab that has the
  589.                    BINCL/EINCL defs for this file */
  590. };
  591.  
  592. /* The actual list and controling variables */
  593. static struct header_file_location *bincl_list, *next_bincl;
  594. static int bincls_allocated;
  595.  
  596. /* Within each object file, various header files are assigned numbers.
  597.    A type is defined or referred to with a pair of numbers
  598.    (FILENUM,TYPENUM) where FILENUM is the number of the header file
  599.    and TYPENUM is the number within that header file.
  600.    TYPENUM is the index within the vector of types for that header file.
  601.  
  602.    FILENUM == 1 is special; it refers to the main source of the object file,
  603.    and not to any header file.  FILENUM != 1 is interpreted by looking it up
  604.    in the following table, which contains indices in header_files.  */
  605.  
  606. static int *this_object_header_files;
  607.  
  608. static int n_this_object_header_files;
  609.  
  610. static int n_allocated_this_object_header_files;
  611.  
  612. /* When a header file is getting special overriding definitions
  613.    for one source file, record here the header_files index
  614.    of its normal definition vector.
  615.    At other times, this is -1.  */
  616.  
  617. static int header_file_prev_index;
  618.  
  619. /* At the start of reading dbx symbols, allocate our tables.  */
  620.  
  621. static void
  622. init_header_files ()
  623. {
  624.   n_allocated_header_files = 10;
  625.   header_files = (struct header_file *) xxmalloc (10 * sizeof (struct header_file));
  626.   n_header_files = 0;
  627.  
  628.   n_allocated_this_object_header_files = 10;
  629.   this_object_header_files = (int *) xxmalloc (10 * sizeof (int));
  630. }
  631.  
  632. /* At the end of reading dbx symbols, free our tables.  */
  633.  
  634. static void
  635. free_header_files ()
  636. {
  637.   register int i;
  638.   for (i = 0; i < n_header_files; i++)
  639.     free (header_files[i].name);
  640.   if (header_files) free (header_files);
  641.   if (this_object_header_files)
  642.     free (this_object_header_files);
  643. }
  644.  
  645. /* Called at the start of each object file's symbols.
  646.    Clear out the mapping of header file numbers to header files.  */
  647.  
  648. static void
  649. new_object_header_files ()
  650. {
  651.   /* Leave FILENUM of 0 free for builtin types and this file's types.  */
  652.   n_this_object_header_files = 1;
  653.   header_file_prev_index = -1;
  654. }
  655.  
  656. /* Add header file number I for this object file
  657.    at the next successive FILENUM.  */
  658.  
  659. static void
  660. add_this_object_header_file (i)
  661.      int i;
  662. {
  663.   if (n_this_object_header_files == n_allocated_this_object_header_files)
  664.     {
  665.       n_allocated_this_object_header_files *= 2;
  666.       this_object_header_files
  667.     = (int *) xrealloc (this_object_header_files,
  668.                 n_allocated_this_object_header_files * sizeof (int));
  669.     }
  670.  
  671.   this_object_header_files[n_this_object_header_files++] = i;
  672. }
  673.  
  674. /* Add to this file an "old" header file, one already seen in
  675.    a previous object file.  NAME is the header file's name.
  676.    INSTANCE is its instance code, to select among multiple
  677.    symbol tables for the same header file.  */
  678.  
  679. static void
  680. add_old_header_file (name, instance)
  681.      char *name;
  682.      int instance;
  683. {
  684.   register struct header_file *p = header_files;
  685.   register int i;
  686.  
  687.   for (i = 0; i < n_header_files; i++)
  688.     if (!strcmp (p[i].name, name) && instance == p[i].instance)
  689.       {
  690.     add_this_object_header_file (i);
  691.     return;
  692.       }
  693.   error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
  694.      symnum);
  695. }
  696.  
  697. /* Add to this file a "new" header file: definitions for its types follow.
  698.    NAME is the header file's name.
  699.    Most often this happens only once for each distinct header file,
  700.    but not necessarily.  If it happens more than once, INSTANCE has
  701.    a different value each time, and references to the header file
  702.    use INSTANCE values to select among them.
  703.  
  704.    dbx output contains "begin" and "end" markers for each new header file,
  705.    but at this level we just need to know which files there have been;
  706.    so we record the file when its "begin" is seen and ignore the "end".  */
  707.  
  708. static void
  709. add_new_header_file (name, instance)
  710.      char *name;
  711.      int instance;
  712. {
  713.   register int i;
  714.   register struct header_file *p = header_files;
  715.   header_file_prev_index = -1;
  716.  
  717. #if 0
  718.   /* This code was used before I knew about the instance codes.
  719.      My first hypothesis is that it is not necessary now
  720.      that instance codes are handled.  */
  721.  
  722.   /* Has this header file a previous definition?
  723.      If so, make a new entry anyway so that this use in this source file
  724.      gets a separate entry.  Later source files get the old entry.
  725.      Record here the index of the old entry, so that any type indices
  726.      not previously defined can get defined in the old entry as
  727.      well as in the new one.  */
  728.  
  729.   for (i = 0; i < n_header_files; i++)
  730.     if (!strcmp (p[i].name, name))
  731.       {
  732.     header_file_prev_index = i;
  733.       }
  734.  
  735. #endif
  736.  
  737.   /* Make sure there is room for one more header file.  */
  738.  
  739.   if (n_header_files == n_allocated_header_files)
  740.     {
  741.       n_allocated_header_files *= 2;
  742.       header_files = (struct header_file *)
  743.     xrealloc (header_files,
  744.           (n_allocated_header_files
  745.            * sizeof (struct header_file)));
  746.     }
  747.  
  748.   /* Create an entry for this header file.  */
  749.  
  750.   i = n_header_files++;
  751.   header_files[i].name = savestring (name, strlen(name));
  752.   header_files[i].instance = instance;
  753.   header_files[i].length = 10;
  754.   header_files[i].vector
  755.     = (struct type **) xxmalloc (10 * sizeof (struct type *));
  756.   bzero (header_files[i].vector, 10 * sizeof (struct type *));
  757.  
  758.   add_this_object_header_file (i);
  759. }
  760.  
  761. /* Look up a dbx type-number pair.  Return the address of the slot
  762.    where the type for that number-pair is stored.
  763.    The number-pair is in TYPENUMS.
  764.  
  765.    This can be used for finding the type associated with that pair
  766.    or for associating a new type with the pair.  */
  767.  
  768. static struct type **
  769. dbx_lookup_type (typenums)
  770.      int typenums[2];
  771. {
  772.   register int filenum = typenums[0], index = typenums[1];
  773.  
  774.   if (filenum < 0 || filenum >= n_this_object_header_files)
  775.     error ("Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
  776.        filenum, index, symnum);
  777.  
  778.   if (filenum == 0)
  779.     {
  780.       /* Type is defined outside of header files.
  781.      Find it in this object file's type vector.  */
  782.       if (index >= type_vector_length)
  783.     {
  784.       type_vector_length *= 2;
  785.       type_vector = (struct typevector *)
  786.         xrealloc (type_vector,
  787.               (sizeof (struct typevector)
  788.                + type_vector_length * sizeof (struct type *)));
  789.       bzero (&type_vector->type[type_vector_length / 2],
  790.          type_vector_length * sizeof (struct type *) / 2);
  791.     }
  792.       return &type_vector->type[index];
  793.     }
  794.   else
  795.     {
  796.       register int real_filenum = this_object_header_files[filenum];
  797.       register struct header_file *f;
  798.  
  799.       if (real_filenum >= n_header_files)
  800.     abort ();
  801.  
  802.       f = &header_files[real_filenum];
  803.  
  804.       if (index >= f->length)
  805.     {
  806.       f->length *= 2;
  807.       f->vector = (struct type **)
  808.         xrealloc (f->vector, f->length * sizeof (struct type *));
  809.       bzero (&f->vector[f->length / 2],
  810.          f->length * sizeof (struct type *) / 2);
  811.     }
  812.       return &f->vector[index];
  813.     }
  814. }
  815.  
  816. /* Create a type object.  Occaisionally used when you need a type
  817.    which isn't going to be given a type number.  */
  818.  
  819. static struct type *
  820. dbx_create_type ()
  821. {
  822.   register struct type *type =
  823.     (struct type *) obstack_alloc (symbol_obstack, sizeof (struct type));
  824.  
  825.   bzero (type, sizeof (struct type));
  826.   TYPE_VPTR_FIELDNO (type) = -1;
  827.   return type;
  828. }
  829.  
  830. /* Make sure there is a type allocated for type numbers TYPENUMS
  831.    and return the type object.
  832.    This can create an empty (zeroed) type object.
  833.    TYPENUMS may be (-1, -1) to return a new type object that is not
  834.    put into the type vector, and so may not be referred to by number. */
  835.  
  836. static struct type *
  837. dbx_alloc_type (typenums)
  838.      int typenums[2];
  839. {
  840.   register struct type **type_addr;
  841.   register struct type *type;
  842.  
  843.   if (typenums[1] != -1)
  844.     {
  845.       type_addr = dbx_lookup_type (typenums);
  846.       type = *type_addr;
  847.     }
  848.   else
  849.     {
  850.       type_addr = 0;
  851.       type = 0;
  852.     }
  853.  
  854.   /* If we are referring to a type not known at all yet,
  855.      allocate an empty type for it.
  856.      We will fill it in later if we find out how.  */
  857.   if (type == 0)
  858.     {
  859.       type = dbx_create_type ();
  860.       if (type_addr)
  861.     *type_addr = type;
  862.     }
  863.   
  864.   return type;
  865. }
  866.  
  867. #if 0
  868. static struct type **
  869. explicit_lookup_type (real_filenum, index)
  870.      int real_filenum, index;
  871. {
  872.   register struct header_file *f = &header_files[real_filenum];
  873.  
  874.   if (index >= f->length)
  875.     {
  876.       f->length *= 2;
  877.       f->vector = (struct type **)
  878.     xrealloc (f->vector, f->length * sizeof (struct type *));
  879.       bzero (&f->vector[f->length / 2],
  880.          f->length * sizeof (struct type *) / 2);
  881.     }
  882.   return &f->vector[index];
  883. }
  884. #endif
  885.  
  886. /* maintain the lists of symbols and blocks */
  887.  
  888. /* Add a symbol to one of the lists of symbols.  */
  889. static void
  890. add_symbol_to_list (symbol, listhead)
  891.      struct symbol *symbol;
  892.      struct pending **listhead;
  893. {
  894.   /* We keep PENDINGSIZE symbols in each link of the list.
  895.      If we don't have a link with room in it, add a new link.  */
  896.   if (*listhead == 0 || (*listhead)->nsyms == PENDINGSIZE)
  897.     {
  898.       register struct pending *link;
  899.       if (free_pendings)
  900.     {
  901.       link = free_pendings;
  902.       free_pendings = link->next;
  903.     }
  904.       else
  905.     link = (struct pending *) xxmalloc (sizeof (struct pending));
  906.  
  907.       link->next = *listhead;
  908.       *listhead = link;
  909.       link->nsyms = 0;
  910.     }
  911.  
  912.   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
  913. }
  914.  
  915. /* At end of reading syms, or in case of quit,
  916.    really free as many `struct pending's as we can easily find.  */
  917.  
  918. static void
  919. really_free_pendings ()
  920. {
  921.   struct pending *next, *next1;
  922.   struct pending_block *bnext, *bnext1;
  923.  
  924.   for (next = free_pendings; next; next = next1)
  925.     {
  926.       next1 = next->next;
  927.       free (next);
  928.     }
  929.   free_pendings = 0;
  930.  
  931.   for (bnext = pending_blocks; bnext; bnext = bnext1)
  932.     {
  933.       bnext1 = bnext->next;
  934.       free (bnext);
  935.     }
  936.   pending_blocks = 0;
  937.  
  938.   for (next = file_symbols; next; next = next1)
  939.     {
  940.       next1 = next->next;
  941.       free (next);
  942.     }
  943.   for (next = global_symbols; next; next = next1)
  944.     {
  945.       next1 = next->next;
  946.       free (next);
  947.     }
  948. }
  949.  
  950. /* Take one of the lists of symbols and make a block from it.
  951.    Keep the order the symbols have in the list (reversed from the input file).
  952.    Put the block on the list of pending blocks.  */
  953.  
  954. static void
  955. finish_block (symbol, listhead, old_blocks, start, end)
  956.      struct symbol *symbol;
  957.      struct pending **listhead;
  958.      struct pending_block *old_blocks;
  959.      CORE_ADDR start, end;
  960. {
  961.   register struct pending *next, *next1;
  962.   register struct block *block;
  963.   register struct pending_block *pblock;
  964.   struct pending_block *opblock;
  965.   register int i;
  966.  
  967.   /* Count the length of the list of symbols.  */
  968.  
  969.   for (next = *listhead, i = 0; next; i += next->nsyms, next = next->next);
  970.  
  971.   block = (struct block *) obstack_alloc (symbol_obstack,
  972.                       (sizeof (struct block)
  973.                        + ((i - 1)
  974.                           * sizeof (struct symbol *))));
  975.  
  976.   /* Copy the symbols into the block.  */
  977.  
  978.   BLOCK_NSYMS (block) = i;
  979.   for (next = *listhead; next; next = next->next)
  980.     {
  981.       register int j;
  982.       for (j = next->nsyms - 1; j >= 0; j--)
  983.     BLOCK_SYM (block, --i) = next->symbol[j];
  984.     }
  985.  
  986.   BLOCK_START (block) = start;
  987.   BLOCK_END (block) = end;
  988.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  989.   BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
  990.  
  991.   /* Put the block in as the value of the symbol that names it.  */
  992.  
  993.   if (symbol)
  994.     {
  995.       SYMBOL_BLOCK_VALUE (symbol) = block;
  996.       BLOCK_FUNCTION (block) = symbol;
  997.     }
  998.   else
  999.     BLOCK_FUNCTION (block) = 0;
  1000.  
  1001.   /* Now "free" the links of the list, and empty the list.  */
  1002.  
  1003.   for (next = *listhead; next; next = next1)
  1004.     {
  1005.       next1 = next->next;
  1006.       next->next = free_pendings;
  1007.       free_pendings = next;
  1008.     }
  1009.   *listhead = 0;
  1010.  
  1011.   /* Install this block as the superblock
  1012.      of all blocks made since the start of this scope
  1013.      that don't have superblocks yet.  */
  1014.  
  1015.   opblock = 0;
  1016.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  1017.     {
  1018.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  1019.     BLOCK_SUPERBLOCK (pblock->block) = block;
  1020.       opblock = pblock;
  1021.     }
  1022.  
  1023.   /* Record this block on the list of all blocks in the file.
  1024.      Put it after opblock, or at the beginning if opblock is 0.
  1025.      This puts the block in the list after all its subblocks.  */
  1026.  
  1027.   /* Allocate in the symbol_obstack to save time.
  1028.      It wastes a little space.  */
  1029.   pblock = (struct pending_block *)
  1030.     obstack_alloc (symbol_obstack,
  1031.            sizeof (struct pending_block));
  1032.   pblock->block = block;
  1033.   if (opblock)
  1034.     {
  1035.       pblock->next = opblock->next;
  1036.       opblock->next = pblock;
  1037.     }
  1038.   else
  1039.     {
  1040.       pblock->next = pending_blocks;
  1041.       pending_blocks = pblock;
  1042.     }
  1043. }
  1044.  
  1045. static struct blockvector *
  1046. make_blockvector ()
  1047. {
  1048.   register struct pending_block *next, *next1;
  1049.   register struct blockvector *blockvector;
  1050.   register int i;
  1051.  
  1052.   /* Count the length of the list of blocks.  */
  1053.  
  1054.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  1055.  
  1056.   blockvector = (struct blockvector *)
  1057.     obstack_alloc (symbol_obstack,
  1058.            (sizeof (struct blockvector)
  1059.             + (i - 1) * sizeof (struct block *)));
  1060.  
  1061.   /* Copy the blocks into the blockvector.
  1062.      This is done in reverse order, which happens to put
  1063.      the blocks into the proper order (ascending starting address).
  1064.      finish_block has hair to insert each block into the list
  1065.      after its subblocks in order to make sure this is true.  */
  1066.  
  1067.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  1068.   for (next = pending_blocks; next; next = next->next)
  1069.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  1070.  
  1071. #if 0 /* Now we make the links in the obstack, so don't free them.  */
  1072.   /* Now free the links of the list, and empty the list.  */
  1073.  
  1074.   for (next = pending_blocks; next; next = next1)
  1075.     {
  1076.       next1 = next->next;
  1077.       free (next);
  1078.     }
  1079. #endif
  1080.   pending_blocks = 0;
  1081.  
  1082.   return blockvector;
  1083. }
  1084.  
  1085. /* Manage the vector of line numbers.  */
  1086.  
  1087. static void
  1088. record_line (line, pc)
  1089.      int line;
  1090.      CORE_ADDR pc;
  1091. {
  1092.   struct linetable_entry *e;
  1093.   /* Ignore the dummy line number in libg.o */
  1094.  
  1095.   if (line == 0xffff)
  1096.     return;
  1097.  
  1098.   /* Make sure line vector is big enough.  */
  1099.  
  1100.   if (line_vector_index + 1 >= line_vector_length)
  1101.     {
  1102.       line_vector_length *= 2;
  1103.       line_vector = (struct linetable *)
  1104.     xrealloc (line_vector,
  1105.           (sizeof (struct linetable)
  1106.            + line_vector_length * sizeof (struct linetable_entry)));
  1107.       current_subfile->line_vector = line_vector;
  1108.     }
  1109.  
  1110.   e = line_vector->item + line_vector_index++;
  1111.   e->line = line; e->pc = pc;
  1112. }
  1113.  
  1114. /* Start a new symtab for a new source file.
  1115.    This is called when a dbx symbol of type N_SO is seen;
  1116.    it indicates the start of data for one original source file.  */
  1117.  
  1118. static void
  1119. start_symtab (name, start_addr)
  1120.      char *name;
  1121.      CORE_ADDR start_addr;
  1122. {
  1123.   register struct symtab *s;
  1124.  
  1125.   last_source_file = name;
  1126.   last_source_start_addr = start_addr;
  1127.   file_symbols = 0;
  1128.   global_symbols = 0;
  1129.   within_function = 0;
  1130.  
  1131.   /* Context stack is initially empty, with room for 10 levels.  */
  1132.   context_stack
  1133.     = (struct context_stack *) xxmalloc (10 * sizeof (struct context_stack));
  1134.   context_stack_size = 10;
  1135.   context_stack_depth = 0;
  1136.  
  1137.   new_object_header_files ();
  1138.  
  1139.   for (s = symseg_chain; s; s = s->next)
  1140.     if (s->ldsymoff == symnum * sizeof (struct nlist))
  1141.       break;
  1142.   current_symseg = s;
  1143.   if (s != 0)
  1144.     return;
  1145.  
  1146.   type_vector_length = 160;
  1147.   type_vector = (struct typevector *)
  1148.     xxmalloc (sizeof (struct typevector)
  1149.           + type_vector_length * sizeof (struct type *));
  1150.   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  1151.  
  1152.   /* Initialize the list of sub source files with one entry
  1153.      for this file (the top-level source file).  */
  1154.  
  1155.   subfiles = 0;
  1156.   current_subfile = 0;
  1157.   start_subfile (name);
  1158.  
  1159. #if 0 /* This is now set at the beginning of read_ofile_symtab */
  1160.   /* Set default for compiler to pcc; assume that we aren't processing
  1161.      a gcc compiled file until proved otherwise.  */
  1162.  
  1163.   processing_gcc_compilation = 0;
  1164. #endif
  1165. }
  1166.  
  1167. /* Handle an N_SOL symbol, which indicates the start of
  1168.    code that came from an included (or otherwise merged-in)
  1169.    source file with a different name.  */
  1170.  
  1171. static void
  1172. start_subfile (name)
  1173.      char *name;
  1174. {
  1175.   register struct subfile *subfile;
  1176.  
  1177.   /* Save the current subfile's line vector data.  */
  1178.  
  1179.   if (current_subfile)
  1180.     {
  1181.       current_subfile->line_vector_index = line_vector_index;
  1182.       current_subfile->line_vector_length = line_vector_length;
  1183.       current_subfile->prev_line_number = prev_line_number;
  1184.     }
  1185.  
  1186.   /* See if this subfile is already known as a subfile of the
  1187.      current main source file.  */
  1188.  
  1189.   for (subfile = subfiles; subfile; subfile = subfile->next)
  1190.     {
  1191.       if (!strcmp (subfile->name, name))
  1192.     {
  1193.       line_vector = subfile->line_vector;
  1194.       line_vector_index = subfile->line_vector_index;
  1195.       line_vector_length = subfile->line_vector_length;
  1196.       prev_line_number = subfile->prev_line_number;
  1197.       current_subfile = subfile;
  1198.       return;
  1199.     }
  1200.     }
  1201.  
  1202.   /* This subfile is not known.  Add an entry for it.  */
  1203.  
  1204.   line_vector_index = 0;
  1205.   line_vector_length = 1000;
  1206.   prev_line_number = -2;    /* Force first line number to be explicit */
  1207.   line_vector = (struct linetable *)
  1208.     xxmalloc (sizeof (struct linetable)
  1209.           + line_vector_length * sizeof (struct linetable_entry));
  1210.  
  1211.   /* Make an entry for this subfile in the list of all subfiles
  1212.      of the current main source file.  */
  1213.  
  1214.   subfile = (struct subfile *) xxmalloc (sizeof (struct subfile));
  1215.   subfile->next = subfiles;
  1216.   subfile->name = savestring (name, strlen (name));
  1217.   subfile->line_vector = line_vector;
  1218.   subfiles = subfile;
  1219.   current_subfile = subfile;
  1220. }
  1221.  
  1222. /* Finish the symbol definitions for one main source file,
  1223.    close off all the lexical contexts for that file
  1224.    (creating struct block's for them), then make the struct symtab
  1225.    for that file and put it in the list of all such.
  1226.  
  1227.    END_ADDR is the address of the end of the file's text.  */
  1228.  
  1229. static void
  1230. end_symtab (end_addr)
  1231.      CORE_ADDR end_addr;
  1232. {
  1233.   register struct symtab *symtab;
  1234.   register struct blockvector *blockvector;
  1235.   register struct subfile *subfile;
  1236.   register struct linetable *lv;
  1237.   struct subfile *nextsub;
  1238.  
  1239.   if (current_symseg != 0)
  1240.     {
  1241.       last_source_file = 0;
  1242.       current_symseg = 0;
  1243.       return;
  1244.     }
  1245.  
  1246.   /* Finish the lexical context of the last function in the file;
  1247.      pop the context stack.  */
  1248.  
  1249.   if (context_stack_depth > 0)
  1250.     {
  1251.       register struct context_stack *cstk;
  1252.       context_stack_depth--;
  1253.       cstk = &context_stack[context_stack_depth];
  1254.       /* Make a block for the local symbols within.  */
  1255.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  1256.             cstk->start_addr, end_addr);
  1257.     }
  1258.  
  1259.   /* Cleanup any undefined types that have been left hanging around
  1260.      (this needs to be done before the finish_blocks so that
  1261.      file_symbols is still good).  */
  1262.   cleanup_undefined_types ();
  1263.  
  1264.   /* Finish defining all the blocks of this symtab.  */
  1265.   finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr);
  1266.   finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr);
  1267.   blockvector = make_blockvector ();
  1268.  
  1269.   current_subfile->line_vector_index = line_vector_index;
  1270.  
  1271.   /* Now create the symtab objects proper, one for each subfile.  */
  1272.   /* (The main file is one of them.)  */
  1273.  
  1274.   for (subfile = subfiles; subfile; subfile = nextsub)
  1275.     {
  1276.       symtab = (struct symtab *) xxmalloc (sizeof (struct symtab));
  1277.       symtab->free_ptr = 0;
  1278.  
  1279.       /* Fill in its components.  */
  1280.       symtab->blockvector = blockvector;
  1281.       type_vector->length = type_vector_length;
  1282.       symtab->typevector = type_vector;
  1283.       symtab->free_code = free_linetable;
  1284.       if (subfile->next == 0)
  1285.     symtab->free_ptr = (char *) type_vector;
  1286.  
  1287.       symtab->filename = subfile->name;
  1288.       lv = subfile->line_vector;
  1289.       lv->nitems = subfile->line_vector_index;
  1290.       symtab->linetable = (struct linetable *)
  1291.     xrealloc (lv, (sizeof (struct linetable)
  1292.                + lv->nitems * sizeof (struct linetable_entry)));
  1293.       symtab->nlines = 0;
  1294.       symtab->line_charpos = 0;
  1295.  
  1296.       /* Link the new symtab into the list of such.  */
  1297.       symtab->next = symtab_list;
  1298.       symtab_list = symtab;
  1299.  
  1300.       nextsub = subfile->next;
  1301.       free (subfile);
  1302.     }
  1303.  
  1304.   type_vector = 0;
  1305.   type_vector_length = -1;
  1306.   line_vector = 0;
  1307.   line_vector_length = -1;
  1308.   last_source_file = 0;
  1309. }
  1310.  
  1311. #ifdef N_BINCL
  1312.  
  1313. /* Handle the N_BINCL and N_EINCL symbol types
  1314.    that act like N_SOL for switching source files
  1315.    (different subfiles, as we call them) within one object file,
  1316.    but using a stack rather than in an arbitrary order.  */
  1317.  
  1318. struct subfile_stack
  1319. {
  1320.   struct subfile_stack *next;
  1321.   char *name;
  1322.   int prev_index;
  1323. };
  1324.  
  1325. struct subfile_stack *subfile_stack;
  1326.  
  1327. static void
  1328. push_subfile ()
  1329. {
  1330.   register struct subfile_stack *tem
  1331.     = (struct subfile_stack *) xxmalloc (sizeof (struct subfile_stack));
  1332.  
  1333.   tem->next = subfile_stack;
  1334.   subfile_stack = tem;
  1335.   if (current_subfile == 0 || current_subfile->name == 0)
  1336.     abort ();
  1337.   tem->name = current_subfile->name;
  1338.   tem->prev_index = header_file_prev_index;
  1339. }
  1340.  
  1341. static char *
  1342. pop_subfile ()
  1343. {
  1344.   register char *name;
  1345.   register struct subfile_stack *link = subfile_stack;
  1346.  
  1347.   if (link == 0)
  1348.     abort ();
  1349.  
  1350.   name = link->name;
  1351.   subfile_stack = link->next;
  1352.   header_file_prev_index = link->prev_index;
  1353.   free (link);
  1354.  
  1355.   return name;
  1356. }
  1357. #endif /* Have N_BINCL */
  1358.  
  1359. /* Accumulate the misc functions in bunches of 127.
  1360.    At the end, copy them all into one newly allocated structure.  */
  1361.  
  1362. #define MISC_BUNCH_SIZE 127
  1363.  
  1364. struct misc_bunch
  1365. {
  1366.   struct misc_bunch *next;
  1367.   struct misc_function contents[MISC_BUNCH_SIZE];
  1368. };
  1369.  
  1370. /* Bunch currently being filled up.
  1371.    The next field points to chain of filled bunches.  */
  1372.  
  1373. static struct misc_bunch *misc_bunch;
  1374.  
  1375. /* Number of slots filled in current bunch.  */
  1376.  
  1377. static int misc_bunch_index;
  1378.  
  1379. /* Total number of misc functions recorded so far.  */
  1380.  
  1381. static int misc_count;
  1382.  
  1383. static void
  1384. init_misc_functions ()
  1385. {
  1386.   misc_count = 0;
  1387.   misc_bunch = 0;
  1388.   misc_bunch_index = MISC_BUNCH_SIZE;
  1389. }
  1390.  
  1391. static void
  1392. record_misc_function (name, address, type)
  1393.      char *name;
  1394.      CORE_ADDR address;
  1395.      int type;
  1396. {
  1397.   register struct misc_bunch *new;
  1398.  
  1399.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  1400.     {
  1401.       new = (struct misc_bunch *) xxmalloc (sizeof (struct misc_bunch));
  1402.       misc_bunch_index = 0;
  1403.       new->next = misc_bunch;
  1404.       misc_bunch = new;
  1405.     }
  1406.   misc_bunch->contents[misc_bunch_index].name = name;
  1407.   misc_bunch->contents[misc_bunch_index].address = address;
  1408.   misc_bunch->contents[misc_bunch_index].type = (unsigned char)
  1409.     (type == (N_TEXT | N_EXT) ? mf_text :
  1410.      (type == (N_DATA | N_EXT)
  1411. #ifdef N_SETV
  1412.       || type == (N_SETV | N_EXT)
  1413. #endif
  1414.       ) ? mf_data :
  1415.      type == (N_BSS | N_EXT) ? mf_bss :
  1416.      type == (N_ABS | N_EXT) ? mf_abs : mf_unknown);
  1417.   misc_bunch_index++;
  1418.   misc_count++;
  1419. }
  1420.  
  1421. static int
  1422. compare_misc_functions (fn1, fn2)
  1423.      struct misc_function *fn1, *fn2;
  1424. {
  1425.   /* Return a signed result based on unsigned comparisons
  1426.      so that we sort into unsigned numeric order.  */
  1427.   if (fn1->address < fn2->address)
  1428.     return -1;
  1429.   if (fn1->address > fn2->address)
  1430.     return 1;
  1431.   return 0;
  1432. }
  1433.  
  1434. static void
  1435. discard_misc_bunches ()
  1436. {
  1437.   register struct misc_bunch *next;
  1438.  
  1439.   while (misc_bunch)
  1440.     {
  1441.       next = misc_bunch->next;
  1442.       free (misc_bunch);
  1443.       misc_bunch = next;
  1444.     }
  1445. }
  1446.  
  1447. /* INCLINK nonzero means bunches are from an incrementally-linked file.
  1448.    Add them to the existing bunches.
  1449.    Otherwise INCLINK is zero, and we start from scratch. */
  1450. static void
  1451. condense_misc_bunches (inclink)
  1452.      int inclink;
  1453. {
  1454.   register int i, j;
  1455.   register struct misc_bunch *bunch;
  1456. #ifdef NAMES_HAVE_UNDERSCORE
  1457.   int offset = 1;
  1458. #else
  1459.   int offset = 0;
  1460. #endif
  1461.  
  1462.   if (inclink)
  1463.     {
  1464.       misc_function_vector
  1465.     = (struct misc_function *)
  1466.       xrealloc (misc_function_vector, (misc_count + misc_function_count)
  1467.             * sizeof (struct misc_function));
  1468.       j = misc_function_count;
  1469.     }
  1470.   else
  1471.     {
  1472.       misc_function_vector
  1473.     = (struct misc_function *)
  1474.       xxmalloc (misc_count * sizeof (struct misc_function));
  1475.       j = 0;
  1476.     }
  1477.  
  1478.   bunch = misc_bunch;
  1479.   while (bunch)
  1480.     {
  1481.       for (i = 0; i < misc_bunch_index; i++)
  1482.     {
  1483.       misc_function_vector[j] = bunch->contents[i];
  1484.       misc_function_vector[j].name
  1485.         = obconcat (misc_function_vector[j].name
  1486.             + (misc_function_vector[j].name[0] == '_' ? offset : 0),
  1487.             "", "");
  1488.       j++;
  1489.     }
  1490.       bunch = bunch->next;
  1491.       misc_bunch_index = MISC_BUNCH_SIZE;
  1492.     }
  1493.  
  1494.   if (inclink)
  1495.     misc_function_count += misc_count;
  1496.   else
  1497.     misc_function_count = j;
  1498.  
  1499.   /* Sort the misc functions by address.  */
  1500.  
  1501.   qsort (misc_function_vector, misc_function_count,
  1502.      sizeof (struct misc_function),
  1503.      compare_misc_functions);
  1504. }
  1505.  
  1506. /* Call sort_syms to sort alphabetically
  1507.    the symbols of each block of each symtab.  */
  1508.  
  1509. static int
  1510. compare_symbols (s1, s2)
  1511.      struct symbol **s1, **s2;
  1512. {
  1513.   register int namediff;
  1514.  
  1515.   /* Compare the initial characters.  */
  1516.   namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
  1517.   if (namediff != 0) return namediff;
  1518.  
  1519.   /* If they match, compare the rest of the names.  */
  1520.   namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  1521.   if (namediff != 0) return namediff;
  1522.  
  1523.   /* For symbols of the same name, registers should come first.  */
  1524.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  1525.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  1526. }
  1527.  
  1528. static void sort_symtab_syms ();
  1529.  
  1530. static void
  1531. sort_syms ()
  1532. {
  1533.   register struct symtab *s;
  1534.  
  1535.   for (s = symtab_list; s; s = s->next)
  1536.     sort_symtab_syms (s);
  1537. }
  1538.  
  1539. static void
  1540. sort_symtab_syms (s)
  1541.      register struct symtab *s;
  1542. {
  1543.   register struct blockvector *bv = BLOCKVECTOR (s);
  1544.   int nbl = BLOCKVECTOR_NBLOCKS (bv);
  1545.   int i;
  1546.   register struct block *b;
  1547.  
  1548.   /* Note that in the following sort, we always make sure that
  1549.      register debug symbol declarations always come before regular
  1550.      debug symbol declarations (as might happen when parameters are
  1551.      then put into registers by the compiler).  We do this by a
  1552.      correct compare in compare_symbols, and by the reversal of the
  1553.      symbols if we don't sort.  This works as long as a register debug
  1554.      symbol always comes after a parameter debug symbol. */
  1555.  
  1556.   /* This is no longer necessary; lookup_block_symbol now always
  1557.      prefers some other declaration over a parameter declaration.  We
  1558.      still sort the thing (that is necessary), but we don't reverse it
  1559.      if we shouldn't sort it.  */
  1560.  
  1561.   for (i = 0; i < nbl; i++)
  1562.     {
  1563.       b = BLOCKVECTOR_BLOCK (bv, i);
  1564.       if (BLOCK_SHOULD_SORT (b))
  1565.     qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  1566.            sizeof (struct symbol *), compare_symbols);
  1567.     }
  1568. }
  1569.  
  1570.  
  1571. extern struct symtab *psymtab_to_symtab ();
  1572.  
  1573. /* The entry point.  */
  1574. static CORE_ADDR entry_point;
  1575.  
  1576. /* This is the symbol-file command.  Read the file, analyze its symbols,
  1577.    and add a struct symtab to symtab_list.  */
  1578.  
  1579. void
  1580. symbol_file_command (name, from_tty)
  1581.      char *name;
  1582.      int from_tty;
  1583. {
  1584.   register int desc;
  1585.   DECLARE_FILE_HEADERS;
  1586.   struct nlist *nlist;
  1587.  
  1588.   /* The string table.  */
  1589.   char *stringtab;
  1590.   
  1591.   /* The size of the string table (buffer is a bizarre name...).  */
  1592.   long buffer;
  1593.   
  1594.   register int val;
  1595.   extern void close ();
  1596.   struct cleanup *old_chain;
  1597.   struct symtab *symseg;
  1598.   struct stat statbuf;
  1599.  
  1600.   dont_repeat ();
  1601.  
  1602.   if (name == 0)
  1603.     {
  1604.       if ((symtab_list || partial_symtab_list)
  1605.       && from_tty
  1606.       && !query ("Discard symbol table? ", 0))
  1607.     error ("Not confirmed.");
  1608.       if (symfile)
  1609.     free (symfile);
  1610.       symfile = 0;
  1611.       free_all_symtabs ();
  1612.       free_all_psymtabs ();
  1613.       return;
  1614.     }
  1615.  
  1616.   name = tilde_expand (name);
  1617.   make_cleanup (free, name);
  1618.  
  1619.   if ((symtab_list || partial_symtab_list)
  1620.       && !query ("Load new symbol table from \"%s\"? ", name))
  1621.     error ("Not confirmed.");
  1622.  
  1623.   {
  1624.     char *absolute_name;
  1625.     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  1626.     if (desc < 0)
  1627.       perror_with_name (name);
  1628.     else
  1629.       name = absolute_name;
  1630.   }
  1631.  
  1632.   old_chain = make_cleanup (close, desc);
  1633.   make_cleanup (free_current_contents, &name);
  1634.  
  1635.   READ_FILE_HEADERS (desc, name);
  1636.  
  1637.   entry_point = ENTRY_POINT;
  1638.  
  1639.   if (NUMBER_OF_SYMBOLS == 0)
  1640.     {
  1641.       if (symfile)
  1642.     free (symfile);
  1643.       symfile = 0;
  1644.       free_all_symtabs ();
  1645.       free_all_psymtabs ();
  1646.       printf ("%s has no symbol-table; symbols discarded.\n", name);
  1647.       fflush (stdout);
  1648.       do_cleanups (old_chain);
  1649.       return;
  1650.     }
  1651.  
  1652.   printf ("Reading symbol data from %s...", name);
  1653.   fflush (stdout);
  1654.  
  1655.   /* Now read the string table, all at once.  */
  1656.   val = lseek (desc, STRING_TABLE_OFFSET, 0);
  1657.   if (val < 0)
  1658.     perror_with_name (name);
  1659.   if (stat (name, &statbuf) == -1)
  1660.     perror_with_name (name);
  1661.   READ_STRING_TABLE_SIZE (buffer);
  1662.   if (buffer >= 0 && buffer < statbuf.st_size)
  1663.     {
  1664. #ifdef BROKEN_LARGE_ALLOCA
  1665.       stringtab = (char *) xmalloc (buffer);
  1666.       make_cleanup (free, stringtab);
  1667. #else
  1668.       stringtab = (char *) alloca (buffer);
  1669. #endif
  1670.     }
  1671.   else
  1672.     stringtab = NULL;
  1673.   if (stringtab == NULL)
  1674.     error ("ridiculous string table size: %d bytes", buffer);
  1675.  
  1676.   /* Usually READ_STRING_TABLE_SIZE will have shifted the file pointer.
  1677.      Occaisionally, it won't.  */
  1678.   val = lseek (desc, STRING_TABLE_OFFSET, L_SET);
  1679.   if (val < 0)
  1680.     perror_with_name (name);
  1681.   val = myread (desc, stringtab, buffer);
  1682.   if (val < 0)
  1683.     perror_with_name (name);
  1684.   
  1685.   /* Throw away the old symbol table.  */
  1686.  
  1687.   if (symfile)
  1688.     free (symfile);
  1689.   symfile = 0;
  1690.   free_all_symtabs ();
  1691.   free_all_psymtabs ();
  1692.  
  1693.   /* Empty the hash table of global syms looking for values.  */
  1694.   bzero (global_sym_chain, sizeof global_sym_chain);
  1695.  
  1696.   /* Symsegs are no longer supported by GDB.  Setting symseg_chain to
  1697.      0 is easier than finding all the symseg code and eliminating it.  */
  1698.   symseg_chain = 0;
  1699.  
  1700.   /* Position to read the symbol table.  Do not read it all at once. */
  1701.   val = lseek (desc, SYMBOL_TABLE_OFFSET, 0);
  1702.   if (val < 0)
  1703.     perror_with_name (name);
  1704.  
  1705.   /* Don't put these on the cleanup chain; they need to stick around
  1706.      until the next call to symbol_file_command.  *Then* we'll free
  1707.      them. */
  1708.   free_header_files ();
  1709.   init_header_files ();
  1710.  
  1711.   init_misc_functions ();
  1712.   make_cleanup (discard_misc_bunches, 0);
  1713.  
  1714.   free_pendings = 0;
  1715.   pending_blocks = 0;
  1716.   file_symbols = 0;
  1717.   global_symbols = 0;
  1718.   make_cleanup (really_free_pendings, 0);
  1719.  
  1720.   /* Now that the symbol table data of the executable file are all in core,
  1721.      process them and define symbols accordingly.  Closes desc.  */
  1722.  
  1723.   read_dbx_symtab (desc, stringtab, buffer, NUMBER_OF_SYMBOLS, 0,
  1724.            ADDR_OF_TEXT_SEGMENT, SIZE_OF_TEXT_SEGMENT);
  1725.  
  1726.   /* Go over the misc functions and install them in vector.  */
  1727.  
  1728.   condense_misc_bunches (0);
  1729.  
  1730.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  1731.  
  1732.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  1733.  
  1734.   /* Make a default for file to list.  */
  1735.  
  1736.   symfile = savestring (name, strlen (name));
  1737.  
  1738.   /* Call to select_source_symtab used to be here; it was using too
  1739.      much time.  I'll make sure that list_sources can handle the lack
  1740.      of current_source_symtab */
  1741.  
  1742.   do_cleanups (old_chain);    /* Descriptor closed here */
  1743.  
  1744.   /* Free the symtabs made by read_symsegs, but not their contents,
  1745.      which have been copied into symtabs on symtab_list.  */
  1746.   while (symseg_chain)
  1747.     {
  1748.       register struct symtab *s = symseg_chain->next;
  1749.       free (symseg_chain);
  1750.       symseg_chain = s;
  1751.     }
  1752.  
  1753.   if (!partial_symtab_list)
  1754.     printf ("\n(no debugging symbols found)...");
  1755.  
  1756.   printf ("done.\n");
  1757.   fflush (stdout);
  1758. }
  1759.  
  1760. /* Return name of file symbols were loaded from, or 0 if none..  */
  1761.  
  1762. char *
  1763. get_sym_file ()
  1764. {
  1765.   return symfile;
  1766. }
  1767.  
  1768. /* Buffer for reading the symbol table entries.  */
  1769. static struct nlist symbuf[4096];
  1770. static int symbuf_idx;
  1771. static int symbuf_end;
  1772.  
  1773. /* I/O descriptor for reading the symbol table.  */
  1774. static int symtab_input_desc;
  1775.  
  1776. /* The address of the string table
  1777.    of the object file we are reading (as copied into core).  */
  1778. static char *stringtab_global;
  1779.  
  1780. /* Refill the symbol table input buffer
  1781.    and set the variables that control fetching entries from it.
  1782.    Reports an error if no data available.
  1783.    This function can read past the end of the symbol table
  1784.    (into the string table) but this does no harm.  */
  1785.  
  1786. static int
  1787. fill_symbuf ()
  1788. {
  1789.   int nbytes = myread (symtab_input_desc, symbuf, sizeof (symbuf));
  1790.   if (nbytes <= 0)
  1791.     error ("error or end of file reading symbol table");
  1792.   symbuf_end = nbytes / sizeof (struct nlist);
  1793.   symbuf_idx = 0;
  1794.   return 1;
  1795. }
  1796.  
  1797. /* dbx allows the text of a symbol name to be continued into the
  1798.    next symbol name!  When such a continuation is encountered
  1799.    (a \ at the end of the text of a name)
  1800.    call this function to get the continuation.  */
  1801.  
  1802. static char *
  1803. next_symbol_text ()
  1804. {
  1805.   if (symbuf_idx == symbuf_end)
  1806.     fill_symbuf ();
  1807.   symnum++;
  1808.   return symbuf[symbuf_idx++].n_un.n_strx + stringtab_global;
  1809. }
  1810.  
  1811. /*
  1812.  * Initializes storage for all of the partial symbols that will be
  1813.  * created by read_dbx_symtab and subsidiaries.
  1814.  */
  1815. void
  1816. init_psymbol_list (total_symbols)
  1817.      int total_symbols;
  1818. {
  1819.   /* Current best guess is that there are approximately a twentieth
  1820.      of the total symbols (in a debugging file) are global or static
  1821.      oriented symbols */
  1822.   global_psymbols.size = total_symbols / 10;
  1823.   static_psymbols.size = total_symbols / 10;
  1824.   global_psymbols.next = global_psymbols.list = (struct partial_symbol *)
  1825.     xmalloc (global_psymbols.size * sizeof (struct partial_symbol));
  1826.   static_psymbols.next = static_psymbols.list = (struct partial_symbol *)
  1827.     xmalloc (static_psymbols.size * sizeof (struct partial_symbol));
  1828. }
  1829.  
  1830. /*
  1831.  * Initialize the list of bincls to contain none and have some
  1832.  * allocated.
  1833.  */
  1834. static void
  1835. init_bincl_list (number)
  1836.      int number;
  1837. {
  1838.   bincls_allocated = number;
  1839.   next_bincl = bincl_list = (struct header_file_location *)
  1840.       xmalloc (bincls_allocated * sizeof(struct header_file_location));
  1841. }
  1842.  
  1843. /*
  1844.  * Add a bincl to the list.
  1845.  */
  1846. static void
  1847. add_bincl_to_list (pst, name, instance)
  1848.      struct partial_symtab *pst;
  1849.      char *name;
  1850.      int instance;
  1851. {
  1852.   if (next_bincl >= bincl_list + bincls_allocated)
  1853.     {
  1854.       int offset = next_bincl - bincl_list;
  1855.       bincls_allocated *= 2;
  1856.       bincl_list = (struct header_file_location *)
  1857.     xrealloc (bincl_list,
  1858.           bincls_allocated * sizeof (struct header_file_location));
  1859.       next_bincl = bincl_list + offset;
  1860.     }
  1861.   next_bincl->pst = pst;
  1862.   next_bincl->instance = instance;
  1863.   next_bincl++->name = name;
  1864. }
  1865.  
  1866. /*
  1867.  * Given a name, value pair, find the corresponding
  1868.  * bincl in the list.  Return the partial symtab associated
  1869.  * with that header_file_location.
  1870.  */
  1871. struct partial_symtab *
  1872. find_corresponding_bincl_psymtab (name, instance)
  1873.      char *name;
  1874.      int instance;
  1875. {
  1876.   struct header_file_location *bincl;
  1877.  
  1878.   for (bincl = bincl_list; bincl < next_bincl; bincl++)
  1879.     if (bincl->instance == instance
  1880.     && !strcmp (name, bincl->name))
  1881.       return bincl->pst;
  1882.  
  1883.   return (struct partial_symtab *) 0;
  1884. }
  1885.  
  1886. /*
  1887.  * Free the storage allocated for the bincl list.
  1888.  */
  1889. static void
  1890. free_bincl_list ()
  1891. {
  1892.   free (bincl_list);
  1893.   bincls_allocated = 0;
  1894. }
  1895.  
  1896. static struct partial_symtab *start_psymtab ();
  1897. static void add_psymtab_dependency ();
  1898. static void end_psymtab();
  1899.  
  1900. /* Given pointers to an a.out symbol table in core containing dbx
  1901.    style data, setup partial_symtab's describing each source file for
  1902.    which debugging information is available.  NLISTLEN is the number
  1903.    of symbols in the symbol table.  All symbol names are given as
  1904.    offsets relative to STRINGTAB.  STRINGTAB_SIZE is the size of
  1905.    STRINGTAB.
  1906.  
  1907.    I have no idea whether or not this routine should be setup to deal
  1908.    with inclinks.  It seems reasonable to me that they be dealt with
  1909.    standardly, so I am not going to make a strong effort to deal with
  1910.    them here.
  1911.    */
  1912.  
  1913. static void
  1914. read_dbx_symtab (desc, stringtab, stringtab_size, nlistlen, inclink,
  1915.          text_addr, text_size)
  1916.      int desc;
  1917.      register char *stringtab;
  1918.      register long stringtab_size;
  1919.      register int nlistlen;
  1920.      int inclink;
  1921.      unsigned text_addr;
  1922.      int text_size;
  1923. {
  1924.   register struct nlist *bufp;
  1925.   register char *namestring;
  1926.   register struct partial_symbol *psym;
  1927.   register struct psymbol_allocation_list *psymbol_struct;
  1928.  
  1929.   int nsl;
  1930.   int past_first_source_file = 0;
  1931.   CORE_ADDR last_o_file_start = 0;
  1932.   struct cleanup *old_chain;
  1933.   char *p;
  1934.   enum namespace ns;
  1935.   enum address_class class;
  1936.  
  1937. #ifdef PROFILE_TYPES
  1938.   int i;
  1939.   int profile_types [256];
  1940.   int strcmp_called = 0;
  1941.   int autovars = 0;
  1942.   int global_funs = 0;
  1943. #endif
  1944.  
  1945.   /* Current partial symtab */
  1946.   struct partial_symtab *pst;
  1947.  
  1948.   /* List of current psymtab's include files */
  1949.   char **psymtab_include_list;
  1950.   int includes_allocated;
  1951.   int includes_used;
  1952.  
  1953.   /* Index within current psymtab dependency list */
  1954.   struct partial_symtab **dependency_list;
  1955.   int dependencies_used, dependencies_allocated;
  1956.  
  1957. #ifdef PROFILE_TYPES
  1958.   for (i = 0; i < 256; i++)
  1959.     profile_types[i] = 0;
  1960. #endif
  1961.  
  1962.   stringtab_global = stringtab;
  1963.   
  1964.   pst = (struct partial_symtab *) 0;
  1965.  
  1966.   includes_allocated = 30;
  1967.   includes_used = 0;
  1968.   psymtab_include_list = (char **) alloca (includes_allocated *
  1969.                        sizeof (char *));
  1970.  
  1971.   dependencies_allocated = 30;
  1972.   dependencies_used = 0;
  1973.   dependency_list =
  1974.     (struct partial_symtab **) alloca (dependencies_allocated *
  1975.                        sizeof (struct partial_symtab *));
  1976.  
  1977.   old_chain = make_cleanup (free_all_psymtabs, 0);
  1978.  
  1979.   /* Init bincl list */
  1980.   init_bincl_list (20);
  1981.   make_cleanup (free_bincl_list, 0);
  1982.  
  1983.   /* Setup global partial symbol list */
  1984.   init_psymbol_list (nlistlen);
  1985.  
  1986.   last_source_file = 0;
  1987.  
  1988. #ifdef END_OF_TEXT_DEFAULT
  1989.   end_of_text_addr = END_OF_TEXT_DEFAULT;
  1990. #else
  1991.   end_of_text_addr = text_addr + text_size;
  1992. #endif
  1993.  
  1994.   symtab_input_desc = desc;    /* This is needed for fill_symbuf below */
  1995.   symbuf_end = symbuf_idx = 0;
  1996.  
  1997.   for (symnum = 0; symnum < nlistlen; symnum++)
  1998.     {
  1999.       /* Get the symbol for this run and pull out some info */
  2000.       QUIT;    /* allow this to be interruptable */
  2001.       if (symbuf_idx == symbuf_end)
  2002.     fill_symbuf ();
  2003.       bufp = &symbuf[symbuf_idx++];
  2004.  
  2005. #ifdef PROFILE_TYPES
  2006.       profile_types[bufp->n_type]++;
  2007. #endif
  2008.  
  2009.       /*
  2010.        * Special case to speed up readin.
  2011.        */
  2012.       if (bufp->n_type == N_SLINE) continue;
  2013.  
  2014.       /* Ok.  There is a lot of code duplicated in the rest of this
  2015.          switch statiement (for efficiency reasons).  Since I don't
  2016.          like duplicating code, I will do my penance here, and
  2017.          describe the code which is duplicated:
  2018.  
  2019.      *) The assignment to namestring.
  2020.      *) The call to index.
  2021.      *) The addition of a partial symbol the the two partial
  2022.         symbol lists.  This last is a large section of code, so
  2023.         I've imbedded it in the following macro.
  2024.      */
  2025.       
  2026. /* Set namestring based on bufp.  */
  2027. #define SET_NAMESTRING()\
  2028.   if (bufp->n_un.n_strx < 0 || bufp->n_un.n_strx >= stringtab_size)  \
  2029.     error ("Invalid symbol data: bad string table offset: %d",       \
  2030.        bufp->n_un.n_strx);                                       \
  2031.   namestring = bufp->n_un.n_strx + stringtab
  2032.  
  2033. #define    ADD_PSYMBOL_TO_LIST(NAME, NAMELENGTH, NAMESPACE, CLASS, LIST, VALUE)\
  2034.   do {                                        \
  2035.     if ((LIST).next >=                            \
  2036.     (LIST).list + (LIST).size)                    \
  2037.       {                                    \
  2038.     (LIST).list = (struct partial_symbol *)                \
  2039.       xrealloc ((LIST).list,                    \
  2040.             ((LIST).size * 2                    \
  2041.              * sizeof (struct partial_symbol)));        \
  2042.     /* Next assumes we only went one over.  Should be good if    \
  2043.        program works correctly */                    \
  2044.     (LIST).next =                            \
  2045.       (LIST).list + (LIST).size;                    \
  2046.     (LIST).size *= 2;                        \
  2047.       }                                    \
  2048.     psym = (LIST).next++;                        \
  2049.                                     \
  2050.     SYMBOL_NAME (psym) = (char *) obstack_alloc (psymbol_obstack,    \
  2051.                          (NAMELENGTH) + 1);    \
  2052.     strncpy (SYMBOL_NAME (psym), (NAME), (NAMELENGTH));            \
  2053.     SYMBOL_NAME (psym)[(NAMELENGTH)] = '\0';                \
  2054.     SYMBOL_NAMESPACE (psym) = (NAMESPACE);                \
  2055.     SYMBOL_CLASS (psym) = (CLASS);                    \
  2056.     SYMBOL_VALUE (psym) = (VALUE);                     \
  2057.   } while (0);
  2058.  
  2059.  
  2060.       switch (bufp->n_type)
  2061.     {
  2062.       /*
  2063.        * Standard, non-debugger, symbols
  2064.        */
  2065.  
  2066.     case N_TEXT | N_EXT:
  2067.       /* Catch etext */
  2068.  
  2069.       SET_NAMESTRING();
  2070.  
  2071.       if (namestring[6] == '\0' && namestring[5] == 't'
  2072.           && namestring[4] == 'x' && namestring[3] == 'e'
  2073.           && namestring[2] == 't' && namestring[1] == 'e'
  2074.           && namestring[0] == '_')
  2075.         end_of_text_addr = bufp->n_value;
  2076.  
  2077.       /* Figure out beginning and end of global linker symbol
  2078.          section and put non-debugger specified symbols on
  2079.          tmp_symchain */
  2080.  
  2081.       last_global_sym = symnum;
  2082.       if (!first_global_sym) first_global_sym = symnum;
  2083.  
  2084.       record_misc_function (namestring, bufp->n_value,
  2085.                 bufp->n_type); /* Always */
  2086.  
  2087.       continue;
  2088.  
  2089. #ifdef N_NBTEXT
  2090.     case N_NBTEXT | N_EXT:
  2091. #endif
  2092. #ifdef N_NBDATA
  2093.     case N_NBDATA | N_EXT:
  2094. #endif
  2095. #ifdef N_NBBSS
  2096.     case N_NBBSS | N_EXT:
  2097. #endif
  2098. #ifdef N_SETV
  2099.     case N_SETV | N_EXT:
  2100. #endif
  2101.     case N_ABS | N_EXT:
  2102.     case N_DATA | N_EXT:
  2103.     case N_BSS | N_EXT:
  2104.       /* Figure out beginning and end of global linker symbol
  2105.          section and put non-debugger specified symbols on
  2106.          tmp_symchain */
  2107.  
  2108.       SET_NAMESTRING();
  2109.  
  2110.       last_global_sym = symnum;
  2111.       if (!first_global_sym) first_global_sym = symnum;
  2112.  
  2113.       /* Not really a function here, but... */
  2114.       record_misc_function (namestring, bufp->n_value,
  2115.                 bufp->n_type); /* Always */
  2116.  
  2117.       continue;
  2118.  
  2119. #ifdef N_NBTEXT
  2120.     case N_NBTEXT:
  2121. #endif
  2122.  
  2123.       /* We need to be able to deal with both N_FN or N_TEXT,
  2124.          because we have no way of knowing whether the sys-supplied ld
  2125.          or GNU ld was used to make the executable.  */
  2126. /* #ifdef OFILE_FN_FLAGGED */
  2127. #if ! (N_FN & N_EXT)
  2128.     case N_FN:
  2129. #endif
  2130.     case N_FN | N_EXT:
  2131. /* #else */
  2132.     case N_TEXT:
  2133. /* #endif */
  2134.       SET_NAMESTRING();
  2135.       if ((namestring[0] == '-' && namestring[1] == 'l')
  2136.           || (namestring [(nsl = strlen (namestring)) - 1] == 'o'
  2137.           && namestring [nsl - 2] == '.'))
  2138.         {
  2139.           if (entry_point < bufp->n_value
  2140.           && entry_point >= last_o_file_start)
  2141.         {
  2142.           startup_file_start = last_o_file_start;
  2143.           startup_file_end = bufp->n_value;
  2144.         }
  2145.           if (past_first_source_file && pst)
  2146.         {
  2147.           end_psymtab (pst, psymtab_include_list, includes_used,
  2148.                    symnum * sizeof (struct nlist), bufp->n_value,
  2149.                    dependency_list, dependencies_used,
  2150.                    global_psymbols.next, static_psymbols.next);
  2151.           pst = (struct partial_symtab *) 0;
  2152.           includes_used = 0;
  2153.           dependencies_used = 0;
  2154.         }
  2155.           else
  2156.         past_first_source_file = 1;
  2157.           last_o_file_start = bufp->n_value;
  2158.         }
  2159.       continue;
  2160.  
  2161. #if 0
  2162.       /* See comments at N_FN above.  */
  2163. #ifdef OFILE_FN_FLAGGED
  2164.     case N_TEXT:
  2165. #else
  2166. #if ! (N_FN & N_EXT)
  2167.     case N_FN:
  2168. #endif
  2169.     case N_FN | N_EXT:
  2170. #endif
  2171. #endif /* 0 */
  2172.     case N_UNDF:
  2173.     case N_UNDF | N_EXT:
  2174.     case N_ABS:
  2175.     case N_DATA:
  2176.     case N_BSS:
  2177. #ifdef N_NBDATA
  2178.     case N_NBDATA:
  2179. #endif
  2180. #ifdef N_NBBSS
  2181.     case N_NBBSS:
  2182. #endif
  2183.  
  2184.       /* Keep going . . .*/
  2185.  
  2186.       /*
  2187.        * Special symbol types for GNU
  2188.        */
  2189. #ifdef N_INDR
  2190.     case N_INDR:
  2191.     case N_INDR | N_EXT:
  2192. #endif
  2193. #ifdef N_SETA
  2194.     case N_SETA:
  2195.     case N_SETA | N_EXT:
  2196.     case N_SETT:
  2197.     case N_SETT | N_EXT:
  2198.     case N_SETD:
  2199.     case N_SETD | N_EXT:
  2200.     case N_SETB:
  2201.     case N_SETB | N_EXT:
  2202.     case N_SETV:
  2203. #endif
  2204.       continue;
  2205.  
  2206.       /*
  2207.        * Debugger symbols
  2208.        */
  2209.  
  2210.     case N_SO:
  2211.       /* End the current partial symtab and start a new one */
  2212.  
  2213.       SET_NAMESTRING();
  2214.  
  2215.       if (pst && past_first_source_file)
  2216.         {
  2217.           end_psymtab (pst, psymtab_include_list, includes_used,
  2218.                symnum * sizeof (struct nlist), bufp->n_value,
  2219.                dependency_list, dependencies_used,
  2220.                global_psymbols.next, static_psymbols.next);
  2221.           pst = (struct partial_symtab *) 0;
  2222.           includes_used = 0;
  2223.           dependencies_used = 0;
  2224.         }
  2225.       else
  2226.         past_first_source_file = 1;
  2227.  
  2228.       pst = start_psymtab (namestring, bufp->n_value,
  2229.                    symnum * sizeof (struct nlist),
  2230.                    global_psymbols.next, static_psymbols.next);
  2231.  
  2232.       continue;
  2233.  
  2234. #ifdef N_BINCL
  2235.     case N_BINCL:
  2236.       /* Add this bincl to the bincl_list for future EXCLs.  No
  2237.          need to save the string; it'll be around until
  2238.          read_dbx_symtab function return */
  2239.  
  2240.       SET_NAMESTRING();
  2241.  
  2242.       add_bincl_to_list (pst, namestring, bufp->n_value);
  2243.  
  2244.       /* Mark down an include file in the current psymtab */
  2245.  
  2246.       psymtab_include_list[includes_used++] = namestring;
  2247.       if (includes_used >= includes_allocated)
  2248.         {
  2249.           char **orig = psymtab_include_list;
  2250.  
  2251.           psymtab_include_list = (char **)
  2252.         alloca ((includes_allocated *= 2) *
  2253.             sizeof (char *));
  2254.           bcopy (orig, psymtab_include_list,
  2255.              includes_used * sizeof (char *));
  2256.         }
  2257.  
  2258.       continue;
  2259. #endif
  2260.  
  2261.     case N_SOL:
  2262.       /* Mark down an include file in the current psymtab */
  2263.  
  2264.       SET_NAMESTRING();
  2265.  
  2266.       /* In C++, one may expect the same filename to come round many
  2267.          times, when code is coming alternately from the main file
  2268.          and from inline functions in other files. So I check to see
  2269.          if this is a file we've seen before.
  2270.  
  2271.          This seems to be a lot of time to be spending on N_SOL, but
  2272.          things like "break expread.y:435" need to work (I
  2273.          suppose the psymtab_include_list could be hashed or put
  2274.          in a binary tree, if profiling shows this is a major hog).  */
  2275.       {
  2276.         register int i;
  2277.         for (i = 0; i < includes_used; i++)
  2278.           if (!strcmp (namestring, psymtab_include_list[i]))
  2279.         {
  2280.           i = -1; 
  2281.           break;
  2282.         }
  2283.         if (i == -1)
  2284.           continue;
  2285.       }
  2286.  
  2287.       psymtab_include_list[includes_used++] = namestring;
  2288.       if (includes_used >= includes_allocated)
  2289.         {
  2290.           char **orig = psymtab_include_list;
  2291.  
  2292.           psymtab_include_list = (char **)
  2293.         alloca ((includes_allocated *= 2) *
  2294.             sizeof (char *));
  2295.           bcopy (orig, psymtab_include_list,
  2296.              includes_used * sizeof (char *));
  2297.         }
  2298.       continue;
  2299.  
  2300.     case N_LSYM:        /* Typedef or automatic variable. */
  2301.       SET_NAMESTRING();
  2302.  
  2303.       p = (char *) index (namestring, ':');
  2304.  
  2305.       /* Skip if there is no :.  */
  2306.       if (!p) continue;
  2307.  
  2308.       switch (p[1])
  2309.         {
  2310.         case 'T':
  2311.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2312.                    STRUCT_NAMESPACE, LOC_TYPEDEF,
  2313.                    static_psymbols, bufp->n_value);
  2314.           goto check_enum;
  2315.         case 't':
  2316.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2317.                    VAR_NAMESPACE, LOC_TYPEDEF,
  2318.                    static_psymbols, bufp->n_value);
  2319.         check_enum:
  2320.           /* If this is an enumerated type, we need to
  2321.          add all the enum constants to the partial symbol
  2322.          table.  This does not cover enums without names, e.g.
  2323.          "enum {a, b} c;" in C, but fortunately those are
  2324.          rare.  There is no way for GDB to find those from the
  2325.          enum type without spending too much time on it.  Thus
  2326.          to solve this problem, the compiler needs to put out separate
  2327.          constant symbols ('c' N_LSYMS) for enum constants in
  2328.          enums without names.  */
  2329.  
  2330.           /* We are looking for something of the form
  2331.          <name> ":" ("t" | "T") [<number> "="] "e"
  2332.          {<constant> ":" <value> ","} ";".  */
  2333.  
  2334.           /* Skip over the colon and the 't' or 'T'.  */
  2335.           p += 2;
  2336.           /* This type may be given a number.  Skip over it.  */
  2337.           while ((*p >= '0' && *p <= '9')
  2338.              || *p == '=')
  2339.         p++;
  2340.  
  2341.           if (*p++ == 'e')
  2342.         {
  2343.           /* We have found an enumerated type.  */
  2344.           /* According to comments in read_enum_type
  2345.              a comma could end it instead of a semicolon.
  2346.              I don't know where that happens.
  2347.              Accept either.  */
  2348.           while (*p && *p != ';' && *p != ',')
  2349.             {
  2350.               char *q;
  2351.  
  2352.               /* Check for and handle cretinous dbx symbol name
  2353.              continuation!  */
  2354.               if (*p == '\\')
  2355.             p = next_symbol_text ();
  2356.  
  2357.               /* Point to the character after the name
  2358.              of the enum constant.  */
  2359.               for (q = p; *q && *q != ':'; q++)
  2360.             ;
  2361.               /* Note that the value doesn't matter for
  2362.              enum constants in psymtabs, just in symtabs.  */
  2363.               ADD_PSYMBOL_TO_LIST (p, q - p,
  2364.                        VAR_NAMESPACE, LOC_CONST,
  2365.                        static_psymbols, 0);
  2366.               /* Point past the name.  */
  2367.               p = q;
  2368.               /* Skip over the value.  */
  2369.               while (*p && *p != ',')
  2370.             p++;
  2371.               /* Advance past the comma.  */
  2372.               if (*p)
  2373.             p++;
  2374.             }
  2375.         }
  2376.  
  2377.           continue;
  2378.         case 'c':
  2379.           /* Constant, e.g. from "const" in Pascal.  */
  2380.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2381.                    VAR_NAMESPACE, LOC_CONST,
  2382.                    static_psymbols, bufp->n_value);
  2383.           continue;
  2384.         default:
  2385. #ifdef PROFILE_TYPES
  2386.           if (isalpha(p[1]))
  2387.           printf ("Funny...LSYM with a letter that isn't a type\n");
  2388.           autovars++;
  2389. #endif
  2390.           /* Skip if the thing following the : is
  2391.              not a letter (which indicates declaration of a local
  2392.              variable, which we aren't interested in).  */
  2393.           continue;
  2394.         }
  2395.  
  2396.     case N_FUN:
  2397. #if 0
  2398.       /* This special-casing of N_FUN is just wrong; N_FUN
  2399.          does not mean "function"; it means "text segment".
  2400.          So N_FUN can go with 'V', etc. as well as 'f' or 'F'.  */
  2401.  
  2402.       SET_NAMESTRING();
  2403.  
  2404.       p = (char *) index (namestring, ':');
  2405.  
  2406.       if (!p || p[1] == 'F') continue;
  2407.  
  2408. #ifdef PROFILE_TYPES
  2409.       if (p[1] != 'f')
  2410.         printf ("Funny...FUN with a letter that isn't 'F' or 'f'.\n");
  2411.       global_funs++;
  2412. #endif
  2413.  
  2414.       ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2415.                    VAR_NAMESPACE, LOC_BLOCK,
  2416.                    static_psymbols, bufp->n_value);
  2417.  
  2418.       continue;
  2419. #endif /* 0 */
  2420.     case N_GSYM:        /* Global (extern) variable; can be
  2421.                    data or bss (sigh).  */
  2422.     case N_STSYM:        /* Data seg var -- static  */
  2423.     case N_LCSYM:        /* BSS      "  */
  2424.  
  2425.     /* Following may probably be ignored; I'll leave them here
  2426.        for now (until I do Pascal and Modula 2 extensions).  */
  2427.  
  2428.     case N_PC:        /* I may or may not need this; I
  2429.                    suspect not.  */
  2430. #ifdef N_M2C
  2431.     case N_M2C:        /* I suspect that I can ignore this here. */
  2432.     case N_SCOPE:        /* Same.   */
  2433. #endif
  2434.  
  2435.       SET_NAMESTRING();
  2436.  
  2437.       p = (char *) index (namestring, ':');
  2438.       if (!p)
  2439.         continue;        /* Not a debugging symbol.   */
  2440.  
  2441.       process_symbol_for_psymtab:
  2442.  
  2443.       /* Main processing section for debugging symbols which
  2444.          the initial read through the symbol tables needs to worry
  2445.          about.  If we reach this point, the symbol which we are
  2446.          considering is definitely one we are interested in.
  2447.          p must also contain the (valid) index into the namestring
  2448.          which indicates the debugging type symbol.  */
  2449.  
  2450.       switch (p[1])
  2451.         {
  2452.         case 'c':
  2453.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2454.                    VAR_NAMESPACE, LOC_CONST,
  2455.                    static_psymbols, bufp->n_value);
  2456.           continue;
  2457.         case 'S':
  2458.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2459.                    VAR_NAMESPACE, LOC_STATIC,
  2460.                    static_psymbols, bufp->n_value);
  2461.           continue;
  2462.         case 'G':
  2463.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2464.                    VAR_NAMESPACE, LOC_EXTERNAL,
  2465.                    global_psymbols, bufp->n_value);
  2466.           continue;
  2467.  
  2468.         case 't':
  2469.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2470.                    VAR_NAMESPACE, LOC_TYPEDEF,
  2471.                    global_psymbols, bufp->n_value);
  2472.           continue;
  2473.  
  2474.         case 'f':
  2475.           ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
  2476.                    VAR_NAMESPACE, LOC_BLOCK,
  2477.                    static_psymbols, bufp->n_value);
  2478.           continue;
  2479.  
  2480.           /* Two things show up here (hopefully); static symbols of
  2481.          local scope (static used inside braces) or extensions
  2482.          of structure symbols.  We can ignore both.  */
  2483.         case 'V':
  2484.         case '(':
  2485.         case '0':
  2486.         case '1':
  2487.         case '2':
  2488.         case '3':
  2489.         case '4':
  2490.         case '5':
  2491.         case '6':
  2492.         case '7':
  2493.         case '8':
  2494.         case '9':
  2495.           /* Global functions are ignored here.  I'm not
  2496.          sure what psymtab they go into (or just the misc
  2497.          function vector).  */
  2498.         case 'F':
  2499.           continue;
  2500.  
  2501.         default:
  2502.           fatal ("Internal error: Unexpected debugging symbol type '%c' at symnum %d.\n",
  2503.              p[1], symnum);
  2504.         }
  2505.  
  2506. #ifdef N_BINCL
  2507.     case N_EXCL:
  2508.  
  2509.       SET_NAMESTRING();
  2510.  
  2511.       /* Find the corresponding bincl and mark that psymtab on the
  2512.          psymtab dependency list */
  2513.       {
  2514.         struct partial_symtab *needed_pst =
  2515.           find_corresponding_bincl_psymtab (namestring, bufp->n_value);
  2516.  
  2517.         /* If this include file was defined earlier in this file,
  2518.            leave it alone.  */
  2519.         if (needed_pst == pst) continue;
  2520.  
  2521.         if (needed_pst)
  2522.           {
  2523.         int i;
  2524.         int found = 0;
  2525.  
  2526.         for (i = 0; i < dependencies_used; i++)
  2527.           if (dependency_list[i] == needed_pst)
  2528.             {
  2529.               found = 1;
  2530.               break;
  2531.             }
  2532.  
  2533.         /* If it's already in the list, skip the rest.  */
  2534.         if (found) continue;
  2535.  
  2536.         dependency_list[dependencies_used++] = needed_pst;
  2537.         if (dependencies_used >= dependencies_allocated)
  2538.           {
  2539.             struct partial_symtab **orig = dependency_list;
  2540.             dependency_list =
  2541.               (struct partial_symtab **)
  2542.             alloca ((dependencies_allocated *= 2)
  2543.                 * sizeof (struct partial_symtab *));
  2544.             bcopy (orig, dependency_list,
  2545.                (dependencies_used
  2546.                 * sizeof (struct partial_symtab *)));
  2547. #ifdef DEBUG_INFO
  2548.             fprintf (stderr, "Had to reallocate dependency list.\n");
  2549.             fprintf (stderr, "New dependencies allocated: %d\n",
  2550.                  dependencies_allocated);
  2551. #endif
  2552.           }
  2553.           }
  2554.         else
  2555.           error ("Invalid symbol data: \"repeated\" header file not previously seen, at symtab pos %d.",
  2556.              symnum);
  2557.       }
  2558.       continue;
  2559.  
  2560.     case N_EINCL:
  2561. #endif
  2562. #ifdef N_DSLINE
  2563.     case N_DSLINE:
  2564. #endif
  2565. #ifdef N_BSLINE
  2566.     case N_BSLINE:
  2567. #endif
  2568.     case N_SSYM:        /* Claim: Structure or union element.
  2569.                    Hopefully, I can ignore this.  */
  2570.     case N_ENTRY:        /* Alternate entry point; can ignore. */
  2571. #ifdef N_MAIN
  2572.     case N_MAIN:        /* Can definitely ignore this.   */
  2573. #endif
  2574.     case N_LENG:
  2575.     case N_BCOMM:
  2576.     case N_ECOMM:
  2577.     case N_ECOML:
  2578.     case N_FNAME:
  2579.     case N_SLINE:
  2580.     case N_RSYM:
  2581.     case N_PSYM:
  2582.     case N_LBRAC:
  2583.     case N_RBRAC:
  2584.       /* These symbols aren't interesting; don't worry about them */
  2585.  
  2586.       continue;
  2587.  
  2588.     default:
  2589.       /* If we haven't found it yet, we've got problems */
  2590.  
  2591.       if (IGNORE_SYMBOL (bufp->n_type))
  2592.         continue;
  2593.  
  2594.       fatal ("Bad symbol type 0x%x encountered in gdb scan", bufp->n_type);
  2595.     }
  2596.     }
  2597.  
  2598.   /* If there's stuff to be cleaned up, clean it up.  */
  2599.   if (entry_point < bufp->n_value
  2600.       && entry_point >= last_o_file_start)
  2601.     {
  2602.       startup_file_start = last_o_file_start;
  2603.       startup_file_end = bufp->n_value;
  2604.     }
  2605.  
  2606.   if (pst)
  2607.     {
  2608.       end_psymtab (pst, psymtab_include_list, includes_used,
  2609.            symnum * sizeof (struct nlist), end_of_text_addr,
  2610.            dependency_list, dependencies_used,
  2611.            global_psymbols.next, static_psymbols.next);
  2612.       includes_used = 0;
  2613.       dependencies_used = 0;
  2614.       pst = (struct partial_symtab *) 0;
  2615.     }
  2616.  
  2617.   free_bincl_list ();
  2618.   discard_cleanups (old_chain);
  2619. #ifdef PROFILE_TYPES
  2620.   {
  2621.     int i, j;
  2622. #define __define_stab(SYM, NUMBER, NAME)    {NUMBER, NAME},
  2623.   static struct xyzzy {
  2624.     unsigned char symnum;
  2625.     char *name;
  2626.   } tmp_list[] = {
  2627. #include "stab.def"
  2628.     {0x1, "eREF"},
  2629.     {0x2, "ABS"},
  2630.     {0x3, "eABS"},
  2631.     {0x4, "TEXT"},
  2632.     {0x5, "eTEXT"},
  2633.     {0x6, "DATA"},
  2634.     {0x7, "eDATA"},
  2635.     {0x8, "BSS"},
  2636.     {0x9, "eBSS"},
  2637.     {0x12, "COMM"},
  2638.     {0x13, "eCOMM"},
  2639.     {0x1f, "FN"},
  2640.     {0, "Unknown"},
  2641. };
  2642.     for (i = 0; i < 256; i++)
  2643.       {
  2644.     for (j = 0; j < (sizeof (tmp_list) / sizeof (struct xyzzy)) - 1; j++)
  2645.       if (tmp_list[j].symnum == i)
  2646.         break;
  2647.     printf ("Symbol \"%s\" (0x%x) occured %d times.\n",
  2648.         tmp_list[j].name, i, profile_types[i]);
  2649.       }
  2650.     printf ("Auto vars (under LSYM): %d\n", autovars);
  2651.     printf ("Global funs (under FUN): %d\n", global_funs);
  2652.   }
  2653. #endif
  2654. }
  2655.  
  2656. /*
  2657.  * Allocate and partially fill a partial symtab.  It will be
  2658.  * completely filled at the end of the symbol list.
  2659.  */
  2660. static struct partial_symtab *
  2661. start_psymtab (filename, textlow, ldsymoff, global_syms, static_syms)
  2662.      char *filename;
  2663.      int textlow;
  2664.      int ldsymoff;
  2665.      struct partial_symbol *global_syms;
  2666.      struct partial_symbol *static_syms;
  2667. {
  2668.   struct partial_symtab *result =
  2669.     (struct partial_symtab *) obstack_alloc (psymbol_obstack,
  2670.                          sizeof (struct partial_symtab));
  2671.  
  2672.   result->filename =
  2673.     (char *) obstack_alloc (psymbol_obstack,
  2674.                 strlen (filename) + 1);
  2675.   strcpy (result->filename, filename);
  2676.  
  2677.   result->textlow = textlow;
  2678.   result->ldsymoff = ldsymoff;
  2679.  
  2680.   result->readin = 0;
  2681.  
  2682.   result->globals_offset = global_syms - global_psymbols.list;
  2683.   result->statics_offset = static_syms - static_psymbols.list;
  2684.  
  2685.   result->n_global_syms = 0;
  2686.   result->n_static_syms = 0;
  2687.  
  2688.   return result;
  2689. }
  2690.  
  2691. static int
  2692. compare_psymbols (s1, s2)
  2693.      register struct partial_symbol *s1, *s2;
  2694. {
  2695.   register char
  2696.     *st1 = SYMBOL_NAME (s1),
  2697.     *st2 = SYMBOL_NAME (s2);
  2698.  
  2699.   return (st1[0] - st2[0] ? st1[0] - st2[0] :
  2700.       strcmp (st1 + 1, st2 + 1));
  2701. }
  2702.  
  2703.  
  2704. /* Close off the current usage of a partial_symbol table entry.  This
  2705.    involves setting the correct number of includes (with a realloc),
  2706.    setting the high text mark, setting the symbol length in the
  2707.    executable, and setting the length of the global and static lists
  2708.    of psymbols.
  2709.  
  2710.    The global symbols and static symbols are then seperately sorted.
  2711.  
  2712.    Then the partial symtab is put on the global list.
  2713.    *** List variables and peculiarities of same. ***
  2714.    */
  2715. static void
  2716. end_psymtab (pst, include_list, num_includes, capping_symbol_offset,
  2717.          capping_text, dependency_list, number_dependencies,
  2718.          capping_global, capping_static)
  2719.      struct partial_symtab *pst;
  2720.      char **include_list;
  2721.      int num_includes;
  2722.      int capping_symbol_offset;
  2723.      int capping_text;
  2724.      struct partial_symtab **dependency_list;
  2725.      int number_dependencies;
  2726.      struct partial_symbol *capping_global, *capping_static;
  2727. {
  2728.   int i;
  2729.  
  2730.   pst->ldsymlen = capping_symbol_offset - pst->ldsymoff;
  2731.   pst->texthigh = capping_text;
  2732.  
  2733.   pst->n_global_syms =
  2734.     capping_global - (global_psymbols.list + pst->globals_offset);
  2735.   pst->n_static_syms =
  2736.     capping_static - (static_psymbols.list + pst->statics_offset);
  2737.  
  2738.   pst->dependencies = (struct partial_symtab **)
  2739.     obstack_alloc (psymbol_obstack,
  2740.            number_dependencies * sizeof (struct partial_symtab *));
  2741.   bcopy (dependency_list, pst->dependencies,
  2742.      number_dependencies * sizeof (struct partial_symtab *));
  2743.   pst->number_of_dependencies = number_dependencies;
  2744.  
  2745.   for (i = 0; i < num_includes; i++)
  2746.     {
  2747.       /* Eventually, put this on obstack */
  2748.       struct partial_symtab *subpst =
  2749.     (struct partial_symtab *)
  2750.       obstack_alloc (psymbol_obstack,
  2751.              sizeof (struct partial_symtab));
  2752.  
  2753.       subpst->filename =
  2754.     (char *) obstack_alloc (psymbol_obstack,
  2755.                 strlen (include_list[i]) + 1);
  2756.       strcpy (subpst->filename, include_list[i]);
  2757.  
  2758.       subpst->ldsymoff =
  2759.     subpst->ldsymlen =
  2760.       subpst->textlow =
  2761.         subpst->texthigh = 0;
  2762.       subpst->readin = 0;
  2763.  
  2764.       subpst->dependencies = (struct partial_symtab **)
  2765.     obstack_alloc (psymbol_obstack,
  2766.                sizeof (struct partial_symtab *));
  2767.       subpst->dependencies[0] = pst;
  2768.       subpst->number_of_dependencies = 1;
  2769.  
  2770.       subpst->globals_offset =
  2771.     subpst->n_global_syms =
  2772.       subpst->statics_offset =
  2773.         subpst->n_static_syms = 0;
  2774.  
  2775.       subpst->next = partial_symtab_list;
  2776.       partial_symtab_list = subpst;
  2777.     }
  2778.  
  2779.   /* Sort the global list; don't sort the static list */
  2780.   qsort (global_psymbols.list + pst->globals_offset, pst->n_global_syms,
  2781.      sizeof (struct partial_symbol), compare_psymbols);
  2782.  
  2783.   /* Put the psymtab on the psymtab list */
  2784.   pst->next = partial_symtab_list;
  2785.   partial_symtab_list = pst;
  2786. }
  2787.  
  2788.  
  2789. /* Helper routines for psymtab_to_symtab.  */
  2790. static void scan_file_globals ();
  2791. static void read_ofile_symtab ();
  2792.  
  2793. static void
  2794. psymtab_to_symtab_1 (pst, desc, stringtab, stringtab_size, sym_offset)
  2795.      struct partial_symtab *pst;
  2796.      int desc;
  2797.      char *stringtab;
  2798.      int stringtab_size;
  2799.      int sym_offset;
  2800. {
  2801.   struct cleanup *old_chain;
  2802.   int i;
  2803.   
  2804.   if (!pst)
  2805.     return;
  2806.  
  2807.   if (pst->readin)
  2808.     {
  2809.       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
  2810.            pst->filename);
  2811.       return;
  2812.     }
  2813.  
  2814.   /* Read in all partial symbtabs on which this one is dependent */
  2815.   for (i = 0; i < pst->number_of_dependencies; i++)
  2816.     if (!pst->dependencies[i]->readin)
  2817.       {
  2818.     /* Inform about additional files that need to be read in.  */
  2819.     if (info_verbose)
  2820.       {
  2821.         printf_filtered (" and %s...", pst->dependencies[i]->filename);
  2822.         fflush (stdout);
  2823.       }
  2824.     psymtab_to_symtab_1 (pst->dependencies[i], desc,
  2825.                  stringtab, stringtab_size, sym_offset);
  2826.       }
  2827.  
  2828.   if (pst->ldsymlen)        /* Otherwise it's a dummy */
  2829.     {
  2830.       /* Init stuff necessary for reading in symbols */
  2831.       free_pendings = 0;
  2832.       pending_blocks = 0;
  2833.       file_symbols = 0;
  2834.       global_symbols = 0;
  2835.       old_chain = make_cleanup (really_free_pendings, 0);
  2836.  
  2837.       /* Read in this files symbols */
  2838.       lseek (desc, sym_offset, L_SET);
  2839.       read_ofile_symtab (desc, stringtab, stringtab_size,
  2840.              pst->ldsymoff,
  2841.              pst->ldsymlen, pst->textlow,
  2842.              pst->texthigh - pst->textlow, 0);
  2843.       sort_symtab_syms (symtab_list); /* At beginning since just added */
  2844.  
  2845.       do_cleanups (old_chain);
  2846.     }
  2847.  
  2848.   pst->readin = 1;
  2849. }
  2850.  
  2851. /*
  2852.  * Read in all of the symbols for a given psymtab for real.  Return
  2853.  * the value of the symtab you create.  Do not free the storage
  2854.  * allocated to the psymtab; it may have pointers to it.
  2855.  */
  2856. struct symtab *
  2857. psymtab_to_symtab(pst)
  2858.      struct partial_symtab *pst;
  2859. {
  2860.   int desc;
  2861.   DECLARE_FILE_HEADERS;
  2862.   char *stringtab;
  2863.   struct partial_symtab **list_patch;
  2864.   int stsize, val;
  2865.   struct stat statbuf;
  2866.   struct cleanup *old_chain;
  2867.   extern void close ();
  2868.   int i;
  2869.   struct symtab *result;
  2870.   char *name = symfile;        /* Some of the macros require the */
  2871.                 /* variable "name" to be defined in */
  2872.                 /* the context in which they execute */
  2873.                 /* (Yech!)  */
  2874.  
  2875.   if (!pst)
  2876.     return 0;
  2877.  
  2878.   if (pst->readin)
  2879.     {
  2880.       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
  2881.            pst->filename);
  2882.       return 0;
  2883.     }
  2884.  
  2885.   if (!name)
  2886.     error("No symbol file currently specified; use command symbol-file");
  2887.  
  2888.   if (pst->ldsymlen || pst->number_of_dependencies)
  2889.     {
  2890.       /* Print the message now, before reading the string table,
  2891.      to avoid disconcerting pauses.  */
  2892.       if (info_verbose)
  2893.     {
  2894.       printf_filtered ("Reading in symbols for %s...", pst->filename);
  2895.       fflush (stdout);
  2896.     }
  2897.  
  2898.       /* Open symbol file and read in string table */
  2899.       if (stat (name, &statbuf) < 0)
  2900.     perror_with_name (name);
  2901.       desc = open(name, O_RDONLY, 0); /* symbol_file_command
  2902.                      guarrantees that the symbol file name
  2903.                      will be absolute, so there is no
  2904.                      need for openp */
  2905.  
  2906.       old_chain = make_cleanup (close, desc);
  2907.  
  2908.       if (desc < 0)
  2909.     error("Symbol file not readable");
  2910.  
  2911.       READ_FILE_HEADERS (desc, name);
  2912.  
  2913.       /* Read in the string table */
  2914.       lseek (desc, STRING_TABLE_OFFSET, L_SET);
  2915.       READ_STRING_TABLE_SIZE (stsize);
  2916.       if (stsize >= 0 && stsize < statbuf.st_size)
  2917.     {
  2918. #ifdef BROKEN_LARGE_ALLOCA
  2919.       stringtab = (char *) xmalloc (stsize);
  2920.       make_cleanup (free, stringtab);
  2921. #else
  2922.       stringtab = (char *) alloca (stsize);
  2923. #endif
  2924.     }
  2925.       else
  2926.     stringtab = NULL;
  2927.       if (stringtab == NULL)
  2928.     error ("ridiculous string table size: %d bytes", stsize);
  2929.  
  2930.       /* Usually READ_STRING_TABLE_SIZE will have shifted the file pointer.
  2931.      Occaisionally, it won't.  */
  2932.       val = lseek (desc, STRING_TABLE_OFFSET, L_SET);
  2933.       if (val < 0)
  2934.     perror_with_name (name);
  2935.       val = myread (desc, stringtab, stsize);
  2936.       if (val < 0)
  2937.     perror_with_name (name);
  2938.  
  2939.       psymtab_to_symtab_1 (pst, desc, stringtab, stsize,
  2940.                SYMBOL_TABLE_OFFSET);
  2941.  
  2942.       /* Match with global symbols.  This  only needs to be done once,
  2943.          after all of the symtabs and dependencies have been read in.   */
  2944.       scan_file_globals ();
  2945.  
  2946.       do_cleanups (old_chain);
  2947.  
  2948.       /* Finish up the debug error message.  */
  2949.       if (info_verbose)
  2950.     printf_filtered ("done.\n");
  2951.     }
  2952.  
  2953.   /* Search through list for correct name. */
  2954.   for (result = symtab_list; result; result = result->next)
  2955.     if (!strcmp (result->filename, pst->filename))
  2956.       return result;
  2957.  
  2958.   return 0;
  2959. }
  2960.  
  2961. /*
  2962.  * Scan through all of the global symbols defined in the object file,
  2963.  * assigning values to the debugging symbols that need to be assigned
  2964.  * to.  Get these symbols from the misc function list.
  2965.  */
  2966. static void
  2967. scan_file_globals ()
  2968. {
  2969.   int hash;
  2970.   int mf;
  2971.  
  2972.   for (mf = 0; mf < misc_function_count; mf++)
  2973.     {
  2974.       char *namestring = misc_function_vector[mf].name;
  2975.       struct symbol *sym, *prev;
  2976.  
  2977.       QUIT;
  2978.  
  2979.       prev = (struct symbol *) 0;
  2980.  
  2981.       /* Get the hash index and check all the symbols
  2982.      under that hash index. */
  2983.  
  2984.       hash = hashname (namestring);
  2985.  
  2986.       for (sym = global_sym_chain[hash]; sym;)
  2987.     {
  2988.       if (*namestring == SYMBOL_NAME (sym)[0]
  2989.           && !strcmp(namestring + 1, SYMBOL_NAME (sym) + 1))
  2990.         {
  2991.           /* Splice this symbol out of the hash chain and
  2992.          assign the value we have to it. */
  2993.           if (prev)
  2994.         SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  2995.           else
  2996.         global_sym_chain[hash]
  2997.           = (struct symbol *) SYMBOL_VALUE (sym);
  2998.           
  2999.           /* Check to see whether we need to fix up a common block.  */
  3000.           /* Note: this code might be executed several times for
  3001.          the same symbol if there are multiple references.  */
  3002.           if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  3003.         fix_common_block (sym, misc_function_vector[mf].address);
  3004.           else
  3005.         SYMBOL_VALUE (sym) = misc_function_vector[mf].address;
  3006.           
  3007.           if (prev)
  3008.         sym = (struct symbol *) SYMBOL_VALUE (prev);
  3009.           else
  3010.         sym = global_sym_chain[hash];
  3011.         }
  3012.       else
  3013.         {
  3014.           prev = sym;
  3015.           sym = (struct symbol *) SYMBOL_VALUE (sym);
  3016.         }
  3017.     }
  3018.     }
  3019. }
  3020.  
  3021. /*
  3022.  * Read in a defined section of a specific object file's symbols.
  3023.  *
  3024.  * DESC is the file descriptor for the file, positioned at the
  3025.  * beginning of the symtab
  3026.  * STRINGTAB is a pointer to the files string
  3027.  * table, already read in
  3028.  * SYM_OFFSET is the offset within the file of
  3029.  * the beginning of the symbols we want to read, NUM_SUMBOLS is the
  3030.  * number of symbols to read
  3031.  * TEXT_OFFSET is the offset to be added to
  3032.  * all values of symbols coming in and
  3033.  * TEXT_SIZE is the size of the text segment read in.
  3034.  * OFFSET is a flag which indicates that the value of all of the
  3035.  * symbols should be offset by TEXT_OFFSET (for the purposes of
  3036.  * incremental linking).
  3037.  */
  3038.  
  3039. static void
  3040. read_ofile_symtab (desc, stringtab, stringtab_size, sym_offset,
  3041.            sym_size, text_offset, text_size, offset)
  3042.      int desc;
  3043.      register char *stringtab;
  3044.      int sym_offset;
  3045.      int sym_size;
  3046.      int text_offset;
  3047.      int text_size;
  3048.      int offset;
  3049. {
  3050.   register char *namestring;
  3051.   register struct symbol *sym, *prev;
  3052.   int hash;
  3053.   struct cleanup *old_chain;
  3054.   struct nlist *bufp;
  3055.   unsigned char type;
  3056. #ifdef N_BINCL
  3057.   subfile_stack = 0;
  3058. #endif
  3059.  
  3060.   stringtab_global = stringtab;
  3061.   last_source_file = 0;
  3062.  
  3063.   symtab_input_desc = desc;
  3064.   symbuf_end = symbuf_idx = 0;
  3065.  
  3066.   /* It is necessary to actually read one symbol *before* the start
  3067.      of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
  3068.      occurs before the N_SO symbol.
  3069.  
  3070.      Detecting this in read_dbx_symtab
  3071.      would slow down initial readin, so we look for it here instead.  */
  3072.   if (sym_offset >= sizeof (struct nlist))
  3073.     {
  3074.       lseek (desc, sym_offset - sizeof (struct nlist), L_INCR);
  3075.       fill_symbuf ();
  3076.       bufp = &symbuf[symbuf_idx++];
  3077.  
  3078.       if (bufp->n_un.n_strx < 0 || bufp->n_un.n_strx >= stringtab_size)
  3079.     error ("Invalid symbol data: bad string table offset: %d",
  3080.            bufp->n_un.n_strx);
  3081.       namestring = bufp->n_un.n_strx + stringtab;
  3082.  
  3083.       processing_gcc_compilation =
  3084.     (bufp->n_type == N_TEXT
  3085.      && !strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL));
  3086.     }
  3087.   else
  3088.     {
  3089.       /* The N_SO starting this symtab is the first symbol, so we
  3090.      better not check the symbol before it.  I'm not this can
  3091.      happen, but it doesn't hurt to check for it.  */
  3092.       lseek(desc, sym_offset, L_INCR);
  3093.       processing_gcc_compilation = 0;
  3094.     }
  3095.  
  3096.   if (symbuf_idx == symbuf_end)
  3097.     fill_symbuf();
  3098.   bufp = &symbuf[symbuf_idx];
  3099.   if ((unsigned char) bufp->n_type != N_SO)
  3100.     fatal("First symbol in segment of executable not a source symbol");
  3101.  
  3102.   for (symnum = 0;
  3103.        symnum < sym_size / sizeof(struct nlist);
  3104.        symnum++)
  3105.     {
  3106.       QUIT;            /* Allow this to be interruptable */
  3107.       if (symbuf_idx == symbuf_end)
  3108.     fill_symbuf();
  3109.       bufp = &symbuf[symbuf_idx++];
  3110.       type = bufp->n_type;
  3111.  
  3112.       if (offset &&
  3113.       (type == N_TEXT || type == N_DATA || type == N_BSS))
  3114.     bufp->n_value += text_offset;
  3115.  
  3116.       if (bufp->n_un.n_strx < 0 || bufp->n_un.n_strx >= stringtab_size)
  3117.     error ("Invalid symbol data: bad string table offset: %d",
  3118.            bufp->n_un.n_strx);
  3119.       namestring = bufp->n_un.n_strx + stringtab;
  3120.  
  3121.       if (type & N_STAB)
  3122.     process_one_symbol(type, bufp->n_desc,
  3123.                bufp->n_value, namestring);
  3124.       /* We skip checking for a new .o or -l file; that should never
  3125.          happen in this routine. */
  3126.       else if (type == N_TEXT
  3127.            && !strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL))
  3128.     /* I don't think this code will ever be executed, because
  3129.        the GCC_COMPILED_FLAG_SYMBOL usually is right before
  3130.        the N_SO symbol which starts this source file.
  3131.        However, there is no reason not to accept
  3132.        the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
  3133.     processing_gcc_compilation = 1;
  3134.       else if (type & N_EXT || type == N_TEXT
  3135. #ifdef N_NBTEXT
  3136.            || type == N_NBTEXT
  3137. #endif
  3138.            )
  3139.       /* Global symbol: see if we came across a dbx defintion for
  3140.          a corresponding symbol.  If so, store the value.  Remove
  3141.          syms from the chain when their values are stored, but
  3142.          search the whole chain, as there may be several syms from
  3143.          different files with the same name. */
  3144.       /* This is probably not true.  Since the files will be read
  3145.          in one at a time, each reference to a global symbol will
  3146.          be satisfied in each file as it appears. So we skip this
  3147.          section. */
  3148.     &stringtab_global;    /* For debugger; am I right? */
  3149.     }
  3150.   end_symtab (text_offset + text_size);
  3151. }
  3152.  
  3153. static int
  3154. hashname (name)
  3155.      char *name;
  3156. {
  3157.   register char *p = name;
  3158.   register int total = p[0];
  3159.   register int c;
  3160.  
  3161.   c = p[1];
  3162.   total += c << 2;
  3163.   if (c)
  3164.     {
  3165.       c = p[2];
  3166.       total += c << 4;
  3167.       if (c)
  3168.     total += p[3] << 6;
  3169.     }
  3170.  
  3171.   /* Ensure result is positive.  */
  3172.   if (total < 0) total += (1000 << 6);
  3173.   return total % HASHSIZE;
  3174. }
  3175.  
  3176. /* Put all appropriate global symbols in the symseg data
  3177.    onto the hash chains so that their addresses will be stored
  3178.    when seen later in loader global symbols.  */
  3179.  
  3180. static void
  3181. hash_symsegs ()
  3182. {
  3183.   /* Look at each symbol in each block in each symseg symtab.  */
  3184.   struct symtab *s;
  3185.   for (s = symseg_chain; s; s = s->next)
  3186.     {
  3187.       register int n;
  3188.       for (n = BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)) - 1; n >= 0; n--)
  3189.     {
  3190.       register struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), n);
  3191.       register int i;
  3192.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  3193.         {
  3194.           register struct symbol *sym = BLOCK_SYM (b, i);
  3195.  
  3196.           /* Put the symbol on a chain if its value is an address
  3197.          that is figured out by the loader.  */
  3198.  
  3199.           if (SYMBOL_CLASS (sym) == LOC_EXTERNAL)
  3200.         {
  3201.           register int hash = hashname (SYMBOL_NAME (sym));
  3202.           SYMBOL_VALUE (sym) = (int) global_sym_chain[hash];
  3203.           global_sym_chain[hash] = sym;
  3204.           SYMBOL_CLASS (sym) = LOC_STATIC;
  3205.         }
  3206.         }
  3207.     }
  3208.     }
  3209. }
  3210.  
  3211. static void
  3212. process_one_symbol (type, desc, value, name)
  3213.      int type, desc;
  3214.      CORE_ADDR value;
  3215.      char *name;
  3216. {
  3217.   register struct context_stack *new;
  3218.   char *colon_pos;
  3219.  
  3220.   /* Something is wrong if we see real data before
  3221.      seeing a source file name.  */
  3222.  
  3223.   if (last_source_file == 0 && type != N_SO)
  3224.     {
  3225.       /* Currently this ignores N_ENTRY on Gould machines, N_NSYM on machines
  3226.      where that code is defined.  */
  3227.       if (IGNORE_SYMBOL (type))
  3228.     return;
  3229.  
  3230.       error ("Invalid symbol data: does not start by identifying a source file.");
  3231.     }
  3232.  
  3233.   switch (type)
  3234.     {
  3235.     case N_FUN:
  3236.     case N_FNAME:
  3237.       /* Either of these types of symbols indicates the start of
  3238.      a new function.  We must process its "name" normally for dbx,
  3239.      but also record the start of a new lexical context, and possibly
  3240.      also the end of the lexical context for the previous function.  */
  3241.       /* This is not always true.  This type of symbol may indicate a
  3242.          text segment variable.  */
  3243.  
  3244.       colon_pos = index (name, ':');
  3245.       if (!colon_pos++
  3246.       || (*colon_pos != 'f' && *colon_pos != 'F'))
  3247.     {
  3248.       define_symbol (value, name, desc);
  3249.       break;
  3250.     }
  3251.  
  3252.       within_function = 1;
  3253.       if (context_stack_depth > 0)
  3254.     {
  3255.       new = &context_stack[--context_stack_depth];
  3256.       /* Make a block for the local symbols within.  */
  3257.       finish_block (new->name, &local_symbols, new->old_blocks,
  3258.             new->start_addr, value);
  3259.     }
  3260.       /* Stack must be empty now.  */
  3261.       if (context_stack_depth != 0)
  3262.     error ("Invalid symbol data: unmatched N_LBRAC before symtab pos %d.",
  3263.            symnum);
  3264.  
  3265.       new = &context_stack[context_stack_depth++];
  3266.       new->old_blocks = pending_blocks;
  3267.       new->start_addr = value;
  3268.       new->name = define_symbol (value, name, desc);
  3269.       local_symbols = 0;
  3270.       break;
  3271.  
  3272.     case N_LBRAC:
  3273.       /* This "symbol" just indicates the start of an inner lexical
  3274.      context within a function.  */
  3275.  
  3276.       if (context_stack_depth == context_stack_size)
  3277.     {
  3278.       context_stack_size *= 2;
  3279.       context_stack = (struct context_stack *)
  3280.         xrealloc (context_stack,
  3281.               (context_stack_size
  3282.                * sizeof (struct context_stack)));
  3283.     }
  3284.  
  3285.       new = &context_stack[context_stack_depth++];
  3286.       new->depth = desc;
  3287.       new->locals = local_symbols;
  3288.       new->old_blocks = pending_blocks;
  3289.       new->start_addr = value;
  3290.       new->name = 0;
  3291.       local_symbols = 0;
  3292.       break;
  3293.  
  3294.     case N_RBRAC:
  3295.       /* This "symbol" just indicates the end of an inner lexical
  3296.      context that was started with N_LBRAC.  */
  3297.       new = &context_stack[--context_stack_depth];
  3298.       if (desc != new->depth)
  3299.     error ("Invalid symbol data: N_LBRAC/N_RBRAC symbol mismatch, symtab pos %d.", symnum);
  3300.  
  3301.       /* Some native compilers put the variable decls inside of an
  3302.          LBRAC/RBRAC block.  This macro should be nonzero if this
  3303.      is true.  DESC is N_DESC from the N_RBRAC symbol.  */
  3304. #if !defined (VARIABLES_INSIDE_BLOCK)
  3305. #define VARIABLES_INSIDE_BLOCK(desc) 0
  3306. #endif
  3307.  
  3308.       /* Can only use new->locals as local symbols here if we're in
  3309.          gcc or on a machine that puts them before the lbrack.  */
  3310.       if (!VARIABLES_INSIDE_BLOCK(desc))
  3311.     local_symbols = new->locals;
  3312.  
  3313.       /* If this is not the outermost LBRAC...RBRAC pair in the
  3314.      function, its local symbols preceded it, and are the ones
  3315.      just recovered from the context stack.  Defined the block for them.
  3316.  
  3317.      If this is the outermost LBRAC...RBRAC pair, there is no
  3318.      need to do anything; leave the symbols that preceded it
  3319.      to be attached to the function's own block.  However, if
  3320.      it is so, we need to indicate that we just moved outside
  3321.      of the function.  */
  3322.       if (local_symbols
  3323.       && context_stack_depth > !VARIABLES_INSIDE_BLOCK(desc))
  3324.     {
  3325.       /* Muzzle a compiler bug that makes end < start.  */
  3326.       if (new->start_addr > value)
  3327.         new->start_addr = value;
  3328.       /* Make a block for the local symbols within.  */
  3329.       finish_block (0, &local_symbols, new->old_blocks,
  3330.             new->start_addr + last_source_start_addr,
  3331.             value + last_source_start_addr);
  3332.     }
  3333.       else
  3334.     {
  3335.       within_function = 0;
  3336.     }
  3337.       if (VARIABLES_INSIDE_BLOCK(desc))
  3338.     /* Now pop locals of block just finished.  */
  3339.     local_symbols = new->locals;
  3340.       break;
  3341.  
  3342.     case N_FN | N_EXT:
  3343.       /* This kind of symbol supposedly indicates the start
  3344.      of an object file.  In fact this type does not appear.  */
  3345.       break;
  3346.  
  3347.     case N_SO:
  3348.       /* This type of symbol indicates the start of data
  3349.      for one source file.
  3350.      Finish the symbol table of the previous source file
  3351.      (if any) and start accumulating a new symbol table.  */
  3352. #ifdef PCC_SOL_BROKEN
  3353.       /* pcc bug, occasionally puts out SO for SOL.  */
  3354.       if (context_stack_depth > 0)
  3355.     {
  3356.       start_subfile (name);
  3357.       break;
  3358.     }
  3359. #endif
  3360.       if (last_source_file)
  3361.     end_symtab (value);
  3362.       start_symtab (name, value);
  3363.       break;
  3364.  
  3365.     case N_SOL:
  3366.       /* This type of symbol indicates the start of data for
  3367.      a sub-source-file, one whose contents were copied or
  3368.      included in the compilation of the main source file
  3369.      (whose name was given in the N_SO symbol.)  */
  3370.       start_subfile (name);
  3371.       break;
  3372.  
  3373. #ifdef N_BINCL
  3374.     case N_BINCL:
  3375.       push_subfile ();
  3376.       add_new_header_file (name, value);
  3377.       start_subfile (name);
  3378.       break;
  3379.  
  3380.     case N_EINCL:
  3381.       start_subfile (pop_subfile ());
  3382.       break;
  3383.  
  3384.     case N_EXCL:
  3385.       add_old_header_file (name, value);
  3386.       break;
  3387. #endif /* have N_BINCL */
  3388.  
  3389.     case N_SLINE:
  3390.       /* This type of "symbol" really just records
  3391.      one line-number -- core-address correspondence.
  3392.      Enter it in the line list for this symbol table.  */
  3393.       record_line (desc, value);
  3394.       break;
  3395.  
  3396.     case N_BCOMM:
  3397.       if (common_block)
  3398.     error ("Invalid symbol data: common within common at symtab pos %d",
  3399.            symnum);
  3400.       common_block = local_symbols;
  3401.       common_block_i = local_symbols ? local_symbols->nsyms : 0;
  3402.       break;
  3403.  
  3404.     case N_ECOMM:
  3405.       /* Symbols declared since the BCOMM are to have the common block
  3406.      start address added in when we know it.  common_block points to
  3407.      the first symbol after the BCOMM in the local_symbols list;
  3408.      copy the list and hang it off the symbol for the common block name
  3409.      for later fixup.  */
  3410.       {
  3411.     int i;
  3412.     struct pending *link = local_symbols;
  3413.     struct symbol *sym =
  3414.       (struct symbol *) xmalloc (sizeof (struct symbol));
  3415.     bzero (sym, sizeof *sym);
  3416.     SYMBOL_NAME (sym) = savestring (name, strlen (name));
  3417.     SYMBOL_CLASS (sym) = LOC_BLOCK;
  3418.     SYMBOL_NAMESPACE (sym) = (enum namespace)((long)
  3419.       copy_pending (local_symbols, common_block_i, common_block));
  3420.     i = hashname (SYMBOL_NAME (sym));
  3421.     SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
  3422.     global_sym_chain[i] = sym;
  3423.     common_block = 0;
  3424.     break;
  3425.       }
  3426.  
  3427.     case N_ECOML:
  3428.     case N_LENG:
  3429.       break;
  3430.  
  3431.     default:
  3432.       if (name)
  3433.     define_symbol (value, name, desc);
  3434.     }
  3435. }
  3436.  
  3437. /* This function was added for C++ functionality.  I presume that it
  3438.    condenses the bunches formed by reading in an additional .o file
  3439.    (incremental linking). */
  3440.  
  3441. static void
  3442. condense_addl_misc_bunches ()
  3443. {
  3444.   register int i, j;
  3445.   register struct misc_bunch *bunch;
  3446. #ifdef NAMES_HAVE_UNDERSCORE
  3447.   int offset = 1;
  3448. #else
  3449.   int offset = 0;
  3450. #endif
  3451.  
  3452.   misc_function_vector
  3453.     = (struct misc_function *)  xrealloc (misc_function_vector,
  3454.                       (misc_count + misc_function_count) * sizeof (struct misc_function));
  3455.  
  3456.   j = misc_function_count;
  3457.   bunch = misc_bunch;
  3458.   while (bunch)
  3459.     {
  3460.       for (i = 0; i < misc_bunch_index; i++)
  3461.     {
  3462.       misc_function_vector[j] = bunch->contents[i];
  3463.       misc_function_vector[j].name
  3464.         = concat (misc_function_vector[j].name
  3465.               + (misc_function_vector[j].name[0] == '_' ? offset : 0),
  3466.               "", "");
  3467.       j++;
  3468.     }
  3469.       bunch = bunch->next;
  3470.       misc_bunch_index = MISC_BUNCH_SIZE;
  3471.     }
  3472.  
  3473.   misc_function_count += misc_count;
  3474.  
  3475.   /* Sort the misc functions by address.  */
  3476.  
  3477.   qsort (misc_function_vector, misc_function_count,
  3478.      sizeof (struct misc_function),  compare_misc_functions);
  3479. }
  3480.  
  3481.  
  3482. /* Read in another .o file and create a symtab entry for it.*/
  3483.  
  3484. static void
  3485. read_addl_syms (desc, stringtab, nlistlen, text_addr, text_size)
  3486.      int desc;
  3487.      register char *stringtab;
  3488.      register int nlistlen;
  3489.      unsigned text_addr;
  3490.      int text_size;
  3491. {
  3492.   FILE *stream = fdopen (desc, "r");
  3493.   register char *namestring;
  3494.   register struct symbol *sym, *prev;
  3495.   int hash;
  3496.  
  3497. #ifdef N_BINCL
  3498.   subfile_stack = 0;
  3499. #endif
  3500.  
  3501.   last_source_file = 0;
  3502.   bzero (global_sym_chain, sizeof global_sym_chain);
  3503.   symtab_input_desc = desc;
  3504.   stringtab_global = stringtab;
  3505.   fill_symbuf ();
  3506.  
  3507.   for (symnum = 0; symnum < nlistlen; symnum++)
  3508.     {
  3509.       struct nlist *bufp;
  3510.       unsigned char type;
  3511.  
  3512.       QUIT;    /* allow this to be interruptable */
  3513.       if (symbuf_idx == symbuf_end)
  3514.     fill_symbuf ();
  3515.       bufp = &symbuf[symbuf_idx++];
  3516.       type = bufp->n_type & N_TYPE;
  3517.       namestring = bufp->n_un.n_strx + stringtab;
  3518.  
  3519.       if( (type == N_TEXT) || (type == N_DATA) || (type == N_BSS) )
  3520.     {
  3521.       /* Relocate this file's symbol table information
  3522.          to the address it has been loaded into.  */
  3523.       bufp->n_value += text_addr;
  3524.     }
  3525.  
  3526.       type = bufp->n_type;
  3527.  
  3528.       if (type & N_STAB)
  3529.     process_one_symbol (type, bufp->n_desc,
  3530.                 bufp->n_value, namestring);
  3531.       /* A static text symbol whose name ends in ".o"
  3532.      can only mean the start of another object file.
  3533.      So end the symtab of the source file we have been processing.
  3534.      This is how we avoid counting the libraries as part
  3535.      or the last source file.
  3536.      Also this way we find end of first object file (crt0).  */
  3537.       else if ((type == N_TEXT
  3538. #ifdef N_NBTEXT
  3539.         || type == N_NBTEXT
  3540. #endif
  3541.         )
  3542.            && (!strcmp (namestring + strlen (namestring) - 2, ".o"))
  3543.            || ! strncmp (namestring, "-l", 2))
  3544.     {
  3545.       if (last_source_file)
  3546.         end_symtab (bufp->n_value);
  3547.     }
  3548.       else if (type & N_EXT || type == N_TEXT
  3549. #ifdef N_NBTEXT
  3550.            || type == N_NBTEXT
  3551. #endif
  3552.            )
  3553.     {
  3554.       int used_up = 0;
  3555.  
  3556.       /* Record the location of _etext.  */
  3557.       if (type == (N_TEXT | N_EXT)
  3558.           && !strcmp (namestring, "_etext"))
  3559.         end_of_text_addr = bufp->n_value;
  3560.  
  3561. #if 0
  3562.       /* 25 Sep 89: The following seems to be stolen from
  3563.          read_ofile_symtab, and is wrong here (i.e. there was no
  3564.          first pass for add-file symbols).  */
  3565.       /* This shouldn't be necessary, as we now do all of this work
  3566.          in scan_global syms and all misc functions should have been
  3567.          recorded on the first pass.  */
  3568.       /* Global symbol: see if we came across a dbx definition
  3569.          for a corresponding symbol.  If so, store the value.
  3570.          Remove syms from the chain when their values are stored,
  3571.          but search the whole chain, as there may be several syms
  3572.          from different files with the same name.  */
  3573.       if (type & N_EXT)
  3574.         {
  3575.           prev = 0;
  3576. #ifdef NAMES_HAVE_UNDERSCORE
  3577.           hash = hashname (namestring + 1);
  3578. #else /* not NAMES_HAVE_UNDERSCORE */
  3579.           hash = hashname (namestring);
  3580. #endif /* not NAMES_HAVE_UNDERSCORE */
  3581.           for (sym = global_sym_chain[hash];
  3582.            sym;)
  3583.         {
  3584.           if (
  3585. #ifdef NAMES_HAVE_UNDERSCORE
  3586.               *namestring == '_'
  3587.               && namestring[1] == SYMBOL_NAME (sym)[0]
  3588.               &&
  3589.               !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
  3590. #else /* NAMES_HAVE_UNDERSCORE */
  3591.               namestring[0] == SYMBOL_NAME (sym)[0]
  3592.               &&
  3593.               !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
  3594. #endif /* NAMES_HAVE_UNDERSCORE */
  3595.               )
  3596.             {
  3597.               if (prev)
  3598.             SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  3599.               else
  3600.             global_sym_chain[hash]
  3601.               = (struct symbol *) SYMBOL_VALUE (sym);
  3602.               if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  3603.             fix_common_block (sym, bufp->n_value);
  3604.               else
  3605.             SYMBOL_VALUE (sym) = bufp->n_value;
  3606.               if (prev)
  3607.             sym = (struct symbol *) SYMBOL_VALUE (prev);
  3608.               else
  3609.             sym = global_sym_chain[hash];
  3610.  
  3611.               used_up = 1;
  3612.             }
  3613.           else
  3614.             {
  3615.               prev = sym;
  3616.               sym = (struct symbol *) SYMBOL_VALUE (sym);
  3617.             }
  3618.         }
  3619.         }
  3620.  
  3621.       /* Defined global or text symbol: record as a misc function
  3622.          if it didn't give its address to a debugger symbol above.  */
  3623.       if (type <= (N_TYPE | N_EXT)
  3624.           && type != N_EXT
  3625.           && ! used_up)
  3626.         record_misc_function (namestring, bufp->n_value,
  3627.                   bufp->n_type);
  3628. #endif /* 0 */
  3629.     }
  3630.     }
  3631.  
  3632.   if (last_source_file)
  3633.     end_symtab (text_addr + text_size);
  3634.  
  3635.   fclose (stream);
  3636. }
  3637.  
  3638. /* C++:
  3639.    This function allows the addition of incrementally linked object files.
  3640.    Since this has a fair amount of code in common with symbol_file_command,
  3641.    it might be worthwhile to consolidate things, as was done with
  3642.    read_dbx_symtab and condense_misc_bunches. */
  3643.  
  3644. void
  3645. add_file_command (arg_string)
  3646.      char* arg_string;
  3647. {
  3648.   register int desc;
  3649.   DECLARE_FILE_HEADERS;
  3650.   struct nlist *nlist;
  3651.   char *stringtab;
  3652.   long buffer;
  3653.   register int val;
  3654.   extern void close ();
  3655.   struct cleanup *old_chain;
  3656.   struct symtab *symseg;
  3657.   struct stat statbuf;
  3658.   char *name;
  3659.   unsigned text_addr;
  3660.  
  3661.   if (arg_string == 0)
  3662.     error ("add-file takes a file name and an address");
  3663.  
  3664.   arg_string = tilde_expand (arg_string);
  3665.   make_cleanup (free, arg_string);
  3666.  
  3667.   for( ; *arg_string == ' '; arg_string++ );
  3668.   name = arg_string;
  3669.   for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
  3670.   *arg_string++ = (char) 0;
  3671.  
  3672.   if (name[0] == 0)
  3673.     error ("add-file takes a file name and an address");
  3674.  
  3675.   text_addr = parse_and_eval_address (arg_string);
  3676.  
  3677.   dont_repeat ();
  3678.  
  3679.   if (!query ("add symbol table from filename \"%s\" at text_addr = 0x%x\n",
  3680.           name, text_addr))
  3681.     error ("Not confirmed.");
  3682.  
  3683.   desc = open (name, O_RDONLY);
  3684.   if (desc < 0)
  3685.     perror_with_name (name);
  3686.  
  3687.   old_chain = make_cleanup (close, desc);
  3688.  
  3689.   READ_FILE_HEADERS (desc, name);
  3690.  
  3691.   if (NUMBER_OF_SYMBOLS == 0)
  3692.     {
  3693.       printf ("%s does not have a symbol-table.\n", name);
  3694.       fflush (stdout);
  3695.       return;
  3696.     }
  3697.  
  3698.   printf ("Reading symbol data from %s...", name);
  3699.   fflush (stdout);
  3700.  
  3701.   /* Now read the string table, all at once.  */
  3702.   val = lseek (desc, STRING_TABLE_OFFSET, 0);
  3703.   if (val < 0)
  3704.     perror_with_name (name);
  3705.   if (stat (name, &statbuf) < 0)
  3706.     perror_with_name (name);
  3707.   READ_STRING_TABLE_SIZE (buffer);
  3708.   if (buffer >= 0 && buffer < statbuf.st_size)
  3709.     {
  3710. #ifdef BROKEN_LARGE_ALLOCA
  3711.       stringtab = (char *) xmalloc (buffer);
  3712.       make_cleanup (free, stringtab);
  3713. #else
  3714.       stringtab = (char *) alloca (buffer);
  3715. #endif
  3716.     }
  3717.   else
  3718.     stringtab = NULL;
  3719.   if (stringtab == NULL)
  3720.     error ("ridiculous string table size: %d bytes", buffer);
  3721.  
  3722.   /* Usually READ_STRING_TABLE_SIZE will have shifted the file pointer.
  3723.      Occaisionally, it won't.  */
  3724.   val = lseek (desc, STRING_TABLE_OFFSET, 0);
  3725.   if (val < 0)
  3726.     perror_with_name (name);
  3727.   val = myread (desc, stringtab, buffer);
  3728.   if (val < 0)
  3729.     perror_with_name (name);
  3730.  
  3731.   /* Symsegs are no longer supported by GDB.  Setting symseg_chain to
  3732.      0 is easier than finding all the symseg code and eliminating it.  */
  3733.   symseg_chain = 0;
  3734.  
  3735.   /* Position to read the symbol table.  Do not read it all at once. */
  3736.   val = lseek (desc, SYMBOL_TABLE_OFFSET, 0);
  3737.   if (val < 0)
  3738.     perror_with_name (name);
  3739.  
  3740.   init_misc_functions ();
  3741.   make_cleanup (discard_misc_bunches, 0);
  3742.   init_header_files ();
  3743.   make_cleanup (free_header_files, 0);
  3744.   free_pendings = 0;
  3745.   pending_blocks = 0;
  3746.   file_symbols = 0;
  3747.   global_symbols = 0;
  3748.   make_cleanup (really_free_pendings, 0);
  3749.  
  3750.   read_addl_syms (desc, stringtab, NUMBER_OF_SYMBOLS, text_addr,
  3751.           SIZE_OF_TEXT_SEGMENT);
  3752.  
  3753.  
  3754.   /* Sort symbols alphabetically within each block.  */
  3755.  
  3756.   sort_syms ();
  3757.  
  3758.   /* Go over the misc functions and install them in vector.  */
  3759.  
  3760.   condense_addl_misc_bunches (1);
  3761.  
  3762.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  3763.  
  3764.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  3765.  
  3766.   do_cleanups (old_chain);
  3767.  
  3768.   /* Free the symtabs made by read_symsegs, but not their contents,
  3769.      which have been copied into symtabs on symtab_list.  */
  3770.   while (symseg_chain)
  3771.     {
  3772.       register struct symtab *s = symseg_chain->next;
  3773.       free (symseg_chain);
  3774.       symseg_chain = s;
  3775.     }
  3776.  
  3777.   printf ("done.\n");
  3778.   fflush (stdout);
  3779. }
  3780.  
  3781. /* Read a number by which a type is referred to in dbx data,
  3782.    or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
  3783.    Just a single number N is equivalent to (0,N).
  3784.    Return the two numbers by storing them in the vector TYPENUMS.
  3785.    TYPENUMS will then be used as an argument to dbx_lookup_type.  */
  3786.  
  3787. static void
  3788. read_type_number (pp, typenums)
  3789.      register char **pp;
  3790.      register int *typenums;
  3791. {
  3792.   if (**pp == '(')
  3793.     {
  3794.       (*pp)++;
  3795.       typenums[0] = read_number (pp, ',');
  3796.       typenums[1] = read_number (pp, ')');
  3797.     }
  3798.   else
  3799.     {
  3800.       typenums[0] = 0;
  3801.       typenums[1] = read_number (pp, 0);
  3802.     }
  3803. }
  3804.  
  3805.  
  3806.  
  3807. static struct symbol *
  3808. define_symbol (value, string, desc)
  3809.      int value;
  3810.      char *string;
  3811.      int desc;
  3812. {
  3813.   register struct symbol *sym
  3814.     = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
  3815.   char *p = (char *) index (string, ':');
  3816.   int deftype;
  3817.   register int i;
  3818.  
  3819.   /* Ignore syms with empty names.  */
  3820.   if (string[0] == 0)
  3821.     return 0;
  3822.  
  3823.   /* Ignore old-style symbols from cc -go  */
  3824.   if (p == 0)
  3825.     return 0;
  3826.  
  3827.   SYMBOL_NAME (sym)
  3828.     = (char *) obstack_alloc (symbol_obstack, ((p - string) + 1));
  3829.   /* Open-coded bcopy--saves function call time.  */
  3830.   {
  3831.     register char *p1 = string;
  3832.     register char *p2 = SYMBOL_NAME (sym);
  3833.     while (p1 != p)
  3834.       *p2++ = *p1++;
  3835.     *p2++ = '\0';
  3836.   }
  3837.   p++;
  3838.   /* Determine the type of name being defined.  */
  3839.   if ((*p >= '0' && *p <= '9') || *p == '(')
  3840.     deftype = 'l';
  3841.   else
  3842.     deftype = *p++;
  3843.  
  3844.   /* c is a special case, not followed by a type-number.
  3845.      SYMBOL:c=iVALUE for an integer constant symbol.
  3846.      SYMBOL:c=rVALUE for a floating constant symbol.
  3847.      SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
  3848.         e.g. "b:c=e6,0" for "const b = blob1"
  3849.     (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
  3850.   if (deftype == 'c')
  3851.     {
  3852.       if (*p++ != '=')
  3853.     error ("Invalid symbol data at symtab pos %d.", symnum);
  3854.       switch (*p++)
  3855.     {
  3856.     case 'r':
  3857.       {
  3858.         double d = atof (p);
  3859.         char *value;
  3860.  
  3861.         SYMBOL_TYPE (sym) = builtin_type_double;
  3862.         value = (char *) obstack_alloc (symbol_obstack, sizeof (double));
  3863.         bcopy (&d, value, sizeof (double));
  3864.         SYMBOL_VALUE_BYTES (sym) = value;
  3865.         SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
  3866.       }
  3867.       break;
  3868.     case 'i':
  3869.       {
  3870.         SYMBOL_TYPE (sym) = builtin_type_int;
  3871.         SYMBOL_VALUE (sym) = atoi (p);
  3872.         SYMBOL_CLASS (sym) = LOC_CONST;
  3873.       }
  3874.       break;
  3875.     case 'e':
  3876.       /* SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
  3877.          e.g. "b:c=e6,0" for "const b = blob1"
  3878.          (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
  3879.       {
  3880.         int typenums[2];
  3881.         
  3882.         read_type_number (&p, typenums);
  3883.         if (*p++ != ',')
  3884.           error ("Invalid symbol data: no comma in enum const symbol");
  3885.         
  3886.         SYMBOL_TYPE (sym) = *dbx_lookup_type (typenums);
  3887.         SYMBOL_VALUE (sym) = atoi (p);
  3888.         SYMBOL_CLASS (sym) = LOC_CONST;
  3889.       }
  3890.       break;
  3891.     default:
  3892.       error ("Invalid symbol data at symtab pos %d.", symnum);
  3893.     }
  3894.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3895.       add_symbol_to_list (sym, &file_symbols);
  3896.       return sym;
  3897.     }
  3898.  
  3899.   /* Now usually comes a number that says which data type,
  3900.      and possibly more stuff to define the type
  3901.      (all of which is handled by read_type)  */
  3902.  
  3903.   if (deftype == 'p' && *p == 'F')
  3904.     /* pF is a two-letter code that means a function parameter in Fortran.
  3905.        The type-number specifies the type of the return value.
  3906.        Translate it into a pointer-to-function type.  */
  3907.     {
  3908.       p++;
  3909.       SYMBOL_TYPE (sym)
  3910.     = lookup_pointer_type (lookup_function_type (read_type (&p)));
  3911.     }
  3912.   else
  3913.     {
  3914.       struct type *type = read_type (&p);
  3915.  
  3916.       if ((deftype == 'F' || deftype == 'f')
  3917.       && TYPE_CODE (type) != TYPE_CODE_FUNC)
  3918.     SYMBOL_TYPE (sym) = lookup_function_type (type);
  3919.       else
  3920.     SYMBOL_TYPE (sym) = type;
  3921.     }
  3922.  
  3923.   switch (deftype)
  3924.     {
  3925.     case 'f':
  3926.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  3927.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3928.       add_symbol_to_list (sym, &file_symbols);
  3929.       break;
  3930.  
  3931.     case 'F':
  3932.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  3933.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3934.       add_symbol_to_list (sym, &global_symbols);
  3935.       break;
  3936.  
  3937.     case 'G':
  3938.       /* For a class G (global) symbol, it appears that the
  3939.      value is not correct.  It is necessary to search for the
  3940.      corresponding linker definition to find the value.
  3941.      These definitions appear at the end of the namelist.  */
  3942.       i = hashname (SYMBOL_NAME (sym));
  3943.       SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
  3944.       global_sym_chain[i] = sym;
  3945.       SYMBOL_CLASS (sym) = LOC_STATIC;
  3946.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3947.       add_symbol_to_list (sym, &global_symbols);
  3948.       break;
  3949.  
  3950.       /* This case is faked by a conditional above,
  3951.      when there is no code letter in the dbx data.
  3952.      Dbx data never actually contains 'l'.  */
  3953.     case 'l':
  3954.       SYMBOL_CLASS (sym) = LOC_LOCAL;
  3955.       SYMBOL_VALUE (sym) = value;
  3956.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3957.       add_symbol_to_list (sym, &local_symbols);
  3958.       break;
  3959.  
  3960.     case 'p':
  3961.       SYMBOL_CLASS (sym) = LOC_ARG;
  3962.       SYMBOL_VALUE (sym) = value;
  3963.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  3964.       add_symbol_to_list (sym, &local_symbols);
  3965.  
  3966.       /* If it's compiled, if it says `short', believe it.  */
  3967.       if (processing_gcc_compilation || BELIEVE_PCC_PROMOTION)
  3968.     break;
  3969.  
  3970. #if defined(BELIEVE_PCC_PROMOTION_TYPE)
  3971.       /* This macro is defined on machines (e.g. sparc) where
  3972.      we should believe the type of a PCC 'short' argument,
  3973.      but shouldn't believe the address (the address is
  3974.      the address of the corresponding int).  Note that
  3975.      this is only different from the BELIEVE_PCC_PROMOTION
  3976.      case on big-endian machines.
  3977.  
  3978.      My guess is that this correction, as opposed to changing
  3979.      the parameter to an 'int' (as done below, for PCC
  3980.      on most machines), is the right thing to do
  3981.      on all machines, but I don't want to risk breaking
  3982.      something that already works.  On most PCC machines,
  3983.      the sparc problem doesn't come up because the calling
  3984.      function has to zero the top bytes (not knowing whether
  3985.      the called function wants an int or a short), so there
  3986.      is no practical difference between an int and a short
  3987.      (except perhaps what happens when the GDB user types
  3988.      "print short_arg = 0x10000;").  */
  3989.       if (SYMBOL_TYPE (sym) == builtin_type_char
  3990.       || SYMBOL_TYPE (sym) == builtin_type_unsigned_char)
  3991.     SYMBOL_VALUE (sym) += 3;
  3992.       if (SYMBOL_TYPE (sym) == builtin_type_short
  3993.       || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  3994.     SYMBOL_VALUE (sym) += 2;
  3995.       break;
  3996.  
  3997. #else /* no BELIEVE_PCC_PROMOTION_TYPE.  */
  3998.  
  3999.       /* If PCC says a parameter is a short or a char,
  4000.      it is really an int.  */
  4001.       if (SYMBOL_TYPE (sym) == builtin_type_char
  4002.       || SYMBOL_TYPE (sym) == builtin_type_short)
  4003.     SYMBOL_TYPE (sym) = builtin_type_int;
  4004.       else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
  4005.            || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  4006.     SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
  4007.       break;
  4008.  
  4009. #endif /* no BELIEVE_PCC_PROMOTION_TYPE.  */
  4010.  
  4011.     case 'P':
  4012.       SYMBOL_CLASS (sym) = LOC_REGPARM;
  4013.       SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (value);
  4014.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4015.       add_symbol_to_list (sym, &local_symbols);
  4016.       break;
  4017.  
  4018.     case 'r':
  4019.       SYMBOL_CLASS (sym) = LOC_REGISTER;
  4020.       SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (value);
  4021.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4022.       add_symbol_to_list (sym, &local_symbols);
  4023.       break;
  4024.  
  4025.     case 'S':
  4026.       /* Static symbol at top level of file */
  4027.       SYMBOL_CLASS (sym) = LOC_STATIC;
  4028.       SYMBOL_VALUE (sym) = value;
  4029.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4030.       add_symbol_to_list (sym, &file_symbols);
  4031.       break;
  4032.  
  4033.     case 't':
  4034.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  4035.       SYMBOL_VALUE (sym) = value;
  4036.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4037.       if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  4038.       && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  4039.     TYPE_NAME (SYMBOL_TYPE (sym)) =
  4040.       obsavestring (SYMBOL_NAME (sym),
  4041.             strlen (SYMBOL_NAME (sym)));
  4042.        /* C++ vagaries: we may have a type which is derived from
  4043.       a base type which did not have its name defined when the
  4044.       derived class was output.  We fill in the derived class's
  4045.       base part member's name here in that case.  */
  4046.        else if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
  4047.          || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
  4048.         && TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)))
  4049.      {
  4050.        int i;
  4051.        for (i = TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)); i > 0; i--)
  4052.          if (TYPE_FIELD_NAME (SYMBOL_TYPE (sym), i - 1) == 0)
  4053.            TYPE_FIELD_NAME (SYMBOL_TYPE (sym), i - 1) =
  4054.          TYPE_NAME (TYPE_BASECLASS (SYMBOL_TYPE (sym), i));
  4055.      }
  4056.  
  4057.       add_symbol_to_list (sym, &file_symbols);
  4058.       break;
  4059.  
  4060.     case 'T':
  4061.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  4062.       SYMBOL_VALUE (sym) = value;
  4063.       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  4064.       if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  4065.       && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  4066.     TYPE_NAME (SYMBOL_TYPE (sym))
  4067.       = obconcat ("",
  4068.               (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
  4069.                ? "enum "
  4070.                : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
  4071.               ? "struct " : "union ")),
  4072.               SYMBOL_NAME (sym));
  4073.       add_symbol_to_list (sym, &file_symbols);
  4074.       break;
  4075.  
  4076.     case 'V':
  4077.       /* Static symbol of local scope */
  4078.       SYMBOL_CLASS (sym) = LOC_STATIC;
  4079.       SYMBOL_VALUE (sym) = value;
  4080.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4081.       add_symbol_to_list (sym, &local_symbols);
  4082.       break;
  4083.  
  4084.     case 'v':
  4085.       /* Reference parameter */
  4086.       SYMBOL_CLASS (sym) = LOC_REF_ARG;
  4087.       SYMBOL_VALUE (sym) = value;
  4088.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4089.       add_symbol_to_list (sym, &local_symbols);
  4090.       break;
  4091.  
  4092.     case 'X':
  4093.       /* This is used by Sun FORTRAN for "function result value".
  4094.      Sun claims ("dbx and dbxtool interfaces", 2nd ed)
  4095.      that Pascal uses it too, but when I tried it Pascal used
  4096.      "x:3" (local symbol) instead.  */
  4097.       SYMBOL_CLASS (sym) = LOC_LOCAL;
  4098.       SYMBOL_VALUE (sym) = value;
  4099.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  4100.       add_symbol_to_list (sym, &local_symbols);
  4101.       break;
  4102.  
  4103.     default:
  4104.       error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
  4105.     }
  4106.   return sym;
  4107. }
  4108.  
  4109. /* What about types defined as forward references inside of a small lexical
  4110.    scope?  */
  4111. /* Add a type to the list of undefined types to be checked through
  4112.    once this file has been read in.  */
  4113. static void
  4114. add_undefined_type (type)
  4115.      struct type *type;
  4116. {
  4117.   if (undef_types_length == undef_types_allocated)
  4118.     {
  4119.       undef_types_allocated *= 2;
  4120.       undef_types = (struct type **)
  4121.     xrealloc (undef_types,
  4122.           undef_types_allocated * sizeof (struct type *));
  4123.     }
  4124.   undef_types[undef_types_length++] = type;
  4125. }
  4126.  
  4127. /* Add here something to go through each undefined type, see if it's
  4128.    still undefined, and do a full lookup if so.  */
  4129. static void
  4130. cleanup_undefined_types ()
  4131. {
  4132.   struct type **type, *ntype;
  4133.   struct symbol *sym;
  4134.  
  4135.   for (type = undef_types; type < undef_types + undef_types_length; type++)
  4136.     {
  4137.       struct type *ntype = 0;
  4138.       /* Reasonable test to see if it's been defined since.  */
  4139.       if (TYPE_NFIELDS (*type) == 0)
  4140.     {
  4141.       struct pending *ppt;
  4142.       int i;
  4143.       /* Name of the type, without "struct" or "union" */
  4144.       char *typename = TYPE_NAME (*type);
  4145.  
  4146.       if (!strncmp (typename, "struct ", 7))
  4147.         typename += 7;
  4148.       if (!strncmp (typename, "union ", 6))
  4149.         typename += 6;
  4150.  
  4151.       for (ppt = file_symbols; ppt; ppt = ppt->next)
  4152.         for (i = 0; i < ppt->nsyms; i++)
  4153.           {
  4154.         struct symbol *sym = ppt->symbol[i];
  4155.  
  4156.         if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  4157.             && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
  4158.             && (TYPE_CODE (SYMBOL_TYPE (sym)) ==
  4159.             TYPE_CODE (*type))
  4160.             && !strcmp (SYMBOL_NAME (sym), typename))
  4161.           bcopy (SYMBOL_TYPE (sym), *type, sizeof (struct type));
  4162.           }
  4163.     }
  4164.       else
  4165.     /* It has been defined; don't mark it as a stub.  */
  4166.     TYPE_FLAGS (*type) &= ~TYPE_FLAG_STUB;
  4167.     }
  4168.   undef_types_length = 0;
  4169. }
  4170.  
  4171.  
  4172.  
  4173. /* Read a dbx type reference or definition;
  4174.    return the type that is meant.
  4175.    This can be just a number, in which case it references
  4176.    a type already defined and placed in type_vector.
  4177.    Or the number can be followed by an =, in which case
  4178.    it means to define a new type according to the text that
  4179.    follows the =.  */
  4180.  
  4181. static
  4182. struct type *
  4183. read_type (pp)
  4184.      register char **pp;
  4185. {
  4186.   register struct type *type = 0;
  4187.   register int n;
  4188.   struct type *type1;
  4189.   int typenums[2];
  4190.   int xtypenums[2];
  4191.   char *tmpc;
  4192.  
  4193.   /* Read type number if present.  The type number may be omitted.
  4194.      for instance in a two-dimensional array declared with type
  4195.      "ar1;1;10;ar1;1;10;4".  */
  4196.   if ((**pp >= '0' && **pp <= '9')
  4197.       || **pp == '(')
  4198.     {
  4199.       read_type_number (pp, typenums);
  4200.       
  4201.       /* Detect random reference to type not yet defined.
  4202.      Allocate a type object but leave it zeroed.  */
  4203.       if (**pp != '=')
  4204.     return dbx_alloc_type (typenums);
  4205.  
  4206.       *pp += 2;
  4207.     }
  4208.   else
  4209.     {
  4210.       /* 'typenums=' not present, type is anonymous.  Read and return
  4211.      the definition, but don't put it in the type vector.  */
  4212.       typenums[0] = typenums[1] = -1;
  4213.       *pp += 1;
  4214.     }
  4215.       
  4216.   switch ((*pp)[-1])
  4217.     {
  4218.     case 'x':
  4219.       {
  4220.     enum type_code code;
  4221.  
  4222.     /* Used to index through file_symbols.  */
  4223.     struct pending *ppt;
  4224.     int i;
  4225.     
  4226.     /* Name including "struct", etc.  */
  4227.     char *type_name;
  4228.     
  4229.     /* Name without "struct", etc.  */
  4230.     char *type_name_only;
  4231.  
  4232.     {
  4233.       char *prefix;
  4234.       char *from, *to;
  4235.       
  4236.       /* Set the type code according to the following letter.  */
  4237.       switch ((*pp)[0])
  4238.         {
  4239.         case 's':
  4240.           code = TYPE_CODE_STRUCT;
  4241.           prefix = "struct ";
  4242.           break;
  4243.         case 'u':
  4244.           code = TYPE_CODE_UNION;
  4245.           prefix = "union ";
  4246.           break;
  4247.         case 'e':
  4248.           code = TYPE_CODE_ENUM;
  4249.           prefix = "enum ";
  4250.           break;
  4251.         default:
  4252.           error ("Bad type cross reference at symnum: %d.", symnum);
  4253.         }
  4254.       
  4255.       to = type_name = (char *)
  4256.         obstack_alloc (symbol_obstack,
  4257.                (strlen (prefix) +
  4258.                 ((char *) index (*pp, ':') - (*pp)) + 1));
  4259.     
  4260.       /* Copy the prefix.  */
  4261.       from = prefix;
  4262.       while (*to++ = *from++)
  4263.         ;
  4264.       to--; 
  4265.     
  4266.       type_name_only = to;
  4267.  
  4268.       /* Copy the name.  */
  4269.       from = *pp + 1;
  4270.       while ((*to++ = *from++) != ':')
  4271.         ;
  4272.       *--to = '\0';
  4273.       
  4274.       /* Set the pointer ahead of the name which we just read.  */
  4275.       *pp = from;
  4276.     
  4277. #if 0
  4278.       /* The following hack is clearly wrong, because it doesn't
  4279.          check whether we are in a baseclass.  I tried to reproduce
  4280.          the case that it is trying to fix, but I couldn't get
  4281.          g++ to put out a cross reference to a basetype.  Perhaps
  4282.          it doesn't do it anymore.  */
  4283.       /* Note: for C++, the cross reference may be to a base type which
  4284.          has not yet been seen.  In this case, we skip to the comma,
  4285.          which will mark the end of the base class name.  (The ':'
  4286.          at the end of the base class name will be skipped as well.)
  4287.          But sometimes (ie. when the cross ref is the last thing on
  4288.          the line) there will be no ','.  */
  4289.       from = (char *) index (*pp, ',');
  4290.       if (from)
  4291.         *pp = from;
  4292. #endif /* 0 */
  4293.     }
  4294.  
  4295.     /* Now check to see whether the type has already been declared.  */
  4296.     /* This is necessary at least in the case where the
  4297.        program says something like
  4298.          struct foo bar[5];
  4299.        The compiler puts out a cross-reference; we better find
  4300.        set the length of the structure correctly so we can
  4301.        set the length of the array.  */
  4302.     for (ppt = file_symbols; ppt; ppt = ppt->next)
  4303.       for (i = 0; i < ppt->nsyms; i++)
  4304.         {
  4305.           struct symbol *sym = ppt->symbol[i];
  4306.  
  4307.           if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  4308.           && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
  4309.           && (TYPE_CODE (SYMBOL_TYPE (sym)) == code)
  4310.           && !strcmp (SYMBOL_NAME (sym), type_name_only))
  4311.         {
  4312.           obstack_free (symbol_obstack, type_name);
  4313.           type = SYMBOL_TYPE (sym);
  4314.           return type;
  4315.         }
  4316.         }
  4317.     
  4318.     /* Didn't find the type to which this refers, so we must
  4319.        be dealing with a forward reference.  Allocate a type
  4320.        structure for it, and keep track of it so we can
  4321.        fill in the rest of the fields when we get the full
  4322.        type.  */
  4323.     type = dbx_alloc_type (typenums);
  4324.     TYPE_CODE (type) = code;
  4325.     TYPE_NAME (type) = type_name;
  4326.  
  4327.     TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
  4328.  
  4329.     add_undefined_type (type);
  4330.     return type;
  4331.       }
  4332.  
  4333.     case '0':
  4334.     case '1':
  4335.     case '2':
  4336.     case '3':
  4337.     case '4':
  4338.     case '5':
  4339.     case '6':
  4340.     case '7':
  4341.     case '8':
  4342.     case '9':
  4343.     case '(':
  4344.       (*pp)--;
  4345.       read_type_number (pp, xtypenums);
  4346.       type = *dbx_lookup_type (xtypenums);
  4347.       if (type == 0)
  4348.     type = builtin_type_void;
  4349.       if (typenums[0] != -1)
  4350.     *dbx_lookup_type (typenums) = type;
  4351.       break;
  4352.  
  4353.     case '*':
  4354.       type1 = read_type (pp);
  4355.       if (TYPE_POINTER_TYPE (type1))
  4356.     {
  4357.       type = TYPE_POINTER_TYPE (type1);
  4358.       if (typenums[0] != -1)
  4359.         *dbx_lookup_type (typenums) = type;
  4360.     }
  4361.       else
  4362.     {
  4363.       type = dbx_alloc_type (typenums);
  4364.       smash_to_pointer_type (type, type1);
  4365.     }
  4366.       break;
  4367.  
  4368.     case '@':
  4369.       {
  4370.     struct type *domain = read_type (pp);
  4371.     char c;
  4372.     struct type *memtype;
  4373.  
  4374.     if (*(*pp)++ != ',')
  4375.       error ("invalid member type data format, at symtab pos %d.",
  4376.          symnum);
  4377.  
  4378.     memtype = read_type (pp);
  4379.     type = dbx_alloc_type (typenums);
  4380.     smash_to_member_type (type, domain, memtype);
  4381.       }
  4382.       break;
  4383.  
  4384.     case '#':
  4385.       {
  4386.     struct type *domain = read_type (pp);
  4387.     char c;
  4388.     struct type *return_type;
  4389.     struct type **args;
  4390.  
  4391.     if (*(*pp)++ != ',')
  4392.       error ("invalid member type data format, at symtab pos %d.",
  4393.          symnum);
  4394.  
  4395.     return_type = read_type (pp);
  4396.     args = read_args (pp, ';');
  4397.     type = dbx_alloc_type (typenums);
  4398.     smash_to_method_type (type, domain, return_type, args);
  4399.       }
  4400.       break;
  4401.  
  4402.     case '&':
  4403.       type1 = read_type (pp);
  4404.       if (TYPE_REFERENCE_TYPE (type1))
  4405.     {
  4406.       type = TYPE_REFERENCE_TYPE (type1);
  4407.       if (typenums[0] != -1)
  4408.         *dbx_lookup_type (typenums) = type;
  4409.     }
  4410.       else
  4411.     {
  4412.       type = dbx_alloc_type (typenums);
  4413.       smash_to_reference_type (type, type1);
  4414.     }
  4415.       break;
  4416.  
  4417.     case 'f':
  4418.       type1 = read_type (pp);
  4419.       if (TYPE_FUNCTION_TYPE (type1))
  4420.     {
  4421.       type = TYPE_FUNCTION_TYPE (type1);
  4422.       if (typenums[0] != -1)
  4423.         *dbx_lookup_type (typenums) = type;
  4424.     }
  4425.       else
  4426.     {
  4427.       type = dbx_alloc_type (typenums);
  4428.       smash_to_function_type (type, type1);
  4429.     }
  4430.       break;
  4431.  
  4432.     case 'r':
  4433.       type = read_range_type (pp, typenums);
  4434.       if (typenums[0] != -1)
  4435.     *dbx_lookup_type (typenums) = type;
  4436.       break;
  4437.  
  4438.     case 'e':
  4439.       type = dbx_alloc_type (typenums);
  4440.       type = read_enum_type (pp, type);
  4441.       *dbx_lookup_type (typenums) = type;
  4442.       break;
  4443.  
  4444.     case 's':
  4445.       type = dbx_alloc_type (typenums);
  4446.       type = read_struct_type (pp, type);
  4447.       break;
  4448.  
  4449.     case 'u':
  4450.       type = dbx_alloc_type (typenums);
  4451.       type = read_struct_type (pp, type);
  4452.       TYPE_CODE (type) = TYPE_CODE_UNION;
  4453.       break;
  4454.  
  4455.     case 'a':
  4456.       if (*(*pp)++ != 'r')
  4457.     error ("Invalid symbol data: unrecognized type-code `a%c' %s %d.",
  4458.            (*pp)[-1], "at symtab position", symnum);
  4459.  
  4460.       type = dbx_alloc_type (typenums);
  4461.       type = read_array_type (pp, type);
  4462.       break;
  4463.  
  4464.     default:
  4465.       error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
  4466.          (*pp)[-1], symnum);
  4467.     }
  4468.  
  4469.   if (type == 0)
  4470.     abort ();
  4471.  
  4472. #if 0
  4473.   /* If this is an overriding temporary alteration for a header file's
  4474.      contents, and this type number is unknown in the global definition,
  4475.      put this type into the global definition at this type number.  */
  4476.   if (header_file_prev_index >= 0)
  4477.     {
  4478.       register struct type **tp
  4479.         = explicit_lookup_type (header_file_prev_index, typenums[1]);
  4480.       if (*tp == 0)
  4481.     *tp = type;
  4482.     }
  4483. #endif
  4484.   return type;
  4485. }
  4486.  
  4487. /* This page contains subroutines of read_type.  */
  4488.  
  4489. /* Read the description of a structure (or union type)
  4490.    and return an object describing the type.  */
  4491.  
  4492. static struct type *
  4493. read_struct_type (pp, type)
  4494.      char **pp;
  4495.      register struct type *type;
  4496. {
  4497.   struct nextfield
  4498.     {
  4499.       struct nextfield *next;
  4500.       int visibility;
  4501.       struct field field;
  4502.     };
  4503.  
  4504.   struct next_fnfield
  4505.     {
  4506.       struct next_fnfield *next;
  4507.       int visibility;
  4508.       struct fn_field fn_field;
  4509.     };
  4510.  
  4511.   struct next_fnfieldlist
  4512.     {
  4513.       struct next_fnfieldlist *next;
  4514.       struct fn_fieldlist fn_fieldlist;
  4515.     };
  4516.  
  4517.   register struct nextfield *list = 0;
  4518.   struct nextfield *new;
  4519.   int totalsize;
  4520.   char *name;
  4521.   register char *p;
  4522.   int nfields = 0;
  4523.   register int n;
  4524.  
  4525.   register struct next_fnfieldlist *mainlist = 0;
  4526.   int nfn_fields = 0;
  4527.   int read_possible_virtual_info = 0;
  4528.  
  4529.   if (TYPE_MAIN_VARIANT (type) == 0)
  4530.     {
  4531.       TYPE_MAIN_VARIANT (type) = type;
  4532.     }
  4533.  
  4534.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  4535.  
  4536.   /* First comes the total size in bytes.  */
  4537.  
  4538.   TYPE_LENGTH (type) = read_number (pp, 0);
  4539.  
  4540.   /* C++: Now, if the class is a derived class, then the next character
  4541.      will be a '!', followed by the number of base classes derived from.
  4542.      Each element in the list contains visibility information,
  4543.      the offset of this base class in the derived structure,
  4544.      and then the base type. */
  4545.   if (**pp == '!')
  4546.     {
  4547.       int i, n_baseclasses, offset;
  4548.       struct type **baseclass_vec;
  4549.       struct type *baseclass;
  4550.       int via_public;
  4551.  
  4552.       /* Nonzero if it is a virtual baseclass, i.e.,
  4553.  
  4554.      struct A{};
  4555.      struct B{};
  4556.      struct C : public B, public virtual A {};
  4557.  
  4558.      B is a baseclass of C; A is a virtual baseclass for C.  This is a C++
  4559.      2.0 language feature.  */
  4560.       int via_virtual;
  4561.  
  4562.       *pp += 1;
  4563.  
  4564.       n_baseclasses = read_number (pp, ',');
  4565.       baseclass_vec = (struct type **)
  4566.     obstack_alloc (symbol_obstack,
  4567.                (n_baseclasses) * sizeof (struct type **)) - 1;
  4568.  
  4569.       for (i = 1; i <= n_baseclasses; i++)
  4570.     {
  4571.       if (**pp == '\\')
  4572.         *pp = next_symbol_text ();
  4573.  
  4574.       switch (*(*pp)++)
  4575.         {
  4576.         case '0':
  4577.           via_virtual = 0;
  4578.           break;
  4579.         case '1':
  4580.           via_virtual = 1;
  4581.           break;
  4582.         default:
  4583.           error ("Invalid symbol data: bad visibility format at symtab pos %d",
  4584.              symnum);
  4585.         }
  4586.  
  4587.       switch (*(*pp)++)
  4588.         {
  4589.         case '0':
  4590.           via_public = 0;
  4591.           break;
  4592.         case '2':
  4593.           via_public = 1;
  4594.           break;
  4595.         default:
  4596.           error ("Invalid symbol data: bad visibility format at symtab pos %d.",
  4597.              symnum);
  4598.         }
  4599.  
  4600.       /* Offset of the portion of the object corresponding to
  4601.          this baseclass.  Always zero in the absence of
  4602.          multiple inheritance.  */
  4603.       offset = read_number (pp, ',');
  4604.       baseclass = read_type (pp);
  4605.       *pp += 1;        /* skip trailing ';' */
  4606.  
  4607.       if (offset != 0)
  4608.         {
  4609.           static int error_printed = 0;
  4610.  
  4611.           if (!error_printed)
  4612.         {
  4613.           fprintf (stderr, 
  4614. "\nWarning:  GDB has limited understanding of multiple inheritance...");
  4615.           error_printed = 1;
  4616.         }
  4617.           offset = 0;
  4618.         }
  4619.  
  4620.       baseclass_vec[i] = lookup_basetype_type (baseclass, offset, via_virtual, via_public);
  4621.  
  4622.           /* Since lookup_basetype_type can copy the type,
  4623.          it might copy a stub type (complete with stub flag).
  4624.          If so, we need to add it to the list of undefined types
  4625.          to clean up later.  Even if lookup_basetype_type
  4626.          didn't copy the type, adding it to the undefined list
  4627.          will not do any harm.  */
  4628.           if (TYPE_FLAGS(baseclass_vec[i]) & TYPE_FLAG_STUB)
  4629.             add_undefined_type (baseclass_vec[i]);
  4630.  
  4631.       /* Make this baseclass visible for structure-printing purposes.  */
  4632.       new = (struct nextfield *) alloca (sizeof (struct nextfield));
  4633.       new->next = list;
  4634.       list = new;
  4635.       list->field.type = baseclass_vec[i];
  4636.       list->field.name = TYPE_NAME (baseclass_vec[i]);
  4637.       list->field.bitpos = offset;
  4638.       list->field.bitsize = 0;    /* this should be an unpacked field! */
  4639.       nfields++;
  4640.     }
  4641.       TYPE_N_BASECLASSES (type) = n_baseclasses;
  4642.       TYPE_BASECLASSES (type) = baseclass_vec;
  4643.     }
  4644.  
  4645.   /* Now come the fields, as NAME:?TYPENUM,BITPOS,BITSIZE; for each one.
  4646.      At the end, we see a semicolon instead of a field.
  4647.  
  4648.      In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
  4649.      a static field.
  4650.  
  4651.      The `?' is a placeholder for one of '+' (public visibility),
  4652.      '0' (protected visibility), and '-' (private visibility).  */
  4653.  
  4654.   /* We better set p right now, in case there are no fields at all...    */
  4655.   p = *pp;
  4656.  
  4657.   while (**pp != ';')
  4658.     {
  4659.       int visibility;
  4660.  
  4661.       /* Check for and handle cretinous dbx symbol name continuation!  */
  4662.       if (**pp == '\\') *pp = next_symbol_text ();
  4663.  
  4664.       /* Get space to record the next field's data.  */
  4665.       new = (struct nextfield *) alloca (sizeof (struct nextfield));
  4666.       new->next = list;
  4667.       list = new;
  4668.  
  4669.       /* Get the field name.  */
  4670.       p = *pp;
  4671.       while (*p != ':') p++;
  4672.       list->field.name = obsavestring (*pp, p - *pp);
  4673.  
  4674.       /* C++: Check to see if we have hit the methods yet.  */
  4675.       if (p[1] == ':')
  4676.     break;
  4677.  
  4678.       *pp = p + 1;
  4679.  
  4680.       /* This means we have a visibility for a field coming. */
  4681.       if (**pp == '/')
  4682.     {
  4683.       switch (*++*pp)
  4684.         {
  4685.         case '0':
  4686.           visibility = 0;
  4687.           *pp += 1;
  4688.           break;
  4689.  
  4690.          case '1':
  4691.            visibility = 1;
  4692.            *pp += 1;
  4693.            break;
  4694.  
  4695.          case '2':
  4696.            visibility = 2;
  4697.            *pp += 1;
  4698.            break;
  4699.          }
  4700.      }
  4701.        /* else normal dbx-style format.  */
  4702.  
  4703.       list->field.type = read_type (pp);
  4704.       if (**pp == ':')
  4705.      {
  4706.        list->field.bitpos = (long)-1;
  4707.        p = ++(*pp);
  4708.        while (*p != ';') p++;
  4709.        list->field.bitsize = (long) savestring (*pp, p - *pp);
  4710.        *pp = p + 1;
  4711.        nfields++;
  4712.        continue;
  4713.      }
  4714.        else if (**pp != ',')
  4715.      error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
  4716.            symnum);
  4717.       (*pp)++;            /* Skip the comma.  */
  4718.       list->field.bitpos = read_number (pp, ',');
  4719.       list->field.bitsize = read_number (pp, ';');
  4720.  
  4721. #if 0
  4722.       /* This is wrong because this is identical to the symbols
  4723.      produced for GCC 0-size arrays.  For example:
  4724.          typedef union {
  4725.        int num;
  4726.        char str[0];
  4727.      } foo;
  4728.      The code which dumped core in such circumstances should be
  4729.      fixed not to dump core.  */
  4730.  
  4731.       /* g++ -g0 can put out bitpos & bitsize zero for a static
  4732.      field.  This does not give us any way of getting its
  4733.      class, so we can't know its name.  But we can just
  4734.      ignore the field so we don't dump core and other nasty
  4735.      stuff.  */
  4736.       if (list->field.bitpos == 0
  4737.       && list->field.bitsize == 0)
  4738.     {
  4739.       /* Have we given the warning yet?  */
  4740.       static int warning_given = 0;
  4741.  
  4742.       /* Only give the warning once, no matter how many class
  4743.          variables there are.  */
  4744.       if (!warning_given)
  4745.         {
  4746.           warning_given = 1;
  4747.           fprintf_filtered (stderr, "\n\
  4748. Warning:  DBX-style class variable debugging information encountered.\n\
  4749. You seem to have compiled your program with \
  4750. \"g++ -g0\" instead of \"g++ -g\".\n\
  4751. Therefore GDB will not know about your class variables.\n\
  4752. ");
  4753.         }
  4754.  
  4755.       /* Ignore this field.  */
  4756.       list = list->next;
  4757.     }
  4758.       else
  4759. #endif /* 0 */
  4760.     {
  4761.       /* Detect an unpacked field and mark it as such.
  4762.          dbx gives a bit size for all fields.
  4763.          Note that forward refs cannot be packed,
  4764.          and treat enums as if they had the width of ints.  */
  4765.       if (TYPE_CODE (list->field.type) != TYPE_CODE_INT
  4766.           && TYPE_CODE (list->field.type) != TYPE_CODE_ENUM)
  4767.         list->field.bitsize = 0;
  4768.       if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
  4769.            || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
  4770.            && (list->field.bitsize
  4771.                == 8 * TYPE_LENGTH (builtin_type_int))
  4772.            )
  4773.            )
  4774.           &&
  4775.           list->field.bitpos % 8 == 0)
  4776.         list->field.bitsize = 0;
  4777.       nfields++;
  4778.     }
  4779.     }
  4780.  
  4781.   /* Now come the method fields, as NAME::methods
  4782.      where each method is of the form TYPENUM,ARGS,...:PHYSNAME;
  4783.      At the end, we see a semicolon instead of a field.
  4784.  
  4785.      For the case of overloaded operators, the format is
  4786.      OPERATOR::*.methods, where OPERATOR is the string "operator",
  4787.      `*' holds the place for an operator name (such as `+=')
  4788.      and `.' marks the end of the operator name.  */
  4789.   if (p[1] == ':')
  4790.     {
  4791.       /* Now, read in the methods.  To simplify matters, we
  4792.      "unread" the name that has been read, so that we can
  4793.      start from the top.  */
  4794.  
  4795.       p = *pp;
  4796.  
  4797.       /* chill the list of fields: the last entry (at the head)
  4798.          is a partially constructed entry which we now scrub.  */
  4799.       list = list->next;
  4800.  
  4801.       /* For each list of method lists... */
  4802.       do
  4803.     {
  4804.       int i;
  4805.       struct next_fnfield *sublist = 0;
  4806.       struct fn_field *fn_fields = 0;
  4807.       int length = 0;
  4808.       struct next_fnfieldlist *new_mainlist =
  4809.         (struct next_fnfieldlist *)alloca (sizeof (struct next_fnfieldlist));
  4810.  
  4811.       /* read in the name.  */
  4812.       while (*p != ':') p++;
  4813.       if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && (*pp)[2] == '$')
  4814.         {
  4815.           static char opname[32] = "operator ";
  4816.           char *o = opname + 9;
  4817.  
  4818.           /* Skip past '::'.  */
  4819.           p += 2;
  4820.           while (*p != '.')
  4821.         *o++ = *p++;
  4822.           new_mainlist->fn_fieldlist.name = savestring (opname, o - opname);
  4823.           /* Skip past '.'  */
  4824.           *pp = p + 1;
  4825.         }
  4826.       else
  4827.         {
  4828.           i = 0;
  4829.           new_mainlist->fn_fieldlist.name = savestring (*pp, p - *pp);
  4830.           /* Skip past '::'.  */
  4831.           *pp = p + 2;
  4832.         }
  4833.  
  4834.       do
  4835.         {
  4836.           struct next_fnfield *new_sublist =
  4837.         (struct next_fnfield *)alloca (sizeof (struct next_fnfield));
  4838.  
  4839.           /* Check for and handle cretinous dbx symbol name continuation!  */
  4840.           if (**pp == '\\') *pp = next_symbol_text ();
  4841.  
  4842.           new_sublist->fn_field.type = read_type (pp);
  4843.           if (**pp != ':')
  4844.         error ("invalid symtab info for method at symbol number %d.",
  4845.                symnum);
  4846.           *pp += 1;
  4847.           new_sublist->fn_field.args =
  4848.         TYPE_ARG_TYPES (new_sublist->fn_field.type);
  4849.           p = *pp;
  4850.           while (*p != ';') p++;
  4851.           new_sublist->fn_field.physname = savestring (*pp, p - *pp);
  4852.           *pp = p + 1;
  4853.           new_sublist->visibility = *(*pp)++ - '0';
  4854.           if (**pp == '\\') *pp = next_symbol_text ();
  4855.  
  4856.           switch (*(*pp)++)
  4857.         {
  4858.         case '*':
  4859.           /* virtual member function, followed by index.  */
  4860.           new_sublist->fn_field.voffset = read_number (pp, ';') + 1;
  4861.           break;
  4862.         case '?':
  4863.           /* static member function.  */
  4864.           new_sublist->fn_field.voffset = 1;
  4865.           break;
  4866.         default:
  4867.           /* **pp == '.'.  */
  4868.           /* normal member function.  */
  4869.           new_sublist->fn_field.voffset = 0;
  4870.           break;
  4871.         }
  4872.  
  4873.           new_sublist->next = sublist;
  4874.           sublist = new_sublist;
  4875.           length++;
  4876.         }
  4877.       while (**pp != ';');
  4878.  
  4879.       *pp += 1;
  4880.  
  4881.       new_mainlist->fn_fieldlist.fn_fields =
  4882.         (struct fn_field *) obstack_alloc (symbol_obstack,
  4883.                            sizeof (struct fn_field) * length);
  4884.       TYPE_FN_PRIVATE_BITS (new_mainlist->fn_fieldlist) =
  4885.         (int *) obstack_alloc (symbol_obstack,
  4886.                    sizeof (int) * (1 + (length >> 5)));
  4887.  
  4888.       TYPE_FN_PROTECTED_BITS (new_mainlist->fn_fieldlist) =
  4889.         (int *) obstack_alloc (symbol_obstack,
  4890.                    sizeof (int) * (1 + (length >> 5)));
  4891.  
  4892.       for (i = length; sublist; sublist = sublist->next)
  4893.         {
  4894.           new_mainlist->fn_fieldlist.fn_fields[--i] = sublist->fn_field;
  4895.           if (sublist->visibility == 0)
  4896.         B_SET (new_mainlist->fn_fieldlist.private_fn_field_bits, i);
  4897.           else if (sublist->visibility == 1)
  4898.         B_SET (new_mainlist->fn_fieldlist.protected_fn_field_bits, i);
  4899.         }
  4900.  
  4901.       new_mainlist->fn_fieldlist.length = length;
  4902.       new_mainlist->next = mainlist;
  4903.       mainlist = new_mainlist;
  4904.       nfn_fields++;
  4905.     }
  4906.       while (**pp != ';');
  4907.     }
  4908.  
  4909.   *pp += 1;
  4910.  
  4911.   /* Now create the vector of fields, and record how big it is.  */
  4912.  
  4913.   TYPE_NFIELDS (type) = nfields;
  4914.   TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
  4915.                                sizeof (struct field) * nfields);
  4916.   TYPE_FIELD_PRIVATE_BITS (type) =
  4917.     (int *) obstack_alloc (symbol_obstack,
  4918.                sizeof (int) * (1 + (nfields >> 5)));
  4919.   TYPE_FIELD_PROTECTED_BITS (type) =
  4920.     (int *) obstack_alloc (symbol_obstack,
  4921.                sizeof (int) * (1 + (nfields >> 5)));
  4922.  
  4923.   TYPE_NFN_FIELDS (type) = nfn_fields;
  4924.   TYPE_NFN_FIELDS_TOTAL (type) = nfn_fields;
  4925.  
  4926.   {
  4927.     int i;
  4928.     for (i = 1; i <= TYPE_N_BASECLASSES (type); ++i)
  4929.       TYPE_NFN_FIELDS_TOTAL (type) +=
  4930.     TYPE_NFN_FIELDS_TOTAL (TYPE_BASECLASS (type, i));
  4931.   }
  4932.  
  4933.   TYPE_FN_FIELDLISTS (type) =
  4934.     (struct fn_fieldlist *) obstack_alloc (symbol_obstack,
  4935.                        sizeof (struct fn_fieldlist) * nfn_fields);
  4936.  
  4937.   /* Copy the saved-up fields into the field vector.  */
  4938.  
  4939.   for (n = nfields; list; list = list->next)
  4940.     {
  4941.       TYPE_FIELD (type, --n) = list->field;
  4942.       if (list->visibility == 0)
  4943.     SET_TYPE_FIELD_PRIVATE (type, n);
  4944.       else if (list->visibility == 1)
  4945.     SET_TYPE_FIELD_PROTECTED (type, n);
  4946.     }
  4947.  
  4948.   for (n = nfn_fields; mainlist; mainlist = mainlist->next)
  4949.     TYPE_FN_FIELDLISTS (type)[--n] = mainlist->fn_fieldlist;
  4950.  
  4951.   if (**pp == '~')
  4952.     {
  4953.       *pp += 1;
  4954.  
  4955.       if (**pp == '=')
  4956.     {
  4957.       TYPE_FLAGS (type)
  4958.         |= TYPE_FLAG_HAS_CONSTRUCTOR | TYPE_FLAG_HAS_DESTRUCTOR;
  4959.       *pp += 1;
  4960.     }
  4961.       else if (**pp == '+')
  4962.     {
  4963.       TYPE_FLAGS (type) |= TYPE_FLAG_HAS_CONSTRUCTOR;
  4964.       *pp += 1;
  4965.     }
  4966.       else if (**pp == '-')
  4967.     {
  4968.       TYPE_FLAGS (type) |= TYPE_FLAG_HAS_DESTRUCTOR;
  4969.       *pp += 1;
  4970.     }
  4971.  
  4972.       /* Read either a '%' or the final ';'.  */
  4973.       if (*(*pp)++ == '%')
  4974.     {
  4975.       /* Now we must record the virtual function table pointer's
  4976.          field information.  */
  4977.  
  4978.       struct type *t;
  4979.       int i;
  4980.  
  4981.       t = read_type (pp);
  4982.       p = (*pp)++;
  4983.       while (*p != ';') p++;
  4984.       TYPE_VPTR_BASETYPE (type) = t;
  4985.       if (type == t)
  4986.         {
  4987.           if (TYPE_FIELD_NAME (t, 0) == 0)
  4988.         TYPE_VPTR_FIELDNO (type) = i = 0;
  4989.           else for (i = TYPE_NFIELDS (t) - 1; i >= 0; --i)
  4990.         if (! strncmp (TYPE_FIELD_NAME (t, i), *pp,
  4991.                    strlen (TYPE_FIELD_NAME (t, i))))
  4992.           {
  4993.             TYPE_VPTR_FIELDNO (type) = i;
  4994.             break;
  4995.           }
  4996.           if (i < 0)
  4997.         error ("virtual function table field not found");
  4998.         }
  4999.       else
  5000.         TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, 1));
  5001.       *pp = p + 1;
  5002.     }
  5003.       else
  5004.     {
  5005.       TYPE_VPTR_BASETYPE (type) = 0;
  5006.       TYPE_VPTR_FIELDNO (type) = -1;
  5007.     }
  5008.     }
  5009.   else
  5010.     {
  5011.       TYPE_VPTR_BASETYPE (type) = 0;
  5012.       TYPE_VPTR_FIELDNO (type) = -1;
  5013.     }
  5014.  
  5015.   return type;
  5016. }
  5017.  
  5018. /* Read a definition of an array type,
  5019.    and create and return a suitable type object.
  5020.    Also creates a range type which represents the bounds of that
  5021.    array.  */
  5022. static struct type *
  5023. read_array_type (pp, type)
  5024.      register char **pp;
  5025.      register struct type *type;
  5026. {
  5027.   struct type *index_type, *element_type, *range_type;
  5028.   int lower, upper;
  5029.   int adjustable = 0;
  5030.  
  5031.   /* Format of an array type:
  5032.      "ar<index type>;lower;upper;<array_contents_type>".  Put code in
  5033.      to handle this.
  5034.  
  5035.      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
  5036.      for these, produce a type like float[][].  */
  5037.  
  5038.   index_type = read_type (pp);
  5039.   if (*(*pp)++ != ';')
  5040.     error ("Invalid symbol data; improper format of array type decl.");
  5041.  
  5042.   if (!(**pp >= '0' && **pp <= '9'))
  5043.     {
  5044.       *pp += 1;
  5045.       adjustable = 1;
  5046.     }
  5047.   lower = read_number (pp, ';');
  5048.  
  5049.   if (!(**pp >= '0' && **pp <= '9'))
  5050.     {
  5051.       *pp += 1;
  5052.       adjustable = 1;
  5053.     }
  5054.   upper = read_number (pp, ';');
  5055.   
  5056.   element_type = read_type (pp);
  5057.  
  5058.   if (adjustable)
  5059.     {
  5060.       lower = 0;
  5061.       upper = -1;
  5062.     }
  5063.  
  5064.   {
  5065.     /* Create range type.  */
  5066.     range_type = (struct type *) obstack_alloc (symbol_obstack,
  5067.                         sizeof (struct type));
  5068.     TYPE_CODE (range_type) = TYPE_CODE_RANGE;
  5069.     TYPE_TARGET_TYPE (range_type) = index_type;
  5070.  
  5071.     /* This should never be needed.  */
  5072.     TYPE_LENGTH (range_type) = sizeof (int);
  5073.  
  5074.     TYPE_NFIELDS (range_type) = 2;
  5075.     TYPE_FIELDS (range_type) =
  5076.       (struct field *) obstack_alloc (symbol_obstack,
  5077.                       2 * sizeof (struct field));
  5078.     TYPE_FIELD_BITPOS (range_type, 0) = lower;
  5079.     TYPE_FIELD_BITPOS (range_type, 1) = upper;
  5080.   }
  5081.  
  5082.   TYPE_CODE (type) = TYPE_CODE_ARRAY;
  5083.   TYPE_TARGET_TYPE (type) = element_type;
  5084.   TYPE_LENGTH (type) = (upper - lower + 1) * TYPE_LENGTH (element_type);
  5085.   TYPE_NFIELDS (type) = 1;
  5086.   TYPE_FIELDS (type) =
  5087.     (struct field *) obstack_alloc (symbol_obstack,
  5088.                     sizeof (struct field));
  5089.   TYPE_FIELD_TYPE (type, 0) = range_type;
  5090.  
  5091.   return type;
  5092. }
  5093.  
  5094.  
  5095. /* Read a definition of an enumeration type,
  5096.    and create and return a suitable type object.
  5097.    Also defines the symbols that represent the values of the type.  */
  5098.  
  5099. static struct type *
  5100. read_enum_type (pp, type)
  5101.      register char **pp;
  5102.      register struct type *type;
  5103. {
  5104.   register char *p;
  5105.   char *name;
  5106.   register long n;
  5107.   register struct symbol *sym;
  5108.   int nsyms = 0;
  5109.   struct pending **symlist;
  5110.   struct pending *osyms, *syms;
  5111.   int o_nsyms;
  5112.  
  5113.   if (within_function)
  5114.     symlist = &local_symbols;
  5115.   else
  5116.     symlist = &file_symbols;
  5117.   osyms = *symlist;
  5118.   o_nsyms = osyms ? osyms->nsyms : 0;
  5119.  
  5120.   /* Read the value-names and their values.
  5121.      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
  5122.      A semicolon or comman instead of a NAME means the end.  */
  5123.   while (**pp && **pp != ';' && **pp != ',')
  5124.     {
  5125.       /* Check for and handle cretinous dbx symbol name continuation!  */
  5126.       if (**pp == '\\')    *pp = next_symbol_text ();
  5127.  
  5128.       p = *pp;
  5129.       while (*p != ':') p++;
  5130.       name = obsavestring (*pp, p - *pp);
  5131.       *pp = p + 1;
  5132.       n = read_number (pp, ',');
  5133.  
  5134.       sym = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
  5135.       bzero (sym, sizeof (struct symbol));
  5136.       SYMBOL_NAME (sym) = name;
  5137.       SYMBOL_CLASS (sym) = LOC_CONST;
  5138.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  5139.       SYMBOL_VALUE (sym) = n;
  5140.       add_symbol_to_list (sym, symlist);
  5141.       nsyms++;
  5142.     }
  5143.  
  5144.   if (**pp == ';')
  5145.     (*pp)++;            /* Skip the semicolon.  */
  5146.  
  5147.   /* Now fill in the fields of the type-structure.  */
  5148.  
  5149.   TYPE_LENGTH (type) = sizeof (int);
  5150.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  5151.   TYPE_NFIELDS (type) = nsyms;
  5152.   TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
  5153.  
  5154.   /* Find the symbols for the values and put them into the type.
  5155.      The symbols can be found in the symlist that we put them on
  5156.      to cause them to be defined.  osyms contains the old value
  5157.      of that symlist; everything up to there was defined by us.  */
  5158.   /* Note that we preserve the order of the enum constants, so
  5159.      that in something like "enum {FOO, LAST_THING=FOO}" we print
  5160.      FOO, not LAST_THING.  */
  5161.  
  5162.   for (syms = *symlist, n = 0; syms; syms = syms->next)
  5163.     {
  5164.       int j = 0;
  5165.       if (syms == osyms)
  5166.     j = o_nsyms;
  5167.       for (; j < syms->nsyms; j++)
  5168.     {
  5169.       struct symbol *sym = syms->symbol[j];
  5170.       SYMBOL_TYPE (sym) = type;
  5171.       TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (sym);
  5172.       TYPE_FIELD_VALUE (type, n) = 0;
  5173.       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (sym);
  5174.       TYPE_FIELD_BITSIZE (type, n++) = 0;
  5175.     }
  5176.       if (syms == osyms)
  5177.     break;
  5178.     }
  5179.  
  5180.   return type;
  5181. }
  5182.  
  5183. #define    MAX_OF_TYPE(t)    ((1 << (sizeof (t) - 1)) - 1)
  5184. #define MIN_OF_TYPE(t)    (-(1 << (sizeof (t) - 1)))
  5185.  
  5186. static struct type *
  5187. read_range_type (pp, typenums)
  5188.      char **pp;
  5189.      int typenums[2];
  5190. {
  5191.   char *errp = *pp;
  5192.   int rangenums[2];
  5193.   int n2, n3;
  5194.   int self_subrange;
  5195.   struct type *result_type;
  5196.  
  5197.   /* First comes a type we are a subrange of.
  5198.      In C it is usually 0, 1 or the type being defined.  */
  5199.   read_type_number (pp, rangenums);
  5200.   self_subrange = (rangenums[0] == typenums[0] &&
  5201.            rangenums[1] == typenums[1]);
  5202.  
  5203.   /* A semicolon should now follow; skip it.  */
  5204.   if (**pp == ';')
  5205.     (*pp)++;
  5206.  
  5207.   /* The remaining two operands are usually lower and upper bounds
  5208.      of the range.  But in some special cases they mean something else.  */
  5209.   n2 = read_number (pp, ';');
  5210.   n3 = read_number (pp, ';');
  5211.  
  5212.   /* A type defined as a subrange of itself, with bounds both 0, is void.  */
  5213.   if (self_subrange && n2 == 0 && n3 == 0)
  5214.     return builtin_type_void;
  5215.  
  5216.   /* If n3 is zero and n2 is not, we want a floating type,
  5217.      and n2 is the width in bytes.
  5218.  
  5219.      Fortran programs appear to use this for complex types also,
  5220.      and they give no way to distinguish between double and single-complex!
  5221.      We don't have complex types, so we would lose on all fortran files!
  5222.      So return type `double' for all of those.  It won't work right
  5223.      for the complex values, but at least it makes the file loadable.  */
  5224.  
  5225.   if (n3 == 0 && n2 > 0)
  5226.     {
  5227.       if (n2 == sizeof (float))
  5228.     return builtin_type_float;
  5229.       return builtin_type_double;
  5230.     }
  5231.  
  5232.   /* If the upper bound is -1, it must really be an unsigned int.  */
  5233.  
  5234.   else if (n2 == 0 && n3 == -1)
  5235.     {
  5236.       if (sizeof (int) == sizeof (long))
  5237.     return builtin_type_unsigned_int;
  5238.       else
  5239.     return builtin_type_unsigned_long;
  5240.     }
  5241.  
  5242.   /* Special case: char is defined (Who knows why) as a subrange of
  5243.      itself with range 0-127.  */
  5244.   else if (self_subrange && n2 == 0 && n3 == 127)
  5245.     return builtin_type_char;
  5246.  
  5247.   /* Assumptions made here: Subrange of self is equivalent to subrange
  5248.      of int.  */
  5249.   else if (n2 == 0
  5250.        && (self_subrange ||
  5251.            *dbx_lookup_type (rangenums) == builtin_type_int))
  5252.     {
  5253.       /* an unsigned type */
  5254. #ifdef LONG_LONG
  5255.       if (n3 == - sizeof (long long))
  5256.     return builtin_type_unsigned_long_long;
  5257. #endif
  5258.       if (n3 == (1 << (8 * sizeof (int))) - 1)
  5259.     return builtin_type_unsigned_int;
  5260.       if (n3 == (1 << (8 * sizeof (short))) - 1)
  5261.     return builtin_type_unsigned_short;
  5262.       if (n3 == (1 << (8 * sizeof (char))) - 1)
  5263.     return builtin_type_unsigned_char;
  5264.     }
  5265. #ifdef LONG_LONG
  5266.   else if (n3 == 0 && n2 == -sizeof (long long))
  5267.     return builtin_type_long_long;
  5268. #endif  
  5269.   else if (n2 == -n3 -1)
  5270.     {
  5271.       /* a signed type */
  5272.       if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
  5273.     return builtin_type_int;
  5274.       if (n3 == (1 << (8 * sizeof (long) - 1)) - 1)
  5275.      return builtin_type_long;
  5276.       if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
  5277.     return builtin_type_short;
  5278.       if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
  5279.     return builtin_type_char;
  5280.     }
  5281.  
  5282.   /* We have a real range type on our hands.  Allocate space and
  5283.      return a real pointer.  */
  5284.  
  5285.   /* At this point I don't have the faintest idea how to deal with
  5286.      a self_subrange type; I'm going to assume that this is used
  5287.      as an idiom, and that all of them are special cases.  So . . .  */
  5288.   if (self_subrange)
  5289.     error ("Type defined as subrange of itself.");
  5290.  
  5291.   result_type = (struct type *) obstack_alloc (symbol_obstack,
  5292.                            sizeof (struct type));
  5293.   bzero (result_type, sizeof (struct type));
  5294.  
  5295.   TYPE_TARGET_TYPE (result_type) = (self_subrange ?
  5296.                     builtin_type_int :
  5297.                     *dbx_lookup_type(rangenums));
  5298.  
  5299.   /* We have to figure out how many bytes it takes to hold this
  5300.      range type.  I'm going to assume that anything that is pushing
  5301.      the bounds of a long was taken care of above.  */
  5302.   if (n2 >= MIN_OF_TYPE(char) && n3 <= MAX_OF_TYPE(char))
  5303.     TYPE_LENGTH (result_type) = 1;
  5304.   else if (n2 >= MIN_OF_TYPE(short) && n3 <= MAX_OF_TYPE(short))
  5305.     TYPE_LENGTH (result_type) = sizeof (short);
  5306.   else if (n2 >= MIN_OF_TYPE(int) && n3 <= MAX_OF_TYPE(int))
  5307.     TYPE_LENGTH (result_type) = sizeof (int);
  5308.   else if (n2 >= MIN_OF_TYPE(long) && n3 <= MAX_OF_TYPE(long))
  5309.     TYPE_LENGTH (result_type) = sizeof (long);
  5310.   else
  5311.     error ("Ranged type doesn't fit within known sizes.");
  5312.  
  5313.   TYPE_LENGTH (result_type) = TYPE_LENGTH (TYPE_TARGET_TYPE (result_type));
  5314.   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
  5315.   TYPE_NFIELDS (result_type) = 2;
  5316.   TYPE_FIELDS (result_type) =
  5317.     (struct field *) obstack_alloc (symbol_obstack,
  5318.                     2 * sizeof (struct field));
  5319.   bzero (TYPE_FIELDS (result_type), 2 * sizeof (struct field));
  5320.   TYPE_FIELD_BITPOS (result_type, 0) = n2;
  5321.   TYPE_FIELD_BITPOS (result_type, 1) = n3;
  5322.  
  5323.   return result_type;
  5324. }
  5325.  
  5326. /* Read a number from the string pointed to by *PP.
  5327.    The value of *PP is advanced over the number.
  5328.    If END is nonzero, the character that ends the
  5329.    number must match END, or an error happens;
  5330.    and that character is skipped if it does match.
  5331.    If END is zero, *PP is left pointing to that character.  */
  5332.  
  5333. static long
  5334. read_number (pp, end)
  5335.      char **pp;
  5336.      int end;
  5337. {
  5338.   register char *p = *pp;
  5339.   register long n = 0;
  5340.   register int c;
  5341.   int sign = 1;
  5342.  
  5343.   /* Handle an optional leading minus sign.  */
  5344.  
  5345.   if (*p == '-')
  5346.     {
  5347.       sign = -1;
  5348.       p++;
  5349.     }
  5350.  
  5351.   /* Read the digits, as far as they go.  */
  5352.  
  5353.   while ((c = *p++) >= '0' && c <= '9')
  5354.     {
  5355.       n *= 10;
  5356.       n += c - '0';
  5357.     }
  5358.   if (end)
  5359.     {
  5360.       if (c && c != end)
  5361.     error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
  5362.     }
  5363.   else
  5364.     --p;
  5365.  
  5366.   *pp = p;
  5367.   return n * sign;
  5368. }
  5369.  
  5370. /* Read in an argument list. This is a list of types. It is terminated with
  5371.    a ':', FYI. Return the list of types read in. */
  5372. static struct type **
  5373. read_args (pp, end)
  5374.      char **pp;
  5375.      int end;
  5376. {
  5377.   struct type *types[1024], **rval; /* allow for fns of 1023 parameters */
  5378.   int n = 0;
  5379.  
  5380.   while (**pp != end)
  5381.     {
  5382.       if (**pp != ',')
  5383.     error ("Invalid argument list: no ',', at symtab pos %d", symnum);
  5384.       *pp += 1;
  5385.  
  5386.       /* Check for and handle cretinous dbx symbol name continuation! */
  5387.       if (**pp == '\\')
  5388.     *pp = next_symbol_text ();
  5389.  
  5390.       types[n++] = read_type (pp);
  5391.     }
  5392.   *pp += 1;            /* get past `end' (the ':' character) */
  5393.  
  5394.   if (n == 1)
  5395.     {
  5396.       rval = (struct type **) xmalloc (2 * sizeof (struct type *));
  5397.     }
  5398.   else if (TYPE_CODE (types[n-1]) != TYPE_CODE_VOID)
  5399.     {
  5400.       rval = (struct type **) xmalloc ((n + 1) * sizeof (struct type *));
  5401.       bzero (rval + n, sizeof (struct type *));
  5402.     }
  5403.   else
  5404.     {
  5405.       rval = (struct type **) xmalloc (n * sizeof (struct type *));
  5406.     }
  5407.   bcopy (types, rval, n * sizeof (struct type *));
  5408.   return rval;
  5409. }
  5410.  
  5411. /* This function is really horrible, but to avoid it, there would need
  5412.    to be more filling in of forward references.  THIS SHOULD BE MOVED OUT
  5413.    OF COFFREAD.C AND DBXREAD.C TO SOME PLACE WHERE IT CAN BE SHARED */
  5414. int
  5415. fill_in_vptr_fieldno (type)
  5416.      struct type *type;
  5417. {
  5418.   if (TYPE_VPTR_FIELDNO (type) < 0)
  5419.     TYPE_VPTR_FIELDNO (type) =
  5420.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
  5421.   return TYPE_VPTR_FIELDNO (type);
  5422. }
  5423.  
  5424. /* Copy a pending list, used to record the contents of a common
  5425.    block for later fixup.  */
  5426. static struct pending *
  5427. copy_pending (beg, begi, end)
  5428.     struct pending *beg, *end;
  5429.     int begi;
  5430. {
  5431.   struct pending *new = 0;
  5432.   struct pending *next;
  5433.  
  5434.   for (next = beg; next != 0 && (next != end || begi < end->nsyms);
  5435.        next = next->next, begi = 0)
  5436.     {
  5437.       register int j;
  5438.       for (j = begi; j < next->nsyms; j++)
  5439.     add_symbol_to_list (next->symbol[j], &new);
  5440.     }
  5441.   return new;
  5442. }
  5443.  
  5444. /* Add a common block's start address to the offset of each symbol
  5445.    declared to be in it (by being between a BCOMM/ECOMM pair that uses
  5446.    the common block name).  */
  5447.  
  5448. static void
  5449. fix_common_block (sym, value)
  5450.     struct symbol *sym;
  5451.     int value;
  5452. {
  5453.   struct pending *next = (struct pending *) SYMBOL_NAMESPACE (sym);
  5454.   for ( ; next; next = next->next)
  5455.     {
  5456.       register int j;
  5457.       for (j = next->nsyms - 1; j >= 0; j--)
  5458.     SYMBOL_VALUE (next->symbol[j]) += value;
  5459.     }
  5460. }
  5461.  
  5462. void
  5463. _initialize_dbxread ()
  5464. {
  5465.   symfile = 0;
  5466.   header_files = (struct header_file *) 0;
  5467.   this_object_header_files = (int *) 0;
  5468.  
  5469.   undef_types_allocated = 20;
  5470.   undef_types_length = 0;
  5471.   undef_types = (struct type **) xmalloc (undef_types_allocated *
  5472.                       sizeof (struct type *));
  5473.  
  5474.   add_com ("symbol-file", class_files, symbol_file_command,
  5475.        "Load symbol table (in dbx format) from executable file FILE.");
  5476.  
  5477.   add_com ("add-file", class_files, add_file_command,
  5478.            "Load the symbols from FILE, assuming its code is at TEXT_START.") ;
  5479. }
  5480.  
  5481. #endif /* READ_DBX_FORMAT */
  5482.