gdb-2.5.2
[external/binutils.git] / gdb / dbxread.c
1 /* Read dbx symbol tables and convert to internal format, for GDB.
2    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@mcc.com)
4
5 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
6 WARRANTY.  No author or distributor accepts responsibility to anyone
7 for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing.
9 Refer to the GDB General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GDB,
12 but only under the conditions described in the GDB General Public
13 License.  A copy of this license is supposed to have been given to you
14 along with GDB so you can know your rights and responsibilities.  It
15 should be in a file named COPYING.  Among other things, the copyright
16 notice and this notice must be preserved on all copies.
17
18 In other words, go ahead and share GDB, but don't try to stop
19 anyone else from sharing it farther.  Help stamp out software hoarding!
20 */
21 \f
22 #include "param.h"
23
24 #ifdef READ_DBX_FORMAT
25
26 #include <a.out.h>
27 #include <stab.h>
28 #include <stdio.h>
29 #include <obstack.h>
30 #include <sys/param.h>
31 #include <sys/file.h>
32 #include "defs.h"
33 #include "initialize.h"
34 #include "symtab.h"
35
36 static void add_symbol_to_list ();
37 static void read_dbx_symtab ();
38 static void process_one_symbol ();
39 static struct type *read_type ();
40 static struct type *read_range_type ();
41 static struct type *read_enum_type ();
42 static struct type *read_struct_type ();
43 static long read_number ();
44 static void finish_block ();
45 static struct blockvector *make_blockvector ();
46 static struct symbol *define_symbol ();
47 static void start_subfile ();
48 static int hashname ();
49 static void hash_symsegs ();
50
51 extern struct symtab *read_symsegs ();
52 extern void free_all_symtabs ();
53
54 /* C++ */
55 static struct type **read_args ();
56
57 START_FILE
58
59 /* Chain of symtabs made from reading the file's symsegs.
60    These symtabs do not go into symtab_list themselves,
61    but the information is copied from them when appropriate
62    to make the symtabs that will exist permanently.  */
63
64 static struct symtab *symseg_chain;
65
66 /* Symseg symbol table for the file whose data we are now processing.
67    It is one of those in symseg_chain.  Or 0, for a compilation that
68    has no symseg.  */
69
70 static struct symtab *current_symseg;
71
72 /* Name of source file whose symbol data we are now processing.
73    This comes from a symbol of type N_SO.  */
74
75 static char *last_source_file;
76
77 /* Core address of start of text of current source file.
78    This too comes from the N_SO symbol.  */
79
80 static CORE_ADDR last_source_start_addr;
81
82 /* End of the text segment of the executable file,
83    as found in the symbol _etext.  */
84
85 static CORE_ADDR end_of_text_addr;
86
87 /* The list of sub-source-files within the current individual compilation.
88    Each file gets its own symtab with its own linetable and associated info,
89    but they all share one blockvector.  */
90
91 struct subfile
92 {
93   struct subfile *next;
94   char *name;
95   struct linetable *line_vector;
96   int line_vector_length;
97   int line_vector_index;
98   int prev_line_number;
99 };
100
101 static struct subfile *subfiles;
102
103 static struct subfile *current_subfile;
104
105 /* The addresses of the symbol table stream and the string table
106    of the object file we are reading (as copied into core).  */
107
108 static FILE *nlist_stream_global;
109 static int nlist_size_global;
110 static char *stringtab_global;
111
112 /* The index in nlist_global of the last dbx symbol to be processed.  */
113
114 static int symnum;
115
116 /* Vector of types defined so far, indexed by their dbx type numbers.
117    (In newer sun systems, dbx uses a pair of numbers in parens,
118     as in "(SUBFILENUM,NUMWITHINSUBFILE)".  Then these numbers must be
119     translated through the type_translations hash table to get
120     the index into the type vector.)  */
121
122 static struct typevector *type_vector;
123
124 /* Number of elements allocated for type_vector currently.  */
125
126 static int type_vector_length;
127
128 /* Vector of line number information.  */
129
130 static struct linetable *line_vector;
131
132 /* Index of next entry to go in line_vector_index.  */
133
134 static int line_vector_index;
135
136 /* Last line number recorded in the line vector.  */
137
138 static int prev_line_number;
139
140 /* Number of elements allocated for line_vector currently.  */
141
142 static int line_vector_length;
143
144 /* Chain of global symbols whose values are not known yet.
145    They are chained thru the SYMBOL_VALUE, since we don't
146    have the correct data for that slot yet.  */
147
148 #define HASHSIZE 127
149 static struct symbol *global_sym_chain[HASHSIZE];
150
151 /* Record the symbols defined for each context in a list.
152    We don't create a struct block for the context until we
153    know how long to make it.  */
154
155 struct pending
156 {
157   struct pending *next;
158   struct symbol *symbol;
159 };
160
161 /* Here are the three lists that symbols are put on.  */
162
163 struct pending *file_symbols;   /* static at top level, and types */
164
165 struct pending *global_symbols; /* global functions and variables */
166
167 struct pending *local_symbols;  /* everything local to lexical context */
168
169 /* List of unclosed lexical contexts
170    (that will become blocks, eventually).  */
171
172 struct context_stack
173 {
174   struct context_stack *next;
175   struct pending *locals;
176   struct pending_block *old_blocks;
177   struct symbol *name;
178   CORE_ADDR start_addr;
179   int depth;
180 };
181
182 struct context_stack *context_stack;
183
184 /* Nonzero if within a function (so symbols should be local,
185    if nothing says specifically).  */
186
187 int within_function;
188
189 /* List of blocks already made (lexical contexts already closed).
190    This is used at the end to make the blockvector.  */
191
192 struct pending_block
193 {
194   struct pending_block *next;
195   struct block *block;
196 };
197
198 struct pending_block *pending_blocks;
199
200 extern CORE_ADDR first_object_file_end; /* From blockframe.c */
201
202 /* File name symbols were loaded from.  */
203
204 static char *symfile;
205 \f
206 /* Support for Sun changes to dbx symbol format */
207
208 /* For each identified header file, we have a table of types defined
209    in that header file.
210
211    header_files maps header file names to their type tables.
212    It is a vector of n_header_files elements.
213    Each element describes one header file.
214    It contains a vector of types.
215
216    Sometimes it can happen that the same header file produces
217    different results when included in different places.
218    This can result from conditionals or from different
219    things done before including the file.
220    When this happens, there are multiple entries for the file in this table,
221    one entry for each distinct set of results.
222    The entries are distinguished by the INSTANCE field.
223    The INSTANCE field appears in the N_BINCL and N_EXCL symbol table and is
224    used to match header-file references to their corresponding data.  */
225
226 struct header_file
227 {
228   char *name;                   /* Name of header file */
229   int instance;                 /* Numeric code distinguishing instances
230                                    of one header file that produced
231                                    different results when included.
232                                    It comes from the N_BINCL or N_EXCL.  */
233   struct type **vector;         /* Pointer to vector of types */
234   int length;                   /* Allocated length (# elts) of that vector */
235 };
236
237 static struct header_file *header_files;
238
239 static int n_header_files;
240
241 static int n_allocated_header_files;
242
243 /* Within each object file, various header files are assigned numbers.
244    A type is defined or referred to with a pair of numbers
245    (FILENUM,TYPENUM) where FILENUM is the number of the header file
246    and TYPENUM is the number within that header file.
247    TYPENUM is the index within the vector of types for that header file.
248
249    FILENUM == 1 is special; it refers to the main source of the object file,
250    and not to any header file.  FILENUM != 1 is interpreted by looking it up
251    in the following table, which contains indices in header_files.  */
252
253 static int *this_object_header_files;
254
255 static int n_this_object_header_files;
256
257 static int n_allocated_this_object_header_files;
258
259 /* When a header file is getting special overriding definitions
260    for one source file, record here the header_files index
261    of its normal definition vector.
262    At other times, this is -1.  */
263
264 static int header_file_prev_index;
265
266 /* At the start of reading dbx symbols, allocate our tables.  */
267
268 static void
269 init_header_files ()
270 {
271   n_allocated_header_files = 10;
272   header_files = (struct header_file *) xmalloc (10 * sizeof (struct header_file));
273   n_header_files = 0;
274
275   n_allocated_this_object_header_files = 10;
276   this_object_header_files = (int *) xmalloc (10 * sizeof (int));
277 }
278
279 /* At the end of reading dbx symbols, free our tables.  */
280
281 static void
282 free_header_files ()
283 {
284   register int i;
285   for (i = 0; i < n_header_files; i++)
286     free (header_files[i].name);
287   free (header_files);
288   free (this_object_header_files);
289 }
290
291 /* Called at the start of each object file's symbols.
292    Clear out the mapping of header file numbers to header files.  */
293
294 static void
295 new_object_header_files ()
296 {
297   /* Leave FILENUM of 0 free for builtin types and this file's types.  */
298   n_this_object_header_files = 1;
299   header_file_prev_index = -1;
300 }
301
302 /* Add header file number I for this object file
303    at the next successive FILENUM.  */
304
305 static void
306 add_this_object_header_file (i)
307      int i;
308 {
309   if (n_this_object_header_files == n_allocated_this_object_header_files)
310     {
311       n_allocated_this_object_header_files *= 2;
312       this_object_header_files
313         = (int *) xrealloc (this_object_header_files,
314                             n_allocated_this_object_header_files * sizeof (int));
315     }
316
317   this_object_header_files[n_this_object_header_files++] = i;
318 }
319
320 /* Add to this file an "old" header file, one already seen in
321    a previous object file.  NAME is the header file's name.
322    INSTANCE is its instance code, to select among multiple
323    symbol tables for the same header file.  */
324
325 static void
326 add_old_header_file (name, instance)
327      char *name;
328      int instance;
329 {
330   register struct header_file *p = header_files;
331   register int i;
332
333   for (i = 0; i < n_header_files; i++)
334     if (!strcmp (p[i].name, name) && instance == p[i].instance)
335       {
336         add_this_object_header_file (i);
337         return;
338       }
339   error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
340          symnum);
341 }
342
343 /* Add to this file a "new" header file: definitions for its types follow.
344    NAME is the header file's name.
345    Most often this happens only once for each distinct header file,
346    but not necessarily.  If it happens more than once, INSTANCE has
347    a different value each time, and references to the header file
348    use INSTANCE values to select among them.
349
350    dbx output contains "begin" and "end" markers for each new header file,
351    but at this level we just need to know which files there have been;
352    so we record the file when its "begin" is seen and ignore the "end".  */
353
354 static void
355 add_new_header_file (name, instance)
356      char *name;
357      int instance;
358 {
359   register int i;
360   register struct header_file *p = header_files;
361   header_file_prev_index = -1;
362
363 #if 0
364   /* This code was used before I knew about the instance codes.
365      My first hypothesis is that it is not necessary now
366      that instance codes are handled.  */
367
368   /* Has this header file a previous definition?
369      If so, make a new entry anyway so that this use in this source file
370      gets a separate entry.  Later source files get the old entry.
371      Record here the index of the old entry, so that any type indices
372      not previously defined can get defined in the old entry as
373      well as in the new one.  */
374
375   for (i = 0; i < n_header_files; i++)
376     if (!strcmp (p[i].name, name))
377       {
378         header_file_prev_index = i;
379       }
380
381 #endif
382
383   /* Make sure there is room for one more header file.  */
384
385   if (n_header_files == n_allocated_header_files)
386     {
387       n_allocated_header_files *= 2;
388       header_files
389         = (struct header_file *) xrealloc (header_files, n_allocated_header_files * sizeof (struct header_file));
390     }
391
392   /* Create an entry for this header file.  */
393
394   i = n_header_files++;
395   header_files[i].name = name;
396   header_files[i].instance = instance;
397   header_files[i].length = 10;
398   header_files[i].vector
399     = (struct type **) xmalloc (10 * sizeof (struct type *));
400   bzero (header_files[i].vector, 10 * sizeof (struct type *));
401
402   add_this_object_header_file (i);
403 }
404
405 /* Look up a dbx type-number pair.  Return the address of the slot
406    where the type for that number-pair is stored.
407    The number-pair is in TYPENUMS.
408
409    This can be used for finding the type associated with that pair
410    or for associating a new type with the pair.  */
411
412 static struct type **
413 dbx_lookup_type (typenums)
414      int typenums[2];
415 {
416   register int filenum = typenums[0], index = typenums[1];
417
418   if (filenum < 0 || filenum >= n_this_object_header_files)
419     error ("Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
420            filenum, index, symnum);
421
422   if (filenum == 0)
423     {
424       /* Type is defined outside of header files.
425          Find it in this object file's type vector.  */
426       if (index >= type_vector_length)
427         {
428           type_vector_length *= 2;
429           type_vector = (struct typevector *)
430             xrealloc (type_vector, sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
431           bzero (&type_vector->type[type_vector_length / 2],
432                  type_vector_length * sizeof (struct type *) / 2);
433         }
434       return &type_vector->type[index];
435     }
436   else
437     {
438       register int real_filenum = this_object_header_files[filenum];
439       register struct header_file *f;
440
441       if (real_filenum >= n_header_files)
442         abort ();
443
444       f = &header_files[real_filenum];
445
446       if (index >= f->length)
447         {
448           f->length *= 2;
449           f->vector = (struct type **)
450             xrealloc (f->vector, f->length * sizeof (struct type *));
451           bzero (&f->vector[f->length / 2],
452                  f->length * sizeof (struct type *) / 2);
453         }
454       return &f->vector[index];
455     }
456 }
457
458 /* Make sure there is a type allocated for type numbers TYPENUMS
459    and return the type object.
460    This can create an empty (zeroed) type object.  */
461
462 static struct type *
463 dbx_alloc_type (typenums)
464      int typenums[2];
465 {
466   register struct type **type_addr = dbx_lookup_type (typenums);
467   register struct type *type = *type_addr;
468
469   /* If we are referring to a type not known at all yet,
470      allocate an empty type for it.
471      We will fill it in later if we find out how.  */
472   if (type == 0)
473     {
474       type = (struct type *) obstack_alloc (symbol_obstack,
475                                             sizeof (struct type));
476       bzero (type, sizeof (struct type));
477       TYPE_VPTR_FIELDNO (type) = -1;
478       *type_addr = type;
479     }
480   return type;
481 }
482
483 #if 0
484 static struct type **
485 explicit_lookup_type (real_filenum, index)
486      int real_filenum, index;
487 {
488   register struct header_file *f = &header_files[real_filenum];
489
490   if (index >= f->length)
491     {
492       f->length *= 2;
493       f->vector = (struct type **)
494         xrealloc (f->vector, f->length * sizeof (struct type *));
495       bzero (&f->vector[f->length / 2],
496              f->length * sizeof (struct type *) / 2);
497     }
498   return &f->vector[index];
499 }
500 #endif
501 \f
502 /* maintain the lists of symbols and blocks */
503
504 /* Add a symbol to one of the lists of symbols.  */
505 static void
506 add_symbol_to_list (symbol, listhead)
507      struct symbol *symbol;
508      struct pending **listhead;
509 {
510   register struct pending *link
511     = (struct pending *) xmalloc (sizeof (struct pending));
512
513   link->next = *listhead;
514   link->symbol = symbol;
515   *listhead = link;
516 }
517
518 /* Take one of the lists of symbols and make a block from it.
519    Put the block on the list of pending blocks.  */
520
521 static void
522 finish_block (symbol, listhead, old_blocks, start, end)
523      struct symbol *symbol;
524      struct pending **listhead;
525      struct pending_block *old_blocks;
526      CORE_ADDR start, end;
527 {
528   register struct pending *next, *next1;
529   register struct block *block;
530   register struct pending_block *pblock;
531   struct pending_block *opblock;
532   register int i;
533
534   /* Count the length of the list of symbols.  */
535
536   for (next = *listhead, i = 0; next; next = next->next, i++);
537
538   block = (struct block *) obstack_alloc (symbol_obstack,
539                                           sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
540
541   /* Copy the symbols into the block.  */
542
543   BLOCK_NSYMS (block) = i;
544   for (next = *listhead; next; next = next->next)
545     BLOCK_SYM (block, --i) = next->symbol;
546
547   BLOCK_START (block) = start;
548   BLOCK_END (block) = end;
549   BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
550
551   /* Put the block in as the value of the symbol that names it.  */
552
553   if (symbol)
554     {
555       SYMBOL_BLOCK_VALUE (symbol) = block;
556       BLOCK_FUNCTION (block) = symbol;
557     }
558   else
559     BLOCK_FUNCTION (block) = 0;
560
561   /* Now free the links of the list, and empty the list.  */
562
563   for (next = *listhead; next; next = next1)
564     {
565       next1 = next->next;
566       free (next);
567     }
568   *listhead = 0;
569
570   /* Install this block as the superblock
571      of all blocks made since the start of this scope
572      that don't have superblocks yet.  */
573
574   opblock = 0;
575   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
576     {
577       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
578         BLOCK_SUPERBLOCK (pblock->block) = block;
579       opblock = pblock;
580     }
581
582   /* Record this block on the list of all blocks in the file.
583      Put it after opblock, or at the beginning if opblock is 0.
584      This puts the block in the list after all its subblocks.  */
585
586   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
587   pblock->block = block;
588   if (opblock)
589     {
590       pblock->next = opblock->next;
591       opblock->next = pblock;
592     }
593   else
594     {
595       pblock->next = pending_blocks;
596       pending_blocks = pblock;
597     }
598 }
599
600 static struct blockvector *
601 make_blockvector ()
602 {
603   register struct pending_block *next, *next1;
604   register struct blockvector *blockvector;
605   register int i;
606
607   /* Count the length of the list of blocks.  */
608
609   for (next = pending_blocks, i = 0; next; next = next->next, i++);
610
611   blockvector = (struct blockvector *) obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
612
613   /* Copy the blocks into the blockvector.
614      This is done in reverse order, which happens to put
615      the blocks into the proper order (ascending starting address).
616      finish_block has hair to insert each block into the list
617      after its subblocks in order to make sure this is true.  */
618
619   BLOCKVECTOR_NBLOCKS (blockvector) = i;
620   for (next = pending_blocks; next; next = next->next)
621     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
622
623   /* Now free the links of the list, and empty the list.  */
624
625   for (next = pending_blocks; next; next = next1)
626     {
627       next1 = next->next;
628       free (next);
629     }
630   pending_blocks = 0;
631
632   return blockvector;
633 }
634 \f
635 /* Manage the vector of line numbers.  */
636
637 static
638 record_line (line, pc)
639      int line;
640      CORE_ADDR pc;
641 {
642   /* Ignore the dummy line number in libg.o */
643
644   if (line == 0xffff)
645     return;
646
647   /* Make sure line vector is big enough.  */
648
649   if (line_vector_index + 1 >= line_vector_length)
650     {
651       line_vector_length *= 2;
652       line_vector = (struct linetable *)
653         xrealloc (line_vector,
654                   sizeof (struct linetable) + line_vector_length * sizeof (int));
655       current_subfile->line_vector = line_vector;
656     }
657
658   /* If this line is not continguous with previous one recorded,
659      record a line-number entry for it.  */
660   if (line != prev_line_number + 1)
661     line_vector->item[line_vector_index++] = - line;
662   prev_line_number = line;
663
664   /* Record the core address of the line.  */
665   line_vector->item[line_vector_index++] = pc;
666 }
667 \f
668 /* Start a new symtab for a new source file.
669    This is called when a dbx symbol of type N_SO is seen;
670    it indicates the start of data for one original source file.  */
671
672 static void
673 start_symtab (name, start_addr)
674      char *name;
675      CORE_ADDR start_addr;
676 {
677   register struct symtab *s;
678
679   last_source_file = name;
680   last_source_start_addr = start_addr;
681   file_symbols = 0;
682   global_symbols = 0;
683   context_stack = 0;
684   within_function = 0;
685
686   new_object_header_files ();
687
688   for (s = symseg_chain; s; s = s->next)
689     if (s->ldsymoff == symnum * sizeof (struct nlist))
690       break;
691   current_symseg = s;
692   if (s != 0)
693     return;
694
695   type_vector_length = 160;
696   type_vector = (struct typevector *) xmalloc (sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
697   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
698
699   /* Initialize the list of sub source files with one entry
700      for this file (the top-level source file).  */
701
702   subfiles = 0;
703   current_subfile = 0;
704   start_subfile (name);
705 }
706
707 /* Handle an N_SOL symbol, which indicates the start of
708    code that came from an included (or otherwise merged-in)
709    source file with a different name.  */
710
711 static void
712 start_subfile (name)
713      char *name;
714 {
715   register struct subfile *subfile;
716
717   /* Save the current subfile's line vector data.  */
718
719   if (current_subfile)
720     {
721       current_subfile->line_vector_index = line_vector_index;
722       current_subfile->line_vector_length = line_vector_length;
723       current_subfile->prev_line_number = prev_line_number;
724     }
725
726   /* See if this subfile is already known as a subfile of the
727      current main source file.  */
728
729   for (subfile = subfiles; subfile; subfile = subfile->next)
730     {
731       if (!strcmp (subfile->name, name))
732         {
733           line_vector = subfile->line_vector;
734           line_vector_index = subfile->line_vector_index;
735           line_vector_length = subfile->line_vector_length;
736           prev_line_number = subfile->prev_line_number;
737           current_subfile = subfile;
738           return;
739         }
740     }
741
742   /* This subfile is not known.  Add an entry for it.  */
743
744   line_vector_index = 0;
745   line_vector_length = 1000;
746   prev_line_number = -2;        /* Force first line number to be explicit */
747   line_vector = (struct linetable *)
748     xmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
749
750   /* Make an entry for this subfile in the list of all subfiles
751      of the current main source file.  */
752
753   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
754   subfile->next = subfiles;
755   subfile->name = savestring (name, strlen (name));
756   subfile->line_vector = line_vector;
757   subfiles = subfile;
758   current_subfile = subfile;
759 }
760
761 /* Finish the symbol definitions for one main source file,
762    close off all the lexical contexts for that file
763    (creating struct block's for them), then make the struct symtab
764    for that file and put it in the list of all such.
765
766    END_ADDR is the address of the end of the file's text.  */
767
768 static void
769 end_symtab (end_addr)
770      CORE_ADDR end_addr;
771 {
772   register struct symtab *symtab;
773   register struct context_stack *cstk;
774   register struct blockvector *blockvector;
775   register struct subfile *subfile;
776   register struct linetable *lv;
777   struct subfile *nextsub;
778
779   if (current_symseg != 0)
780     {
781       last_source_file = 0;
782       current_symseg = 0;
783       return;
784     }
785
786   /* Finish the lexical context of the last function in the file.  */
787
788   if (context_stack)
789     {
790       cstk = context_stack;
791       /* Make a block for the local symbols within.  */
792       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
793                     cstk->start_addr, end_addr);
794       free (cstk);
795     }
796
797   /* Finish defining all the blocks of this symtab.  */
798   finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr);
799   finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr);
800   blockvector = make_blockvector ();
801
802   current_subfile->line_vector_index = line_vector_index;
803
804   /* Now create the symtab objects proper, one for each subfile.  */
805   /* (The main file is one of them.)  */
806
807   for (subfile = subfiles; subfile; subfile = nextsub)
808     {
809       symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
810       symtab->free_ptr = 0;
811
812       /* Fill in its components.  */
813       symtab->blockvector = blockvector;
814       type_vector->length = type_vector_length;
815       symtab->typevector = type_vector;
816       symtab->free_code = free_linetable;
817       if (subfile->next == 0)
818         symtab->free_ptr = (char *) type_vector;
819
820       symtab->filename = subfile->name;
821       lv = subfile->line_vector;
822       lv->nitems = subfile->line_vector_index;
823       symtab->linetable = (struct linetable *)
824         xrealloc (lv, sizeof (struct linetable) + lv->nitems * sizeof (int));
825       symtab->nlines = 0;
826       symtab->line_charpos = 0;
827
828       /* Link the new symtab into the list of such.  */
829       symtab->next = symtab_list;
830       symtab_list = symtab;
831
832       nextsub = subfile->next;
833       free (subfile);
834     }
835
836   type_vector = 0;
837   type_vector_length = -1;
838   line_vector = 0;
839   line_vector_length = -1;
840   last_source_file = 0;
841 }
842 \f
843 #ifdef N_BINCL
844
845 /* Handle the N_BINCL and N_EINCL symbol types
846    that act like N_SOL for switching source files
847    (different subfiles, as we call them) within one object file,
848    but using a stack rather than in an arbitrary order.  */
849
850 struct subfile_stack
851 {
852   struct subfile_stack *next;
853   char *name;
854   int prev_index;
855 };
856
857 struct subfile_stack *subfile_stack;
858
859 static void
860 push_subfile ()
861 {
862   register struct subfile_stack *tem
863     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
864
865   tem->next = subfile_stack;
866   subfile_stack = tem;
867   if (current_subfile == 0 || current_subfile->name == 0)
868     abort ();
869   tem->name = current_subfile->name;
870   tem->prev_index = header_file_prev_index;
871 }
872
873 static char *
874 pop_subfile ()
875 {
876   register char *name;
877   register struct subfile_stack *link = subfile_stack;
878
879   if (link == 0)
880     abort ();
881
882   name = link->name;
883   subfile_stack = link->next;
884   header_file_prev_index = link->prev_index;
885   free (link);
886
887   return name;
888 }
889 #endif /* Have N_BINCL */
890 \f
891 /* Accumulate the misc functions in bunches of 127.
892    At the end, copy them all into one newly allocated structure.  */
893
894 #define MISC_BUNCH_SIZE 127
895
896 struct misc_bunch
897 {
898   struct misc_bunch *next;
899   struct misc_function contents[MISC_BUNCH_SIZE];
900 };
901
902 /* Bunch currently being filled up.
903    The next field points to chain of filled bunches.  */
904
905 static struct misc_bunch *misc_bunch;
906
907 /* Number of slots filled in current bunch.  */
908
909 static int misc_bunch_index;
910
911 /* Total number of misc functions recorded so far.  */
912
913 static int misc_count;
914
915 static void
916 init_misc_functions ()
917 {
918   misc_count = 0;
919   misc_bunch = 0;
920   misc_bunch_index = MISC_BUNCH_SIZE;
921 }
922
923 static void
924 record_misc_function (name, address)
925      char *name;
926      CORE_ADDR address;
927 {
928   register struct misc_bunch *new;
929
930   if (misc_bunch_index == MISC_BUNCH_SIZE)
931     {
932       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
933       misc_bunch_index = 0;
934       new->next = misc_bunch;
935       misc_bunch = new;
936     }
937   misc_bunch->contents[misc_bunch_index].name = name;
938   misc_bunch->contents[misc_bunch_index].address = address;
939   misc_bunch_index++;
940   misc_count++;
941 }
942
943 static int
944 compare_misc_functions (fn1, fn2)
945      struct misc_function *fn1, *fn2;
946 {
947   /* Return a signed result based on unsigned comparisons
948      so that we sort into unsigned numeric order.  */
949   if (fn1->address < fn2->address)
950     return -1;
951   if (fn1->address > fn2->address)
952     return 1;
953   return 0;
954 }
955
956 static void
957 discard_misc_bunches ()
958 {
959   register struct misc_bunch *next;
960
961   while (misc_bunch)
962     {
963       next = misc_bunch->next;
964       free (misc_bunch);
965       misc_bunch = next;
966     }
967 }
968
969 static void
970 condense_misc_bunches ()
971 {
972   register int i, j;
973   register struct misc_bunch *bunch;
974 #ifdef NAMES_HAVE_UNDERSCORE
975   int offset = 1;
976 #else
977   int offset = 0;
978 #endif
979
980   misc_function_vector
981     = (struct misc_function *)
982       xmalloc (misc_count * sizeof (struct misc_function));
983
984   j = 0;
985   bunch = misc_bunch;
986   while (bunch)
987     {
988       for (i = 0; i < misc_bunch_index; i++)
989         {
990           misc_function_vector[j] = bunch->contents[i];
991           misc_function_vector[j].name
992             = concat (misc_function_vector[j].name
993                       + (misc_function_vector[j].name[0] == '_' ? offset : 0),
994                       "", "");
995           j++;
996         }
997       bunch = bunch->next;
998       misc_bunch_index = MISC_BUNCH_SIZE;
999     }
1000
1001   misc_function_count = j;
1002
1003   /* Sort the misc functions by address.  */
1004
1005   qsort (misc_function_vector, j, sizeof (struct misc_function),
1006          compare_misc_functions);
1007 }
1008 \f
1009 /* Call sort_syms to sort alphabetically
1010    the symbols of each block of each symtab.  */
1011
1012 static int
1013 compare_symbols (s1, s2)
1014      struct symbol **s1, **s2;
1015 {
1016   /* Names that are less should come first.  */
1017   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
1018   if (namediff != 0) return namediff;
1019   /* For symbols of the same name, registers should come first.  */
1020   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
1021           - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
1022 }
1023
1024 static void
1025 sort_syms ()
1026 {
1027   register struct symtab *s;
1028   register int i, nbl;
1029   register struct blockvector *bv;
1030   register struct block *b;
1031
1032   for (s = symtab_list; s; s = s->next)
1033     {
1034       bv = BLOCKVECTOR (s);
1035       nbl = BLOCKVECTOR_NBLOCKS (bv);
1036       for (i = 0; i < nbl; i++)
1037         {
1038           b = BLOCKVECTOR_BLOCK (bv, i);
1039           qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
1040                  sizeof (struct symbol *), compare_symbols);
1041         }
1042     }
1043 }
1044 \f
1045 /* This is the symbol-file command.  Read the file, analyze its symbols,
1046    and add a struct symtab to symtab_list.  */
1047
1048 void
1049 symbol_file_command (name)
1050      char *name;
1051 {
1052   register int desc;
1053   struct exec hdr;
1054   struct nlist *nlist;
1055   char *stringtab;
1056   long buffer;
1057   register int val;
1058   extern void close ();
1059   struct cleanup *old_chain;
1060   struct symtab *symseg;
1061
1062   dont_repeat ();
1063
1064   if (name == 0)
1065     {
1066       if (symtab_list && !query ("Discard symbol table? ", 0))
1067         error ("Not confirmed.");
1068       free_all_symtabs ();
1069       return;
1070     }
1071
1072   if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
1073     error ("Not confirmed.");
1074
1075   if (symfile)
1076     free (symfile);
1077   symfile = 0;
1078
1079   {
1080     char *absolute_name;
1081     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
1082     if (desc < 0)
1083       perror_with_name (name);
1084     else
1085       name = absolute_name;
1086   }
1087
1088   old_chain = make_cleanup (close, desc);
1089   make_cleanup (free_current_contents, &name);
1090
1091   val = myread (desc, &hdr, sizeof hdr);
1092   if (val < 0)
1093     perror_with_name (name);
1094
1095   if (N_BADMAG (hdr))
1096     error ("File \"%s\" not in executable format.", name);
1097
1098   if (hdr.a_syms == 0)
1099     {
1100       free_all_symtabs ();
1101       printf ("%s does not have a symbol-table.\n", name);
1102       fflush (stdout);
1103       return;
1104     }
1105
1106   printf ("Reading symbol data from %s...", name);
1107   fflush (stdout);
1108
1109   /* Now read the string table, all at once.  */
1110   val = lseek (desc, N_SYMOFF (hdr) + hdr.a_syms, 0);
1111   if (val < 0)
1112     perror_with_name (name);
1113   val = myread (desc, &buffer, sizeof buffer);
1114   if (val < 0)
1115     perror_with_name (name);
1116   stringtab = (char *) alloca (buffer);
1117   if (stringtab == NULL)
1118     {
1119       free_all_symtabs ();
1120       printf("%s: symbol table too large for alloca: %d bytes.\n", name, 
1121         buffer);
1122       fflush (stdout);
1123       return;
1124     }
1125   bcopy (&buffer, stringtab, sizeof buffer);
1126   val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
1127   if (val < 0)
1128     perror_with_name (name);
1129
1130   /* Throw away the old symbol table.  */
1131
1132   free_all_symtabs ();
1133
1134 #ifdef READ_GDB_SYMSEGS
1135   /* That puts us at the symsegs.  Read them.  */
1136   symseg_chain = read_symsegs (desc, name);
1137   hash_symsegs ();
1138
1139   /* Free the symtabs made by read_symsegs, but not their contents,
1140      which have been copied into symtabs on symtab_list.  */
1141   for (symseg = symseg_chain; symseg; symseg = symseg->next)
1142     {
1143       int i;
1144       struct sourcevector *sv = (struct sourcevector *) symseg->linetable;
1145
1146       for (i = 0; i < sv->length; i++)
1147         {
1148           int j;
1149           struct source *source = sv->source[i];
1150           struct symtab *sp1
1151             = (struct symtab *) xmalloc (sizeof (struct symtab));
1152
1153           bcopy (symseg, sp1, sizeof (struct symtab));
1154           sp1->filename = savestring (source->name, strlen (source->name));
1155           sp1->linetable = &source->contents;
1156           sp1->free_code = free_nothing;
1157           sp1->free_ptr = (i == 0) ? (char *) symseg : 0;
1158
1159           sp1->next = symtab_list;
1160           symtab_list = sp1;
1161         }
1162     }
1163 #else
1164   /* Where people are using the 4.2 ld program, must not check for
1165      symsegs, because that ld puts randonm garbage at the end of
1166      the output file and that would trigger an error message.  */
1167   symseg_chain = 0;
1168 #endif
1169
1170   /* Position to read the symbol table.  Do not read it all at once. */
1171   val = lseek (desc, N_SYMOFF (hdr), 0);
1172   if (val < 0)
1173     perror_with_name (name);
1174
1175   init_misc_functions ();
1176   make_cleanup (discard_misc_bunches, 0);
1177   init_header_files ();
1178   make_cleanup (free_header_files, 0);
1179
1180   /* Now that the symbol table data of the executable file are all in core,
1181      process them and define symbols accordingly.  Closes desc.  */
1182
1183   read_dbx_symtab (desc, stringtab, hdr.a_syms / sizeof (struct nlist));
1184
1185   /* Sort symbols alphabetically within each block.  */
1186
1187   sort_syms ();
1188
1189   /* Go over the misc functions and install them in vector.  */
1190
1191   condense_misc_bunches ();
1192
1193   /* Don't allow char * to have a typename (else would get caddr_t.)  */
1194
1195   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
1196
1197   /* Make a default for file to list.  */
1198
1199   select_source_symtab (symtab_list);
1200
1201   symfile = savestring (name, strlen (name));
1202
1203   do_cleanups (old_chain);
1204
1205   /* Free the symtabs made by read_symsegs, but not their contents,
1206      which have been copied into symtabs on symtab_list.  */
1207   while (symseg_chain)
1208     {
1209       register struct symtab *s = symseg_chain->next;
1210       free (symseg_chain);
1211       symseg_chain = s;
1212     }
1213
1214   printf ("done.\n");
1215   fflush (stdout);
1216 }
1217
1218 /* Return name of file symbols were loaded from, or 0 if none..  */
1219
1220 char *
1221 get_sym_file ()
1222 {
1223   return symfile;
1224 }
1225 \f
1226 /* Given pointers to a a.out symbol table in core containing  dbx style data,
1227    analyze them and create struct symtab's describing the symbols.
1228    NLISTLEN is the number of symbols in the symbol table.
1229    We read them one at a time using stdio.
1230    All symbol names are given as offsets relative to STRINGTAB.  */
1231
1232 static void
1233 read_dbx_symtab (desc, stringtab, nlistlen)
1234      int desc;
1235      register char *stringtab;
1236      register int nlistlen;
1237 {
1238   FILE *stream = fdopen (desc, "r");
1239   struct nlist buf;
1240   register char *namestring;
1241   register struct symbol *sym, *prev;
1242   int hash;
1243   int num_object_files = 0;
1244   struct cleanup *old_chain;
1245
1246 #ifdef N_BINCL
1247   subfile_stack = 0;
1248 #endif
1249
1250   old_chain = make_cleanup (free_all_symtabs, 0);
1251   nlist_stream_global = stream;
1252   nlist_size_global = nlistlen;
1253   stringtab_global = stringtab;
1254   last_source_file = 0;
1255   bzero (global_sym_chain, sizeof global_sym_chain);
1256
1257   for (symnum = 0; symnum < nlistlen; symnum++)
1258     {
1259       QUIT;     /* allow this to be interruptable */
1260       fread (&buf, sizeof buf, 1, stream);
1261       namestring = buf.n_un.n_strx ? buf.n_un.n_strx + stringtab : "";
1262       if (buf.n_type & N_STAB)
1263         process_one_symbol (buf.n_type, buf.n_desc,
1264                             buf.n_value, namestring);
1265       /* A static text symbol whose name ends in ".o"
1266          can only mean the start of another object file.
1267          So end the symtab of the source file we have been processing.
1268          This is how we avoid counting the libraries as part
1269          or the last source file.
1270          Also this way we find end of first object file (crt0).  */
1271       else if (buf.n_type == N_TEXT
1272                && !strcmp (namestring + strlen (namestring) - 2, ".o"))
1273         {
1274           if (num_object_files++ == 1)
1275             first_object_file_end = buf.n_value;
1276           if (last_source_file)
1277             end_symtab (buf.n_value);
1278         }
1279       else if (buf.n_type & N_EXT || buf.n_type == N_TEXT)
1280         {
1281           int used_up = 0;
1282
1283           /* Record the location of _etext.  */
1284           if (buf.n_type == (N_TEXT | N_EXT)
1285               && !strcmp (namestring, "_etext"))
1286             end_of_text_addr = buf.n_value;
1287
1288           /* Global symbol: see if we came across a dbx definition
1289              for a corresponding symbol.  If so, store the value.
1290              Remove syms from the chain when their values are stored,
1291              but search the whole chain, as there may be several syms
1292              from different files with the same name.  */
1293           if (buf.n_type & N_EXT)
1294             {
1295               prev = 0;
1296 #ifdef NAMES_HAVE_UNDERSCORE
1297               hash = hashname (namestring + 1);
1298 #else /* not NAMES_HAVE_UNDERSCORE */
1299               hash = hashname (namestring);
1300 #endif /* not NAMES_HAVE_UNDERSCORE */
1301               for (sym = global_sym_chain[hash];
1302                    sym;)
1303                 {
1304                   if (
1305 #ifdef NAMES_HAVE_UNDERSCORE
1306                       *namestring == '_'
1307                       && namestring[1] == SYMBOL_NAME (sym)[0]
1308                       &&
1309                       !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
1310 #else /* NAMES_HAVE_UNDERSCORE */
1311                       namestring[0] == SYMBOL_NAME (sym)[0]
1312                       &&
1313                       !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
1314 #endif /* NAMES_HAVE_UNDERSCORE */
1315                       )
1316                     {
1317                       if (prev)
1318                         SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1319                       else
1320                         global_sym_chain[hash]
1321                           = (struct symbol *) SYMBOL_VALUE (sym);
1322                       SYMBOL_VALUE (sym) = buf.n_value;
1323                       if (prev)
1324                         sym = (struct symbol *) SYMBOL_VALUE (prev);
1325                       else
1326                         sym = global_sym_chain[hash];
1327
1328                       used_up = 1;
1329                     }
1330                   else
1331                     {
1332                       prev = sym;
1333                       sym = (struct symbol *) SYMBOL_VALUE (sym);
1334                     }
1335                 }
1336             }
1337
1338           /* Defined global or text symbol: record as a misc function
1339              if it didn't give its address to a debugger symbol above.  */
1340           if (buf.n_type <= (N_TYPE | N_EXT)
1341               && buf.n_type != N_EXT
1342               && ! used_up)
1343             record_misc_function (namestring, buf.n_value);
1344         }
1345     }
1346
1347   if (last_source_file)
1348     end_symtab (end_of_text_addr);
1349
1350   fclose (stream);
1351   discard_cleanups (old_chain);
1352 }
1353
1354 /* dbx allows the text of a symbol name to be continued into the
1355    next symbol name!  When such a continuation is encountered
1356    (a \ at the end of the text of a name)
1357    call this function to get the continuation.  */
1358
1359 static char *
1360 next_symbol_text ()
1361 {
1362   struct nlist buf;
1363   fread (&buf, sizeof buf, 1, nlist_stream_global);
1364   symnum++;
1365   return buf.n_un.n_strx + stringtab_global;
1366 }
1367
1368 static int
1369 hashname (name)
1370      char *name;
1371 {
1372   register char *p = name;
1373   register int total = p[0];
1374   register int c;
1375
1376   c = p[1];
1377   total += c << 2;
1378   if (c)
1379     {
1380       c = p[2];
1381       total += c << 4;
1382       if (c)
1383         total += p[3] << 6;
1384     }
1385   
1386   return total % HASHSIZE;
1387 }
1388
1389 /* Put all appropriate global symbols in the symseg data
1390    onto the hash chains so that their addresses will be stored
1391    when seen later in loader global symbols.  */
1392
1393 static void
1394 hash_symsegs ()
1395 {
1396   /* Look at each symbol in each block in each symseg symtab.  */
1397   struct symtab *s;
1398   for (s = symseg_chain; s; s = s->next)
1399     {
1400       register int n;
1401       for (n = BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)) - 1; n >= 0; n--)
1402         {
1403           register struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), n);
1404           register int i;
1405           for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1406             {
1407               register struct symbol *sym = BLOCK_SYM (b, i);
1408
1409               /* Put the symbol on a chain if its value is an address
1410                  that is figured out by the loader.  */
1411
1412               if (SYMBOL_CLASS (sym) == LOC_EXTERNAL)
1413                 {
1414                   register int hash = hashname (SYMBOL_NAME (sym));
1415                   SYMBOL_VALUE (sym) = (int) global_sym_chain[hash];
1416                   global_sym_chain[hash] = sym;
1417                   SYMBOL_CLASS (sym) = LOC_STATIC;
1418                 }
1419             }
1420         }
1421     }
1422 }
1423 \f
1424 static void
1425 process_one_symbol (type, desc, value, name)
1426      int type, desc;
1427      CORE_ADDR value;
1428      char *name;
1429 {
1430   register struct context_stack *new;
1431   
1432   /* Something is wrong if we see real data before
1433      seeing a source file name.  */
1434   
1435 #ifdef N_NSYMS
1436   if (type == N_NSYMS) return;
1437 #endif
1438
1439   if (type != N_SO && last_source_file == 0)
1440     error ("Invalid symbol data: does not start by identifying a source file.");
1441
1442   switch (type)
1443     {
1444     case N_FUN:
1445     case N_FNAME:
1446       /* Either of these types of symbols indicates the start of
1447          a new function.  We must process its "name" normally for dbx,
1448          but also record the start of a new lexical context, and possibly
1449          also the end of the lexical context for the previous function.  */
1450       new = context_stack;
1451       within_function = 1;
1452       if (new)
1453         {
1454           /* Make a block for the local symbols within.  */
1455           finish_block (new->name, &local_symbols, new->old_blocks,
1456                         new->start_addr, value);
1457         }
1458       else
1459         {
1460           new = (struct context_stack *) xmalloc (sizeof (struct context_stack));
1461           new->next = 0;
1462           new->depth = -1;
1463           context_stack = new;
1464         }
1465       new->locals = 0;
1466       new->old_blocks = pending_blocks;
1467       new->start_addr = value;
1468       new->name = define_symbol (value, name, desc);
1469       local_symbols = 0;
1470       break;
1471
1472     case N_LBRAC:
1473       /* This "symbol" just indicates the start of an inner lexical
1474          context within a function.  */
1475       new = (struct context_stack *) xmalloc (sizeof (struct context_stack));
1476       new->depth = desc;
1477       new->next = context_stack;
1478       context_stack = new;
1479       new->locals = local_symbols;
1480       new->old_blocks = pending_blocks;
1481       new->start_addr = value;
1482       new->name = 0;
1483       local_symbols = 0;
1484       break;
1485
1486     case N_RBRAC:
1487       /* This "symbol" just indicates the end of an inner lexical
1488          context that was started with N_RBRAC.  */
1489       new = context_stack;
1490       if (new == 0 || desc != new->depth)
1491         error ("Invalid symbol data: N_LBRAC/N_RBRAC symbol mismatch, symtab pos %d.", symnum);
1492       local_symbols = new->locals;
1493       context_stack = new->next;
1494       /* If this is not the outermost LBRAC...RBRAC pair in the
1495          function, its local symbols preceded it, and are the ones
1496          just recovered from the context stack.  Defined the block for them.
1497
1498          If this is the outermost LBRAC...RBRAC pair, there is no
1499          need to do anything; leave the symbols that preceded it
1500          to be attached to the function's own block.  */
1501       if (local_symbols && context_stack->next)
1502         {
1503           /* Muzzle a compiler bug that makes end > start.  */
1504           if (new->start_addr > value)
1505             new->start_addr = value;
1506           /* Make a block for the local symbols within.  */
1507           finish_block (0, &local_symbols, new->old_blocks,
1508                         new->start_addr + last_source_start_addr,
1509                         value + last_source_start_addr);
1510         }
1511       free (new);
1512       break;
1513
1514     case N_FN:
1515       /* This kind of symbol supposedly indicates the start
1516          of an object file.  In fact this type does not appear.  */
1517       break;
1518
1519     case N_SO:
1520       /* This type of symbol indicates the start of data
1521          for one source file.
1522          Finish the symbol table of the previous source file
1523          (if any) and start accumulating a new symbol table.  */
1524       if (last_source_file)
1525         end_symtab (value);
1526       start_symtab (name, value);
1527       break;
1528
1529     case N_SOL:
1530       /* This type of symbol indicates the start of data for
1531          a sub-source-file, one whose contents were copied or
1532          included in the compilation of the main source file
1533          (whose name was given in the N_SO symbol.)  */
1534       start_subfile (name);
1535       break;
1536
1537 #ifdef N_BINCL
1538     case N_BINCL:
1539       push_subfile ();
1540       add_new_header_file (name, value);
1541       start_subfile (name);
1542       break;
1543
1544     case N_EINCL:
1545       start_subfile (pop_subfile ());
1546       break;
1547
1548     case N_EXCL:
1549       add_old_header_file (name, value);
1550       break;
1551 #endif /* have N_BINCL */
1552
1553     case N_SLINE:
1554       /* This type of "symbol" really just records
1555          one line-number -- core-address correspondence.
1556          Enter it in the line list for this symbol table.  */
1557       record_line (desc, value);
1558       break;
1559
1560     default:
1561       if (name)
1562         define_symbol (value, name, desc);
1563     }
1564 }
1565 \f
1566 /************************ READ_ADDL_SYM() ***********************************/
1567
1568 static void
1569 read_addl_syms (desc, stringtab, nlistlen, text_addr, text_size)
1570      int desc;
1571      register char *stringtab;
1572      register int nlistlen;
1573      unsigned text_addr;
1574      int text_size;
1575 {
1576   FILE *stream = fdopen (desc, "r");
1577   struct nlist buf;
1578   register char *namestring;
1579   register struct symbol *sym, *prev;
1580   int hash;
1581   int num_object_files = 0;
1582
1583 #ifdef N_BINCL
1584   subfile_stack = 0;
1585 #endif
1586
1587   nlist_stream_global = stream;
1588   nlist_size_global = nlistlen;
1589   stringtab_global = stringtab;
1590   last_source_file = 0;
1591   bzero (global_sym_chain, sizeof global_sym_chain);
1592
1593   for (symnum = 0; symnum < nlistlen; symnum++)
1594     {
1595       unsigned type;
1596
1597       fread (&buf, sizeof buf, 1, stream);
1598
1599       type = buf.n_type & N_TYPE;
1600       if( (type == N_TEXT) || (type == N_DATA) || (type == N_BSS) )
1601         {
1602           buf.n_value += text_addr;        
1603         }                       /* Right?? ###### */
1604
1605
1606       namestring = buf.n_un.n_strx ? buf.n_un.n_strx + stringtab : "";
1607       if (buf.n_type & N_STAB)
1608         process_one_symbol (buf.n_type, buf.n_desc,
1609                             buf.n_value, namestring);
1610       /* A static text symbol whose name ends in ".o"
1611          can only mean the start of another object file.
1612          So end the symtab of the source file we have been processing.
1613          This is how we avoid counting the libraries as part
1614          or the last source file.
1615          Also this way we find end of first object file (crt0).  */
1616       else if (buf.n_type == N_TEXT
1617                && !strcmp (namestring + strlen (namestring) - 2, ".o"))
1618         {
1619           if (num_object_files++ == 1)
1620             first_object_file_end = buf.n_value;
1621           if (last_source_file)
1622             {
1623               end_symtab (buf.n_value); /* All this not used##### */
1624             } 
1625         }
1626       else if (buf.n_type & N_EXT || buf.n_type == N_TEXT)
1627         {
1628           int used_up = 0;
1629
1630           /* Record the location of _etext.  */
1631           if (buf.n_type == (N_TEXT | N_EXT)
1632               && !strcmp (namestring, "_etext"))
1633             {
1634               end_of_text_addr = buf.n_value;
1635             }
1636
1637           /* Global symbol: see if we came across a dbx definition
1638              for a corresponding symbol.  If so, store the value.
1639              Remove syms from the chain when their values are stored,
1640              but search the whole chain, as there may be several syms
1641              from different files with the same name.  */
1642           if (buf.n_type & N_EXT)
1643             {
1644               prev = 0;
1645 #ifdef NAMES_HAVE_UNDERSCORE
1646               hash = hashname (namestring + 1);
1647 #else                           /* not NAMES_HAVE_UNDERSCORE */
1648               hash = hashname (namestring);
1649 #endif                          /* not NAMES_HAVE_UNDERSCORE */
1650               for (sym = global_sym_chain[hash];
1651                    sym;)
1652                 {
1653                   if (
1654 #ifdef NAMES_HAVE_UNDERSCORE
1655                       *namestring == '_'
1656                       && namestring[1] == SYMBOL_NAME (sym)[0]
1657                       &&
1658                       !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
1659 #else                           /* NAMES_HAVE_UNDERSCORE */
1660                       namestring[0] == SYMBOL_NAME (sym)[0]
1661                       &&
1662                       !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
1663 #endif                          /* NAMES_HAVE_UNDERSCORE */
1664                       )
1665                     {
1666                       if (prev)
1667                         SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1668                       else
1669                         global_sym_chain[hash]
1670                           = (struct symbol *) SYMBOL_VALUE (sym);
1671                       SYMBOL_VALUE (sym) = buf.n_value;
1672                       if (prev)
1673                         sym = (struct symbol *) SYMBOL_VALUE (prev);
1674                       else
1675                         sym = global_sym_chain[hash];
1676
1677                       used_up = 1;
1678                     }
1679                   else
1680                     {
1681                       prev = sym;
1682                       sym = (struct symbol *) SYMBOL_VALUE (sym);
1683                     }
1684                 }
1685             }
1686
1687           /* Defined global or text symbol: record as a misc function
1688              if it didn't give its address to a debugger symbol above.  */
1689           if (buf.n_type <= (N_TYPE | N_EXT)
1690               && buf.n_type != N_EXT
1691               && ! used_up)
1692             record_misc_function (namestring, buf.n_value);
1693         }
1694     }
1695
1696   if (last_source_file)
1697     { 
1698       end_symtab (text_addr + text_size);
1699     }
1700
1701   fclose (stream);
1702 }
1703
1704 /***************************** CONDENSE_ADDL_MISC_BUNCHES *******************/
1705
1706 static void
1707 condense_addl_misc_bunches ()
1708
1709   register int i, j;
1710   register struct misc_bunch *bunch;
1711 #ifdef NAMES_HAVE_UNDERSCORE
1712   int offset = 1;
1713 #else
1714   int offset = 0;
1715 #endif
1716
1717   misc_function_vector
1718     = (struct misc_function *)  xrealloc (misc_function_vector,
1719                                           (misc_count + misc_function_count) * sizeof (struct misc_function));
1720
1721   j = misc_function_count;
1722   bunch = misc_bunch;
1723   while (bunch)
1724     {
1725       for (i = 0; i < misc_bunch_index; i++)
1726         {
1727           misc_function_vector[j] = bunch->contents[i];
1728           misc_function_vector[j].name
1729             = concat (misc_function_vector[j].name
1730                       + (misc_function_vector[j].name[0] == '_' ? offset : 0),
1731                       "", "");
1732           j++;
1733         }
1734       bunch = bunch->next;
1735       misc_bunch_index = MISC_BUNCH_SIZE;
1736     }
1737
1738   misc_function_count += misc_count;
1739
1740   /* Sort the misc functions by address.  */
1741
1742   qsort (misc_function_vector, misc_function_count,
1743          sizeof (struct misc_function),  compare_misc_functions);
1744 }
1745 \f
1746 /**************************** ADD_FILE_COMMAND() ****************************/
1747 /* This function allows the addition of incrementally linked object files.  */
1748
1749 void
1750 add_file_command (arg_string)
1751      char* arg_string;
1752
1753   register int desc;
1754   struct exec hdr;
1755   struct nlist *nlist;
1756   char *stringtab;
1757   long buffer;
1758   register int val;
1759   extern void close ();
1760   struct cleanup *old_chain;
1761   char* name;
1762   unsigned text_addr;
1763
1764   if (arg_string == 0)
1765     error ("add-file takes a file name and an address");
1766
1767   for( ; *arg_string == ' '; arg_string++ );
1768   name = arg_string;
1769   for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
1770   *arg_string++ = (char) 0;
1771
1772   if (name[0] == 0)
1773     error ("add-file takes a file name and an address");
1774
1775   text_addr = parse_and_eval_address (arg_string);
1776
1777   dont_repeat ();
1778
1779   if (query ("add symbol table from filename \"%s\" at text_addr = 0x%x\n", name, text_addr))
1780     {
1781       desc = open (name, O_RDONLY);
1782       if (desc < 0)
1783         perror_with_name (name);
1784
1785       old_chain = make_cleanup (close, desc);
1786       make_cleanup (free_current_contents, &name);
1787
1788       val = myread (desc, &hdr, sizeof hdr);
1789       if (val < 0)
1790         perror_with_name (name);
1791
1792       if (N_BADMAG (hdr))
1793         error ("File \"%s\" has a bad header.", name);
1794
1795       if (hdr.a_syms == 0)
1796         {
1797           printf ("%s does not have a symbol-table.\n", name);
1798           fflush (stdout);
1799           return;
1800         }
1801
1802       /* Now read the string table, all at once.  */
1803       val = lseek (desc, N_SYMOFF (hdr) + hdr.a_syms, 0);
1804       if (val < 0)
1805         perror_with_name (name);
1806       val = myread (desc, &buffer, sizeof buffer);
1807       if (val < 0)
1808         perror_with_name (name);
1809       stringtab = (char *) alloca (buffer);
1810       bcopy (&buffer, stringtab, sizeof buffer);
1811       val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
1812       if (val < 0)
1813         perror_with_name (name);
1814
1815       /* That puts us at the symsegs.  Read them. ########## Also need other
1816          changes if they exist. */
1817
1818       /* Position to read the symbol table.  Do not read it all at once. */
1819       val = lseek (desc, N_SYMOFF (hdr), 0);
1820       if (val < 0)
1821         perror_with_name (name);
1822
1823       printf ("Reading symbol data from %s...", name);
1824       fflush (stdout);
1825
1826       init_misc_functions ();
1827       make_cleanup (discard_misc_bunches, 0);
1828       init_header_files ();
1829       make_cleanup (free_header_files, 0);
1830
1831       read_addl_syms (desc, stringtab, hdr.a_syms / sizeof(struct nlist),
1832                       text_addr, hdr.a_text) ;
1833
1834       /* Sort symbols alphabetically within each block.  */
1835
1836       sort_syms ();
1837
1838       /* Go over the all misc functions and install them in vector.  */
1839
1840       condense_addl_misc_bunches ();
1841
1842       /* Don't allow char * to have a typename (else would get caddr_t.)  */
1843
1844       TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
1845
1846       /* Make a default for file to list.  */
1847
1848       select_source_symtab (symtab_list);
1849
1850       do_cleanups (old_chain);
1851
1852       /* Free the symtabs made by read_symsegs, but not their contents,
1853          which have been copied into symtabs on symtab_list.  */
1854       while (symseg_chain)
1855         {
1856           register struct symtab *s = symseg_chain->next;
1857           free (symseg_chain);
1858           symseg_chain = s;
1859         }
1860
1861       printf ("done.\n");
1862       fflush (stdout);
1863     }
1864   else error ("Not confirmed.");
1865 }
1866 \f
1867 static struct symbol *
1868 define_symbol (value, string, desc)
1869      int value;
1870      char *string;
1871      int desc;
1872 {
1873   register struct symbol *sym
1874     = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1875   char *p = (char *) index (string, ':');
1876   int deftype;
1877   register int i;
1878
1879   bzero (sym, sizeof (struct symbol));
1880   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, string, p - string);
1881   p++;
1882   /* Determine the type of name being defined.  */
1883   if ((*p >= '0' && *p <= '9') || *p == '(')
1884     deftype = 'l';
1885   else
1886     deftype = *p++;
1887
1888   /* c is a special case, not followed by a type-number.
1889      SYMBOL:c=iVALUE for an integer constant symbol.
1890      SYMBOL:c=rVALUE for a floating constant symbol.  */
1891   if (deftype == 'c')
1892     {
1893       if (*p++ != '=')
1894         error ("Invalid symbol data at symtab pos %d.", symnum);
1895       switch (*p++)
1896         {
1897         case 'r':
1898           {
1899             double d = atof (p);
1900             char *value;
1901
1902             SYMBOL_TYPE (sym) = builtin_type_double;
1903             value = (char *) obstack_alloc (symbol_obstack, sizeof (double));
1904             bcopy (&d, value, sizeof (double));
1905             SYMBOL_VALUE_BYTES (sym) = value;
1906             SYMBOL_CLASS (sym) = LOC_CONST;
1907           }
1908           break;
1909         case 'i':
1910           {
1911             SYMBOL_TYPE (sym) = builtin_type_int;
1912             SYMBOL_VALUE (sym) = atoi (p);
1913             SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1914           }
1915           break;
1916         default:
1917           error ("Invalid symbol data at symtab pos %d.", symnum);
1918         }
1919       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1920       add_symbol_to_list (sym, &file_symbols);
1921       return sym;
1922     }
1923
1924   /* Now usually comes a number that says which data type,
1925      and possibly more stuff to define the type
1926      (all of which is handled by read_type)  */
1927
1928   if (deftype == 'p' && *p == 'F')
1929     /* pF is a two-letter code that means a function parameter in Fortran.
1930        The type-number specifies the type of the return value.
1931        Translate it into a pointer-to-function type.  */
1932     {
1933       p++;
1934       SYMBOL_TYPE (sym)
1935         = lookup_pointer_type (lookup_function_type (read_type (&p)));
1936     }
1937   else
1938     SYMBOL_TYPE (sym) = read_type (&p);
1939
1940   switch (deftype)
1941     {
1942     case 'f':
1943       SYMBOL_CLASS (sym) = LOC_BLOCK;
1944       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1945       add_symbol_to_list (sym, &file_symbols);
1946       break;
1947
1948     case 'F':
1949       SYMBOL_CLASS (sym) = LOC_BLOCK;
1950       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1951       add_symbol_to_list (sym, &global_symbols);
1952       break;
1953
1954     case 'G':
1955       /* For a class G (global) symbol, it appears that the
1956          value is not correct.  It is necessary to search for the
1957          corresponding linker definition to find the value.
1958          These definitions appear at the end of the namelist.  */
1959       i = hashname (SYMBOL_NAME (sym));
1960       SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
1961       global_sym_chain[i] = sym;
1962       SYMBOL_CLASS (sym) = LOC_STATIC;
1963       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1964       add_symbol_to_list (sym, &global_symbols);
1965       break;
1966
1967       /* This case is faked by a conditional above,
1968          when there is no code letter in the dbx data.
1969          Dbx data never actually contains 'l'.  */
1970     case 'l':
1971       SYMBOL_CLASS (sym) = LOC_LOCAL;
1972       SYMBOL_VALUE (sym) = value;
1973       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1974       add_symbol_to_list (sym, &local_symbols);
1975       break;
1976
1977     case 'p':
1978       SYMBOL_CLASS (sym) = LOC_ARG;
1979       SYMBOL_VALUE (sym) = value;
1980       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1981       add_symbol_to_list (sym, &local_symbols);
1982       /* DESC == 0 implies compiled with GCC.
1983          In this case, if it says `short', believe it.  */
1984       if (desc == 0)
1985         break;
1986       /* If PCC says a parameter is a short or a char,
1987          it is really an int.  */
1988       if (SYMBOL_TYPE (sym) == builtin_type_char
1989           || SYMBOL_TYPE (sym) == builtin_type_short)
1990         SYMBOL_TYPE (sym) = builtin_type_int;
1991       else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1992                || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1993         SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1994       break;
1995
1996     case 'r':
1997       SYMBOL_CLASS (sym) = LOC_REGISTER;
1998       SYMBOL_VALUE (sym) = value;
1999       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2000       add_symbol_to_list (sym, &local_symbols);
2001       break;
2002
2003     case 'S':
2004       /* Static symbol at top level of file */
2005       SYMBOL_CLASS (sym) = LOC_STATIC;
2006       SYMBOL_VALUE (sym) = value;
2007       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2008       add_symbol_to_list (sym, &file_symbols);
2009       break;
2010
2011     case 't':
2012       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2013       SYMBOL_VALUE (sym) = value;
2014       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2015       if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
2016           && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
2017         TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), "", "");
2018       /* C++ vagaries: we may have a type which is derived from
2019          a base type which did not have its name defined when the
2020          derived class was output.  We fill in the derived class's
2021          base part member's name here in that case.  */
2022       else if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
2023                 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
2024                && TYPE_BASECLASS (SYMBOL_TYPE (sym))
2025                && TYPE_FIELD_NAME (SYMBOL_TYPE (sym), 0) == 0)
2026         TYPE_FIELD_NAME (SYMBOL_TYPE (sym), 0) = TYPE_NAME (TYPE_BASECLASS (SYMBOL_TYPE (sym)));
2027
2028       add_symbol_to_list (sym, &file_symbols);
2029       break;
2030
2031     case 'T':
2032       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2033       SYMBOL_VALUE (sym) = value;
2034       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
2035       if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
2036           && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
2037         TYPE_NAME (SYMBOL_TYPE (sym))
2038           = concat ("",
2039                     (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
2040                      ? "enum "
2041                      : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
2042                         ? "struct " : "union ")),
2043                     SYMBOL_NAME (sym));
2044       add_symbol_to_list (sym, &file_symbols);
2045       break;
2046
2047     case 'V':
2048     case 'v':
2049       /* Static symbol of local scope */
2050       SYMBOL_CLASS (sym) = LOC_STATIC;
2051       SYMBOL_VALUE (sym) = value;
2052       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2053       add_symbol_to_list (sym, &local_symbols);
2054       break;
2055
2056     default:
2057       error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
2058     }
2059   return sym;
2060 }
2061 \f
2062 /* Read a number by which a type is referred to in dbx data,
2063    or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
2064    Just a single number N is equivalent to (0,N).
2065    Return the two numbers by storing them in the vector TYPENUMS.
2066    TYPENUMS will then be used as an argument to dbx_lookup_type.  */
2067
2068 static void
2069 read_type_number (pp, typenums)
2070      register char **pp;
2071      register int *typenums;
2072 {
2073   if (**pp == '(')
2074     {
2075       (*pp)++;
2076       typenums[0] = read_number (pp, ',');
2077       typenums[1] = read_number (pp, ')');
2078     }
2079   else
2080     {
2081       typenums[0] = 0;
2082       typenums[1] = read_number (pp, 0);
2083     }
2084 }
2085
2086 /* Read a dbx type reference or definition;
2087    return the type that is meant.
2088    This can be just a number, in which case it references
2089    a type already defined and placed in type_vector.
2090    Or the number can be followed by an =, in which case
2091    it means to define a new type according to the text that
2092    follows the =.  */
2093
2094 static
2095 struct type *
2096 read_type (pp)
2097      register char **pp;
2098 {
2099   register struct type *type = 0;
2100   register int n;
2101   struct type *type1;
2102   int typenums[2];
2103   int xtypenums[2];
2104
2105   read_type_number (pp, typenums);
2106
2107   /* Detect random reference to type not yet defined.
2108      Allocate a type object but leave it zeroed.  */
2109   if (**pp != '=')
2110     return dbx_alloc_type (typenums);
2111
2112   *pp += 2;
2113   switch ((*pp)[-1])
2114     {
2115     case 'x':
2116       type = dbx_alloc_type (typenums);
2117       /* Set the type code according to the following letter.  */
2118       switch ((*pp)[0])
2119         {
2120         case 's':
2121           TYPE_CODE (type) = TYPE_CODE_STRUCT;
2122           break;
2123         case 'u':
2124           TYPE_CODE (type) = TYPE_CODE_UNION;
2125           break;
2126         case 'e':
2127           TYPE_CODE (type) = TYPE_CODE_ENUM;
2128           break;
2129         }
2130       /* Skip the name the cross-ref points to.  */
2131       /* Note: for C++, the cross reference may be to a base type which
2132          has not yet been seen.  In this case, we skip to the comma,
2133          which will mark the end of the base class name.  (The ':'
2134          at the end of the base class name will be skipped as well.)  */
2135       *pp = (char *) index (*pp, ',');
2136       /* Just allocate the type and leave it zero if nothing known */
2137       return dbx_alloc_type (typenums);
2138
2139     case '0':
2140     case '1':
2141     case '2':
2142     case '3':
2143     case '4':
2144     case '5':
2145     case '6':
2146     case '7':
2147     case '8':
2148     case '9':
2149     case '(':
2150       (*pp)--;
2151       read_type_number (pp, xtypenums);
2152       type = *dbx_lookup_type (xtypenums);
2153       if (type == 0)
2154         type = builtin_type_void;
2155       *dbx_lookup_type (typenums) = type;
2156       break;
2157       
2158     case '*':
2159       type = dbx_alloc_type (typenums);
2160       smash_to_pointer_type (type, read_type (pp));
2161       break;
2162
2163     case '@':
2164       {
2165         struct type *domain = read_type (pp);
2166         char c;
2167         struct type *memtype;
2168
2169         if (*(*pp)++ != ',')
2170           error ("invalid member type data format, at symtab pos %d.",
2171                  symnum);
2172
2173         memtype = read_type (pp);
2174         type = dbx_alloc_type (typenums);
2175         smash_to_member_type (type, domain, memtype);
2176       }
2177       break;
2178
2179     case '&':
2180       type = dbx_alloc_type (typenums);
2181       smash_to_reference_type (type, read_type (pp));
2182       break;
2183
2184     case 'f':
2185       type = dbx_alloc_type (typenums);
2186       smash_to_function_type (type, read_type (pp));
2187       break;
2188
2189     case 'r':
2190       type = read_range_type (pp, typenums);
2191       *dbx_lookup_type (typenums) = type;
2192       break;
2193
2194     case 'e':
2195       type = dbx_alloc_type (typenums);
2196       type = read_enum_type (pp, type);
2197       *dbx_lookup_type (typenums) = type;
2198       break;
2199
2200     case 's':
2201       type = dbx_alloc_type (typenums);
2202       type = read_struct_type (pp, type);
2203       break;
2204
2205     case 'u':
2206       type = dbx_alloc_type (typenums);
2207       type = read_struct_type (pp, type);
2208       TYPE_CODE (type) = TYPE_CODE_UNION;
2209       break;
2210
2211     case 'a':
2212       /* Define an array type.  */
2213       type = dbx_alloc_type (typenums);
2214
2215       /* dbx expresses array types in terms of a range type for the index,
2216          and that range type is specified right inside the array type spec
2217          making ar1;MIN;MAX;VALTYPE  */
2218       if (!strncmp (*pp, "r1;0;", 5))
2219         (*pp) += 5;
2220       else if (!strncmp (*pp, "r(0,1);0;", 9))
2221         (*pp) += 9;
2222       else break;
2223
2224       TYPE_CODE (type) = TYPE_CODE_ARRAY;
2225       /* In Fortran, an upper bound may be T... meaning a parameter specifies
2226          the length of the data.  In this case, just pretend the bound is 1.
2227          This happens only for array parameters, which are really passed
2228          as pointers anyway, and we will translate them into such.  */
2229       if (**pp == 'T')
2230         {
2231           n = 1;
2232           while (**pp != ';')
2233             (*pp)++;
2234         }
2235       else
2236         n = read_number (pp, ';') + 1;
2237       TYPE_TARGET_TYPE (type) = read_type (pp);
2238       TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type)) * n;
2239       break;
2240
2241     default:
2242       error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
2243              (*pp)[-1], symnum);
2244     }
2245
2246   if (type == 0)
2247     abort ();
2248
2249 #if 0
2250   /* If this is an overriding temporary alteration for a header file's
2251      contents, and this type number is unknown in the global definition,
2252      put this type into the global definition at this type number.  */
2253   if (header_file_prev_index >= 0)
2254     {
2255       register struct type **tp
2256         = explicit_lookup_type (header_file_prev_index, typenums[1]);
2257       if (*tp == 0)
2258         *tp = type;
2259     }
2260 #endif
2261   return type;
2262 }
2263 \f
2264 /* This page contains subroutines of read_type.  */
2265
2266 /* Read the description of a structure (or union type)
2267    and return an object describing the type.  */
2268
2269 static struct type *
2270 read_struct_type (pp, type)
2271      char **pp;
2272      register struct type *type;
2273 {
2274   struct nextfield
2275     {
2276       struct nextfield *next;
2277       int visibility;
2278       struct field field;
2279     };
2280
2281   struct next_fnfield
2282     {
2283       struct next_fnfield *next;
2284       int visibility;
2285       struct fn_field fn_field;
2286     };
2287
2288   struct next_fnfieldlist
2289     {
2290       struct next_fnfieldlist *next;
2291       struct fn_fieldlist fn_fieldlist;
2292     };
2293
2294   register struct nextfield *list = 0;
2295   struct nextfield *new;
2296   int totalsize;
2297   char *name;
2298   register char *p;
2299   int nfields = 0;
2300   register int n;
2301
2302   register struct next_fnfieldlist *mainlist = 0;
2303   int nfn_fields = 0;
2304   struct type *baseclass = NULL;
2305   int read_possible_virtual_info = 0;
2306
2307   TYPE_CODE (type) = TYPE_CODE_STRUCT;
2308
2309   /* First comes the total size in bytes.  */
2310
2311   TYPE_LENGTH (type) = read_number (pp, 0);
2312
2313   /* C++: Now, if the class is a derived class, then the next character
2314      will be a '!', followed by the type of the base class. Allocate
2315      pretend that that base class is a sub-structure of this one,
2316      with its field name being the type name of the derived class. This
2317      cannot cause a naming conflict, since field names cannot be
2318      type names. This will magically recurse itself to gound terms
2319      when all is read and done. */
2320   if (**pp == '!')
2321     {
2322       *pp += 1;
2323
2324       switch (*(*pp)++)
2325         {
2326         case '0':
2327           break;
2328         case '1':
2329           TYPE_VIA_PROTECTED (type) = 1;
2330           break;
2331         case '2':
2332           TYPE_VIA_PUBLIC (type) = 1;
2333           break;
2334         default:
2335           error ("Invalid symbol data: bad visibility format at symtab pos %d.",
2336                  symnum);
2337         }
2338
2339       if (**pp == '\\') *pp = next_symbol_text ();
2340       new = (struct nextfield *) alloca (sizeof (struct nextfield));
2341       new->next = list;
2342       list = new;
2343
2344       baseclass = read_type (pp);
2345       list->field.type = baseclass;
2346       list->field.name = TYPE_NAME (baseclass);
2347       *pp += 1;                 /* skip ',' */
2348       list->field.bitpos = 0;
2349       list->field.bitsize = 0;  /* this should be an unpacked field! */
2350       nfields++;
2351     }
2352
2353   /* Now come the fields, as NAME:?TYPENUM,BITPOS,BITSIZE; for each one.
2354      At the end, we see a semicolon instead of a field.
2355
2356      In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2357      a static field.
2358
2359      The `?' is a placeholder for one of '+' (public visibility),
2360      '0' (protected visibility), and '-' (private visibility).  */
2361
2362   while (**pp != ';')
2363     {
2364       int visibility;
2365
2366       /* Check for and handle cretinous dbx symbol name continuation!  */
2367       if (**pp == '\\') *pp = next_symbol_text ();
2368
2369       /* Get space to record the next field's data.  */
2370       new = (struct nextfield *) alloca (sizeof (struct nextfield));
2371       new->next = list;
2372       list = new;
2373
2374       /* Read the data.  */
2375       p = *pp;
2376       while (*p != ':') p++;
2377       list->field.name = savestring (*pp, p - *pp);
2378
2379       /* Check to see if we have hit the methods yet.  */
2380       if (p[1] == ':')
2381         break;
2382
2383       *pp = p + 1;
2384
2385       /* This means we have a visibility for a field coming.  */
2386       if (**pp == '/')
2387         {
2388           switch (*++*pp)
2389             {
2390             case '0':
2391               visibility = 0;
2392               *pp += 1;
2393               break;
2394
2395             case '1':
2396               visibility = 1;
2397               *pp += 1;
2398               break;
2399
2400             case '2':
2401               visibility = 2;
2402               *pp += 1;
2403               break;
2404             }
2405         }
2406       /* else normal dbx-style format.  */
2407
2408       list->field.type = read_type (pp);
2409       if (**pp == ':')
2410         {
2411           list->field.bitpos = (long)-1;
2412           p = ++(*pp);
2413           while (*p != ';') p++;
2414           list->field.bitsize = (long) savestring (*pp, p - *pp);
2415           *pp = p + 1;
2416           nfields++;
2417           continue;
2418         }
2419       else if (**pp != ',')
2420         error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
2421                symnum);
2422       (*pp)++;                  /* Skip the comma.  */
2423       list->field.bitpos = read_number (pp, ',');
2424       list->field.bitsize = read_number (pp, ';');
2425       /* Detect an unpacked field and mark it as such.
2426          dbx gives a bit size for all fields.
2427          Also detect forward refs to structures and unions,
2428          and treat enums as if they had the width of ints.  */
2429       if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
2430            || TYPE_CODE (list->field.type) == TYPE_CODE_STRUCT
2431            || TYPE_CODE (list->field.type) == TYPE_CODE_UNION
2432            || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
2433                && list->field.bitsize == 8 * TYPE_LENGTH (builtin_type_int)))
2434           &&
2435           list->field.bitpos % 8 == 0)
2436         list->field.bitsize = 0;
2437       nfields++;
2438     }
2439
2440   /* Now come the method fields, as NAME::methods
2441      where each method is of the form TYPENUM,ARGS,...:PHYSNAME;
2442      At the end, we see a semicolon instead of a field.
2443
2444      For the case of overloaded operators, the format is
2445      OPERATOR::*.methods, where OPERATOR is the string "operator",
2446      `*' holds the place for an operator name (such as `+=')
2447      and `.' marks the end of the operator name.  */
2448   if (p[1] == ':')
2449     {
2450       /* Now, read in the methods.  To simplify matters, we
2451          "unread" the name that has been read, so that we can
2452          start from the top.  */
2453
2454       p = *pp;
2455
2456       /* chill the list of fields: the last entry (at the head)
2457          is a partially constructed entry which we now scrub.  */
2458       list = list->next;
2459
2460       /* For each list of method lists... */
2461       do
2462         {
2463           int i;
2464           struct next_fnfield *sublist = 0;
2465           struct fn_field *fn_fields = 0;
2466           int length = 0;
2467           struct next_fnfieldlist *new_mainlist =
2468             (struct next_fnfieldlist *)alloca (sizeof (struct next_fnfieldlist));
2469
2470           /* read in the name.  */
2471           while (*p != ':') p++;
2472           if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && (*pp)[2] == '$')
2473             {
2474               static char opname[32] = "operator ";
2475               char *o = opname + 9;
2476
2477               /* Skip past '::'.  */
2478               p += 2;
2479               while (*p != '.')
2480                 *o++ = *p++;
2481               new_mainlist->fn_fieldlist.name = savestring (opname, o - opname);
2482               /* Skip past '.'  */
2483               *pp = p + 1;
2484             }
2485           else
2486             {
2487               i = 0;
2488               new_mainlist->fn_fieldlist.name = savestring (*pp, p - *pp);
2489               /* Skip past '::'.  */
2490               *pp = p + 2;
2491             }
2492
2493           do
2494             {
2495               struct next_fnfield *new_sublist =
2496                 (struct next_fnfield *)alloca (sizeof (struct next_fnfield));
2497
2498               /* Check for and handle cretinous dbx symbol name continuation!  */
2499               if (**pp == '\\') *pp = next_symbol_text ();
2500
2501               new_sublist->fn_field.type = read_type (pp);
2502               new_sublist->fn_field.args = read_args (pp, ':');
2503               p = *pp;
2504               while (*p != ';') p++;
2505               new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2506               *pp = p + 1;
2507               new_sublist->visibility = *(*pp)++ - '0';
2508               if (**pp == '\\') *pp = next_symbol_text ();
2509
2510               if (*(*pp)++ == '*')
2511                 new_sublist->fn_field.voffset = read_number (pp, ';') + 1;
2512               else
2513                 new_sublist->fn_field.voffset = 0;
2514
2515               new_sublist->next = sublist;
2516               sublist = new_sublist;
2517               length++;
2518             }
2519           while (**pp != ';');
2520
2521           *pp += 1;
2522
2523           new_mainlist->fn_fieldlist.fn_fields =
2524             (struct fn_field *) obstack_alloc (symbol_obstack,
2525                                                sizeof (struct fn_field) * length);
2526           TYPE_FN_PRIVATE_BITS (new_mainlist->fn_fieldlist) =
2527             (int *) obstack_alloc (symbol_obstack,
2528                                    sizeof (int) * (1 + (length >> 5)));
2529
2530           TYPE_FN_PROTECTED_BITS (new_mainlist->fn_fieldlist) =
2531             (int *) obstack_alloc (symbol_obstack,
2532                                    sizeof (int) * (1 + (length >> 5)));
2533
2534           for (i = length; sublist; sublist = sublist->next)
2535             {
2536               new_mainlist->fn_fieldlist.fn_fields[--i] = sublist->fn_field;
2537               if (sublist->visibility == 0)
2538                 B_SET (new_mainlist->fn_fieldlist.private_fn_field_bits, i);
2539               else if (sublist->visibility == 1)
2540                 B_SET (new_mainlist->fn_fieldlist.protected_fn_field_bits, i);
2541             }
2542
2543           new_mainlist->fn_fieldlist.length = length;
2544           new_mainlist->next = mainlist;
2545           mainlist = new_mainlist;
2546           nfn_fields++;
2547         }
2548       while (**pp != ';');
2549
2550       *pp += 2;
2551
2552       if (**pp == '~')
2553         {
2554           read_possible_virtual_info = 1;
2555           *pp += 1;
2556         }
2557       if (**pp == '-')
2558         {
2559           TYPE_HAS_DESTRUCTOR (type) = 1;
2560           TYPE_HAS_CONSTRUCTOR (type) = 1;
2561           *pp += 1;
2562         }
2563       else if (**pp == '+')
2564         {
2565           TYPE_HAS_CONSTRUCTOR (type) = 1;
2566           *pp += 1;
2567         }
2568     }
2569   else *pp += 1;
2570
2571   /* Now create the vector of fields, and record how big it is.  */
2572
2573   TYPE_NFIELDS (type) = nfields;
2574   TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
2575                                                        sizeof (struct field) * nfields);
2576   TYPE_FIELD_PRIVATE_BITS (type) =
2577     (int *) obstack_alloc (symbol_obstack,
2578                            sizeof (int) * (1 + (nfields >> 5)));
2579   TYPE_FIELD_PROTECTED_BITS (type) =
2580     (int *) obstack_alloc (symbol_obstack,
2581                            sizeof (int) * (1 + (nfields >> 5)));
2582
2583   TYPE_NFN_FIELDS (type) = nfn_fields;
2584   TYPE_NFN_FIELDS_TOTAL (type) = nfn_fields;
2585   if (baseclass)
2586     TYPE_NFN_FIELDS_TOTAL (type) += TYPE_NFN_FIELDS_TOTAL (baseclass);
2587
2588   TYPE_FN_FIELDLISTS (type) =
2589     (struct fn_fieldlist *) obstack_alloc (symbol_obstack,
2590                                            sizeof (struct fn_fieldlist) * nfn_fields);
2591
2592   /* Copy the saved-up fields into the field vector.  */
2593
2594   for (n = nfields; list; list = list->next)
2595     {
2596       TYPE_FIELD (type, --n) = list->field;
2597       if (list->visibility == 0)
2598         SET_TYPE_FIELD_PRIVATE (type, n);
2599       else if (list->visibility == 1)
2600         SET_TYPE_FIELD_PROTECTED (type, n);
2601     }
2602
2603   for (n = nfn_fields; mainlist; mainlist = mainlist->next)
2604     TYPE_FN_FIELDLISTS (type)[--n] = mainlist->fn_fieldlist;
2605
2606   TYPE_BASECLASS (type) = baseclass;
2607
2608   if (read_possible_virtual_info)
2609     {
2610       /* Read either a '%' or the final ';'.  */
2611       if (*(*pp)++ == '%')
2612         {
2613           /* Now we must record the virtual function table pointer's
2614              field information.  */
2615
2616           struct type *t;
2617           int i;
2618
2619           t = read_type (pp);
2620           p = (*pp)++;
2621           while (*p != ';') p++;
2622           TYPE_VPTR_BASETYPE (type) = t;
2623           if (type == t)
2624             {
2625               if (TYPE_FIELD_NAME (t, 0) == 0)
2626                 TYPE_VPTR_FIELDNO (type) = i = 0;
2627               else for (i = TYPE_NFIELDS (t) - 1; i >= 0; --i)
2628                 if (! strncmp (TYPE_FIELD_NAME (t, i), *pp,
2629                                strlen (TYPE_FIELD_NAME (t, i))))
2630                   {
2631                     TYPE_VPTR_FIELDNO (type) = i;
2632                     break;
2633                   }
2634               if (i < 0)
2635                 error ("virtual function table field not found");
2636             }
2637           else
2638             TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type));
2639           *pp = p;
2640         }
2641       else
2642         {
2643           TYPE_VPTR_BASETYPE (type) = 0;
2644           TYPE_VPTR_FIELDNO (type) = -1;
2645         }
2646     }
2647   else
2648     {
2649       TYPE_VPTR_BASETYPE (type) = 0;
2650       TYPE_VPTR_FIELDNO (type) = -1;
2651     }
2652
2653   return type;
2654 }
2655
2656 /* Read a definition of an enumeration type,
2657    and create and return a suitable type object.
2658    Also defines the symbols that represent the values of the type.  */
2659
2660 static struct type *
2661 read_enum_type (pp, type)
2662      register char **pp;
2663      register struct type *type;
2664 {
2665   register char *p;
2666   char *name;
2667   register long n;
2668   register struct symbol *sym;
2669   int nsyms = 0;
2670   struct pending **symlist;
2671   struct pending *osyms, *syms;
2672
2673   if (within_function)
2674     symlist = &local_symbols;
2675   else
2676     symlist = &file_symbols;
2677   osyms = *symlist;
2678
2679   /* Read the value-names and their values.
2680      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2681      A semicolon instead of a NAME means the end.  */
2682   while (**pp && **pp != ';')
2683     {
2684       /* Check for and handle cretinous dbx symbol name continuation!  */
2685       if (**pp == '\\') *pp = next_symbol_text ();
2686
2687       p = *pp;
2688       while (*p != ':') p++;
2689       name = savestring (*pp, p - *pp);
2690       *pp = p + 1;
2691       n = read_number (pp, ',');
2692       
2693       sym = (struct symbol *) xmalloc (sizeof (struct symbol));
2694       bzero (sym, sizeof (struct symbol));
2695       SYMBOL_NAME (sym) = name;
2696       SYMBOL_CLASS (sym) = LOC_CONST;
2697       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2698       SYMBOL_VALUE (sym) = n;
2699       add_symbol_to_list (sym, symlist);
2700       nsyms++;
2701     }
2702
2703   (*pp)++;                      /* Skip the semicolon.  */
2704
2705   /* Now fill in the fields of the type-structure.  */
2706
2707   TYPE_LENGTH (type) = sizeof (int);
2708   TYPE_CODE (type) = TYPE_CODE_ENUM;
2709   TYPE_NFIELDS (type) = nsyms;
2710   TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
2711
2712   /* Find the symbols for the values and put them into the type.
2713      The symbols can be found in the symlist that we put them on
2714      to cause them to be defined.  osyms contains the old value
2715      of that symlist; everything up to there was defined by us.  */
2716
2717   for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
2718     {
2719       SYMBOL_TYPE (syms->symbol) = type;
2720       TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
2721       TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (syms->symbol);
2722       TYPE_FIELD_BITPOS (type, n) = 0;
2723       TYPE_FIELD_BITSIZE (type, n) = 0;
2724     }
2725
2726   return type;
2727 }
2728   
2729 static struct type *
2730 read_range_type (pp, typenums)
2731      char **pp;
2732      int typenums[2];
2733 {
2734   char *errp = *pp;
2735   int rangenums[2];
2736   int n1, n2, n3;
2737
2738   /* First comes a type we are a subrange of.
2739      In practice it is usually 0, 1 or the type being defined.  */
2740   read_type_number (pp, rangenums);
2741   n1 = rangenums[1];
2742
2743   /* A semicolon should now follow; skip it.  */
2744   if (**pp == ';')
2745     (*pp)++;
2746
2747   /* The remaining two operands are usually lower and upper bounds
2748      of the range.  But in some special cases they mean something else.  */
2749   n2 = read_number (pp, ';');
2750   n3 = read_number (pp, ';');
2751
2752   /* A type defined as a subrange of itself, with bounds both 0, is void.  */
2753   if (rangenums[0] == typenums[0] && rangenums[1] == typenums[1]
2754       && n2 == 0 && n3 == 0)
2755     return builtin_type_void;
2756
2757   /* If n3 is zero and n2 is not, we want a floating type,
2758      and n2 is the width in bytes.
2759
2760      Fortran programs appear to use this for complex types also,
2761      and they give no way to distinguish between double and single-complex!
2762      We don't have complex types, so we would lose on all fortran files!
2763      So return type `double' for all of those.  It won't work right
2764      for the complex values, but at least it makes the file loadable.  */
2765
2766   if (n3 == 0 && n2 > 0)
2767     {
2768       if (n2 == sizeof (float))
2769         return builtin_type_float;
2770       return builtin_type_double;
2771     }
2772
2773   /* If the upper bound is -1, it must really be an unsigned int.  */
2774
2775   else if (n2 == 0 && n3 == -1)
2776     {
2777       if (sizeof (int) == sizeof (long))
2778         return builtin_type_unsigned_int;
2779       else
2780         return builtin_type_unsigned_long;
2781     }
2782
2783   /* Detect unsigned subranges of int.  Int is normally 1.
2784      Note that `char' is usually given bounds of 0 to 127,
2785      and would therefore appear unsigned; but it is described
2786      as a subrange of itself, so we reject it here.  */
2787
2788   else if (n2 == 0 && n1 == 1)
2789     {
2790       /* an unsigned type */
2791       if (n3 == (1 << (8 * sizeof (int))) - 1)
2792         return builtin_type_unsigned_int;
2793       if (n3 == (1 << (8 * sizeof (short))) - 1)
2794         return builtin_type_unsigned_short;
2795       if (n3 == (1 << (8 * sizeof (char))) - 1)
2796         return builtin_type_unsigned_char;
2797     }
2798   else
2799     {
2800       /* a signed type */
2801       if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
2802         return builtin_type_int;
2803       if (n3 == (1 << (8 * sizeof (long) - 1)) - 1)
2804          return builtin_type_long;
2805       if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
2806         return builtin_type_short;
2807       if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
2808         return builtin_type_char;
2809     }
2810   error ("Invalid symbol data: range type spec %s at symtab pos %d.",
2811          errp - 1, symnum);
2812 }
2813
2814 /* Read a number from the string pointed to by *PP.
2815    The value of *PP is advanced over the number.
2816    If END is nonzero, the character that ends the
2817    number must match END, or an error happens;
2818    and that character is skipped if it does match.
2819    If END is zero, *PP is left pointing to that character.  */
2820
2821 static long
2822 read_number (pp, end)
2823      char **pp;
2824      int end;
2825 {
2826   register char *p = *pp;
2827   register long n = 0;
2828   register int c;
2829   int sign = 1;
2830
2831   /* Handle an optional leading minus sign.  */
2832
2833   if (*p == '-')
2834     {
2835       sign = -1;
2836       p++;
2837     }
2838
2839   /* Read the digits, as far as they go.  */
2840
2841   while ((c = *p++) >= '0' && c <= '9')
2842     {
2843       n *= 10;
2844       n += c - '0';
2845     }
2846   if (end)
2847     {
2848       if (c != end)
2849         error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
2850     }
2851   else
2852     --p;
2853
2854   *pp = p;
2855   return n * sign;
2856 }
2857
2858 /* Read in an argument list. This is a list of types. It is terminated with
2859    a ':', FYI. Return the list of types read in. */
2860 static struct type **
2861 read_args (pp, end)
2862      char **pp;
2863      int end;
2864 {
2865   struct type *types[1024], **rval; /* allow for fns of 1023 parameters */
2866   int n = 0;
2867
2868   while (**pp != end)
2869     {
2870       if (**pp != ',')
2871         error ("Invalid argument list: no ',', at symtab pos %d", symnum);
2872       *pp += 1;
2873       types[n++] = read_type (pp);
2874     }
2875   *pp += 1;                     /* get past `end' (the ':' character) */
2876
2877   if (n == 1)
2878     {
2879       rval = (struct type **) xmalloc (2 * sizeof (struct type *));
2880     }
2881   else
2882     {
2883       rval = (struct type **) xmalloc (n * sizeof (struct type *));
2884     }
2885   bcopy (types, rval, n * sizeof (struct type *));
2886   return rval;
2887 }
2888
2889 /* This function is really horrible, but to avoid it, there would need
2890    to be more filling in of forward references.  */
2891 int
2892 fill_in_vptr_fieldno (type)
2893      struct type *type;
2894 {
2895   if (TYPE_VPTR_FIELDNO (type) < 0)
2896     TYPE_VPTR_FIELDNO (type) = fill_in_vptr_fieldno (TYPE_BASECLASS (type));
2897   return TYPE_VPTR_FIELDNO (type);
2898 }
2899
2900 static
2901 initialize ()
2902 {
2903   symfile = 0;
2904
2905   add_com ("symbol-file", class_files, symbol_file_command,
2906            "Load symbol table (in dbx format) from executable file FILE.");
2907
2908   add_com ("add-file", class_files, add_file_command,
2909            "Load the symbols from FILE, assuming its codes is at TEXT_START.") ;
2910 }
2911
2912 END_FILE
2913
2914 #endif /* READ_DBX_FORMAT */