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