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