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