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