gdb-2.5.1
[deliverable/binutils-gdb.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 Useful for debugging `sun_kick'. */
1749
1750 void
1751 add_file_command (arg_string)
1752 char* arg_string;
1753 {
1754 register int desc;
1755 struct exec hdr;
1756 struct nlist *nlist;
1757 char *stringtab;
1758 long buffer;
1759 register int val;
1760 extern void close ();
1761 struct cleanup *old_chain;
1762 char* name;
1763 unsigned text_addr;
1764
1765 for( ; *arg_string == ' '; arg_string++ );
1766 name = arg_string;
1767 for( ; *arg_string != ' ' ; arg_string++ );
1768 *arg_string++ = (char) 0;
1769 for( ; *arg_string == ' '; arg_string++ );
1770 text_addr = (unsigned) atoi(arg_string);
1771
1772 printf("filename \"%s\", and text_addr = 0x%x\n", name, text_addr );
1773
1774 dont_repeat ();
1775
1776 if (name == 0)
1777 {
1778 if (symtab_list && !query ("Discard symbol table? ", 0))
1779 error ("Not confirmed.");
1780 free_all_symtabs ();
1781 return;
1782 }
1783
1784 /* if (symtab_list && !query ("Add new symbols from \"%s\"? ", name))
1785 error ("Not confirmed.");
1786 */
1787 {
1788 char *absolute_name;
1789 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
1790 if (desc < 0)
1791 perror_with_name (name);
1792 else
1793 name = absolute_name;
1794 }
1795
1796 old_chain = make_cleanup (close, desc);
1797 make_cleanup (free_current_contents, &name);
1798
1799 val = myread (desc, &hdr, sizeof hdr);
1800 if (val < 0)
1801 perror_with_name (name);
1802
1803 if (N_BADMAG (hdr))
1804 error ("File \"%s\" has a bad header.", name);
1805
1806 if (hdr.a_syms == 0)
1807 {
1808 printf ("%s does not have a symbol-table.\n", name);
1809 fflush (stdout);
1810 return;
1811 }
1812
1813 /* Now read the string table, all at once. */
1814 val = lseek (desc, N_SYMOFF (hdr) + hdr.a_syms, 0);
1815 if (val < 0)
1816 perror_with_name (name);
1817 val = myread (desc, &buffer, sizeof buffer);
1818 if (val < 0)
1819 perror_with_name (name);
1820 stringtab = (char *) alloca (buffer);
1821 bcopy (&buffer, stringtab, sizeof buffer);
1822 val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
1823 if (val < 0)
1824 perror_with_name (name);
1825
1826 /* That puts us at the symsegs. Read them. ########## Also need other
1827 changes if they exist. */
1828
1829
1830 /* Position to read the symbol table. Do not read it all at once. */
1831 val = lseek (desc, N_SYMOFF (hdr), 0);
1832 if (val < 0)
1833 perror_with_name (name);
1834
1835 printf ("Reading symbol data from %s...", name);
1836 fflush (stdout);
1837
1838 init_misc_functions ();
1839 make_cleanup (discard_misc_bunches, 0);
1840 init_header_files ();
1841 make_cleanup (free_header_files, 0);
1842
1843 read_addl_syms (desc, stringtab, hdr.a_syms / sizeof(struct nlist)
1844 ,text_addr, hdr.a_text) ;
1845
1846 /* Sort symbols alphabetically within each block. */
1847
1848 sort_syms ();
1849
1850 /* Go over the all misc functions and install them in vector. */
1851
1852 condense_addl_misc_bunches ();
1853
1854 /* Don't allow char * to have a typename (else would get caddr_t.) */
1855
1856 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
1857
1858 /* Make a default for file to list. */
1859
1860 select_source_symtab (symtab_list);
1861
1862 do_cleanups (old_chain);
1863
1864 /* Free the symtabs made by read_symsegs, but not their contents,
1865 which have been copied into symtabs on symtab_list. */
1866 while (symseg_chain)
1867 {
1868 register struct symtab *s = symseg_chain->next;
1869 free (symseg_chain);
1870 symseg_chain = s;
1871 }
1872
1873 printf ("done.\n");
1874 fflush (stdout);
1875 }
1876 \f
1877 static struct symbol *
1878 define_symbol (value, string, desc)
1879 int value;
1880 char *string;
1881 int desc;
1882 {
1883 register struct symbol *sym
1884 = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1885 char *p = (char *) index (string, ':');
1886 int deftype;
1887 register int i;
1888
1889 bzero (sym, sizeof (struct symbol));
1890 SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, string, p - string);
1891 p++;
1892 /* Determine the type of name being defined. */
1893 if ((*p >= '0' && *p <= '9') || *p == '(')
1894 deftype = 'l';
1895 else
1896 deftype = *p++;
1897
1898 /* c is a special case, not followed by a type-number.
1899 SYMBOL:c=iVALUE for an integer constant symbol.
1900 SYMBOL:c=rVALUE for a floating constant symbol. */
1901 if (deftype == 'c')
1902 {
1903 if (*p++ != '=')
1904 error ("Invalid symbol data at symtab pos %d.", symnum);
1905 switch (*p++)
1906 {
1907 case 'r':
1908 {
1909 double d = atof (p);
1910 char *value;
1911
1912 SYMBOL_TYPE (sym) = builtin_type_double;
1913 value = (char *) obstack_alloc (symbol_obstack, sizeof (double));
1914 bcopy (&d, value, sizeof (double));
1915 SYMBOL_VALUE_BYTES (sym) = value;
1916 SYMBOL_CLASS (sym) = LOC_CONST;
1917 }
1918 break;
1919 case 'i':
1920 {
1921 SYMBOL_TYPE (sym) = builtin_type_int;
1922 SYMBOL_VALUE (sym) = atoi (p);
1923 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1924 }
1925 break;
1926 default:
1927 error ("Invalid symbol data at symtab pos %d.", symnum);
1928 }
1929 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1930 add_symbol_to_list (sym, &file_symbols);
1931 return sym;
1932 }
1933
1934 /* Now usually comes a number that says which data type,
1935 and possibly more stuff to define the type
1936 (all of which is handled by read_type) */
1937
1938 if (deftype == 'p' && *p == 'F')
1939 /* pF is a two-letter code that means a function parameter in Fortran.
1940 The type-number specifies the type of the return value.
1941 Translate it into a pointer-to-function type. */
1942 {
1943 p++;
1944 SYMBOL_TYPE (sym)
1945 = lookup_pointer_type (lookup_function_type (read_type (&p)));
1946 }
1947 else
1948 SYMBOL_TYPE (sym) = read_type (&p);
1949
1950 switch (deftype)
1951 {
1952 case 'f':
1953 SYMBOL_CLASS (sym) = LOC_BLOCK;
1954 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1955 add_symbol_to_list (sym, &file_symbols);
1956 break;
1957
1958 case 'F':
1959 SYMBOL_CLASS (sym) = LOC_BLOCK;
1960 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1961 add_symbol_to_list (sym, &global_symbols);
1962 break;
1963
1964 case 'G':
1965 /* For a class G (global) symbol, it appears that the
1966 value is not correct. It is necessary to search for the
1967 corresponding linker definition to find the value.
1968 These definitions appear at the end of the namelist. */
1969 i = hashname (SYMBOL_NAME (sym));
1970 SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
1971 global_sym_chain[i] = sym;
1972 SYMBOL_CLASS (sym) = LOC_STATIC;
1973 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1974 add_symbol_to_list (sym, &global_symbols);
1975 break;
1976
1977 /* This case is faked by a conditional above,
1978 when there is no code letter in the dbx data.
1979 Dbx data never actually contains 'l'. */
1980 case 'l':
1981 SYMBOL_CLASS (sym) = LOC_LOCAL;
1982 SYMBOL_VALUE (sym) = value;
1983 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1984 add_symbol_to_list (sym, &local_symbols);
1985 break;
1986
1987 case 'p':
1988 SYMBOL_CLASS (sym) = LOC_ARG;
1989 SYMBOL_VALUE (sym) = value;
1990 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1991 add_symbol_to_list (sym, &local_symbols);
1992 /* DESC == 0 implies compiled with GCC.
1993 In this case, if it says `short', believe it. */
1994 if (desc == 0)
1995 break;
1996 /* If PCC says a parameter is a short or a char,
1997 it is really an int. */
1998 if (SYMBOL_TYPE (sym) == builtin_type_char
1999 || SYMBOL_TYPE (sym) == builtin_type_short)
2000 SYMBOL_TYPE (sym) = builtin_type_int;
2001 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
2002 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
2003 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
2004 break;
2005
2006 case 'r':
2007 SYMBOL_CLASS (sym) = LOC_REGISTER;
2008 SYMBOL_VALUE (sym) = value;
2009 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2010 add_symbol_to_list (sym, &local_symbols);
2011 break;
2012
2013 case 'S':
2014 /* Static symbol at top level of file */
2015 SYMBOL_CLASS (sym) = LOC_STATIC;
2016 SYMBOL_VALUE (sym) = value;
2017 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2018 add_symbol_to_list (sym, &file_symbols);
2019 break;
2020
2021 case 't':
2022 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2023 SYMBOL_VALUE (sym) = value;
2024 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2025 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
2026 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
2027 TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), "", "");
2028 /* C++ vagaries: we may have a type which is derived from
2029 a base type which did not have its name defined when the
2030 derived class was output. We fill in the derived class's
2031 base part member's name here in that case. */
2032 else if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
2033 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
2034 && TYPE_BASECLASS (SYMBOL_TYPE (sym))
2035 && TYPE_FIELD_NAME (SYMBOL_TYPE (sym), 0) == 0)
2036 TYPE_FIELD_NAME (SYMBOL_TYPE (sym), 0) = TYPE_NAME (TYPE_BASECLASS (SYMBOL_TYPE (sym)));
2037
2038 add_symbol_to_list (sym, &file_symbols);
2039 break;
2040
2041 case 'T':
2042 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2043 SYMBOL_VALUE (sym) = value;
2044 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
2045 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
2046 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
2047 TYPE_NAME (SYMBOL_TYPE (sym))
2048 = concat ("",
2049 (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
2050 ? "enum "
2051 : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
2052 ? "struct " : "union ")),
2053 SYMBOL_NAME (sym));
2054 add_symbol_to_list (sym, &file_symbols);
2055 break;
2056
2057 case 'V':
2058 case 'v':
2059 /* Static symbol of local scope */
2060 SYMBOL_CLASS (sym) = LOC_STATIC;
2061 SYMBOL_VALUE (sym) = value;
2062 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2063 add_symbol_to_list (sym, &local_symbols);
2064 break;
2065
2066 default:
2067 error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
2068 }
2069 return sym;
2070 }
2071 \f
2072 /* Read a number by which a type is referred to in dbx data,
2073 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
2074 Just a single number N is equivalent to (0,N).
2075 Return the two numbers by storing them in the vector TYPENUMS.
2076 TYPENUMS will then be used as an argument to dbx_lookup_type. */
2077
2078 static void
2079 read_type_number (pp, typenums)
2080 register char **pp;
2081 register int *typenums;
2082 {
2083 if (**pp == '(')
2084 {
2085 (*pp)++;
2086 typenums[0] = read_number (pp, ',');
2087 typenums[1] = read_number (pp, ')');
2088 }
2089 else
2090 {
2091 typenums[0] = 0;
2092 typenums[1] = read_number (pp, 0);
2093 }
2094 }
2095
2096 /* Read a dbx type reference or definition;
2097 return the type that is meant.
2098 This can be just a number, in which case it references
2099 a type already defined and placed in type_vector.
2100 Or the number can be followed by an =, in which case
2101 it means to define a new type according to the text that
2102 follows the =. */
2103
2104 static
2105 struct type *
2106 read_type (pp)
2107 register char **pp;
2108 {
2109 register struct type *type = 0;
2110 register int n;
2111 struct type *type1;
2112 int typenums[2];
2113 int xtypenums[2];
2114
2115 read_type_number (pp, typenums);
2116
2117 /* Detect random reference to type not yet defined.
2118 Allocate a type object but leave it zeroed. */
2119 if (**pp != '=')
2120 return dbx_alloc_type (typenums);
2121
2122 *pp += 2;
2123 switch ((*pp)[-1])
2124 {
2125 case 'x':
2126 type = dbx_alloc_type (typenums);
2127 /* Set the type code according to the following letter. */
2128 switch ((*pp)[0])
2129 {
2130 case 's':
2131 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2132 break;
2133 case 'u':
2134 TYPE_CODE (type) = TYPE_CODE_UNION;
2135 break;
2136 case 'e':
2137 TYPE_CODE (type) = TYPE_CODE_ENUM;
2138 break;
2139 }
2140 /* Skip the name the cross-ref points to. */
2141 /* Note: for C++, the cross reference may be to a base type which
2142 has not yet been seen. In this case, we skip to the comma,
2143 which will mark the end of the base class name. (The ':'
2144 at the end of the base class name will be skipped as well.) */
2145 *pp = (char *) index (*pp, ',');
2146 /* Just allocate the type and leave it zero if nothing known */
2147 return dbx_alloc_type (typenums);
2148
2149 case '0':
2150 case '1':
2151 case '2':
2152 case '3':
2153 case '4':
2154 case '5':
2155 case '6':
2156 case '7':
2157 case '8':
2158 case '9':
2159 case '(':
2160 (*pp)--;
2161 read_type_number (pp, xtypenums);
2162 type = *dbx_lookup_type (xtypenums);
2163 if (type == 0)
2164 type = builtin_type_void;
2165 *dbx_lookup_type (typenums) = type;
2166 break;
2167
2168 case '*':
2169 type = dbx_alloc_type (typenums);
2170 smash_to_pointer_type (type, read_type (pp));
2171 break;
2172
2173 case '@':
2174 {
2175 struct type *domain = read_type (pp);
2176 char c;
2177 struct type *ptrtype;
2178
2179 if (*(*pp)++ != ',')
2180 error ("invalid member pointer data format, at symtab pos %d.",
2181 symnum);
2182
2183 ptrtype = read_type (pp);
2184 type = dbx_alloc_type (typenums);
2185 smash_to_member_pointer_type (type, domain, ptrtype);
2186 }
2187 break;
2188
2189 case '&':
2190 type = dbx_alloc_type (typenums);
2191 smash_to_reference_type (type, read_type (pp));
2192 break;
2193
2194 case 'f':
2195 type = dbx_alloc_type (typenums);
2196 smash_to_function_type (type, read_type (pp));
2197 break;
2198
2199 case 'r':
2200 type = read_range_type (pp, typenums);
2201 *dbx_lookup_type (typenums) = type;
2202 break;
2203
2204 case 'e':
2205 type = dbx_alloc_type (typenums);
2206 type = read_enum_type (pp, type);
2207 *dbx_lookup_type (typenums) = type;
2208 break;
2209
2210 case 's':
2211 type = dbx_alloc_type (typenums);
2212 type = read_struct_type (pp, type);
2213 break;
2214
2215 case 'u':
2216 type = dbx_alloc_type (typenums);
2217 type = read_struct_type (pp, type);
2218 TYPE_CODE (type) = TYPE_CODE_UNION;
2219 break;
2220
2221 case 'a':
2222 /* Define an array type. */
2223 type = dbx_alloc_type (typenums);
2224
2225 /* dbx expresses array types in terms of a range type for the index,
2226 and that range type is specified right inside the array type spec
2227 making ar1;MIN;MAX;VALTYPE */
2228 if (!strncmp (*pp, "r1;0;", 5))
2229 (*pp) += 5;
2230 else if (!strncmp (*pp, "r(0,1);0;", 9))
2231 (*pp) += 9;
2232 else break;
2233
2234 TYPE_CODE (type) = TYPE_CODE_ARRAY;
2235 /* In Fortran, an upper bound may be T... meaning a parameter specifies
2236 the length of the data. In this case, just pretend the bound is 1.
2237 This happens only for array parameters, which are really passed
2238 as pointers anyway, and we will translate them into such. */
2239 if (**pp == 'T')
2240 {
2241 n = 1;
2242 while (**pp != ';')
2243 (*pp)++;
2244 }
2245 else
2246 n = read_number (pp, ';') + 1;
2247 TYPE_TARGET_TYPE (type) = read_type (pp);
2248 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type)) * n;
2249 break;
2250
2251 default:
2252 error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
2253 (*pp)[-1], symnum);
2254 }
2255
2256 if (type == 0)
2257 abort ();
2258
2259 #if 0
2260 /* If this is an overriding temporary alteration for a header file's
2261 contents, and this type number is unknown in the global definition,
2262 put this type into the global definition at this type number. */
2263 if (header_file_prev_index >= 0)
2264 {
2265 register struct type **tp
2266 = explicit_lookup_type (header_file_prev_index, typenums[1]);
2267 if (*tp == 0)
2268 *tp = type;
2269 }
2270 #endif
2271 return type;
2272 }
2273 \f
2274 /* This page contains subroutines of read_type. */
2275
2276 /* Read the description of a structure (or union type)
2277 and return an object describing the type. */
2278
2279 static struct type *
2280 read_struct_type (pp, type)
2281 char **pp;
2282 register struct type *type;
2283 {
2284 struct nextfield
2285 {
2286 struct nextfield *next;
2287 int visibility;
2288 struct field field;
2289 };
2290
2291 struct next_fnfield
2292 {
2293 struct next_fnfield *next;
2294 int visibility;
2295 struct fn_field fn_field;
2296 };
2297
2298 struct next_fnfieldlist
2299 {
2300 struct next_fnfieldlist *next;
2301 struct fn_fieldlist fn_fieldlist;
2302 };
2303
2304 register struct nextfield *list = 0;
2305 struct nextfield *new;
2306 int totalsize;
2307 char *name;
2308 register char *p;
2309 int nfields = 0;
2310 register int n;
2311
2312 register struct next_fnfieldlist *mainlist = 0;
2313 int nfn_fields = 0;
2314 struct type *baseclass = NULL;
2315 int read_possible_virtual_info = 0;
2316
2317 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2318
2319 /* First comes the total size in bytes. */
2320
2321 TYPE_LENGTH (type) = read_number (pp, 0);
2322
2323 /* C++: Now, if the class is a derived class, then the next character
2324 will be a '!', followed by the type of the base class. Allocate
2325 pretend that that base class is a sub-structure of this one,
2326 with its field name being the type name of the derived class. This
2327 cannot cause a naming conflict, since field names cannot be
2328 type names. This will magically recurse itself to gound terms
2329 when all is read and done. */
2330 if (**pp == '!')
2331 {
2332 *pp += 1;
2333
2334 switch (*(*pp)++)
2335 {
2336 case '0':
2337 break;
2338 case '1':
2339 TYPE_VIA_PROTECTED (type) = 1;
2340 break;
2341 case '2':
2342 TYPE_VIA_PUBLIC (type) = 1;
2343 break;
2344 default:
2345 error ("Invalid symbol data: bad visibility format at symtab pos %d.",
2346 symnum);
2347 }
2348
2349 if (**pp == '\\') *pp = next_symbol_text ();
2350 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2351 new->next = list;
2352 list = new;
2353
2354 baseclass = read_type (pp);
2355 list->field.type = baseclass;
2356 list->field.name = TYPE_NAME (baseclass);
2357 *pp += 1; /* skip ',' */
2358 list->field.bitpos = 0;
2359 list->field.bitsize = 0; /* this should be an unpacked field! */
2360 nfields++;
2361 }
2362
2363 /* Now come the fields, as NAME:?TYPENUM,BITPOS,BITSIZE; for each one.
2364 At the end, we see a semicolon instead of a field.
2365
2366 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2367 a static field.
2368
2369 The `?' is a placeholder for one of '+' (public visibility),
2370 '0' (protected visibility), and '-' (private visibility). */
2371
2372 while (**pp != ';')
2373 {
2374 int visibility;
2375
2376 /* Check for and handle cretinous dbx symbol name continuation! */
2377 if (**pp == '\\') *pp = next_symbol_text ();
2378
2379 /* Get space to record the next field's data. */
2380 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2381 new->next = list;
2382 list = new;
2383
2384 /* Read the data. */
2385 p = *pp;
2386 while (*p != ':') p++;
2387 list->field.name = savestring (*pp, p - *pp);
2388
2389 /* Check to see if we have hit the methods yet. */
2390 if (p[1] == ':')
2391 break;
2392
2393 *pp = p + 1;
2394
2395 /* This means we have a visibility for a field coming. */
2396 if (**pp == '/')
2397 {
2398 switch (*++*pp)
2399 {
2400 case '0':
2401 visibility = 0;
2402 *pp += 1;
2403 break;
2404
2405 case '1':
2406 visibility = 1;
2407 *pp += 1;
2408 break;
2409
2410 case '2':
2411 visibility = 2;
2412 *pp += 1;
2413 break;
2414 }
2415 }
2416 /* else normal dbx-style format. */
2417
2418 list->field.type = read_type (pp);
2419 if (**pp == ':')
2420 {
2421 list->field.bitpos = (long)-1;
2422 p = ++(*pp);
2423 while (*p != ';') p++;
2424 list->field.bitsize = (long) savestring (*pp, p - *pp);
2425 *pp = p + 1;
2426 nfields++;
2427 continue;
2428 }
2429 else if (**pp != ',')
2430 error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
2431 symnum);
2432 (*pp)++; /* Skip the comma. */
2433 list->field.bitpos = read_number (pp, ',');
2434 list->field.bitsize = read_number (pp, ';');
2435 /* Detect an unpacked field and mark it as such.
2436 dbx gives a bit size for all fields.
2437 Also detect forward refs to structures and unions,
2438 and treat enums as if they had the width of ints. */
2439 if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
2440 || TYPE_CODE (list->field.type) == TYPE_CODE_STRUCT
2441 || TYPE_CODE (list->field.type) == TYPE_CODE_UNION
2442 || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
2443 && list->field.bitsize == 8 * TYPE_LENGTH (builtin_type_int)))
2444 &&
2445 list->field.bitpos % 8 == 0)
2446 list->field.bitsize = 0;
2447 nfields++;
2448 }
2449
2450 /* Now come the method fields, as NAME::methods
2451 where each method is of the form TYPENUM,ARGS,...:PHYSNAME;
2452 At the end, we see a semicolon instead of a field.
2453
2454 For the case of overloaded operators, the format is
2455 OPERATOR::*.methods, where OPERATOR is the string "operator",
2456 `*' holds the place for an operator name (such as `+=')
2457 and `.' marks the end of the operator name. */
2458 if (p[1] == ':')
2459 {
2460 /* Now, read in the methods. To simplify matters, we
2461 "unread" the name that has been read, so that we can
2462 start from the top. */
2463
2464 p = *pp;
2465
2466 /* chill the list of fields: the last entry (at the head)
2467 is a partially constructed entry which we now scrub. */
2468 list = list->next;
2469
2470 /* For each list of method lists... */
2471 do
2472 {
2473 int i;
2474 struct next_fnfield *sublist = 0;
2475 struct fn_field *fn_fields = 0;
2476 int length = 0;
2477 struct next_fnfieldlist *new_mainlist =
2478 (struct next_fnfieldlist *)alloca (sizeof (struct next_fnfieldlist));
2479
2480 /* read in the name. */
2481 while (*p != ':') p++;
2482 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && (*pp)[2] == '$')
2483 {
2484 static char opname[32] = "operator ";
2485 char *o = opname + 9;
2486
2487 /* Skip past '::'. */
2488 p += 2;
2489 while (*p != '.')
2490 *o++ = *p++;
2491 new_mainlist->fn_fieldlist.name = savestring (opname, o - opname);
2492 /* Skip past '.' */
2493 *pp = p + 1;
2494 }
2495 else
2496 {
2497 i = 0;
2498 new_mainlist->fn_fieldlist.name = savestring (*pp, p - *pp);
2499 /* Skip past '::'. */
2500 *pp = p + 2;
2501 }
2502
2503 do
2504 {
2505 struct next_fnfield *new_sublist =
2506 (struct next_fnfield *)alloca (sizeof (struct next_fnfield));
2507
2508 /* Check for and handle cretinous dbx symbol name continuation! */
2509 if (**pp == '\\') *pp = next_symbol_text ();
2510
2511 new_sublist->fn_field.type = read_type (pp);
2512 new_sublist->fn_field.args = read_args (pp, ':');
2513 p = *pp;
2514 while (*p != ';') p++;
2515 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2516 *pp = p + 1;
2517 new_sublist->visibility = *(*pp)++ - '0';
2518 if (**pp == '\\') *pp = next_symbol_text ();
2519
2520 if (*(*pp)++ == '*')
2521 new_sublist->fn_field.voffset = read_number (pp, ';') + 1;
2522 else
2523 new_sublist->fn_field.voffset = 0;
2524
2525 new_sublist->next = sublist;
2526 sublist = new_sublist;
2527 length++;
2528 }
2529 while (**pp != ';');
2530
2531 *pp += 1;
2532
2533 new_mainlist->fn_fieldlist.fn_fields =
2534 (struct fn_field *) obstack_alloc (symbol_obstack,
2535 sizeof (struct fn_field) * length);
2536 TYPE_FN_PRIVATE_BITS (new_mainlist->fn_fieldlist) =
2537 (int *) obstack_alloc (symbol_obstack,
2538 sizeof (int) * (1 + (length >> 5)));
2539
2540 TYPE_FN_PROTECTED_BITS (new_mainlist->fn_fieldlist) =
2541 (int *) obstack_alloc (symbol_obstack,
2542 sizeof (int) * (1 + (length >> 5)));
2543
2544 for (i = length; sublist; sublist = sublist->next)
2545 {
2546 new_mainlist->fn_fieldlist.fn_fields[--i] = sublist->fn_field;
2547 if (sublist->visibility == 0)
2548 B_SET (new_mainlist->fn_fieldlist.private_fn_field_bits, i);
2549 else if (sublist->visibility == 1)
2550 B_SET (new_mainlist->fn_fieldlist.protected_fn_field_bits, i);
2551 }
2552
2553 new_mainlist->fn_fieldlist.length = length;
2554 new_mainlist->next = mainlist;
2555 mainlist = new_mainlist;
2556 nfn_fields++;
2557 }
2558 while (**pp != ';');
2559
2560 *pp += 2;
2561
2562 if (**pp == '~')
2563 {
2564 read_possible_virtual_info = 1;
2565 *pp += 1;
2566 }
2567 if (**pp == '-')
2568 {
2569 TYPE_HAS_DESTRUCTOR (type) = 1;
2570 TYPE_HAS_CONSTRUCTOR (type) = 1;
2571 *pp += 1;
2572 }
2573 else if (**pp == '+')
2574 {
2575 TYPE_HAS_CONSTRUCTOR (type) = 1;
2576 *pp += 1;
2577 }
2578 }
2579 else *pp += 1;
2580
2581 /* Now create the vector of fields, and record how big it is. */
2582
2583 TYPE_NFIELDS (type) = nfields;
2584 TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
2585 sizeof (struct field) * nfields);
2586 TYPE_FIELD_PRIVATE_BITS (type) =
2587 (int *) obstack_alloc (symbol_obstack,
2588 sizeof (int) * (1 + (nfields >> 5)));
2589 TYPE_FIELD_PROTECTED_BITS (type) =
2590 (int *) obstack_alloc (symbol_obstack,
2591 sizeof (int) * (1 + (nfields >> 5)));
2592
2593 TYPE_NFN_FIELDS (type) = nfn_fields;
2594 TYPE_NFN_FIELDS_TOTAL (type) = nfn_fields;
2595 if (baseclass)
2596 TYPE_NFN_FIELDS_TOTAL (type) += TYPE_NFN_FIELDS_TOTAL (baseclass);
2597
2598 TYPE_FN_FIELDLISTS (type) =
2599 (struct fn_fieldlist *) obstack_alloc (symbol_obstack,
2600 sizeof (struct fn_fieldlist) * nfn_fields);
2601
2602 /* Copy the saved-up fields into the field vector. */
2603
2604 for (n = nfields; list; list = list->next)
2605 {
2606 TYPE_FIELD (type, --n) = list->field;
2607 if (list->visibility == 0)
2608 SET_TYPE_FIELD_PRIVATE (type, n);
2609 else if (list->visibility == 1)
2610 SET_TYPE_FIELD_PROTECTED (type, n);
2611 }
2612
2613 for (n = nfn_fields; mainlist; mainlist = mainlist->next)
2614 TYPE_FN_FIELDLISTS (type)[--n] = mainlist->fn_fieldlist;
2615
2616 TYPE_BASECLASS (type) = baseclass;
2617
2618 if (read_possible_virtual_info)
2619 {
2620 /* Read either a '%' or the final ';'. */
2621 if (*(*pp)++ == '%')
2622 {
2623 /* Now we must record the virtual function table pointer's
2624 field information. */
2625
2626 struct type *t;
2627 int i;
2628
2629 t = read_type (pp);
2630 p = (*pp)++;
2631 while (*p != ';') p++;
2632 TYPE_VPTR_BASETYPE (type) = t;
2633 if (type == t)
2634 {
2635 if (TYPE_FIELD_NAME (t, 0) == 0)
2636 TYPE_VPTR_FIELDNO (type) = i = 0;
2637 else for (i = TYPE_NFIELDS (t) - 1; i >= 0; --i)
2638 if (! strncmp (TYPE_FIELD_NAME (t, i), *pp,
2639 strlen (TYPE_FIELD_NAME (t, i))))
2640 {
2641 TYPE_VPTR_FIELDNO (type) = i;
2642 break;
2643 }
2644 if (i < 0)
2645 error ("virtual function table field not found");
2646 }
2647 else
2648 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type));
2649 *pp = p;
2650 }
2651 else
2652 {
2653 TYPE_VPTR_BASETYPE (type) = 0;
2654 TYPE_VPTR_FIELDNO (type) = -1;
2655 }
2656 }
2657 else
2658 {
2659 TYPE_VPTR_BASETYPE (type) = 0;
2660 TYPE_VPTR_FIELDNO (type) = -1;
2661 }
2662
2663 return type;
2664 }
2665
2666 /* Read a definition of an enumeration type,
2667 and create and return a suitable type object.
2668 Also defines the symbols that represent the values of the type. */
2669
2670 static struct type *
2671 read_enum_type (pp, type)
2672 register char **pp;
2673 register struct type *type;
2674 {
2675 register char *p;
2676 char *name;
2677 register long n;
2678 register struct symbol *sym;
2679 int nsyms = 0;
2680 struct pending **symlist;
2681 struct pending *osyms, *syms;
2682
2683 if (within_function)
2684 symlist = &local_symbols;
2685 else
2686 symlist = &file_symbols;
2687 osyms = *symlist;
2688
2689 /* Read the value-names and their values.
2690 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2691 A semicolon instead of a NAME means the end. */
2692 while (**pp && **pp != ';')
2693 {
2694 /* Check for and handle cretinous dbx symbol name continuation! */
2695 if (**pp == '\\') *pp = next_symbol_text ();
2696
2697 p = *pp;
2698 while (*p != ':') p++;
2699 name = savestring (*pp, p - *pp);
2700 *pp = p + 1;
2701 n = read_number (pp, ',');
2702
2703 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
2704 bzero (sym, sizeof (struct symbol));
2705 SYMBOL_NAME (sym) = name;
2706 SYMBOL_CLASS (sym) = LOC_CONST;
2707 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2708 SYMBOL_VALUE (sym) = n;
2709 add_symbol_to_list (sym, symlist);
2710 nsyms++;
2711 }
2712
2713 (*pp)++; /* Skip the semicolon. */
2714
2715 /* Now fill in the fields of the type-structure. */
2716
2717 TYPE_LENGTH (type) = sizeof (int);
2718 TYPE_CODE (type) = TYPE_CODE_ENUM;
2719 TYPE_NFIELDS (type) = nsyms;
2720 TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
2721
2722 /* Find the symbols for the values and put them into the type.
2723 The symbols can be found in the symlist that we put them on
2724 to cause them to be defined. osyms contains the old value
2725 of that symlist; everything up to there was defined by us. */
2726
2727 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
2728 {
2729 SYMBOL_TYPE (syms->symbol) = type;
2730 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
2731 TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (syms->symbol);
2732 TYPE_FIELD_BITPOS (type, n) = 0;
2733 TYPE_FIELD_BITSIZE (type, n) = 0;
2734 }
2735
2736 return type;
2737 }
2738
2739 static struct type *
2740 read_range_type (pp, typenums)
2741 char **pp;
2742 int typenums[2];
2743 {
2744 char *errp = *pp;
2745 int rangenums[2];
2746 int n1, n2, n3;
2747
2748 /* First comes a type we are a subrange of.
2749 In practice it is usually 0, 1 or the type being defined. */
2750 read_type_number (pp, rangenums);
2751 n1 = rangenums[1];
2752
2753 /* A semicolon should now follow; skip it. */
2754 if (**pp == ';')
2755 (*pp)++;
2756
2757 /* The remaining two operands are usually lower and upper bounds
2758 of the range. But in some special cases they mean something else. */
2759 n2 = read_number (pp, ';');
2760 n3 = read_number (pp, ';');
2761
2762 /* A type defined as a subrange of itself, with bounds both 0, is void. */
2763 if (rangenums[0] == typenums[0] && rangenums[1] == typenums[1]
2764 && n2 == 0 && n3 == 0)
2765 return builtin_type_void;
2766
2767 /* If n3 is zero and n2 is not, we want a floating type,
2768 and n2 is the width in bytes.
2769
2770 Fortran programs appear to use this for complex types also,
2771 and they give no way to distinguish between double and single-complex!
2772 We don't have complex types, so we would lose on all fortran files!
2773 So return type `double' for all of those. It won't work right
2774 for the complex values, but at least it makes the file loadable. */
2775
2776 if (n3 == 0 && n2 > 0)
2777 {
2778 if (n2 == sizeof (float))
2779 return builtin_type_float;
2780 return builtin_type_double;
2781 }
2782
2783 /* If the upper bound is -1, it must really be an unsigned int. */
2784
2785 else if (n2 == 0 && n3 == -1)
2786 {
2787 if (sizeof (int) == sizeof (long))
2788 return builtin_type_unsigned_int;
2789 else
2790 return builtin_type_unsigned_long;
2791 }
2792
2793 /* Detect unsigned subranges of int. Int is normally 1.
2794 Note that `char' is usually given bounds of 0 to 127,
2795 and would therefore appear unsigned; but it is described
2796 as a subrange of itself, so we reject it here. */
2797
2798 else if (n2 == 0 && n1 == 1)
2799 {
2800 /* an unsigned type */
2801 if (n3 == (1 << (8 * sizeof (int))) - 1)
2802 return builtin_type_unsigned_int;
2803 if (n3 == (1 << (8 * sizeof (short))) - 1)
2804 return builtin_type_unsigned_short;
2805 if (n3 == (1 << (8 * sizeof (char))) - 1)
2806 return builtin_type_unsigned_char;
2807 }
2808 else
2809 {
2810 /* a signed type */
2811 if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
2812 return builtin_type_int;
2813 if (n3 == (1 << (8 * sizeof (long) - 1)) - 1)
2814 return builtin_type_long;
2815 if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
2816 return builtin_type_short;
2817 if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
2818 return builtin_type_char;
2819 }
2820 error ("Invalid symbol data: range type spec %s at symtab pos %d.",
2821 errp - 1, symnum);
2822 }
2823
2824 /* Read a number from the string pointed to by *PP.
2825 The value of *PP is advanced over the number.
2826 If END is nonzero, the character that ends the
2827 number must match END, or an error happens;
2828 and that character is skipped if it does match.
2829 If END is zero, *PP is left pointing to that character. */
2830
2831 static long
2832 read_number (pp, end)
2833 char **pp;
2834 int end;
2835 {
2836 register char *p = *pp;
2837 register long n = 0;
2838 register int c;
2839 int sign = 1;
2840
2841 /* Handle an optional leading minus sign. */
2842
2843 if (*p == '-')
2844 {
2845 sign = -1;
2846 p++;
2847 }
2848
2849 /* Read the digits, as far as they go. */
2850
2851 while ((c = *p++) >= '0' && c <= '9')
2852 {
2853 n *= 10;
2854 n += c - '0';
2855 }
2856 if (end)
2857 {
2858 if (c != end)
2859 error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
2860 }
2861 else
2862 --p;
2863
2864 *pp = p;
2865 return n * sign;
2866 }
2867
2868 /* Read in an argument list. This is a list of types. It is terminated with
2869 a ':', FYI. Return the list of types read in. */
2870 static struct type **
2871 read_args (pp, end)
2872 char **pp;
2873 int end;
2874 {
2875 struct type *types[1024], **rval; /* allow for fns of 1023 parameters */
2876 int n = 0;
2877
2878 while (**pp != end)
2879 {
2880 if (**pp != ',')
2881 error ("Invalid argument list: no ',', at symtab pos %d", symnum);
2882 *pp += 1;
2883 types[n++] = read_type (pp);
2884 }
2885 *pp += 1; /* get past `end' (the ':' character) */
2886
2887 if (n == 1)
2888 {
2889 rval = (struct type **) xmalloc (2 * sizeof (struct type *));
2890 }
2891 else
2892 {
2893 rval = (struct type **) xmalloc (n * sizeof (struct type *));
2894 }
2895 bcopy (types, rval, n * sizeof (struct type *));
2896 return rval;
2897 }
2898
2899 /* This function is really horrible, but to avoid it, there would need
2900 to be more filling in of forward references. */
2901 int
2902 fill_in_vptr_fieldno (type)
2903 struct type *type;
2904 {
2905 if (TYPE_VPTR_FIELDNO (type) < 0)
2906 TYPE_VPTR_FIELDNO (type) = fill_in_vptr_fieldno (TYPE_BASECLASS (type));
2907 return TYPE_VPTR_FIELDNO (type);
2908 }
2909
2910 static
2911 initialize ()
2912 {
2913 symfile = 0;
2914
2915 add_com ("symbol-file", class_files, symbol_file_command,
2916 "Load symbol table (in dbx format) from executable file FILE.");
2917
2918 add_com ("add-file", class_files, add_file_command,
2919 "Load the symbols from FILE, assuming its codes is at TEXT_START.") ;
2920 }
2921
2922 END_FILE
2923
2924 #endif /* READ_DBX_FORMAT */
This page took 0.089053 seconds and 5 git commands to generate.