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