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