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