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