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