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