1 /* Read dbx symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
23 #ifdef READ_DBX_FORMAT
29 #include <sys/param.h>
33 #include "initialize.h"
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 ();
51 extern struct symtab *read_symsegs ();
52 extern void free_all_symtabs ();
54 /* Macro for number of symbol table entries (in usual a.out format).
55 Some machines override this definition. */
56 #ifndef NUMBER_OF_SYMBOLS
57 #define NUMBER_OF_SYMBOLS (hdr.a_syms / sizeof (struct nlist))
60 /* Macro for file-offset of symbol table (in usual a.out format). */
61 #ifndef SYMBOL_TABLE_OFFSET
62 #define SYMBOL_TABLE_OFFSET N_SYMOFF (hdr)
65 /* Macro for file-offset of string table (in usual a.out format). */
66 #ifndef STRING_TABLE_OFFSET
67 #define STRING_TABLE_OFFSET (N_SYMOFF (hdr) + hdr.a_syms)
70 /* Macro to store the length of the string table data in INTO. */
71 #ifndef READ_STRING_TABLE_SIZE
72 #define READ_STRING_TABLE_SIZE(INTO) \
73 { val = myread (desc, &INTO, sizeof INTO); \
74 if (val < 0) perror_with_name (name); }
77 /* Macro to declare variables to hold the file's header data. */
78 #ifndef DECLARE_FILE_HEADERS
79 #define DECLARE_FILE_HEADERS struct exec hdr
82 /* Macro to read the header data from descriptor DESC and validate it.
83 NAME is the file name, for error messages. */
84 #ifndef READ_FILE_HEADERS
85 #define READ_FILE_HEADERS(DESC, NAME) \
86 { val = myread (DESC, &hdr, sizeof hdr); \
87 if (val < 0) perror_with_name (NAME); \
89 error ("File \"%s\" not in executable format.", NAME); }
94 /* Chain of symtabs made from reading the file's symsegs.
95 These symtabs do not go into symtab_list themselves,
96 but the information is copied from them when appropriate
97 to make the symtabs that will exist permanently. */
99 static struct symtab *symseg_chain;
101 /* Symseg symbol table for the file whose data we are now processing.
102 It is one of those in symseg_chain. Or 0, for a compilation that
105 static struct symtab *current_symseg;
107 /* Name of source file whose symbol data we are now processing.
108 This comes from a symbol of type N_SO. */
110 static char *last_source_file;
112 /* Core address of start of text of current source file.
113 This too comes from the N_SO symbol. */
115 static CORE_ADDR last_source_start_addr;
117 /* End of the text segment of the executable file,
118 as found in the symbol _etext. */
120 static CORE_ADDR end_of_text_addr;
122 /* The list of sub-source-files within the current individual compilation.
123 Each file gets its own symtab with its own linetable and associated info,
124 but they all share one blockvector. */
128 struct subfile *next;
130 struct linetable *line_vector;
131 int line_vector_length;
132 int line_vector_index;
133 int prev_line_number;
136 static struct subfile *subfiles;
138 static struct subfile *current_subfile;
140 /* Count symbols as they are processed, for error messages. */
144 /* Vector of types defined so far, indexed by their dbx type numbers.
145 (In newer sun systems, dbx uses a pair of numbers in parens,
146 as in "(SUBFILENUM,NUMWITHINSUBFILE)". Then these numbers must be
147 translated through the type_translations hash table to get
148 the index into the type vector.) */
150 static struct typevector *type_vector;
152 /* Number of elements allocated for type_vector currently. */
154 static int type_vector_length;
156 /* Vector of line number information. */
158 static struct linetable *line_vector;
160 /* Index of next entry to go in line_vector_index. */
162 static int line_vector_index;
164 /* Last line number recorded in the line vector. */
166 static int prev_line_number;
168 /* Number of elements allocated for line_vector currently. */
170 static int line_vector_length;
172 /* Hash table of global symbols whose values are not known yet.
173 They are chained thru the SYMBOL_VALUE, since we don't
174 have the correct data for that slot yet. */
177 static struct symbol *global_sym_chain[HASHSIZE];
179 /* Record the symbols defined for each context in a list.
180 We don't create a struct block for the context until we
181 know how long to make it. */
183 #define PENDINGSIZE 100
187 struct pending *next;
189 struct symbol *symbol[PENDINGSIZE];
192 /* List of free `struct pending' structures for reuse. */
193 struct pending *free_pendings;
195 /* Here are the three lists that symbols are put on. */
197 struct pending *file_symbols; /* static at top level, and types */
199 struct pending *global_symbols; /* global functions and variables */
201 struct pending *local_symbols; /* everything local to lexical context */
203 /* Stack representing unclosed lexical contexts
204 (that will become blocks, eventually). */
208 struct pending *locals;
209 struct pending_block *old_blocks;
211 CORE_ADDR start_addr;
215 struct context_stack *context_stack;
217 /* Index of first unused entry in context stack. */
218 int context_stack_depth;
220 /* Currently allocated size of context stack. */
222 int context_stack_size;
224 /* Nonzero if within a function (so symbols should be local,
225 if nothing says specifically). */
229 /* List of blocks already made (lexical contexts already closed).
230 This is used at the end to make the blockvector. */
234 struct pending_block *next;
238 struct pending_block *pending_blocks;
240 extern CORE_ADDR first_object_file_end; /* From blockframe.c */
242 /* File name symbols were loaded from. */
244 static char *symfile;
255 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
256 (and add a null character at the end in the copy).
257 Returns the address of the copy. */
260 obsavestring (ptr, size)
264 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
265 /* Open-coded bcopy--saves function call time.
266 These strings are usually short. */
268 register char *p1 = ptr;
269 register char *p2 = p;
270 char *end = ptr + size;
278 /* Concatenate strings S1, S2 and S3; return the new string.
279 Space is found in the symbol_obstack. */
282 obconcat (s1, s2, s3)
285 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
286 register char *val = (char *) obstack_alloc (symbol_obstack, len);
293 /* Support for Sun changes to dbx symbol format */
295 /* For each identified header file, we have a table of types defined
298 header_files maps header file names to their type tables.
299 It is a vector of n_header_files elements.
300 Each element describes one header file.
301 It contains a vector of types.
303 Sometimes it can happen that the same header file produces
304 different results when included in different places.
305 This can result from conditionals or from different
306 things done before including the file.
307 When this happens, there are multiple entries for the file in this table,
308 one entry for each distinct set of results.
309 The entries are distinguished by the INSTANCE field.
310 The INSTANCE field appears in the N_BINCL and N_EXCL symbol table and is
311 used to match header-file references to their corresponding data. */
315 char *name; /* Name of header file */
316 int instance; /* Numeric code distinguishing instances
317 of one header file that produced
318 different results when included.
319 It comes from the N_BINCL or N_EXCL. */
320 struct type **vector; /* Pointer to vector of types */
321 int length; /* Allocated length (# elts) of that vector */
324 static struct header_file *header_files;
326 static int n_header_files;
328 static int n_allocated_header_files;
330 /* Within each object file, various header files are assigned numbers.
331 A type is defined or referred to with a pair of numbers
332 (FILENUM,TYPENUM) where FILENUM is the number of the header file
333 and TYPENUM is the number within that header file.
334 TYPENUM is the index within the vector of types for that header file.
336 FILENUM == 1 is special; it refers to the main source of the object file,
337 and not to any header file. FILENUM != 1 is interpreted by looking it up
338 in the following table, which contains indices in header_files. */
340 static int *this_object_header_files;
342 static int n_this_object_header_files;
344 static int n_allocated_this_object_header_files;
346 /* When a header file is getting special overriding definitions
347 for one source file, record here the header_files index
348 of its normal definition vector.
349 At other times, this is -1. */
351 static int header_file_prev_index;
353 /* At the start of reading dbx symbols, allocate our tables. */
358 n_allocated_header_files = 10;
359 header_files = (struct header_file *) xxmalloc (10 * sizeof (struct header_file));
362 n_allocated_this_object_header_files = 10;
363 this_object_header_files = (int *) xxmalloc (10 * sizeof (int));
366 /* At the end of reading dbx symbols, free our tables. */
372 for (i = 0; i < n_header_files; i++)
373 free (header_files[i].name);
375 free (this_object_header_files);
378 /* Called at the start of each object file's symbols.
379 Clear out the mapping of header file numbers to header files. */
382 new_object_header_files ()
384 /* Leave FILENUM of 0 free for builtin types and this file's types. */
385 n_this_object_header_files = 1;
386 header_file_prev_index = -1;
389 /* Add header file number I for this object file
390 at the next successive FILENUM. */
393 add_this_object_header_file (i)
396 if (n_this_object_header_files == n_allocated_this_object_header_files)
398 n_allocated_this_object_header_files *= 2;
399 this_object_header_files
400 = (int *) xrealloc (this_object_header_files,
401 n_allocated_this_object_header_files * sizeof (int));
404 this_object_header_files[n_this_object_header_files++] = i;
407 /* Add to this file an "old" header file, one already seen in
408 a previous object file. NAME is the header file's name.
409 INSTANCE is its instance code, to select among multiple
410 symbol tables for the same header file. */
413 add_old_header_file (name, instance)
417 register struct header_file *p = header_files;
420 for (i = 0; i < n_header_files; i++)
421 if (!strcmp (p[i].name, name) && instance == p[i].instance)
423 add_this_object_header_file (i);
426 error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
430 /* Add to this file a "new" header file: definitions for its types follow.
431 NAME is the header file's name.
432 Most often this happens only once for each distinct header file,
433 but not necessarily. If it happens more than once, INSTANCE has
434 a different value each time, and references to the header file
435 use INSTANCE values to select among them.
437 dbx output contains "begin" and "end" markers for each new header file,
438 but at this level we just need to know which files there have been;
439 so we record the file when its "begin" is seen and ignore the "end". */
442 add_new_header_file (name, instance)
447 register struct header_file *p = header_files;
448 header_file_prev_index = -1;
451 /* This code was used before I knew about the instance codes.
452 My first hypothesis is that it is not necessary now
453 that instance codes are handled. */
455 /* Has this header file a previous definition?
456 If so, make a new entry anyway so that this use in this source file
457 gets a separate entry. Later source files get the old entry.
458 Record here the index of the old entry, so that any type indices
459 not previously defined can get defined in the old entry as
460 well as in the new one. */
462 for (i = 0; i < n_header_files; i++)
463 if (!strcmp (p[i].name, name))
465 header_file_prev_index = i;
470 /* Make sure there is room for one more header file. */
472 if (n_header_files == n_allocated_header_files)
474 n_allocated_header_files *= 2;
476 = (struct header_file *) xrealloc (header_files, n_allocated_header_files * sizeof (struct header_file));
479 /* Create an entry for this header file. */
481 i = n_header_files++;
482 header_files[i].name = name;
483 header_files[i].instance = instance;
484 header_files[i].length = 10;
485 header_files[i].vector
486 = (struct type **) xxmalloc (10 * sizeof (struct type *));
487 bzero (header_files[i].vector, 10 * sizeof (struct type *));
489 add_this_object_header_file (i);
492 /* Look up a dbx type-number pair. Return the address of the slot
493 where the type for that number-pair is stored.
494 The number-pair is in TYPENUMS.
496 This can be used for finding the type associated with that pair
497 or for associating a new type with the pair. */
499 static struct type **
500 dbx_lookup_type (typenums)
503 register int filenum = typenums[0], index = typenums[1];
505 if (filenum < 0 || filenum >= n_this_object_header_files)
506 error ("Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
507 filenum, index, symnum);
511 /* Type is defined outside of header files.
512 Find it in this object file's type vector. */
513 if (index >= type_vector_length)
515 type_vector_length *= 2;
516 type_vector = (struct typevector *)
517 xrealloc (type_vector, sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
518 bzero (&type_vector->type[type_vector_length / 2],
519 type_vector_length * sizeof (struct type *) / 2);
521 return &type_vector->type[index];
525 register int real_filenum = this_object_header_files[filenum];
526 register struct header_file *f;
528 if (real_filenum >= n_header_files)
531 f = &header_files[real_filenum];
533 if (index >= f->length)
536 f->vector = (struct type **)
537 xrealloc (f->vector, f->length * sizeof (struct type *));
538 bzero (&f->vector[f->length / 2],
539 f->length * sizeof (struct type *) / 2);
541 return &f->vector[index];
545 /* Make sure there is a type allocated for type numbers TYPENUMS
546 and return the type object.
547 This can create an empty (zeroed) type object. */
550 dbx_alloc_type (typenums)
553 register struct type **type_addr = dbx_lookup_type (typenums);
554 register struct type *type = *type_addr;
556 /* If we are referring to a type not known at all yet,
557 allocate an empty type for it.
558 We will fill it in later if we find out how. */
561 type = (struct type *) obstack_alloc (symbol_obstack,
562 sizeof (struct type));
563 bzero (type, sizeof (struct type));
570 static struct type **
571 explicit_lookup_type (real_filenum, index)
572 int real_filenum, index;
574 register struct header_file *f = &header_files[real_filenum];
576 if (index >= f->length)
579 f->vector = (struct type **)
580 xrealloc (f->vector, f->length * sizeof (struct type *));
581 bzero (&f->vector[f->length / 2],
582 f->length * sizeof (struct type *) / 2);
584 return &f->vector[index];
588 /* maintain the lists of symbols and blocks */
590 /* Add a symbol to one of the lists of symbols. */
592 add_symbol_to_list (symbol, listhead)
593 struct symbol *symbol;
594 struct pending **listhead;
596 /* We keep PENDINGSIZE symbols in each link of the list.
597 If we don't have a link with room in it, add a new link. */
598 if (*listhead == 0 || (*listhead)->nsyms == PENDINGSIZE)
600 register struct pending *link;
603 link = free_pendings;
604 free_pendings = link->next;
607 link = (struct pending *) xxmalloc (sizeof (struct pending));
609 link->next = *listhead;
614 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
617 /* At end of reading syms, or in case of quit,
618 really free as many `struct pending's as we can easily find. */
621 really_free_pendings ()
623 struct pending *next, *next1;
624 struct pending_block *bnext, *bnext1;
626 for (next = free_pendings; next; next = next1)
633 for (bnext = pending_blocks; bnext; bnext = bnext1)
635 bnext1 = bnext->next;
640 for (next = file_symbols; next; next = next1)
645 for (next = global_symbols; next; next = next1)
652 /* Take one of the lists of symbols and make a block from it.
653 Keep the order the symbols have in the list (reversed from the input file).
654 Put the block on the list of pending blocks. */
657 finish_block (symbol, listhead, old_blocks, start, end)
658 struct symbol *symbol;
659 struct pending **listhead;
660 struct pending_block *old_blocks;
661 CORE_ADDR start, end;
663 register struct pending *next, *next1;
664 register struct block *block;
665 register struct pending_block *pblock;
666 struct pending_block *opblock;
669 /* Count the length of the list of symbols. */
671 for (next = *listhead, i = 0; next; i += next->nsyms, next = next->next);
673 block = (struct block *) obstack_alloc (symbol_obstack,
674 sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
676 /* Copy the symbols into the block. */
678 BLOCK_NSYMS (block) = i;
679 for (next = *listhead; next; next = next->next)
682 for (j = next->nsyms - 1; j >= 0; j--)
683 BLOCK_SYM (block, --i) = next->symbol[j];
686 BLOCK_START (block) = start;
687 BLOCK_END (block) = end;
688 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
690 /* Put the block in as the value of the symbol that names it. */
694 SYMBOL_BLOCK_VALUE (symbol) = block;
695 BLOCK_FUNCTION (block) = symbol;
698 BLOCK_FUNCTION (block) = 0;
700 /* Now "free" the links of the list, and empty the list. */
702 for (next = *listhead; next; next = next1)
705 next->next = free_pendings;
706 free_pendings = next;
710 /* Install this block as the superblock
711 of all blocks made since the start of this scope
712 that don't have superblocks yet. */
715 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
717 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
718 BLOCK_SUPERBLOCK (pblock->block) = block;
722 /* Record this block on the list of all blocks in the file.
723 Put it after opblock, or at the beginning if opblock is 0.
724 This puts the block in the list after all its subblocks. */
726 /* Allocate in the symbol_obstack to save time.
727 It wastes a little space. */
728 pblock = (struct pending_block *) obstack_alloc (symbol_obstack,
729 sizeof (struct pending_block));
730 pblock->block = block;
733 pblock->next = opblock->next;
734 opblock->next = pblock;
738 pblock->next = pending_blocks;
739 pending_blocks = pblock;
743 static struct blockvector *
746 register struct pending_block *next, *next1;
747 register struct blockvector *blockvector;
750 /* Count the length of the list of blocks. */
752 for (next = pending_blocks, i = 0; next; next = next->next, i++);
754 blockvector = (struct blockvector *) obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
756 /* Copy the blocks into the blockvector.
757 This is done in reverse order, which happens to put
758 the blocks into the proper order (ascending starting address).
759 finish_block has hair to insert each block into the list
760 after its subblocks in order to make sure this is true. */
762 BLOCKVECTOR_NBLOCKS (blockvector) = i;
763 for (next = pending_blocks; next; next = next->next)
764 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
766 #if 0 /* Now we make the links in the obstack, so don't free them. */
767 /* Now free the links of the list, and empty the list. */
769 for (next = pending_blocks; next; next = next1)
780 /* Manage the vector of line numbers. */
783 record_line (line, pc)
787 /* Ignore the dummy line number in libg.o */
792 /* Make sure line vector is big enough. */
794 if (line_vector_index + 1 >= line_vector_length)
796 line_vector_length *= 2;
797 line_vector = (struct linetable *)
798 xrealloc (line_vector,
799 sizeof (struct linetable) + line_vector_length * sizeof (int));
800 current_subfile->line_vector = line_vector;
803 /* If this line is not continguous with previous one recorded,
804 record a line-number entry for it. */
805 if (line != prev_line_number + 1)
806 line_vector->item[line_vector_index++] = - line;
807 prev_line_number = line;
809 /* Record the core address of the line. */
810 line_vector->item[line_vector_index++] = pc;
813 /* Start a new symtab for a new source file.
814 This is called when a dbx symbol of type N_SO is seen;
815 it indicates the start of data for one original source file. */
818 start_symtab (name, start_addr)
820 CORE_ADDR start_addr;
822 register struct symtab *s;
824 last_source_file = name;
825 last_source_start_addr = start_addr;
830 /* Context stack is initially empty, with room for 10 levels. */
832 = (struct context_stack *) xxmalloc (10 * sizeof (struct context_stack));
833 context_stack_size = 10;
834 context_stack_depth = 0;
836 new_object_header_files ();
838 for (s = symseg_chain; s; s = s->next)
839 if (s->ldsymoff == symnum * sizeof (struct nlist))
845 type_vector_length = 160;
846 type_vector = (struct typevector *) xxmalloc (sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
847 bzero (type_vector->type, type_vector_length * sizeof (struct type *));
849 /* Initialize the list of sub source files with one entry
850 for this file (the top-level source file). */
854 start_subfile (name);
857 /* Handle an N_SOL symbol, which indicates the start of
858 code that came from an included (or otherwise merged-in)
859 source file with a different name. */
865 register struct subfile *subfile;
867 /* Save the current subfile's line vector data. */
871 current_subfile->line_vector_index = line_vector_index;
872 current_subfile->line_vector_length = line_vector_length;
873 current_subfile->prev_line_number = prev_line_number;
876 /* See if this subfile is already known as a subfile of the
877 current main source file. */
879 for (subfile = subfiles; subfile; subfile = subfile->next)
881 if (!strcmp (subfile->name, name))
883 line_vector = subfile->line_vector;
884 line_vector_index = subfile->line_vector_index;
885 line_vector_length = subfile->line_vector_length;
886 prev_line_number = subfile->prev_line_number;
887 current_subfile = subfile;
892 /* This subfile is not known. Add an entry for it. */
894 line_vector_index = 0;
895 line_vector_length = 1000;
896 prev_line_number = -2; /* Force first line number to be explicit */
897 line_vector = (struct linetable *)
898 xxmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
900 /* Make an entry for this subfile in the list of all subfiles
901 of the current main source file. */
903 subfile = (struct subfile *) xxmalloc (sizeof (struct subfile));
904 subfile->next = subfiles;
905 subfile->name = savestring (name, strlen (name));
906 subfile->line_vector = line_vector;
908 current_subfile = subfile;
911 /* Finish the symbol definitions for one main source file,
912 close off all the lexical contexts for that file
913 (creating struct block's for them), then make the struct symtab
914 for that file and put it in the list of all such.
916 END_ADDR is the address of the end of the file's text. */
919 end_symtab (end_addr)
922 register struct symtab *symtab;
923 register struct blockvector *blockvector;
924 register struct subfile *subfile;
925 register struct linetable *lv;
926 struct subfile *nextsub;
928 if (current_symseg != 0)
930 last_source_file = 0;
935 /* Finish the lexical context of the last function in the file;
936 pop the context stack. */
938 if (context_stack_depth > 0)
940 register struct context_stack *cstk;
941 context_stack_depth--;
942 cstk = &context_stack[context_stack_depth];
943 /* Make a block for the local symbols within. */
944 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
945 cstk->start_addr, end_addr);
948 /* Finish defining all the blocks of this symtab. */
949 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr);
950 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr);
951 blockvector = make_blockvector ();
953 current_subfile->line_vector_index = line_vector_index;
955 /* Now create the symtab objects proper, one for each subfile. */
956 /* (The main file is one of them.) */
958 for (subfile = subfiles; subfile; subfile = nextsub)
960 symtab = (struct symtab *) xxmalloc (sizeof (struct symtab));
961 symtab->free_ptr = 0;
963 /* Fill in its components. */
964 symtab->blockvector = blockvector;
965 type_vector->length = type_vector_length;
966 symtab->typevector = type_vector;
967 symtab->free_code = free_linetable;
968 if (subfile->next == 0)
969 symtab->free_ptr = (char *) type_vector;
971 symtab->filename = subfile->name;
972 lv = subfile->line_vector;
973 lv->nitems = subfile->line_vector_index;
974 symtab->linetable = (struct linetable *)
975 xrealloc (lv, sizeof (struct linetable) + lv->nitems * sizeof (int));
977 symtab->line_charpos = 0;
979 /* Link the new symtab into the list of such. */
980 symtab->next = symtab_list;
981 symtab_list = symtab;
983 nextsub = subfile->next;
988 type_vector_length = -1;
990 line_vector_length = -1;
991 last_source_file = 0;
996 /* Handle the N_BINCL and N_EINCL symbol types
997 that act like N_SOL for switching source files
998 (different subfiles, as we call them) within one object file,
999 but using a stack rather than in an arbitrary order. */
1001 struct subfile_stack
1003 struct subfile_stack *next;
1008 struct subfile_stack *subfile_stack;
1013 register struct subfile_stack *tem
1014 = (struct subfile_stack *) xxmalloc (sizeof (struct subfile_stack));
1016 tem->next = subfile_stack;
1017 subfile_stack = tem;
1018 if (current_subfile == 0 || current_subfile->name == 0)
1020 tem->name = current_subfile->name;
1021 tem->prev_index = header_file_prev_index;
1027 register char *name;
1028 register struct subfile_stack *link = subfile_stack;
1034 subfile_stack = link->next;
1035 header_file_prev_index = link->prev_index;
1040 #endif /* Have N_BINCL */
1042 /* Accumulate the misc functions in bunches of 127.
1043 At the end, copy them all into one newly allocated structure. */
1045 #define MISC_BUNCH_SIZE 127
1049 struct misc_bunch *next;
1050 struct misc_function contents[MISC_BUNCH_SIZE];
1053 /* Bunch currently being filled up.
1054 The next field points to chain of filled bunches. */
1056 static struct misc_bunch *misc_bunch;
1058 /* Number of slots filled in current bunch. */
1060 static int misc_bunch_index;
1062 /* Total number of misc functions recorded so far. */
1064 static int misc_count;
1067 init_misc_functions ()
1071 misc_bunch_index = MISC_BUNCH_SIZE;
1075 record_misc_function (name, address)
1079 register struct misc_bunch *new;
1081 if (misc_bunch_index == MISC_BUNCH_SIZE)
1083 new = (struct misc_bunch *) xxmalloc (sizeof (struct misc_bunch));
1084 misc_bunch_index = 0;
1085 new->next = misc_bunch;
1088 misc_bunch->contents[misc_bunch_index].name = name;
1089 misc_bunch->contents[misc_bunch_index].address = address;
1095 compare_misc_functions (fn1, fn2)
1096 struct misc_function *fn1, *fn2;
1098 /* Return a signed result based on unsigned comparisons
1099 so that we sort into unsigned numeric order. */
1100 if (fn1->address < fn2->address)
1102 if (fn1->address > fn2->address)
1108 discard_misc_bunches ()
1110 register struct misc_bunch *next;
1114 next = misc_bunch->next;
1121 condense_misc_bunches ()
1124 register struct misc_bunch *bunch;
1125 #ifdef NAMES_HAVE_UNDERSCORE
1131 misc_function_vector
1132 = (struct misc_function *)
1133 xxmalloc (misc_count * sizeof (struct misc_function));
1139 for (i = 0; i < misc_bunch_index; i++)
1141 misc_function_vector[j] = bunch->contents[i];
1142 misc_function_vector[j].name
1143 = obconcat (misc_function_vector[j].name
1144 + (misc_function_vector[j].name[0] == '_' ? offset : 0),
1148 bunch = bunch->next;
1149 misc_bunch_index = MISC_BUNCH_SIZE;
1152 misc_function_count = j;
1154 /* Sort the misc functions by address. */
1156 qsort (misc_function_vector, j, sizeof (struct misc_function),
1157 compare_misc_functions);
1160 /* Call sort_syms to sort alphabetically
1161 the symbols of each block of each symtab. */
1164 compare_symbols (s1, s2)
1165 struct symbol **s1, **s2;
1167 register int namediff;
1169 /* Compare the initial characters. */
1170 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
1171 if (namediff != 0) return namediff;
1173 /* If they match, compare the rest of the names. */
1174 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
1175 if (namediff != 0) return namediff;
1177 /* For symbols of the same name, registers should come first. */
1178 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
1179 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
1185 register struct symtab *s;
1187 register struct blockvector *bv;
1188 register struct block *b;
1190 for (s = symtab_list; s; s = s->next)
1192 bv = BLOCKVECTOR (s);
1193 nbl = BLOCKVECTOR_NBLOCKS (bv);
1194 for (i = 0; i < nbl; i++)
1196 b = BLOCKVECTOR_BLOCK (bv, i);
1197 if (BLOCK_SHOULD_SORT (b))
1198 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
1199 sizeof (struct symbol *), compare_symbols);
1202 int lastindex = BLOCK_NSYMS (b) - 1;
1204 for (j = (lastindex - 1) / 2; j >= 0; j--)
1206 register struct symbol *sym;
1207 sym = BLOCK_SYM (b, j);
1208 BLOCK_SYM (b, j) = BLOCK_SYM (b, lastindex - j);
1209 BLOCK_SYM (b, lastindex - j) = sym;
1216 /* This is the symbol-file command. Read the file, analyze its symbols,
1217 and add a struct symtab to symtab_list. */
1220 symbol_file_command (name)
1224 DECLARE_FILE_HEADERS;
1225 struct nlist *nlist;
1229 extern void close ();
1230 struct cleanup *old_chain;
1231 struct symtab *symseg;
1232 struct stat statbuf;
1238 if (symtab_list && !query ("Discard symbol table? ", 0))
1239 error ("Not confirmed.");
1240 free_all_symtabs ();
1244 if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
1245 error ("Not confirmed.");
1248 char *absolute_name;
1249 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
1251 perror_with_name (name);
1253 name = absolute_name;
1256 old_chain = make_cleanup (close, desc);
1257 make_cleanup (free_current_contents, &name);
1259 READ_FILE_HEADERS (desc, name);
1261 if (NUMBER_OF_SYMBOLS == 0)
1266 free_all_symtabs ();
1267 printf ("%s has no symbol-table; symbols discarded.\n", name);
1272 printf ("Reading symbol data from %s...", name);
1275 /* Now read the string table, all at once. */
1276 val = lseek (desc, STRING_TABLE_OFFSET, 0);
1278 perror_with_name (name);
1279 stat (name, &statbuf);
1280 READ_STRING_TABLE_SIZE (buffer);
1281 if (buffer >= 0 && buffer < statbuf.st_size)
1282 stringtab = (char *) alloca (buffer);
1285 if (stringtab == NULL)
1286 error ("ridiculous string table size: %d bytes", name, buffer);
1288 bcopy (&buffer, stringtab, sizeof buffer);
1289 val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
1291 perror_with_name (name);
1293 /* Throw away the old symbol table. */
1298 free_all_symtabs ();
1300 /* Empty the hash table of global syms looking for values. */
1301 bzero (global_sym_chain, sizeof global_sym_chain);
1303 #ifdef READ_GDB_SYMSEGS
1304 /* That puts us at the symsegs. Read them. */
1305 symseg_chain = read_symsegs (desc, name);
1308 /* Free the symtabs made by read_symsegs, but not their contents,
1309 which have been copied into symtabs on symtab_list. */
1310 for (symseg = symseg_chain; symseg; symseg = symseg->next)
1313 struct sourcevector *sv = (struct sourcevector *) symseg->linetable;
1315 for (i = 0; i < sv->length; i++)
1318 struct source *source = sv->source[i];
1320 = (struct symtab *) xxmalloc (sizeof (struct symtab));
1322 bcopy (symseg, sp1, sizeof (struct symtab));
1323 sp1->filename = savestring (source->name, strlen (source->name));
1324 sp1->linetable = &source->contents;
1325 sp1->free_code = free_nothing;
1326 sp1->free_ptr = (i == 0) ? (char *) symseg : 0;
1328 sp1->next = symtab_list;
1333 /* Where people are using the 4.2 ld program, must not check for
1334 symsegs, because that ld puts randonm garbage at the end of
1335 the output file and that would trigger an error message. */
1339 /* Position to read the symbol table. Do not read it all at once. */
1340 val = lseek (desc, SYMBOL_TABLE_OFFSET, 0);
1342 perror_with_name (name);
1344 init_misc_functions ();
1345 make_cleanup (discard_misc_bunches, 0);
1346 init_header_files ();
1347 make_cleanup (free_header_files, 0);
1352 make_cleanup (really_free_pendings, 0);
1354 /* Now that the symbol table data of the executable file are all in core,
1355 process them and define symbols accordingly. Closes desc. */
1357 read_dbx_symtab (desc, stringtab, NUMBER_OF_SYMBOLS);
1360 /* Sort symbols alphabetically within each block. */
1364 /* Go over the misc functions and install them in vector. */
1366 condense_misc_bunches ();
1368 /* Don't allow char * to have a typename (else would get caddr_t.) */
1370 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
1372 /* Make a default for file to list. */
1374 select_source_symtab (symtab_list);
1376 symfile = savestring (name, strlen (name));
1378 do_cleanups (old_chain);
1380 /* Free the symtabs made by read_symsegs, but not their contents,
1381 which have been copied into symtabs on symtab_list. */
1382 while (symseg_chain)
1384 register struct symtab *s = symseg_chain->next;
1385 free (symseg_chain);
1393 /* Return name of file symbols were loaded from, or 0 if none.. */
1401 /* Buffer for reading the symbol table entries. */
1402 static struct nlist symbuf[2048];
1403 static int symbuf_idx;
1404 static int symbuf_end;
1406 /* I/O descriptor for reading the symbol table. */
1407 static int symtab_input_desc;
1409 /* The address of the string table
1410 of the object file we are reading (as copied into core). */
1411 static char *stringtab_global;
1413 /* Refill the symbol table input buffer
1414 and set the variables that control fetching entries from it.
1415 Reports an error if no data available.
1416 This function can read past the end of the symbol table
1417 (into the string table) but this does no harm. */
1422 int nbytes = myread (symtab_input_desc, symbuf, sizeof (symbuf));
1424 error ("error or end of file reading symbol table");
1425 symbuf_end = nbytes / sizeof (struct nlist);
1430 /* dbx allows the text of a symbol name to be continued into the
1431 next symbol name! When such a continuation is encountered
1432 (a \ at the end of the text of a name)
1433 call this function to get the continuation. */
1438 if (symbuf_idx == symbuf_end)
1441 return symbuf[symbuf_idx++].n_un.n_strx + stringtab_global;
1444 /* Given pointers to a a.out symbol table in core containing dbx style data,
1445 analyze them and create struct symtab's describing the symbols.
1446 NLISTLEN is the number of symbols in the symbol table.
1447 We read them one at a time using stdio.
1448 All symbol names are given as offsets relative to STRINGTAB. */
1451 read_dbx_symtab (desc, stringtab, nlistlen)
1453 register char *stringtab;
1454 register int nlistlen;
1456 register char *namestring;
1457 register struct symbol *sym, *prev;
1459 int num_object_files = 0;
1460 struct cleanup *old_chain;
1466 old_chain = make_cleanup (free_all_symtabs, 0);
1467 stringtab_global = stringtab;
1468 last_source_file = 0;
1470 #ifdef END_OF_TEXT_DEFAULT
1471 end_of_text_addr = END_OF_TEXT_DEFAULT;
1474 symtab_input_desc = desc;
1475 symbuf_end = symbuf_idx = 0;
1477 for (symnum = 0; symnum < nlistlen; symnum++)
1482 QUIT; /* allow this to be interruptable */
1483 if (symbuf_idx == symbuf_end)
1485 bufp = &symbuf[symbuf_idx++];
1486 type = bufp->n_type;
1487 namestring = bufp->n_un.n_strx ? bufp->n_un.n_strx + stringtab : "";
1490 process_one_symbol (type, bufp->n_desc,
1491 bufp->n_value, namestring);
1492 /* A static text symbol whose name ends in ".o"
1493 or begins with "-l" means the start of another object file.
1494 So end the symtab of the source file we have been processing.
1495 This is how we avoid counting the libraries as part
1496 or the last source file.
1497 Also this way we find end of first object file (crt0). */
1504 && (!strcmp (namestring + strlen (namestring) - 2, ".o")
1505 || !strncmp (namestring, "-l", 2)))
1507 if (num_object_files++ == 1)
1508 first_object_file_end = bufp->n_value;
1509 if (last_source_file)
1510 end_symtab (bufp->n_value);
1512 else if (type & N_EXT || type == N_TEXT
1520 /* Record the location of _etext. */
1521 if (type == (N_TEXT | N_EXT)
1522 && !strcmp (namestring, "_etext"))
1523 end_of_text_addr = bufp->n_value;
1525 /* Global symbol: see if we came across a dbx definition
1526 for a corresponding symbol. If so, store the value.
1527 Remove syms from the chain when their values are stored,
1528 but search the whole chain, as there may be several syms
1529 from different files with the same name. */
1533 #ifdef NAMES_HAVE_UNDERSCORE
1534 hash = hashname (namestring + 1);
1535 #else /* not NAMES_HAVE_UNDERSCORE */
1536 hash = hashname (namestring);
1537 #endif /* not NAMES_HAVE_UNDERSCORE */
1538 for (sym = global_sym_chain[hash];
1542 #ifdef NAMES_HAVE_UNDERSCORE
1544 && namestring[1] == SYMBOL_NAME (sym)[0]
1546 !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
1547 #else /* NAMES_HAVE_UNDERSCORE */
1548 namestring[0] == SYMBOL_NAME (sym)[0]
1550 !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
1551 #endif /* NAMES_HAVE_UNDERSCORE */
1555 SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1557 global_sym_chain[hash]
1558 = (struct symbol *) SYMBOL_VALUE (sym);
1559 SYMBOL_VALUE (sym) = bufp->n_value;
1561 sym = (struct symbol *) SYMBOL_VALUE (prev);
1563 sym = global_sym_chain[hash];
1570 sym = (struct symbol *) SYMBOL_VALUE (sym);
1575 /* Defined global or text symbol: record as a misc function
1576 if it didn't give its address to a debugger symbol above. */
1577 if (type <= (N_TYPE | N_EXT)
1580 record_misc_function (namestring, bufp->n_value);
1584 if (last_source_file)
1585 end_symtab (end_of_text_addr);
1587 discard_cleanups (old_chain);
1594 register char *p = name;
1595 register int total = p[0];
1608 /* Ensure result is positive. */
1609 if (total < 0) total += (1000 << 6);
1610 return total % HASHSIZE;
1613 /* Put all appropriate global symbols in the symseg data
1614 onto the hash chains so that their addresses will be stored
1615 when seen later in loader global symbols. */
1620 /* Look at each symbol in each block in each symseg symtab. */
1622 for (s = symseg_chain; s; s = s->next)
1625 for (n = BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)) - 1; n >= 0; n--)
1627 register struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), n);
1629 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1631 register struct symbol *sym = BLOCK_SYM (b, i);
1633 /* Put the symbol on a chain if its value is an address
1634 that is figured out by the loader. */
1636 if (SYMBOL_CLASS (sym) == LOC_EXTERNAL)
1638 register int hash = hashname (SYMBOL_NAME (sym));
1639 SYMBOL_VALUE (sym) = (int) global_sym_chain[hash];
1640 global_sym_chain[hash] = sym;
1641 SYMBOL_CLASS (sym) = LOC_STATIC;
1649 process_one_symbol (type, desc, value, name)
1654 register struct context_stack *new;
1656 /* Something is wrong if we see real data before
1657 seeing a source file name. */
1659 if (last_source_file == 0 && type != N_SO)
1662 /* This code is used on Ultrix; ignore this sym. */
1663 if (type == N_NSYMS)
1667 if (type == N_ENTRY)
1668 /* This code appears in libraries on Gould machines. */
1670 error ("Invalid symbol data: does not start by identifying a source file.");
1677 /* Either of these types of symbols indicates the start of
1678 a new function. We must process its "name" normally for dbx,
1679 but also record the start of a new lexical context, and possibly
1680 also the end of the lexical context for the previous function. */
1682 within_function = 1;
1683 if (context_stack_depth > 0)
1685 new = &context_stack[--context_stack_depth];
1686 /* Make a block for the local symbols within. */
1687 finish_block (new->name, &local_symbols, new->old_blocks,
1688 new->start_addr, value);
1690 /* Stack must be empty now. */
1691 if (context_stack_depth != 0)
1692 error ("Invalid symbol data: unmatched N_LBRAC before symtab pos %d.",
1695 new = &context_stack[context_stack_depth++];
1696 new->old_blocks = pending_blocks;
1697 new->start_addr = value;
1698 new->name = define_symbol (value, name, desc);
1703 /* This "symbol" just indicates the start of an inner lexical
1704 context within a function. */
1706 if (context_stack_depth == context_stack_size)
1708 context_stack_size *= 2;
1710 = (struct context_stack *) xrealloc (context_stack,
1712 * sizeof (struct context_stack));
1715 new = &context_stack[context_stack_depth++];
1717 new->locals = local_symbols;
1718 new->old_blocks = pending_blocks;
1719 new->start_addr = value;
1725 /* This "symbol" just indicates the end of an inner lexical
1726 context that was started with N_RBRAC. */
1727 new = &context_stack[--context_stack_depth];
1728 if (desc != new->depth)
1729 error ("Invalid symbol data: N_LBRAC/N_RBRAC symbol mismatch, symtab pos %d.", symnum);
1730 local_symbols = new->locals;
1732 /* If this is not the outermost LBRAC...RBRAC pair in the
1733 function, its local symbols preceded it, and are the ones
1734 just recovered from the context stack. Defined the block for them.
1736 If this is the outermost LBRAC...RBRAC pair, there is no
1737 need to do anything; leave the symbols that preceded it
1738 to be attached to the function's own block. */
1739 if (local_symbols && context_stack_depth > 1)
1741 /* Muzzle a compiler bug that makes end > start. */
1742 if (new->start_addr > value)
1743 new->start_addr = value;
1744 /* Make a block for the local symbols within. */
1745 finish_block (0, &local_symbols, new->old_blocks,
1746 new->start_addr + last_source_start_addr,
1747 value + last_source_start_addr);
1752 /* This kind of symbol supposedly indicates the start
1753 of an object file. In fact this type does not appear. */
1757 /* This type of symbol indicates the start of data
1758 for one source file.
1759 Finish the symbol table of the previous source file
1760 (if any) and start accumulating a new symbol table. */
1761 if (last_source_file)
1763 start_symtab (name, value);
1767 /* This type of symbol indicates the start of data for
1768 a sub-source-file, one whose contents were copied or
1769 included in the compilation of the main source file
1770 (whose name was given in the N_SO symbol.) */
1771 start_subfile (name);
1777 add_new_header_file (name, value);
1778 start_subfile (name);
1782 start_subfile (pop_subfile ());
1786 add_old_header_file (name, value);
1788 #endif /* have N_BINCL */
1791 /* This type of "symbol" really just records
1792 one line-number -- core-address correspondence.
1793 Enter it in the line list for this symbol table. */
1794 record_line (desc, value);
1805 define_symbol (value, name, desc);
1809 static struct symbol *
1810 define_symbol (value, string, desc)
1815 register struct symbol *sym
1816 = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1817 char *p = (char *) index (string, ':');
1821 /* Ignore syms with empty names. */
1826 = (char *) obstack_alloc (symbol_obstack, ((p - string) + 1));
1827 /* Open-coded bcopy--saves function call time. */
1829 register char *p1 = string;
1830 register char *p2 = SYMBOL_NAME (sym);
1836 /* Determine the type of name being defined. */
1837 if ((*p >= '0' && *p <= '9') || *p == '(')
1842 /* c is a special case, not followed by a type-number.
1843 SYMBOL:c=iVALUE for an integer constant symbol.
1844 SYMBOL:c=rVALUE for a floating constant symbol. */
1848 error ("Invalid symbol data at symtab pos %d.", symnum);
1853 double d = atof (p);
1856 SYMBOL_TYPE (sym) = builtin_type_double;
1857 value = (char *) obstack_alloc (symbol_obstack, sizeof (double));
1858 bcopy (&d, value, sizeof (double));
1859 SYMBOL_VALUE_BYTES (sym) = value;
1860 SYMBOL_CLASS (sym) = LOC_CONST;
1865 SYMBOL_TYPE (sym) = builtin_type_int;
1866 SYMBOL_VALUE (sym) = atoi (p);
1867 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1871 error ("Invalid symbol data at symtab pos %d.", symnum);
1873 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1874 add_symbol_to_list (sym, &file_symbols);
1878 /* Now usually comes a number that says which data type,
1879 and possibly more stuff to define the type
1880 (all of which is handled by read_type) */
1882 if (deftype == 'p' && *p == 'F')
1883 /* pF is a two-letter code that means a function parameter in Fortran.
1884 The type-number specifies the type of the return value.
1885 Translate it into a pointer-to-function type. */
1889 = lookup_pointer_type (lookup_function_type (read_type (&p)));
1893 struct type *type = read_type (&p);
1895 if ((deftype == 'F' || deftype == 'f')
1896 && TYPE_CODE (type) != TYPE_CODE_FUNC)
1897 SYMBOL_TYPE (sym) = lookup_function_type (type);
1899 SYMBOL_TYPE (sym) = type;
1905 SYMBOL_CLASS (sym) = LOC_BLOCK;
1906 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1907 add_symbol_to_list (sym, &file_symbols);
1911 SYMBOL_CLASS (sym) = LOC_BLOCK;
1912 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1913 add_symbol_to_list (sym, &global_symbols);
1917 /* For a class G (global) symbol, it appears that the
1918 value is not correct. It is necessary to search for the
1919 corresponding linker definition to find the value.
1920 These definitions appear at the end of the namelist. */
1921 i = hashname (SYMBOL_NAME (sym));
1922 SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
1923 global_sym_chain[i] = sym;
1924 SYMBOL_CLASS (sym) = LOC_STATIC;
1925 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1926 add_symbol_to_list (sym, &global_symbols);
1929 /* This case is faked by a conditional above,
1930 when there is no code letter in the dbx data.
1931 Dbx data never actually contains 'l'. */
1933 SYMBOL_CLASS (sym) = LOC_LOCAL;
1934 SYMBOL_VALUE (sym) = value;
1935 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1936 add_symbol_to_list (sym, &local_symbols);
1940 SYMBOL_CLASS (sym) = LOC_ARG;
1941 SYMBOL_VALUE (sym) = value;
1942 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1943 add_symbol_to_list (sym, &local_symbols);
1944 /* DESC == 0 implies compiled with GCC.
1945 In this case, if it says `short', believe it. */
1948 /* If PCC says a parameter is a short or a char,
1949 it is really an int. */
1950 if (SYMBOL_TYPE (sym) == builtin_type_char
1951 || SYMBOL_TYPE (sym) == builtin_type_short)
1952 SYMBOL_TYPE (sym) = builtin_type_int;
1953 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1954 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1955 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1959 SYMBOL_CLASS (sym) = LOC_REGISTER;
1960 SYMBOL_VALUE (sym) = value;
1961 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1962 add_symbol_to_list (sym, &local_symbols);
1966 /* Static symbol at top level of file */
1967 SYMBOL_CLASS (sym) = LOC_STATIC;
1968 SYMBOL_VALUE (sym) = value;
1969 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1970 add_symbol_to_list (sym, &file_symbols);
1974 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1975 SYMBOL_VALUE (sym) = value;
1976 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1977 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1978 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1979 TYPE_NAME (SYMBOL_TYPE (sym)) =
1980 obsavestring (SYMBOL_NAME (sym),
1981 strlen (SYMBOL_NAME (sym)));
1982 add_symbol_to_list (sym, &file_symbols);
1986 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1987 SYMBOL_VALUE (sym) = value;
1988 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1989 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1990 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1991 TYPE_NAME (SYMBOL_TYPE (sym))
1993 (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
1995 : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1996 ? "struct " : "union ")),
1998 add_symbol_to_list (sym, &file_symbols);
2003 /* Static symbol of local scope */
2004 SYMBOL_CLASS (sym) = LOC_STATIC;
2005 SYMBOL_VALUE (sym) = value;
2006 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2007 add_symbol_to_list (sym, &local_symbols);
2011 error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
2016 /* Read a number by which a type is referred to in dbx data,
2017 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
2018 Just a single number N is equivalent to (0,N).
2019 Return the two numbers by storing them in the vector TYPENUMS.
2020 TYPENUMS will then be used as an argument to dbx_lookup_type. */
2023 read_type_number (pp, typenums)
2025 register int *typenums;
2030 typenums[0] = read_number (pp, ',');
2031 typenums[1] = read_number (pp, ')');
2036 typenums[1] = read_number (pp, 0);
2040 /* Read a dbx type reference or definition;
2041 return the type that is meant.
2042 This can be just a number, in which case it references
2043 a type already defined and placed in type_vector.
2044 Or the number can be followed by an =, in which case
2045 it means to define a new type according to the text that
2053 register struct type *type = 0;
2059 read_type_number (pp, typenums);
2061 /* Detect random reference to type not yet defined.
2062 Allocate a type object but leave it zeroed. */
2064 return dbx_alloc_type (typenums);
2070 type = dbx_alloc_type (typenums);
2071 /* Set the type code according to the following letter. */
2075 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2078 TYPE_CODE (type) = TYPE_CODE_UNION;
2081 TYPE_CODE (type) = TYPE_CODE_ENUM;
2084 /* Skip the name the cross-ref points to. */
2085 *pp = (char *) index (*pp, ',');
2086 /* Just allocate the type and leave it zero if nothing known */
2087 return dbx_alloc_type (typenums);
2101 read_type_number (pp, xtypenums);
2102 type = *dbx_lookup_type (xtypenums);
2104 type = builtin_type_void;
2105 *dbx_lookup_type (typenums) = type;
2109 type = dbx_alloc_type (typenums);
2110 smash_to_pointer_type (type, read_type (pp));
2114 type = dbx_alloc_type (typenums);
2115 smash_to_function_type (type, read_type (pp));
2119 type = read_range_type (pp, typenums);
2120 *dbx_lookup_type (typenums) = type;
2124 type = dbx_alloc_type (typenums);
2125 type = read_enum_type (pp, type);
2126 *dbx_lookup_type (typenums) = type;
2130 type = dbx_alloc_type (typenums);
2131 type = read_struct_type (pp, type);
2135 type = dbx_alloc_type (typenums);
2136 type = read_struct_type (pp, type);
2137 TYPE_CODE (type) = TYPE_CODE_UNION;
2141 /* Define an array type. */
2142 type = dbx_alloc_type (typenums);
2144 /* dbx expresses array types in terms of a range type for the index,
2145 and that range type is specified right inside the array type spec
2146 making ar1;MIN;MAX;VALTYPE */
2147 if (!strncmp (*pp, "r1;0;", 5))
2149 else if (!strncmp (*pp, "r(0,1);0;", 9))
2153 TYPE_CODE (type) = TYPE_CODE_ARRAY;
2154 /* In Fortran, an upper bound may be T... meaning a parameter specifies
2155 the length of the data. In this case, just pretend the bound is 1.
2156 This happens only for array parameters, which are really passed
2157 as pointers anyway, and we will translate them into such. */
2165 n = read_number (pp, ';') + 1;
2166 TYPE_TARGET_TYPE (type) = read_type (pp);
2167 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type)) * n;
2171 error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
2179 /* If this is an overriding temporary alteration for a header file's
2180 contents, and this type number is unknown in the global definition,
2181 put this type into the global definition at this type number. */
2182 if (header_file_prev_index >= 0)
2184 register struct type **tp
2185 = explicit_lookup_type (header_file_prev_index, typenums[1]);
2193 /* This page contains subroutines of read_type. */
2195 /* Read the description of a structure (or union type)
2196 and return an object describing the type. */
2198 static struct type *
2199 read_struct_type (pp, type)
2201 register struct type *type;
2205 struct nextfield *next;
2209 register struct nextfield *list = 0;
2210 struct nextfield *new;
2217 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2219 /* First comes the total size in bytes. */
2221 TYPE_LENGTH (type) = read_number (pp, 0);
2223 /* Now come the fields, as NAME:TYPENUM,BITPOS,BITSIZE; for each one.
2224 At the end, we see a semicolon instead of a field. */
2228 /* Check for and handle cretinous dbx symbol name continuation! */
2230 *pp = next_symbol_text ();
2232 /* Get space to record the next field's data. */
2233 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2237 /* Read the data. */
2239 while (*p != ':') p++;
2240 list->field.name = obsavestring (*pp, p - *pp);
2242 list->field.type = read_type (pp);
2244 error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
2246 (*pp)++; /* Skip the comma. */
2247 list->field.bitpos = read_number (pp, ',');
2248 list->field.bitsize = read_number (pp, ';');
2249 /* Detect an unpacked field and mark it as such.
2250 dbx gives a bit size for all fields.
2251 Note that forward refs cannot be packed,
2252 and treat enums as if they had the width of ints. */
2253 if (TYPE_CODE (list->field.type) != TYPE_CODE_INT
2254 && TYPE_CODE (list->field.type) != TYPE_CODE_ENUM)
2255 list->field.bitsize = 0;
2256 if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
2257 || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
2258 && list->field.bitsize == 8 * TYPE_LENGTH (builtin_type_int)))
2260 list->field.bitpos % 8 == 0)
2261 list->field.bitsize = 0;
2265 (*pp)++; /* Skip the terminating ';'. */
2267 /* Now create the vector of fields, and record how big it is. */
2269 TYPE_NFIELDS (type) = nfields;
2270 TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
2271 sizeof (struct field) * nfields);
2273 /* Copy the saved-up fields into the field vector. */
2275 for (n = nfields; list; list = list->next)
2276 TYPE_FIELD (type, --n) = list->field;
2281 /* Read a definition of an enumeration type,
2282 and create and return a suitable type object.
2283 Also defines the symbols that represent the values of the type. */
2285 static struct type *
2286 read_enum_type (pp, type)
2288 register struct type *type;
2293 register struct symbol *sym;
2295 struct pending **symlist;
2296 struct pending *osyms, *syms;
2299 if (within_function)
2300 symlist = &local_symbols;
2302 symlist = &file_symbols;
2304 o_nsyms = osyms ? osyms->nsyms : 0;
2306 /* Read the value-names and their values.
2307 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2308 A semicolon instead of a NAME means the end. */
2309 while (**pp && **pp != ';')
2311 /* Check for and handle cretinous dbx symbol name continuation! */
2313 *pp = next_symbol_text ();
2316 while (*p != ':') p++;
2317 name = obsavestring (*pp, p - *pp);
2319 n = read_number (pp, ',');
2321 sym = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
2322 bzero (sym, sizeof (struct symbol));
2323 SYMBOL_NAME (sym) = name;
2324 SYMBOL_CLASS (sym) = LOC_CONST;
2325 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2326 SYMBOL_VALUE (sym) = n;
2327 add_symbol_to_list (sym, symlist);
2331 (*pp)++; /* Skip the semicolon. */
2333 /* Now fill in the fields of the type-structure. */
2335 TYPE_LENGTH (type) = sizeof (int);
2336 TYPE_CODE (type) = TYPE_CODE_ENUM;
2337 TYPE_NFIELDS (type) = nsyms;
2338 TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
2340 /* Find the symbols for the values and put them into the type.
2341 The symbols can be found in the symlist that we put them on
2342 to cause them to be defined. osyms contains the old value
2343 of that symlist; everything up to there was defined by us. */
2345 for (syms = *symlist, n = nsyms; syms; syms = syms->next)
2350 for (; j < syms->nsyms; j++)
2352 struct symbol *sym = syms->symbol[j];
2353 SYMBOL_TYPE (sym) = type;
2354 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (sym);
2355 TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (sym);
2356 TYPE_FIELD_BITPOS (type, n) = 0;
2357 TYPE_FIELD_BITSIZE (type, n) = 0;
2366 static struct type *
2367 read_range_type (pp, typenums)
2375 /* First comes a type we are a subrange of.
2376 In practice it is usually 0, 1 or the type being defined. */
2377 read_type_number (pp, rangenums);
2380 /* A semicolon should now follow; skip it. */
2384 /* The remaining two operands are usually lower and upper bounds
2385 of the range. But in some special cases they mean something else. */
2386 n2 = read_number (pp, ';');
2387 n3 = read_number (pp, ';');
2389 /* A type defined as a subrange of itself, with bounds both 0, is void. */
2390 if (rangenums[0] == typenums[0] && rangenums[1] == typenums[1]
2391 && n2 == 0 && n3 == 0)
2392 return builtin_type_void;
2394 /* If n3 is zero and n2 is not, we want a floating type,
2395 and n2 is the width in bytes.
2397 Fortran programs appear to use this for complex types also,
2398 and they give no way to distinguish between double and single-complex!
2399 We don't have complex types, so we would lose on all fortran files!
2400 So return type `double' for all of those. It won't work right
2401 for the complex values, but at least it makes the file loadable. */
2403 if (n3 == 0 && n2 > 0)
2405 if (n2 == sizeof (float))
2406 return builtin_type_float;
2407 return builtin_type_double;
2410 /* If the upper bound is -1, it must really be an unsigned int. */
2412 else if (n2 == 0 && n3 == -1)
2414 if (sizeof (int) == sizeof (long))
2415 return builtin_type_unsigned_int;
2417 return builtin_type_unsigned_long;
2420 /* Detect unsigned subranges of int. Int is normally 1.
2421 Note that `char' is usually given bounds of 0 to 127,
2422 and would therefore appear unsigned; but it is described
2423 as a subrange of itself, so we reject it here. */
2425 else if (n2 == 0 && n1 == 1)
2427 /* an unsigned type */
2428 if (n3 == (1 << (8 * sizeof (int))) - 1)
2429 return builtin_type_unsigned_int;
2430 if (n3 == (1 << (8 * sizeof (short))) - 1)
2431 return builtin_type_unsigned_short;
2432 if (n3 == (1 << (8 * sizeof (char))) - 1)
2433 return builtin_type_unsigned_char;
2438 if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
2439 return builtin_type_int;
2440 if (n3 == (1 << (8 * sizeof (long) - 1)) - 1)
2441 return builtin_type_long;
2442 if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
2443 return builtin_type_short;
2444 if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
2445 return builtin_type_char;
2447 error ("Invalid symbol data: range type spec %s at symtab pos %d.",
2451 /* Read a number from the string pointed to by *PP.
2452 The value of *PP is advanced over the number.
2453 If END is nonzero, the character that ends the
2454 number must match END, or an error happens;
2455 and that character is skipped if it does match.
2456 If END is zero, *PP is left pointing to that character. */
2459 read_number (pp, end)
2463 register char *p = *pp;
2464 register long n = 0;
2468 /* Handle an optional leading minus sign. */
2476 /* Read the digits, as far as they go. */
2478 while ((c = *p++) >= '0' && c <= '9')
2486 error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
2500 add_com ("symbol-file", class_files, symbol_file_command,
2501 "Load symbol table (in dbx format) from executable file FILE.");
2506 #endif /* READ_DBX_FORMAT */