* alpha.h (struct alpha_operand): Pack elements into bitfields.
[deliverable/binutils-gdb.git] / gdb / dbxread.c
CommitLineData
c906108c 1/* Read dbx symbol tables and convert to internal format, for GDB.
b6ba6518
KB
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001
c906108c
SS
4 Free Software Foundation, Inc.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23/* This module provides three functions: dbx_symfile_init,
24 which initializes to read a symbol file; dbx_new_init, which
25 discards existing cached information when all symbols are being
26 discarded; and dbx_symfile_read, which reads a symbol table
27 from a file.
28
29 dbx_symfile_read only does the minimum work necessary for letting the
30 user "name" things symbolically; it does not read the entire symtab.
31 Instead, it reads the external and static symbols and puts them in partial
32 symbol tables. When more extensive information is requested of a
33 file, the corresponding partial symbol table is mutated into a full
34 fledged symbol table by going back and reading the symbols
35 for real. dbx_psymtab_to_symtab() is the function that does this */
36
37#include "defs.h"
38#include "gdb_string.h"
39
40#if defined(USG) || defined(__CYGNUSCLIB__)
41#include <sys/types.h>
42#include <fcntl.h>
43#endif
44
45#include "obstack.h"
46#include "gdb_stat.h"
c906108c
SS
47#include "symtab.h"
48#include "breakpoint.h"
c906108c
SS
49#include "target.h"
50#include "gdbcore.h" /* for bfd stuff */
c5aa993b 51#include "libaout.h" /* FIXME Secret internal BFD stuff for a.out */
c906108c
SS
52#include "symfile.h"
53#include "objfiles.h"
54#include "buildsym.h"
55#include "stabsread.h"
56#include "gdb-stabs.h"
57#include "demangle.h"
58#include "language.h" /* Needed inside partial-stab.h */
59#include "complaints.h"
015a42b4 60#include "cp-abi.h"
c906108c
SS
61
62#include "aout/aout64.h"
63#include "aout/stab_gnu.h" /* We always use GNU stabs, not native, now */
c906108c 64\f
c5aa993b 65
c906108c
SS
66/* This macro returns the size field of a minimal symbol, which is normally
67 stored in the "info" field. The macro can be overridden for specific
68 targets (e.g. MIPS16) that use the info field for other purposes. */
69#ifndef MSYMBOL_SIZE
70#define MSYMBOL_SIZE(msym) ((long) MSYMBOL_INFO (msym))
71#endif
72
73
74/* We put a pointer to this structure in the read_symtab_private field
75 of the psymtab. */
76
c5aa993b
JM
77struct symloc
78 {
c906108c 79
c5aa993b
JM
80 /* Offset within the file symbol table of first local symbol for this
81 file. */
c906108c 82
c5aa993b 83 int ldsymoff;
c906108c 84
c5aa993b
JM
85 /* Length (in bytes) of the section of the symbol table devoted to
86 this file's symbols (actually, the section bracketed may contain
87 more than just this file's symbols). If ldsymlen is 0, the only
88 reason for this thing's existence is the dependency list. Nothing
89 else will happen when it is read in. */
c906108c 90
c5aa993b 91 int ldsymlen;
c906108c 92
c5aa993b 93 /* The size of each symbol in the symbol file (in external form). */
c906108c 94
c5aa993b 95 int symbol_size;
c906108c 96
c5aa993b
JM
97 /* Further information needed to locate the symbols if they are in
98 an ELF file. */
c906108c 99
c5aa993b
JM
100 int symbol_offset;
101 int string_offset;
102 int file_string_offset;
103 };
c906108c
SS
104
105#define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
106#define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
107#define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
108#define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
109#define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
110#define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
111#define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
c906108c 112\f
c5aa993b 113
c906108c
SS
114/* Remember what we deduced to be the source language of this psymtab. */
115
116static enum language psymtab_language = language_unknown;
117
118/* Nonzero means give verbose info on gdb action. From main.c. */
119
120extern int info_verbose;
121
122/* The BFD for this file -- implicit parameter to next_symbol_text. */
123
124static bfd *symfile_bfd;
125
126/* The size of each symbol in the symbol file (in external form).
127 This is set by dbx_symfile_read when building psymtabs, and by
128 dbx_psymtab_to_symtab when building symtabs. */
129
130static unsigned symbol_size;
131
132/* This is the offset of the symbol table in the executable file. */
133
134static unsigned symbol_table_offset;
135
136/* This is the offset of the string table in the executable file. */
137
138static unsigned string_table_offset;
139
140/* For elf+stab executables, the n_strx field is not a simple index
141 into the string table. Instead, each .o file has a base offset in
142 the string table, and the associated symbols contain offsets from
143 this base. The following two variables contain the base offset for
144 the current and next .o files. */
145
146static unsigned int file_string_table_offset;
147static unsigned int next_file_string_table_offset;
148
149/* .o and NLM files contain unrelocated addresses which are based at
150 0. When non-zero, this flag disables some of the special cases for
151 Solaris elf+stab text addresses at location 0. */
152
153static int symfile_relocatable = 0;
154
155/* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are
156 relative to the function start address. */
157
158static int block_address_function_relative = 0;
159\f
160/* The lowest text address we have yet encountered. This is needed
161 because in an a.out file, there is no header field which tells us
162 what address the program is actually going to be loaded at, so we
163 need to make guesses based on the symbols (which *are* relocated to
164 reflect the address it will be loaded at). */
165
166static CORE_ADDR lowest_text_address;
167
168/* Non-zero if there is any line number info in the objfile. Prevents
169 end_psymtab from discarding an otherwise empty psymtab. */
170
171static int has_line_numbers;
172
173/* Complaints about the symbols we have encountered. */
174
c5aa993b
JM
175struct complaint lbrac_complaint =
176{"bad block start address patched", 0, 0};
c906108c
SS
177
178struct complaint string_table_offset_complaint =
c5aa993b 179{"bad string table offset in symbol %d", 0, 0};
c906108c
SS
180
181struct complaint unknown_symtype_complaint =
c5aa993b 182{"unknown symbol type %s", 0, 0};
c906108c
SS
183
184struct complaint unknown_symchar_complaint =
c5aa993b 185{"unknown symbol descriptor `%c'", 0, 0};
c906108c
SS
186
187struct complaint lbrac_rbrac_complaint =
c5aa993b 188{"block start larger than block end", 0, 0};
c906108c
SS
189
190struct complaint lbrac_unmatched_complaint =
c5aa993b 191{"unmatched N_LBRAC before symtab pos %d", 0, 0};
c906108c
SS
192
193struct complaint lbrac_mismatch_complaint =
c5aa993b 194{"N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", 0, 0};
c906108c
SS
195
196struct complaint repeated_header_complaint =
c5aa993b 197{"\"repeated\" header file %s not previously seen, at symtab pos %d", 0, 0};
c906108c
SS
198
199struct complaint unclaimed_bincl_complaint =
c5aa993b 200{"N_BINCL %s not in entries for any file, at symtab pos %d", 0, 0};
7a292a7a
SS
201\f
202/* find_text_range --- find start and end of loadable code sections
203
204 The find_text_range function finds the shortest address range that
205 encloses all sections containing executable code, and stores it in
206 objfile's text_addr and text_size members.
207
208 dbx_symfile_read will use this to finish off the partial symbol
209 table, in some cases. */
210
211static void
c5aa993b 212find_text_range (bfd * sym_bfd, struct objfile *objfile)
7a292a7a
SS
213{
214 asection *sec;
215 int found_any = 0;
b9179dbc
EZ
216 CORE_ADDR start = 0;
217 CORE_ADDR end = 0;
c5aa993b 218
7a292a7a
SS
219 for (sec = sym_bfd->sections; sec; sec = sec->next)
220 if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
221 {
222 CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
223 CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
224
225 if (found_any)
226 {
c5aa993b
JM
227 if (sec_start < start)
228 start = sec_start;
229 if (sec_end > end)
230 end = sec_end;
7a292a7a
SS
231 }
232 else
233 {
234 start = sec_start;
235 end = sec_end;
236 }
237
238 found_any = 1;
239 }
240
c5aa993b 241 if (!found_any)
7a292a7a
SS
242 error ("Can't find any code sections in symbol file");
243
244 DBX_TEXT_ADDR (objfile) = start;
245 DBX_TEXT_SIZE (objfile) = end - start;
246}
c5aa993b 247\f
7a292a7a
SS
248
249
c906108c
SS
250/* During initial symbol readin, we need to have a structure to keep
251 track of which psymtabs have which bincls in them. This structure
252 is used during readin to setup the list of dependencies within each
253 partial symbol table. */
254
255struct header_file_location
256{
257 char *name; /* Name of header file */
258 int instance; /* See above */
259 struct partial_symtab *pst; /* Partial symtab that has the
260 BINCL/EINCL defs for this file */
261};
262
263/* The actual list and controling variables */
264static struct header_file_location *bincl_list, *next_bincl;
265static int bincls_allocated;
266
267/* Local function prototypes */
268
a14ed312 269extern void _initialize_dbxread (void);
392a587b 270
a14ed312 271static void process_now (struct objfile *);
c906108c 272
a14ed312 273static void free_header_files (void);
c906108c 274
a14ed312 275static void init_header_files (void);
c906108c 276
a14ed312 277static void read_ofile_symtab (struct partial_symtab *);
c906108c 278
a14ed312 279static void dbx_psymtab_to_symtab (struct partial_symtab *);
c906108c 280
a14ed312 281static void dbx_psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 282
a14ed312 283static void read_dbx_dynamic_symtab (struct objfile *objfile);
c906108c 284
a14ed312 285static void read_dbx_symtab (struct objfile *);
c906108c 286
a14ed312 287static void free_bincl_list (struct objfile *);
c906108c 288
a14ed312 289static struct partial_symtab *find_corresponding_bincl_psymtab (char *, int);
c906108c 290
a14ed312 291static void add_bincl_to_list (struct partial_symtab *, char *, int);
c906108c 292
a14ed312 293static void init_bincl_list (int, struct objfile *);
c906108c 294
a14ed312 295static char *dbx_next_symbol_text (struct objfile *);
c906108c 296
a14ed312 297static void fill_symbuf (bfd *);
c906108c 298
a14ed312 299static void dbx_symfile_init (struct objfile *);
c906108c 300
a14ed312 301static void dbx_new_init (struct objfile *);
c906108c 302
a14ed312 303static void dbx_symfile_read (struct objfile *, int);
c906108c 304
a14ed312 305static void dbx_symfile_finish (struct objfile *);
c906108c 306
a14ed312 307static void record_minimal_symbol (char *, CORE_ADDR, int, struct objfile *);
c906108c 308
a14ed312 309static void add_new_header_file (char *, int);
c906108c 310
a14ed312 311static void add_old_header_file (char *, int);
c906108c 312
a14ed312 313static void add_this_object_header_file (int);
c906108c 314
a14ed312
KB
315static struct partial_symtab *start_psymtab (struct objfile *, char *,
316 CORE_ADDR, int,
317 struct partial_symbol **,
318 struct partial_symbol **);
d4f3574e 319
c906108c
SS
320/* Free up old header file tables */
321
322static void
fba45db2 323free_header_files (void)
c906108c
SS
324{
325 if (this_object_header_files)
326 {
b8c9b27d 327 xfree (this_object_header_files);
c906108c
SS
328 this_object_header_files = NULL;
329 }
330 n_allocated_this_object_header_files = 0;
331}
332
333/* Allocate new header file tables */
334
335static void
fba45db2 336init_header_files (void)
c906108c
SS
337{
338 n_allocated_this_object_header_files = 10;
339 this_object_header_files = (int *) xmalloc (10 * sizeof (int));
340}
341
342/* Add header file number I for this object file
343 at the next successive FILENUM. */
344
345static void
fba45db2 346add_this_object_header_file (int i)
c906108c
SS
347{
348 if (n_this_object_header_files == n_allocated_this_object_header_files)
349 {
350 n_allocated_this_object_header_files *= 2;
351 this_object_header_files
352 = (int *) xrealloc ((char *) this_object_header_files,
c5aa993b 353 n_allocated_this_object_header_files * sizeof (int));
c906108c
SS
354 }
355
356 this_object_header_files[n_this_object_header_files++] = i;
357}
358
359/* Add to this file an "old" header file, one already seen in
360 a previous object file. NAME is the header file's name.
361 INSTANCE is its instance code, to select among multiple
362 symbol tables for the same header file. */
363
364static void
fba45db2 365add_old_header_file (char *name, int instance)
c906108c
SS
366{
367 register struct header_file *p = HEADER_FILES (current_objfile);
368 register int i;
369
370 for (i = 0; i < N_HEADER_FILES (current_objfile); i++)
371 if (STREQ (p[i].name, name) && instance == p[i].instance)
372 {
373 add_this_object_header_file (i);
374 return;
375 }
376 complain (&repeated_header_complaint, name, symnum);
377}
378
379/* Add to this file a "new" header file: definitions for its types follow.
380 NAME is the header file's name.
381 Most often this happens only once for each distinct header file,
382 but not necessarily. If it happens more than once, INSTANCE has
383 a different value each time, and references to the header file
384 use INSTANCE values to select among them.
385
386 dbx output contains "begin" and "end" markers for each new header file,
387 but at this level we just need to know which files there have been;
388 so we record the file when its "begin" is seen and ignore the "end". */
389
390static void
fba45db2 391add_new_header_file (char *name, int instance)
c906108c
SS
392{
393 register int i;
394 register struct header_file *hfile;
395
396 /* Make sure there is room for one more header file. */
397
398 i = N_ALLOCATED_HEADER_FILES (current_objfile);
399
400 if (N_HEADER_FILES (current_objfile) == i)
401 {
402 if (i == 0)
403 {
404 N_ALLOCATED_HEADER_FILES (current_objfile) = 10;
405 HEADER_FILES (current_objfile) = (struct header_file *)
406 xmalloc (10 * sizeof (struct header_file));
407 }
408 else
409 {
410 i *= 2;
411 N_ALLOCATED_HEADER_FILES (current_objfile) = i;
412 HEADER_FILES (current_objfile) = (struct header_file *)
413 xrealloc ((char *) HEADER_FILES (current_objfile),
414 (i * sizeof (struct header_file)));
415 }
416 }
417
418 /* Create an entry for this header file. */
419
420 i = N_HEADER_FILES (current_objfile)++;
421 hfile = HEADER_FILES (current_objfile) + i;
c5aa993b 422 hfile->name = savestring (name, strlen (name));
c906108c
SS
423 hfile->instance = instance;
424 hfile->length = 10;
425 hfile->vector
426 = (struct type **) xmalloc (10 * sizeof (struct type *));
427 memset (hfile->vector, 0, 10 * sizeof (struct type *));
428
429 add_this_object_header_file (i);
430}
431
432#if 0
433static struct type **
fba45db2 434explicit_lookup_type (int real_filenum, int index)
c906108c
SS
435{
436 register struct header_file *f = &HEADER_FILES (current_objfile)[real_filenum];
437
438 if (index >= f->length)
439 {
440 f->length *= 2;
441 f->vector = (struct type **)
442 xrealloc (f->vector, f->length * sizeof (struct type *));
443 memset (&f->vector[f->length / 2],
c5aa993b 444 '\0', f->length * sizeof (struct type *) / 2);
c906108c
SS
445 }
446 return &f->vector[index];
447}
448#endif
449\f
450static void
fba45db2
KB
451record_minimal_symbol (char *name, CORE_ADDR address, int type,
452 struct objfile *objfile)
c906108c
SS
453{
454 enum minimal_symbol_type ms_type;
455 int section;
456 asection *bfd_section;
457
458 switch (type)
459 {
460 case N_TEXT | N_EXT:
461 ms_type = mst_text;
b8fbeb18 462 section = SECT_OFF_TEXT (objfile);
c906108c
SS
463 bfd_section = DBX_TEXT_SECTION (objfile);
464 break;
465 case N_DATA | N_EXT:
466 ms_type = mst_data;
b8fbeb18 467 section = SECT_OFF_DATA (objfile);
c906108c
SS
468 bfd_section = DBX_DATA_SECTION (objfile);
469 break;
470 case N_BSS | N_EXT:
471 ms_type = mst_bss;
b8fbeb18 472 section = SECT_OFF_BSS (objfile);
c906108c
SS
473 bfd_section = DBX_BSS_SECTION (objfile);
474 break;
475 case N_ABS | N_EXT:
476 ms_type = mst_abs;
477 section = -1;
478 bfd_section = NULL;
479 break;
480#ifdef N_SETV
481 case N_SETV | N_EXT:
482 ms_type = mst_data;
b8fbeb18 483 section = SECT_OFF_DATA (objfile);
c906108c
SS
484 bfd_section = DBX_DATA_SECTION (objfile);
485 break;
486 case N_SETV:
487 /* I don't think this type actually exists; since a N_SETV is the result
c5aa993b
JM
488 of going over many .o files, it doesn't make sense to have one
489 file local. */
c906108c 490 ms_type = mst_file_data;
b8fbeb18 491 section = SECT_OFF_DATA (objfile);
c906108c
SS
492 bfd_section = DBX_DATA_SECTION (objfile);
493 break;
494#endif
495 case N_TEXT:
496 case N_NBTEXT:
497 case N_FN:
498 case N_FN_SEQ:
499 ms_type = mst_file_text;
b8fbeb18 500 section = SECT_OFF_TEXT (objfile);
c906108c
SS
501 bfd_section = DBX_TEXT_SECTION (objfile);
502 break;
503 case N_DATA:
504 ms_type = mst_file_data;
505
506 /* Check for __DYNAMIC, which is used by Sun shared libraries.
c5aa993b
JM
507 Record it as global even if it's local, not global, so
508 lookup_minimal_symbol can find it. We don't check symbol_leading_char
509 because for SunOS4 it always is '_'. */
c906108c
SS
510 if (name[8] == 'C' && STREQ ("__DYNAMIC", name))
511 ms_type = mst_data;
512
513 /* Same with virtual function tables, both global and static. */
514 {
515 char *tempstring = name;
516 if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
517 ++tempstring;
015a42b4 518 if (is_vtable_name (tempstring))
c906108c
SS
519 ms_type = mst_data;
520 }
b8fbeb18 521 section = SECT_OFF_DATA (objfile);
c906108c
SS
522 bfd_section = DBX_DATA_SECTION (objfile);
523 break;
524 case N_BSS:
525 ms_type = mst_file_bss;
b8fbeb18 526 section = SECT_OFF_BSS (objfile);
c906108c
SS
527 bfd_section = DBX_BSS_SECTION (objfile);
528 break;
529 default:
530 ms_type = mst_unknown;
531 section = -1;
532 bfd_section = NULL;
533 break;
c5aa993b 534 }
c906108c
SS
535
536 if ((ms_type == mst_file_text || ms_type == mst_text)
537 && address < lowest_text_address)
538 lowest_text_address = address;
539
540 prim_record_minimal_symbol_and_info
541 (name, address, ms_type, NULL, section, bfd_section, objfile);
542}
543\f
544/* Scan and build partial symbols for a symbol file.
545 We have been initialized by a call to dbx_symfile_init, which
546 put all the relevant info into a "struct dbx_symfile_info",
547 hung off the objfile structure.
548
c906108c
SS
549 MAINLINE is true if we are reading the main symbol
550 table (as opposed to a shared lib or dynamically loaded file). */
551
552static void
9df3df99 553dbx_symfile_read (struct objfile *objfile, int mainline)
c906108c
SS
554{
555 bfd *sym_bfd;
556 int val;
557 struct cleanup *back_to;
558
c906108c
SS
559 sym_bfd = objfile->obfd;
560
561 /* .o and .nlm files are relocatables with text, data and bss segs based at
562 0. This flag disables special (Solaris stabs-in-elf only) fixups for
563 symbols with a value of 0. */
564
565 symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
566
567 /* This is true for Solaris (and all other systems which put stabs
568 in sections, hopefully, since it would be silly to do things
569 differently from Solaris), and false for SunOS4 and other a.out
570 file formats. */
571 block_address_function_relative =
572 ((0 == strncmp (bfd_get_target (sym_bfd), "elf", 3))
573 || (0 == strncmp (bfd_get_target (sym_bfd), "som", 3))
574 || (0 == strncmp (bfd_get_target (sym_bfd), "coff", 4))
575 || (0 == strncmp (bfd_get_target (sym_bfd), "pe", 2))
c2d11a7d 576 || (0 == strncmp (bfd_get_target (sym_bfd), "epoc-pe", 7))
c906108c
SS
577 || (0 == strncmp (bfd_get_target (sym_bfd), "nlm", 3)));
578
579 val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
580 if (val < 0)
581 perror_with_name (objfile->name);
582
583 /* If we are reinitializing, or if we have never loaded syms yet, init */
584 if (mainline
ef96bde8
EZ
585 || (objfile->global_psymbols.size == 0
586 && objfile->static_psymbols.size == 0))
c906108c
SS
587 init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
588
589 symbol_size = DBX_SYMBOL_SIZE (objfile);
590 symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
591
592 free_pending_blocks ();
a0b3c4fd 593 back_to = make_cleanup (really_free_pendings, 0);
c906108c
SS
594
595 init_minimal_symbol_collection ();
56e290f4 596 make_cleanup_discard_minimal_symbols ();
c906108c 597
d4f3574e 598 /* Read stabs data from executable file and define symbols. */
c906108c 599
d4f3574e 600 read_dbx_symtab (objfile);
c906108c
SS
601
602 /* Add the dynamic symbols. */
603
96baa820 604 read_dbx_dynamic_symtab (objfile);
c906108c
SS
605
606 /* Install any minimal symbols that have been collected as the current
607 minimal symbols for this objfile. */
608
609 install_minimal_symbols (objfile);
610
611 do_cleanups (back_to);
612}
613
614/* Initialize anything that needs initializing when a completely new
615 symbol file is specified (not just adding some symbols from another
616 file, e.g. a shared library). */
617
618static void
fba45db2 619dbx_new_init (struct objfile *ignore)
c906108c
SS
620{
621 stabsread_new_init ();
622 buildsym_new_init ();
623 init_header_files ();
624}
625
626
627/* dbx_symfile_init ()
628 is the dbx-specific initialization routine for reading symbols.
629 It is passed a struct objfile which contains, among other things,
630 the BFD for the file whose symbols are being read, and a slot for a pointer
631 to "private data" which we fill with goodies.
632
633 We read the string table into malloc'd space and stash a pointer to it.
634
635 Since BFD doesn't know how to read debug symbols in a format-independent
636 way (and may never do so...), we have to do it ourselves. We will never
637 be called unless this is an a.out (or very similar) file.
638 FIXME, there should be a cleaner peephole into the BFD environment here. */
639
c5aa993b 640#define DBX_STRINGTAB_SIZE_SIZE sizeof(long) /* FIXME */
c906108c
SS
641
642static void
fba45db2 643dbx_symfile_init (struct objfile *objfile)
c906108c
SS
644{
645 int val;
646 bfd *sym_bfd = objfile->obfd;
647 char *name = bfd_get_filename (sym_bfd);
648 asection *text_sect;
649 unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
650
651 /* Allocate struct to keep track of the symfile */
652 objfile->sym_stab_info = (struct dbx_symfile_info *)
c5aa993b 653 xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
c906108c
SS
654 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
655
656 DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
657 DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
658 DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
659
660 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
661#define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
662#define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
663
664 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
665
666 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
c5aa993b 667
c906108c
SS
668 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
669 if (!text_sect)
670 error ("Can't find .text section in symbol file");
671 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
672 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
673
674 DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
675 DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
676 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
677
678 /* Read the string table and stash it away in the psymbol_obstack. It is
679 only needed as long as we need to expand psymbols into full symbols,
680 so when we blow away the psymbol the string table goes away as well.
681 Note that gdb used to use the results of attempting to malloc the
682 string table, based on the size it read, as a form of sanity check
683 for botched byte swapping, on the theory that a byte swapped string
684 table size would be so totally bogus that the malloc would fail. Now
685 that we put in on the psymbol_obstack, we can't do this since gdb gets
686 a fatal error (out of virtual memory) if the size is bogus. We can
687 however at least check to see if the size is less than the size of
688 the size field itself, or larger than the size of the entire file.
689 Note that all valid string tables have a size greater than zero, since
690 the bytes used to hold the size are included in the count. */
691
692 if (STRING_TABLE_OFFSET == 0)
693 {
694 /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
c5aa993b
JM
695 will never be zero, even when there is no string table. This
696 would appear to be a bug in bfd. */
c906108c
SS
697 DBX_STRINGTAB_SIZE (objfile) = 0;
698 DBX_STRINGTAB (objfile) = NULL;
699 }
700 else
701 {
702 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
703 if (val < 0)
704 perror_with_name (name);
c5aa993b 705
c906108c
SS
706 memset ((PTR) size_temp, 0, sizeof (size_temp));
707 val = bfd_read ((PTR) size_temp, sizeof (size_temp), 1, sym_bfd);
708 if (val < 0)
709 {
710 perror_with_name (name);
711 }
712 else if (val == 0)
713 {
714 /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
715 EOF if there is no string table, and attempting to read the size
716 from EOF will read zero bytes. */
717 DBX_STRINGTAB_SIZE (objfile) = 0;
718 DBX_STRINGTAB (objfile) = NULL;
719 }
720 else
721 {
722 /* Read some data that would appear to be the string table size.
723 If there really is a string table, then it is probably the right
724 size. Byteswap if necessary and validate the size. Note that
725 the minimum is DBX_STRINGTAB_SIZE_SIZE. If we just read some
726 random data that happened to be at STRING_TABLE_OFFSET, because
727 bfd can't tell us there is no string table, the sanity checks may
728 or may not catch this. */
729 DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
c5aa993b 730
c906108c
SS
731 if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
732 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
733 error ("ridiculous string table size (%d bytes).",
734 DBX_STRINGTAB_SIZE (objfile));
c5aa993b 735
c906108c 736 DBX_STRINGTAB (objfile) =
c5aa993b 737 (char *) obstack_alloc (&objfile->psymbol_obstack,
c906108c
SS
738 DBX_STRINGTAB_SIZE (objfile));
739 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
c5aa993b 740
c906108c 741 /* Now read in the string table in one big gulp. */
c5aa993b 742
c906108c
SS
743 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
744 if (val < 0)
745 perror_with_name (name);
746 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
747 sym_bfd);
748 if (val != DBX_STRINGTAB_SIZE (objfile))
749 perror_with_name (name);
750 }
751 }
752}
753
754/* Perform any local cleanups required when we are done with a particular
755 objfile. I.E, we are in the process of discarding all symbol information
756 for an objfile, freeing up all memory held for it, and unlinking the
757 objfile struct from the global list of known objfiles. */
758
759static void
fba45db2 760dbx_symfile_finish (struct objfile *objfile)
c906108c
SS
761{
762 if (objfile->sym_stab_info != NULL)
763 {
764 if (HEADER_FILES (objfile) != NULL)
765 {
766 register int i = N_HEADER_FILES (objfile);
767 register struct header_file *hfiles = HEADER_FILES (objfile);
768
769 while (--i >= 0)
770 {
b8c9b27d
KB
771 xfree (hfiles[i].name);
772 xfree (hfiles[i].vector);
c906108c 773 }
b8c9b27d 774 xfree (hfiles);
c906108c 775 }
c5aa993b 776 mfree (objfile->md, objfile->sym_stab_info);
c906108c
SS
777 }
778 free_header_files ();
779}
c906108c 780\f
c5aa993b 781
c906108c
SS
782/* Buffer for reading the symbol table entries. */
783static struct external_nlist symbuf[4096];
784static int symbuf_idx;
785static int symbuf_end;
786
787/* cont_elem is used for continuing information in cfront.
788 It saves information about which types need to be fixed up and
789 completed after all the stabs are read. */
c5aa993b 790struct cont_elem
c906108c 791 {
0fb34c3a 792 /* sym and stabstring for continuing information in cfront */
c5aa993b
JM
793 struct symbol *sym;
794 char *stabs;
114d1f2c 795 /* state dependencies (statics that must be preserved) */
c906108c
SS
796 int sym_idx;
797 int sym_end;
798 int symnum;
507f3c78 799 int (*func) (struct objfile *, struct symbol *, char *);
114d1f2c 800 /* other state dependencies include:
c906108c 801 (assumption is that these will not change since process_now FIXME!!)
c5aa993b
JM
802 stringtab_global
803 n_stabs
804 objfile
805 symfile_bfd */
806 };
c906108c
SS
807
808static struct cont_elem *cont_list = 0;
809static int cont_limit = 0;
810static int cont_count = 0;
811
812/* Arrange for function F to be called with arguments SYM and P later
813 in the stabs reading process. */
c5aa993b 814void
9df3df99
KB
815process_later (struct symbol *sym, char *p,
816 int (*f) (struct objfile *, struct symbol *, char *))
c906108c
SS
817{
818
819 /* Allocate more space for the deferred list. */
820 if (cont_count >= cont_limit - 1)
821 {
c5aa993b 822 cont_limit += 32; /* chunk size */
c906108c
SS
823
824 cont_list
c5aa993b
JM
825 = (struct cont_elem *) xrealloc (cont_list,
826 (cont_limit
827 * sizeof (struct cont_elem)));
c906108c 828 if (!cont_list)
c5aa993b 829 error ("Virtual memory exhausted\n");
c906108c
SS
830 }
831
832 /* Save state variables so we can process these stabs later. */
833 cont_list[cont_count].sym_idx = symbuf_idx;
834 cont_list[cont_count].sym_end = symbuf_end;
835 cont_list[cont_count].symnum = symnum;
836 cont_list[cont_count].sym = sym;
837 cont_list[cont_count].stabs = p;
838 cont_list[cont_count].func = f;
839 cont_count++;
840}
841
842/* Call deferred funtions in CONT_LIST. */
843
c5aa993b 844static void
fba45db2 845process_now (struct objfile *objfile)
c906108c
SS
846{
847 int i;
848 int save_symbuf_idx;
849 int save_symbuf_end;
850 int save_symnum;
851 struct symbol *sym;
852 char *stabs;
853 int err;
507f3c78 854 int (*func) (struct objfile *, struct symbol *, char *);
c906108c
SS
855
856 /* Save the state of our caller, we'll want to restore it before
857 returning. */
858 save_symbuf_idx = symbuf_idx;
859 save_symbuf_end = symbuf_end;
860 save_symnum = symnum;
861
862 /* Iterate over all the deferred stabs. */
863 for (i = 0; i < cont_count; i++)
864 {
865 /* Restore the state for this deferred stab. */
866 symbuf_idx = cont_list[i].sym_idx;
c5aa993b
JM
867 symbuf_end = cont_list[i].sym_end;
868 symnum = cont_list[i].symnum;
c906108c
SS
869 sym = cont_list[i].sym;
870 stabs = cont_list[i].stabs;
871 func = cont_list[i].func;
872
873 /* Call the function to handle this deferrd stab. */
874 err = (*func) (objfile, sym, stabs);
875 if (err)
876 error ("Internal error: unable to resolve stab.\n");
877 }
878
879 /* Restore our caller's state. */
880 symbuf_idx = save_symbuf_idx;
881 symbuf_end = save_symbuf_end;
882 symnum = save_symnum;
883 cont_count = 0;
884}
885
886
887/* Name of last function encountered. Used in Solaris to approximate
888 object file boundaries. */
889static char *last_function_name;
890
891/* The address in memory of the string table of the object file we are
892 reading (which might not be the "main" object file, but might be a
893 shared library or some other dynamically loaded thing). This is
894 set by read_dbx_symtab when building psymtabs, and by
895 read_ofile_symtab when building symtabs, and is used only by
896 next_symbol_text. FIXME: If that is true, we don't need it when
897 building psymtabs, right? */
898static char *stringtab_global;
899
900/* These variables are used to control fill_symbuf when the stabs
901 symbols are not contiguous (as may be the case when a COFF file is
902 linked using --split-by-reloc). */
903static struct stab_section_list *symbuf_sections;
904static unsigned int symbuf_left;
905static unsigned int symbuf_read;
906
907/* Refill the symbol table input buffer
908 and set the variables that control fetching entries from it.
909 Reports an error if no data available.
910 This function can read past the end of the symbol table
911 (into the string table) but this does no harm. */
912
913static void
fba45db2 914fill_symbuf (bfd *sym_bfd)
c906108c
SS
915{
916 unsigned int count;
917 int nbytes;
918
919 if (symbuf_sections == NULL)
920 count = sizeof (symbuf);
921 else
922 {
923 if (symbuf_left <= 0)
924 {
925 file_ptr filepos = symbuf_sections->section->filepos;
926 if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
927 perror_with_name (bfd_get_filename (sym_bfd));
928 symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
929 symbol_table_offset = filepos - symbuf_read;
930 symbuf_sections = symbuf_sections->next;
931 }
932
933 count = symbuf_left;
934 if (count > sizeof (symbuf))
935 count = sizeof (symbuf);
936 }
937
c5aa993b 938 nbytes = bfd_read ((PTR) symbuf, count, 1, sym_bfd);
c906108c
SS
939 if (nbytes < 0)
940 perror_with_name (bfd_get_filename (sym_bfd));
941 else if (nbytes == 0)
942 error ("Premature end of file reading symbol table");
943 symbuf_end = nbytes / symbol_size;
944 symbuf_idx = 0;
945 symbuf_left -= nbytes;
946 symbuf_read += nbytes;
947}
948
c906108c
SS
949#define INTERNALIZE_SYMBOL(intern, extern, abfd) \
950 { \
951 (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type); \
952 (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx); \
953 (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc); \
40b3352b
L
954 if (bfd_get_sign_extend_vma (abfd)) \
955 (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value); \
956 else \
957 (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value); \
c906108c
SS
958 }
959
960/* Invariant: The symbol pointed to by symbuf_idx is the first one
961 that hasn't been swapped. Swap the symbol at the same time
962 that symbuf_idx is incremented. */
963
964/* dbx allows the text of a symbol name to be continued into the
965 next symbol name! When such a continuation is encountered
966 (a \ at the end of the text of a name)
967 call this function to get the continuation. */
968
969static char *
fba45db2 970dbx_next_symbol_text (struct objfile *objfile)
c906108c
SS
971{
972 struct internal_nlist nlist;
973
974 if (symbuf_idx == symbuf_end)
975 fill_symbuf (symfile_bfd);
976
977 symnum++;
c5aa993b 978 INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
c906108c
SS
979 OBJSTAT (objfile, n_stabs++);
980
981 symbuf_idx++;
982
983 return nlist.n_strx + stringtab_global + file_string_table_offset;
984}
985\f
986/* Initialize the list of bincls to contain none and have some
987 allocated. */
988
989static void
fba45db2 990init_bincl_list (int number, struct objfile *objfile)
c906108c
SS
991{
992 bincls_allocated = number;
993 next_bincl = bincl_list = (struct header_file_location *)
c5aa993b 994 xmmalloc (objfile->md, bincls_allocated * sizeof (struct header_file_location));
c906108c
SS
995}
996
997/* Add a bincl to the list. */
998
999static void
fba45db2 1000add_bincl_to_list (struct partial_symtab *pst, char *name, int instance)
c906108c
SS
1001{
1002 if (next_bincl >= bincl_list + bincls_allocated)
1003 {
1004 int offset = next_bincl - bincl_list;
1005 bincls_allocated *= 2;
1006 bincl_list = (struct header_file_location *)
c5aa993b
JM
1007 xmrealloc (pst->objfile->md, (char *) bincl_list,
1008 bincls_allocated * sizeof (struct header_file_location));
c906108c
SS
1009 next_bincl = bincl_list + offset;
1010 }
1011 next_bincl->pst = pst;
1012 next_bincl->instance = instance;
1013 next_bincl++->name = name;
1014}
1015
1016/* Given a name, value pair, find the corresponding
1017 bincl in the list. Return the partial symtab associated
1018 with that header_file_location. */
1019
1020static struct partial_symtab *
fba45db2 1021find_corresponding_bincl_psymtab (char *name, int instance)
c906108c
SS
1022{
1023 struct header_file_location *bincl;
1024
1025 for (bincl = bincl_list; bincl < next_bincl; bincl++)
1026 if (bincl->instance == instance
1027 && STREQ (name, bincl->name))
1028 return bincl->pst;
1029
1030 complain (&repeated_header_complaint, name, symnum);
1031 return (struct partial_symtab *) 0;
1032}
1033
1034/* Free the storage allocated for the bincl list. */
1035
1036static void
fba45db2 1037free_bincl_list (struct objfile *objfile)
c906108c 1038{
c5aa993b 1039 mfree (objfile->md, (PTR) bincl_list);
c906108c
SS
1040 bincls_allocated = 0;
1041}
1042
74b7792f
AC
1043static void
1044do_free_bincl_list_cleanup (void *objfile)
1045{
1046 free_bincl_list (objfile);
1047}
1048
1049static struct cleanup *
1050make_cleanup_free_bincl_list (struct objfile *objfile)
1051{
1052 return make_cleanup (do_free_bincl_list_cleanup, objfile);
1053}
1054
c906108c
SS
1055/* Scan a SunOs dynamic symbol table for symbols of interest and
1056 add them to the minimal symbol table. */
1057
1058static void
fba45db2 1059read_dbx_dynamic_symtab (struct objfile *objfile)
c906108c
SS
1060{
1061 bfd *abfd = objfile->obfd;
1062 struct cleanup *back_to;
1063 int counter;
1064 long dynsym_size;
1065 long dynsym_count;
1066 asymbol **dynsyms;
1067 asymbol **symptr;
1068 arelent **relptr;
1069 long dynrel_size;
1070 long dynrel_count;
1071 arelent **dynrels;
1072 CORE_ADDR sym_value;
1073 char *name;
1074
1075 /* Check that the symbol file has dynamic symbols that we know about.
1076 bfd_arch_unknown can happen if we are reading a sun3 symbol file
1077 on a sun4 host (and vice versa) and bfd is not configured
1078 --with-target=all. This would trigger an assertion in bfd/sunos.c,
1079 so we ignore the dynamic symbols in this case. */
1080 if (bfd_get_flavour (abfd) != bfd_target_aout_flavour
1081 || (bfd_get_file_flags (abfd) & DYNAMIC) == 0
1082 || bfd_get_arch (abfd) == bfd_arch_unknown)
1083 return;
1084
1085 dynsym_size = bfd_get_dynamic_symtab_upper_bound (abfd);
1086 if (dynsym_size < 0)
1087 return;
1088
1089 dynsyms = (asymbol **) xmalloc (dynsym_size);
b8c9b27d 1090 back_to = make_cleanup (xfree, dynsyms);
c906108c
SS
1091
1092 dynsym_count = bfd_canonicalize_dynamic_symtab (abfd, dynsyms);
1093 if (dynsym_count < 0)
1094 {
1095 do_cleanups (back_to);
1096 return;
1097 }
1098
1099 /* Enter dynamic symbols into the minimal symbol table
1100 if this is a stripped executable. */
1101 if (bfd_get_symcount (abfd) <= 0)
1102 {
1103 symptr = dynsyms;
1104 for (counter = 0; counter < dynsym_count; counter++, symptr++)
1105 {
1106 asymbol *sym = *symptr;
1107 asection *sec;
1108 int type;
1109
1110 sec = bfd_get_section (sym);
1111
1112 /* BFD symbols are section relative. */
1113 sym_value = sym->value + sec->vma;
1114
1115 if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
1116 {
b8fbeb18 1117 sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1118 type = N_TEXT;
1119 }
1120 else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
1121 {
b8fbeb18 1122 sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
c906108c
SS
1123 type = N_DATA;
1124 }
1125 else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
1126 {
b8fbeb18 1127 sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_BSS (objfile));
c906108c
SS
1128 type = N_BSS;
1129 }
1130 else
1131 continue;
1132
1133 if (sym->flags & BSF_GLOBAL)
1134 type |= N_EXT;
1135
1136 record_minimal_symbol ((char *) bfd_asymbol_name (sym), sym_value,
1137 type, objfile);
1138 }
1139 }
1140
1141 /* Symbols from shared libraries have a dynamic relocation entry
1142 that points to the associated slot in the procedure linkage table.
1143 We make a mininal symbol table entry with type mst_solib_trampoline
1144 at the address in the procedure linkage table. */
1145 dynrel_size = bfd_get_dynamic_reloc_upper_bound (abfd);
1146 if (dynrel_size < 0)
1147 {
1148 do_cleanups (back_to);
1149 return;
1150 }
c5aa993b 1151
c906108c 1152 dynrels = (arelent **) xmalloc (dynrel_size);
b8c9b27d 1153 make_cleanup (xfree, dynrels);
c906108c
SS
1154
1155 dynrel_count = bfd_canonicalize_dynamic_reloc (abfd, dynrels, dynsyms);
1156 if (dynrel_count < 0)
1157 {
1158 do_cleanups (back_to);
1159 return;
1160 }
1161
1162 for (counter = 0, relptr = dynrels;
1163 counter < dynrel_count;
1164 counter++, relptr++)
1165 {
1166 arelent *rel = *relptr;
1167 CORE_ADDR address =
b8fbeb18 1168 rel->address + ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
c906108c
SS
1169
1170 switch (bfd_get_arch (abfd))
1171 {
1172 case bfd_arch_sparc:
1173 if (rel->howto->type != RELOC_JMP_SLOT)
1174 continue;
1175 break;
1176 case bfd_arch_m68k:
1177 /* `16' is the type BFD produces for a jump table relocation. */
1178 if (rel->howto->type != 16)
1179 continue;
1180
1181 /* Adjust address in the jump table to point to
1182 the start of the bsr instruction. */
1183 address -= 2;
1184 break;
1185 default:
1186 continue;
1187 }
1188
1189 name = (char *) bfd_asymbol_name (*rel->sym_ptr_ptr);
1190 prim_record_minimal_symbol (name, address, mst_solib_trampoline,
1191 objfile);
1192 }
1193
1194 do_cleanups (back_to);
1195}
1196
d4f3574e
SS
1197/* Setup partial_symtab's describing each source file for which
1198 debugging information is available. */
c906108c
SS
1199
1200static void
fba45db2 1201read_dbx_symtab (struct objfile *objfile)
c906108c
SS
1202{
1203 register struct external_nlist *bufp = 0; /* =0 avoids gcc -Wall glitch */
1204 struct internal_nlist nlist;
d4f3574e
SS
1205 CORE_ADDR text_addr;
1206 int text_size;
c906108c
SS
1207
1208 register char *namestring;
1209 int nsl;
1210 int past_first_source_file = 0;
1211 CORE_ADDR last_o_file_start = 0;
1212 CORE_ADDR last_function_start = 0;
1213 struct cleanup *back_to;
1214 bfd *abfd;
1215 int textlow_not_set;
1216
1217 /* Current partial symtab */
1218 struct partial_symtab *pst;
1219
1220 /* List of current psymtab's include files */
1221 char **psymtab_include_list;
1222 int includes_allocated;
1223 int includes_used;
1224
1225 /* Index within current psymtab dependency list */
1226 struct partial_symtab **dependency_list;
1227 int dependencies_used, dependencies_allocated;
1228
d4f3574e
SS
1229 text_addr = DBX_TEXT_ADDR (objfile);
1230 text_size = DBX_TEXT_SIZE (objfile);
1231
c906108c
SS
1232 /* FIXME. We probably want to change stringtab_global rather than add this
1233 while processing every symbol entry. FIXME. */
1234 file_string_table_offset = 0;
1235 next_file_string_table_offset = 0;
1236
1237 stringtab_global = DBX_STRINGTAB (objfile);
c5aa993b 1238
c906108c
SS
1239 pst = (struct partial_symtab *) 0;
1240
1241 includes_allocated = 30;
1242 includes_used = 0;
1243 psymtab_include_list = (char **) alloca (includes_allocated *
1244 sizeof (char *));
1245
1246 dependencies_allocated = 30;
1247 dependencies_used = 0;
1248 dependency_list =
1249 (struct partial_symtab **) alloca (dependencies_allocated *
1250 sizeof (struct partial_symtab *));
1251
1252 /* Init bincl list */
1253 init_bincl_list (20, objfile);
74b7792f 1254 back_to = make_cleanup_free_bincl_list (objfile);
c906108c
SS
1255
1256 last_source_file = NULL;
1257
96baa820 1258 lowest_text_address = (CORE_ADDR) -1;
c906108c
SS
1259
1260 symfile_bfd = objfile->obfd; /* For next_text_symbol */
1261 abfd = objfile->obfd;
1262 symbuf_end = symbuf_idx = 0;
1263 next_symbol_text_func = dbx_next_symbol_text;
1264 textlow_not_set = 1;
1265 has_line_numbers = 0;
1266
1267 for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1268 {
1269 /* Get the symbol for this run and pull out some info */
c5aa993b 1270 QUIT; /* allow this to be interruptable */
c906108c
SS
1271 if (symbuf_idx == symbuf_end)
1272 fill_symbuf (abfd);
1273 bufp = &symbuf[symbuf_idx++];
1274
1275 /*
1276 * Special case to speed up readin.
1277 */
1278 if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
1279 {
1280 has_line_numbers = 1;
1281 continue;
1282 }
1283
1284 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1285 OBJSTAT (objfile, n_stabs++);
1286
1287 /* Ok. There is a lot of code duplicated in the rest of this
1288 switch statement (for efficiency reasons). Since I don't
1289 like duplicating code, I will do my penance here, and
1290 describe the code which is duplicated:
1291
c5aa993b
JM
1292 *) The assignment to namestring.
1293 *) The call to strchr.
1294 *) The addition of a partial symbol the the two partial
1295 symbol lists. This last is a large section of code, so
1296 I've imbedded it in the following macro.
1297 */
1298
c906108c
SS
1299/* Set namestring based on nlist. If the string table index is invalid,
1300 give a fake name, and print a single error message per symbol file read,
1301 rather than abort the symbol reading or flood the user with messages. */
1302
1303/*FIXME: Too many adds and indirections in here for the inner loop. */
1304#define SET_NAMESTRING()\
1305 if (((unsigned)CUR_SYMBOL_STRX + file_string_table_offset) >= \
1306 DBX_STRINGTAB_SIZE (objfile)) { \
1307 complain (&string_table_offset_complaint, symnum); \
1308 namestring = "<bad string table offset>"; \
1309 } else \
1310 namestring = CUR_SYMBOL_STRX + file_string_table_offset + \
1311 DBX_STRINGTAB (objfile)
1312
1313#define CUR_SYMBOL_TYPE nlist.n_type
1314#define CUR_SYMBOL_VALUE nlist.n_value
1315#define CUR_SYMBOL_STRX nlist.n_strx
1316#define DBXREAD_ONLY
d4f3574e
SS
1317#define START_PSYMTAB(ofile,fname,low,symoff,global_syms,static_syms)\
1318 start_psymtab(ofile, fname, low, symoff, global_syms, static_syms)
c906108c
SS
1319#define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps,textlow_not_set)\
1320 end_psymtab(pst,ilist,ninc,c_off,c_text,dep_list,n_deps,textlow_not_set)
1321
1322#include "partial-stab.h"
1323 }
1324
1325 /* If there's stuff to be cleaned up, clean it up. */
c5aa993b 1326 if (DBX_SYMCOUNT (objfile) > 0 /* We have some syms */
c906108c
SS
1327/*FIXME, does this have a bug at start address 0? */
1328 && last_o_file_start
c5aa993b
JM
1329 && objfile->ei.entry_point < nlist.n_value
1330 && objfile->ei.entry_point >= last_o_file_start)
c906108c 1331 {
c5aa993b
JM
1332 objfile->ei.entry_file_lowpc = last_o_file_start;
1333 objfile->ei.entry_file_highpc = nlist.n_value;
c906108c
SS
1334 }
1335
1336 if (pst)
1337 {
1338 /* Don't set pst->texthigh lower than it already is. */
1339 CORE_ADDR text_end =
96baa820 1340 (lowest_text_address == (CORE_ADDR) -1
b8fbeb18 1341 ? (text_addr + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)))
c5aa993b
JM
1342 : lowest_text_address)
1343 + text_size;
c906108c
SS
1344
1345 end_psymtab (pst, psymtab_include_list, includes_used,
1346 symnum * symbol_size,
1347 text_end > pst->texthigh ? text_end : pst->texthigh,
1348 dependency_list, dependencies_used, textlow_not_set);
1349 }
1350
1351 do_cleanups (back_to);
1352}
1353
1354/* Allocate and partially fill a partial symtab. It will be
1355 completely filled at the end of the symbol list.
1356
1357 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1358 is the address relative to which its symbols are (incremental) or 0
1359 (normal). */
1360
1361
d4f3574e 1362static struct partial_symtab *
fba45db2
KB
1363start_psymtab (struct objfile *objfile, char *filename, CORE_ADDR textlow,
1364 int ldsymoff, struct partial_symbol **global_syms,
1365 struct partial_symbol **static_syms)
c906108c
SS
1366{
1367 struct partial_symtab *result =
d4f3574e 1368 start_psymtab_common (objfile, objfile->section_offsets,
c5aa993b 1369 filename, textlow, global_syms, static_syms);
c906108c
SS
1370
1371 result->read_symtab_private = (char *)
c5aa993b
JM
1372 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct symloc));
1373 LDSYMOFF (result) = ldsymoff;
c906108c 1374 result->read_symtab = dbx_psymtab_to_symtab;
c5aa993b
JM
1375 SYMBOL_SIZE (result) = symbol_size;
1376 SYMBOL_OFFSET (result) = symbol_table_offset;
1377 STRING_OFFSET (result) = string_table_offset;
1378 FILE_STRING_OFFSET (result) = file_string_table_offset;
c906108c
SS
1379
1380 /* If we're handling an ELF file, drag some section-relocation info
1381 for this source file out of the ELF symbol table, to compensate for
1382 Sun brain death. This replaces the section_offsets in this psymtab,
1383 if successful. */
1384 elfstab_offset_sections (objfile, result);
1385
1386 /* Deduce the source language from the filename for this psymtab. */
1387 psymtab_language = deduce_language_from_filename (filename);
1388
1389 return result;
1390}
1391
1392/* Close off the current usage of PST.
1393 Returns PST or NULL if the partial symtab was empty and thrown away.
1394
1395 FIXME: List variables and peculiarities of same. */
1396
1397struct partial_symtab *
fba45db2
KB
1398end_psymtab (struct partial_symtab *pst, char **include_list, int num_includes,
1399 int capping_symbol_offset, CORE_ADDR capping_text,
1400 struct partial_symtab **dependency_list, int number_dependencies,
1401 int textlow_not_set)
c906108c
SS
1402{
1403 int i;
c5aa993b 1404 struct objfile *objfile = pst->objfile;
c906108c
SS
1405
1406 if (capping_symbol_offset != -1)
c5aa993b 1407 LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
c906108c
SS
1408 pst->texthigh = capping_text;
1409
1410#ifdef SOFUN_ADDRESS_MAYBE_MISSING
1411 /* Under Solaris, the N_SO symbols always have a value of 0,
1412 instead of the usual address of the .o file. Therefore,
1413 we have to do some tricks to fill in texthigh and textlow.
1414 The first trick is in partial-stab.h: if we see a static
1415 or global function, and the textlow for the current pst
1416 is not set (ie: textlow_not_set), then we use that function's
1417 address for the textlow of the pst. */
1418
1419 /* Now, to fill in texthigh, we remember the last function seen
1420 in the .o file (also in partial-stab.h). Also, there's a hack in
1421 bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
1422 to here via the misc_info field. Therefore, we can fill in
1423 a reliable texthigh by taking the address plus size of the
1424 last function in the file. */
1425
1426 if (pst->texthigh == 0 && last_function_name)
1427 {
1428 char *p;
1429 int n;
1430 struct minimal_symbol *minsym;
1431
1432 p = strchr (last_function_name, ':');
1433 if (p == NULL)
1434 p = last_function_name;
1435 n = p - last_function_name;
1436 p = alloca (n + 2);
1437 strncpy (p, last_function_name, n);
1438 p[n] = 0;
c5aa993b 1439
c906108c
SS
1440 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
1441 if (minsym == NULL)
1442 {
1443 /* Sun Fortran appends an underscore to the minimal symbol name,
1444 try again with an appended underscore if the minimal symbol
1445 was not found. */
1446 p[n] = '_';
1447 p[n + 1] = 0;
1448 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
1449 }
1450
1451 if (minsym)
1452 pst->texthigh = SYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym);
1453
1454 last_function_name = NULL;
1455 }
1456
1457 /* this test will be true if the last .o file is only data */
1458 if (textlow_not_set)
1459 pst->textlow = pst->texthigh;
1460 else
1461 {
1462 struct partial_symtab *p1;
1463
1464 /* If we know our own starting text address, then walk through all other
c5aa993b
JM
1465 psymtabs for this objfile, and if any didn't know their ending text
1466 address, set it to our starting address. Take care to not set our
1467 own ending address to our starting address, nor to set addresses on
1468 `dependency' files that have both textlow and texthigh zero. */
c906108c
SS
1469
1470 ALL_OBJFILE_PSYMTABS (objfile, p1)
c5aa993b
JM
1471 {
1472 if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
1473 {
1474 p1->texthigh = pst->textlow;
1475 /* if this file has only data, then make textlow match texthigh */
1476 if (p1->textlow == 0)
1477 p1->textlow = p1->texthigh;
1478 }
1479 }
c906108c
SS
1480 }
1481
1482 /* End of kludge for patching Solaris textlow and texthigh. */
1483#endif /* SOFUN_ADDRESS_MAYBE_MISSING. */
1484
1485 pst->n_global_syms =
1486 objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
1487 pst->n_static_syms =
1488 objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
1489
1490 pst->number_of_dependencies = number_dependencies;
1491 if (number_dependencies)
1492 {
1493 pst->dependencies = (struct partial_symtab **)
1494 obstack_alloc (&objfile->psymbol_obstack,
c5aa993b 1495 number_dependencies * sizeof (struct partial_symtab *));
c906108c 1496 memcpy (pst->dependencies, dependency_list,
c5aa993b 1497 number_dependencies * sizeof (struct partial_symtab *));
c906108c
SS
1498 }
1499 else
1500 pst->dependencies = 0;
1501
1502 for (i = 0; i < num_includes; i++)
1503 {
1504 struct partial_symtab *subpst =
c5aa993b 1505 allocate_psymtab (include_list[i], objfile);
c906108c 1506
b8fbeb18 1507 /* Copy the sesction_offsets array from the main psymtab. */
c906108c
SS
1508 subpst->section_offsets = pst->section_offsets;
1509 subpst->read_symtab_private =
c5aa993b
JM
1510 (char *) obstack_alloc (&objfile->psymbol_obstack,
1511 sizeof (struct symloc));
1512 LDSYMOFF (subpst) =
1513 LDSYMLEN (subpst) =
1514 subpst->textlow =
1515 subpst->texthigh = 0;
c906108c
SS
1516
1517 /* We could save slight bits of space by only making one of these,
c5aa993b 1518 shared by the entire set of include files. FIXME-someday. */
c906108c
SS
1519 subpst->dependencies = (struct partial_symtab **)
1520 obstack_alloc (&objfile->psymbol_obstack,
1521 sizeof (struct partial_symtab *));
1522 subpst->dependencies[0] = pst;
1523 subpst->number_of_dependencies = 1;
1524
1525 subpst->globals_offset =
1526 subpst->n_global_syms =
c5aa993b
JM
1527 subpst->statics_offset =
1528 subpst->n_static_syms = 0;
c906108c
SS
1529
1530 subpst->readin = 0;
1531 subpst->symtab = 0;
1532 subpst->read_symtab = pst->read_symtab;
1533 }
1534
1535 sort_pst_symbols (pst);
1536
1537 /* If there is already a psymtab or symtab for a file of this name, remove it.
1538 (If there is a symtab, more drastic things also happen.)
1539 This happens in VxWorks. */
1540 free_named_symtabs (pst->filename);
1541
1542 if (num_includes == 0
1543 && number_dependencies == 0
1544 && pst->n_global_syms == 0
1545 && pst->n_static_syms == 0
1546 && has_line_numbers == 0)
1547 {
1548 /* Throw away this psymtab, it's empty. We can't deallocate it, since
c5aa993b 1549 it is on the obstack, but we can forget to chain it on the list. */
c906108c 1550 /* Empty psymtabs happen as a result of header files which don't have
c5aa993b
JM
1551 any symbols in them. There can be a lot of them. But this check
1552 is wrong, in that a psymtab with N_SLINE entries but nothing else
1553 is not empty, but we don't realize that. Fixing that without slowing
1554 things down might be tricky. */
c906108c
SS
1555
1556 discard_psymtab (pst);
1557
1558 /* Indicate that psymtab was thrown away. */
c5aa993b 1559 pst = (struct partial_symtab *) NULL;
c906108c
SS
1560 }
1561 return pst;
1562}
1563\f
1564static void
fba45db2 1565dbx_psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c
SS
1566{
1567 struct cleanup *old_chain;
1568 int i;
c5aa993b 1569
c906108c
SS
1570 if (!pst)
1571 return;
1572
1573 if (pst->readin)
1574 {
1575 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
c5aa993b 1576 pst->filename);
c906108c
SS
1577 return;
1578 }
1579
1580 /* Read in all partial symtabs on which this one is dependent */
1581 for (i = 0; i < pst->number_of_dependencies; i++)
1582 if (!pst->dependencies[i]->readin)
1583 {
1584 /* Inform about additional files that need to be read in. */
1585 if (info_verbose)
1586 {
1587 fputs_filtered (" ", gdb_stdout);
1588 wrap_here ("");
1589 fputs_filtered ("and ", gdb_stdout);
1590 wrap_here ("");
1591 printf_filtered ("%s...", pst->dependencies[i]->filename);
c5aa993b 1592 wrap_here (""); /* Flush output */
c906108c
SS
1593 gdb_flush (gdb_stdout);
1594 }
1595 dbx_psymtab_to_symtab_1 (pst->dependencies[i]);
1596 }
1597
c5aa993b 1598 if (LDSYMLEN (pst)) /* Otherwise it's a dummy */
c906108c
SS
1599 {
1600 /* Init stuff necessary for reading in symbols */
1601 stabsread_init ();
1602 buildsym_init ();
a0b3c4fd 1603 old_chain = make_cleanup (really_free_pendings, 0);
c906108c
SS
1604 file_string_table_offset = FILE_STRING_OFFSET (pst);
1605 symbol_size = SYMBOL_SIZE (pst);
1606
1607 /* Read in this file's symbols */
1608 bfd_seek (pst->objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
1609 read_ofile_symtab (pst);
1610 sort_symtab_syms (pst->symtab);
1611
1612 do_cleanups (old_chain);
1613 }
1614
1615 pst->readin = 1;
1616}
1617
1618/* Read in all of the symbols for a given psymtab for real.
1619 Be verbose about it if the user wants that. */
1620
1621static void
fba45db2 1622dbx_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
1623{
1624 bfd *sym_bfd;
1625
1626 if (!pst)
1627 return;
1628
1629 if (pst->readin)
1630 {
1631 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
c5aa993b 1632 pst->filename);
c906108c
SS
1633 return;
1634 }
1635
c5aa993b 1636 if (LDSYMLEN (pst) || pst->number_of_dependencies)
c906108c
SS
1637 {
1638 /* Print the message now, before reading the string table,
c5aa993b 1639 to avoid disconcerting pauses. */
c906108c
SS
1640 if (info_verbose)
1641 {
1642 printf_filtered ("Reading in symbols for %s...", pst->filename);
1643 gdb_flush (gdb_stdout);
1644 }
1645
1646 sym_bfd = pst->objfile->obfd;
1647
1648 next_symbol_text_func = dbx_next_symbol_text;
1649
1650 dbx_psymtab_to_symtab_1 (pst);
1651
1652 /* Match with global symbols. This only needs to be done once,
1653 after all of the symtabs and dependencies have been read in. */
1654 scan_file_globals (pst->objfile);
1655
1656 /* Finish up the debug error message. */
1657 if (info_verbose)
1658 printf_filtered ("done.\n");
1659 }
1660}
1661
1662/* Read in a defined section of a specific object file's symbols. */
c5aa993b 1663
c906108c 1664static void
fba45db2 1665read_ofile_symtab (struct partial_symtab *pst)
c906108c
SS
1666{
1667 register char *namestring;
1668 register struct external_nlist *bufp;
1669 struct internal_nlist nlist;
1670 unsigned char type;
1671 unsigned max_symnum;
1672 register bfd *abfd;
1673 struct objfile *objfile;
1674 int sym_offset; /* Offset to start of symbols to read */
1675 int sym_size; /* Size of symbols to read */
1676 CORE_ADDR text_offset; /* Start of text segment for symbols */
1677 int text_size; /* Size of text segment for symbols */
1678 struct section_offsets *section_offsets;
1679
1680 objfile = pst->objfile;
c5aa993b
JM
1681 sym_offset = LDSYMOFF (pst);
1682 sym_size = LDSYMLEN (pst);
c906108c
SS
1683 text_offset = pst->textlow;
1684 text_size = pst->texthigh - pst->textlow;
b8fbeb18
EZ
1685 /* This cannot be simply objfile->section_offsets because of
1686 elfstab_offset_sections() which initializes the psymtab section
1687 offsets information in a special way, and that is different from
1688 objfile->section_offsets. */
c906108c
SS
1689 section_offsets = pst->section_offsets;
1690
1691 current_objfile = objfile;
1692 subfile_stack = NULL;
1693
1694 stringtab_global = DBX_STRINGTAB (objfile);
1695 last_source_file = NULL;
1696
1697 abfd = objfile->obfd;
1698 symfile_bfd = objfile->obfd; /* Implicit param to next_text_symbol */
1699 symbuf_end = symbuf_idx = 0;
1700
1701 /* It is necessary to actually read one symbol *before* the start
1702 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
1703 occurs before the N_SO symbol.
1704
1705 Detecting this in read_dbx_symtab
1706 would slow down initial readin, so we look for it here instead. */
c5aa993b 1707 if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
c906108c
SS
1708 {
1709 bfd_seek (symfile_bfd, sym_offset - symbol_size, SEEK_CUR);
1710 fill_symbuf (abfd);
1711 bufp = &symbuf[symbuf_idx++];
1712 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1713 OBJSTAT (objfile, n_stabs++);
1714
1715 SET_NAMESTRING ();
1716
1717 processing_gcc_compilation = 0;
1718 if (nlist.n_type == N_TEXT)
1719 {
1720 const char *tempstring = namestring;
1721
1722 if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1723 processing_gcc_compilation = 1;
1724 else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1725 processing_gcc_compilation = 2;
1726 if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
1727 ++tempstring;
1728 if (STREQN (tempstring, "__gnu_compiled", 14))
1729 processing_gcc_compilation = 2;
1730 }
1731
1732 /* Try to select a C++ demangling based on the compilation unit
c5aa993b 1733 producer. */
c906108c 1734
8052a17a
JM
1735#if 0
1736 /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
1737 know whether it will use the old style or v3 mangling. */
c906108c
SS
1738 if (processing_gcc_compilation)
1739 {
1740 if (AUTO_DEMANGLING)
1741 {
1742 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1743 }
1744 }
8052a17a 1745#endif
c906108c
SS
1746 }
1747 else
1748 {
1749 /* The N_SO starting this symtab is the first symbol, so we
c5aa993b
JM
1750 better not check the symbol before it. I'm not this can
1751 happen, but it doesn't hurt to check for it. */
c906108c
SS
1752 bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
1753 processing_gcc_compilation = 0;
1754 }
1755
1756 if (symbuf_idx == symbuf_end)
1757 fill_symbuf (abfd);
1758 bufp = &symbuf[symbuf_idx];
1759 if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
c5aa993b 1760 error ("First symbol in segment of executable not a source symbol");
c906108c
SS
1761
1762 max_symnum = sym_size / symbol_size;
1763
1764 for (symnum = 0;
1765 symnum < max_symnum;
1766 symnum++)
1767 {
1768 QUIT; /* Allow this to be interruptable */
1769 if (symbuf_idx == symbuf_end)
c5aa993b 1770 fill_symbuf (abfd);
c906108c
SS
1771 bufp = &symbuf[symbuf_idx++];
1772 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1773 OBJSTAT (objfile, n_stabs++);
1774
1775 type = bfd_h_get_8 (abfd, bufp->e_type);
1776
1777 SET_NAMESTRING ();
1778
c5aa993b
JM
1779 if (type & N_STAB)
1780 {
c906108c
SS
1781 process_one_symbol (type, nlist.n_desc, nlist.n_value,
1782 namestring, section_offsets, objfile);
c5aa993b 1783 }
c906108c
SS
1784 /* We skip checking for a new .o or -l file; that should never
1785 happen in this routine. */
1786 else if (type == N_TEXT)
1787 {
1788 /* I don't think this code will ever be executed, because
1789 the GCC_COMPILED_FLAG_SYMBOL usually is right before
1790 the N_SO symbol which starts this source file.
1791 However, there is no reason not to accept
1792 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
1793
1794 if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1795 processing_gcc_compilation = 1;
1796 else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1797 processing_gcc_compilation = 2;
1798
8052a17a
JM
1799#if 0
1800 /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
1801 know whether it will use the old style or v3 mangling. */
c906108c
SS
1802 if (AUTO_DEMANGLING)
1803 {
1804 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1805 }
8052a17a 1806#endif
c906108c 1807 }
c5aa993b
JM
1808 else if (type & N_EXT || type == (unsigned char) N_TEXT
1809 || type == (unsigned char) N_NBTEXT
1810 )
1811 {
c906108c
SS
1812 /* Global symbol: see if we came across a dbx defintion for
1813 a corresponding symbol. If so, store the value. Remove
1814 syms from the chain when their values are stored, but
1815 search the whole chain, as there may be several syms from
1816 different files with the same name. */
1817 /* This is probably not true. Since the files will be read
1818 in one at a time, each reference to a global symbol will
1819 be satisfied in each file as it appears. So we skip this
1820 section. */
1821 ;
c5aa993b 1822 }
c906108c
SS
1823 }
1824
1825 current_objfile = NULL;
1826
1827 /* In a Solaris elf file, this variable, which comes from the
1828 value of the N_SO symbol, will still be 0. Luckily, text_offset,
1829 which comes from pst->textlow is correct. */
1830 if (last_source_start_addr == 0)
1831 last_source_start_addr = text_offset;
1832
1833 /* In reordered executables last_source_start_addr may not be the
1834 lower bound for this symtab, instead use text_offset which comes
1835 from pst->textlow which is correct. */
1836 if (last_source_start_addr > text_offset)
1837 last_source_start_addr = text_offset;
1838
b8fbeb18 1839 pst->symtab = end_symtab (text_offset + text_size, objfile, SECT_OFF_TEXT (objfile));
c906108c 1840
114d1f2c 1841 /* Process items which we had to "process_later" due to dependencies
c906108c 1842 on other stabs. */
c5aa993b 1843 process_now (objfile);
c906108c
SS
1844
1845 end_stabs ();
1846}
c906108c 1847\f
c5aa993b 1848
c906108c
SS
1849/* This handles a single symbol from the symbol-file, building symbols
1850 into a GDB symtab. It takes these arguments and an implicit argument.
1851
1852 TYPE is the type field of the ".stab" symbol entry.
1853 DESC is the desc field of the ".stab" entry.
1854 VALU is the value field of the ".stab" entry.
1855 NAME is the symbol name, in our address space.
1856 SECTION_OFFSETS is a set of amounts by which the sections of this object
c5aa993b 1857 file were relocated when it was loaded into memory.
b8fbeb18
EZ
1858 Note that these section_offsets are not the
1859 objfile->section_offsets but the pst->section_offsets.
c5aa993b
JM
1860 All symbols that refer
1861 to memory locations need to be offset by these amounts.
c906108c 1862 OBJFILE is the object file from which we are reading symbols.
c5aa993b 1863 It is used in end_symtab. */
c906108c
SS
1864
1865void
fba45db2
KB
1866process_one_symbol (int type, int desc, CORE_ADDR valu, char *name,
1867 struct section_offsets *section_offsets,
1868 struct objfile *objfile)
c906108c
SS
1869{
1870#ifdef SUN_FIXED_LBRAC_BUG
1871 /* If SUN_FIXED_LBRAC_BUG is defined, then it tells us whether we need
1872 to correct the address of N_LBRAC's. If it is not defined, then
1873 we never need to correct the addresses. */
1874
1875 /* This records the last pc address we've seen. We depend on there being
1876 an SLINE or FUN or SO before the first LBRAC, since the variable does
1877 not get reset in between reads of different symbol files. */
1878 static CORE_ADDR last_pc_address;
1879#endif
1880
1881 register struct context_stack *new;
1882 /* This remembers the address of the start of a function. It is used
1883 because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries are
1884 relative to the current function's start address. On systems
1885 other than Solaris 2, this just holds the SECT_OFF_TEXT value, and is
1886 used to relocate these symbol types rather than SECTION_OFFSETS. */
1887 static CORE_ADDR function_start_offset;
1888
1889 /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this source
1890 file. Used to detect the SunPRO solaris compiler. */
1891 static int n_opt_found;
1892
1893 /* The stab type used for the definition of the last function.
1894 N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers. */
1895 static int function_stab_type = 0;
1896
1897 if (!block_address_function_relative)
1898 /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
1899 function start address, so just use the text offset. */
b8fbeb18 1900 function_start_offset = ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1901
1902 /* Something is wrong if we see real data before
1903 seeing a source file name. */
1904
c5aa993b 1905 if (last_source_file == NULL && type != (unsigned char) N_SO)
c906108c
SS
1906 {
1907 /* Ignore any symbols which appear before an N_SO symbol.
c5aa993b
JM
1908 Currently no one puts symbols there, but we should deal
1909 gracefully with the case. A complain()t might be in order,
1910 but this should not be an error (). */
c906108c
SS
1911 return;
1912 }
1913
1914 switch (type)
1915 {
1916 case N_FUN:
1917 case N_FNAME:
1918
1919 if (*name == '\000')
1920 {
1921 /* This N_FUN marks the end of a function. This closes off the
1922 current block. */
1923 within_function = 0;
1924 new = pop_context ();
1925
1926 /* Make a block for the local symbols within. */
1927 finish_block (new->name, &local_symbols, new->old_blocks,
1928 new->start_addr, new->start_addr + valu,
1929 objfile);
1930
1931 /* May be switching to an assembler file which may not be using
1932 block relative stabs, so reset the offset. */
1933 if (block_address_function_relative)
1934 function_start_offset = 0;
1935
1936 break;
1937 }
1938
1939 /* Relocate for dynamic loading */
b8fbeb18 1940 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1941#ifdef SMASH_TEXT_ADDRESS
1942 SMASH_TEXT_ADDRESS (valu);
1943#endif
1944 goto define_a_symbol;
1945
1946 case N_LBRAC:
1947 /* This "symbol" just indicates the start of an inner lexical
c5aa993b 1948 context within a function. */
c906108c
SS
1949
1950 /* Ignore extra outermost context from SunPRO cc and acc. */
1951 if (n_opt_found && desc == 1)
1952 break;
1953
1954 if (block_address_function_relative)
1955 /* Relocate for Sun ELF acc fn-relative syms. */
1956 valu += function_start_offset;
1957 else
1958 /* On most machines, the block addresses are relative to the
1959 N_SO, the linker did not relocate them (sigh). */
1960 valu += last_source_start_addr;
1961
1962#ifdef SUN_FIXED_LBRAC_BUG
c5aa993b
JM
1963 if (!SUN_FIXED_LBRAC_BUG && valu < last_pc_address)
1964 {
1965 /* Patch current LBRAC pc value to match last handy pc value */
1966 complain (&lbrac_complaint);
1967 valu = last_pc_address;
1968 }
c906108c
SS
1969#endif
1970 new = push_context (desc, valu);
1971 break;
1972
1973 case N_RBRAC:
1974 /* This "symbol" just indicates the end of an inner lexical
c5aa993b 1975 context that was started with N_LBRAC. */
c906108c
SS
1976
1977 /* Ignore extra outermost context from SunPRO cc and acc. */
1978 if (n_opt_found && desc == 1)
1979 break;
1980
1981 if (block_address_function_relative)
1982 /* Relocate for Sun ELF acc fn-relative syms. */
1983 valu += function_start_offset;
1984 else
1985 /* On most machines, the block addresses are relative to the
1986 N_SO, the linker did not relocate them (sigh). */
1987 valu += last_source_start_addr;
1988
c5aa993b 1989 new = pop_context ();
c906108c
SS
1990 if (desc != new->depth)
1991 complain (&lbrac_mismatch_complaint, symnum);
1992
1993 /* Some compilers put the variable decls inside of an
1994 LBRAC/RBRAC block. This macro should be nonzero if this
c5aa993b
JM
1995 is true. DESC is N_DESC from the N_RBRAC symbol.
1996 GCC_P is true if we've detected the GCC_COMPILED_SYMBOL
1997 or the GCC2_COMPILED_SYMBOL. */
c906108c
SS
1998#if !defined (VARIABLES_INSIDE_BLOCK)
1999#define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
2000#endif
2001
2002 /* Can only use new->locals as local symbols here if we're in
2003 gcc or on a machine that puts them before the lbrack. */
c5aa993b 2004 if (!VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
c906108c
SS
2005 local_symbols = new->locals;
2006
2007 if (context_stack_depth
c5aa993b 2008 > !VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
c906108c
SS
2009 {
2010 /* This is not the outermost LBRAC...RBRAC pair in the function,
2011 its local symbols preceded it, and are the ones just recovered
2012 from the context stack. Define the block for them (but don't
2013 bother if the block contains no symbols. Should we complain
2014 on blocks without symbols? I can't think of any useful purpose
2015 for them). */
2016 if (local_symbols != NULL)
2017 {
2018 /* Muzzle a compiler bug that makes end < start. (which
c5aa993b 2019 compilers? Is this ever harmful?). */
c906108c
SS
2020 if (new->start_addr > valu)
2021 {
2022 complain (&lbrac_rbrac_complaint);
2023 new->start_addr = valu;
2024 }
2025 /* Make a block for the local symbols within. */
2026 finish_block (0, &local_symbols, new->old_blocks,
2027 new->start_addr, valu, objfile);
2028 }
2029 }
2030 else
2031 {
2032 /* This is the outermost LBRAC...RBRAC pair. There is no
2033 need to do anything; leave the symbols that preceded it
2034 to be attached to the function's own block. We need to
2035 indicate that we just moved outside of the function. */
2036 within_function = 0;
2037 }
2038
c5aa993b 2039 if (VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
c906108c
SS
2040 /* Now pop locals of block just finished. */
2041 local_symbols = new->locals;
2042 break;
2043
2044 case N_FN:
2045 case N_FN_SEQ:
2046 /* This kind of symbol indicates the start of an object file. */
2047 /* Relocate for dynamic loading */
b8fbeb18 2048 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2049 break;
2050
2051 case N_SO:
2052 /* This type of symbol indicates the start of data
c5aa993b
JM
2053 for one source file.
2054 Finish the symbol table of the previous source file
2055 (if any) and start accumulating a new symbol table. */
c906108c 2056 /* Relocate for dynamic loading */
b8fbeb18 2057 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2058
2059 n_opt_found = 0;
2060
2061#ifdef SUN_FIXED_LBRAC_BUG
2062 last_pc_address = valu; /* Save for SunOS bug circumcision */
2063#endif
2064
2065#ifdef PCC_SOL_BROKEN
2066 /* pcc bug, occasionally puts out SO for SOL. */
2067 if (context_stack_depth > 0)
2068 {
2069 start_subfile (name, NULL);
2070 break;
2071 }
2072#endif
2073 if (last_source_file)
2074 {
2075 /* Check if previous symbol was also an N_SO (with some
2076 sanity checks). If so, that one was actually the directory
2077 name, and the current one is the real file name.
c5aa993b 2078 Patch things up. */
c906108c
SS
2079 if (previous_stab_code == (unsigned char) N_SO)
2080 {
2081 patch_subfile_names (current_subfile, name);
2082 break; /* Ignore repeated SOs */
2083 }
b8fbeb18 2084 end_symtab (valu, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
2085 end_stabs ();
2086 }
2087
2088 /* Null name means this just marks the end of text for this .o file.
c5aa993b 2089 Don't start a new symtab in this case. */
c906108c
SS
2090 if (*name == '\000')
2091 break;
2092
2093 if (block_address_function_relative)
c5aa993b 2094 function_start_offset = 0;
c906108c
SS
2095
2096 start_stabs ();
2097 start_symtab (name, NULL, valu);
2098 record_debugformat ("stabs");
2099 break;
2100
2101 case N_SOL:
2102 /* This type of symbol indicates the start of data for
c5aa993b
JM
2103 a sub-source-file, one whose contents were copied or
2104 included in the compilation of the main source file
2105 (whose name was given in the N_SO symbol.) */
c906108c 2106 /* Relocate for dynamic loading */
b8fbeb18 2107 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2108 start_subfile (name, current_subfile->dirname);
2109 break;
2110
2111 case N_BINCL:
2112 push_subfile ();
2113 add_new_header_file (name, valu);
2114 start_subfile (name, current_subfile->dirname);
2115 break;
2116
2117 case N_EINCL:
2118 start_subfile (pop_subfile (), current_subfile->dirname);
2119 break;
2120
2121 case N_EXCL:
2122 add_old_header_file (name, valu);
2123 break;
2124
2125 case N_SLINE:
2126 /* This type of "symbol" really just records
c5aa993b
JM
2127 one line-number -- core-address correspondence.
2128 Enter it in the line list for this symbol table. */
c906108c
SS
2129
2130 /* Relocate for dynamic loading and for ELF acc fn-relative syms. */
2131 valu += function_start_offset;
2132
2133#ifdef SUN_FIXED_LBRAC_BUG
2134 last_pc_address = valu; /* Save for SunOS bug circumcision */
2135#endif
2136 record_line (current_subfile, desc, valu);
2137 break;
2138
2139 case N_BCOMM:
2140 common_block_start (name, objfile);
2141 break;
2142
2143 case N_ECOMM:
2144 common_block_end (objfile);
2145 break;
2146
c5aa993b
JM
2147 /* The following symbol types need to have the appropriate offset added
2148 to their value; then we process symbol definitions in the name. */
c906108c
SS
2149
2150 case N_STSYM: /* Static symbol in data seg */
2151 case N_LCSYM: /* Static symbol in BSS seg */
2152 case N_ROSYM: /* Static symbol in Read-only data seg */
c5aa993b
JM
2153 /* HORRID HACK DEPT. However, it's Sun's furgin' fault.
2154 Solaris2's stabs-in-elf makes *most* symbols relative
2155 but leaves a few absolute (at least for Solaris 2.1 and version
2156 2.0.1 of the SunPRO compiler). N_STSYM and friends sit on the fence.
2157 .stab "foo:S...",N_STSYM is absolute (ld relocates it)
2158 .stab "foo:V...",N_STSYM is relative (section base subtracted).
2159 This leaves us no choice but to search for the 'S' or 'V'...
2160 (or pass the whole section_offsets stuff down ONE MORE function
2161 call level, which we really don't want to do). */
c906108c
SS
2162 {
2163 char *p;
2164
2165 /* .o files and NLMs have non-zero text seg offsets, but don't need
2166 their static syms offset in this fashion. XXX - This is really a
2167 crock that should be fixed in the solib handling code so that I
2168 don't have to work around it here. */
2169
2170 if (!symfile_relocatable)
2171 {
2172 p = strchr (name, ':');
2173 if (p != 0 && p[1] == 'S')
2174 {
2175 /* The linker relocated it. We don't want to add an
2176 elfstab_offset_sections-type offset, but we *do* want
2177 to add whatever solib.c passed to symbol_file_add as
2178 addr (this is known to affect SunOS4, and I suspect ELF
2179 too). Since elfstab_offset_sections currently does not
2180 muck with the text offset (there is no Ttext.text
2181 symbol), we can get addr from the text offset. If
2182 elfstab_offset_sections ever starts dealing with the
2183 text offset, and we still need to do this, we need to
2184 invent a SECT_OFF_ADDR_KLUDGE or something. */
b8fbeb18 2185 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2186 goto define_a_symbol;
2187 }
2188 }
2189 /* Since it's not the kludge case, re-dispatch to the right handler. */
c5aa993b
JM
2190 switch (type)
2191 {
2192 case N_STSYM:
2193 goto case_N_STSYM;
2194 case N_LCSYM:
2195 goto case_N_LCSYM;
2196 case N_ROSYM:
2197 goto case_N_ROSYM;
2198 default:
e1e9e218 2199 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c5aa993b 2200 }
c906108c
SS
2201 }
2202
2203 case_N_STSYM: /* Static symbol in data seg */
2204 case N_DSLINE: /* Source line number, data seg */
b8fbeb18 2205 valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
c906108c
SS
2206 goto define_a_symbol;
2207
2208 case_N_LCSYM: /* Static symbol in BSS seg */
2209 case N_BSLINE: /* Source line number, bss seg */
c5aa993b 2210 /* N_BROWS: overlaps with N_BSLINE */
b8fbeb18 2211 valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
c906108c
SS
2212 goto define_a_symbol;
2213
2214 case_N_ROSYM: /* Static symbol in Read-only data seg */
b8fbeb18 2215 valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
c906108c
SS
2216 goto define_a_symbol;
2217
2218 case N_ENTRY: /* Alternate entry point */
2219 /* Relocate for dynamic loading */
b8fbeb18 2220 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2221 goto define_a_symbol;
2222
c5aa993b
JM
2223 /* The following symbol types we don't know how to process. Handle
2224 them in a "default" way, but complain to people who care. */
c906108c
SS
2225 default:
2226 case N_CATCH: /* Exception handler catcher */
2227 case N_EHDECL: /* Exception handler name */
2228 case N_PC: /* Global symbol in Pascal */
c5aa993b
JM
2229 case N_M2C: /* Modula-2 compilation unit */
2230 /* N_MOD2: overlaps with N_EHDECL */
c906108c
SS
2231 case N_SCOPE: /* Modula-2 scope information */
2232 case N_ECOML: /* End common (local name) */
2233 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
2234 case N_NBDATA:
2235 case N_NBBSS:
2236 case N_NBSTS:
2237 case N_NBLCS:
2238 complain (&unknown_symtype_complaint, local_hex_string (type));
2239 /* FALLTHROUGH */
2240
c5aa993b
JM
2241 /* The following symbol types don't need the address field relocated,
2242 since it is either unused, or is absolute. */
c906108c
SS
2243 define_a_symbol:
2244 case N_GSYM: /* Global variable */
2245 case N_NSYMS: /* Number of symbols (ultrix) */
2246 case N_NOMAP: /* No map? (ultrix) */
2247 case N_RSYM: /* Register variable */
2248 case N_DEFD: /* Modula-2 GNU module dependency */
2249 case N_SSYM: /* Struct or union element */
2250 case N_LSYM: /* Local symbol in stack */
2251 case N_PSYM: /* Parameter variable */
2252 case N_LENG: /* Length of preceding symbol type */
2253 if (name)
2254 {
2255 int deftype;
2256 char *colon_pos = strchr (name, ':');
2257 if (colon_pos == NULL)
2258 deftype = '\0';
2259 else
2260 deftype = colon_pos[1];
2261
2262 switch (deftype)
2263 {
2264 case 'f':
2265 case 'F':
2266 function_stab_type = type;
2267
2268#ifdef SOFUN_ADDRESS_MAYBE_MISSING
2269 /* Deal with the SunPRO 3.0 compiler which omits the address
c5aa993b 2270 from N_FUN symbols. */
c906108c 2271 if (type == N_FUN
b8fbeb18 2272 && valu == ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)))
c2c6d25f
JM
2273 valu =
2274 find_stab_function_addr (name, last_source_file, objfile);
c906108c
SS
2275#endif
2276
2277#ifdef SUN_FIXED_LBRAC_BUG
2278 /* The Sun acc compiler, under SunOS4, puts out
c5aa993b
JM
2279 functions with N_GSYM or N_STSYM. The problem is
2280 that the address of the symbol is no good (for N_GSYM
2281 it doesn't even attept an address; for N_STSYM it
2282 puts out an address but then it gets relocated
2283 relative to the data segment, not the text segment).
2284 Currently we can't fix this up later as we do for
2285 some types of symbol in scan_file_globals.
2286 Fortunately we do have a way of finding the address -
2287 we know that the value in last_pc_address is either
2288 the one we want (if we're dealing with the first
2289 function in an object file), or somewhere in the
2290 previous function. This means that we can use the
2291 minimal symbol table to get the address. */
c906108c
SS
2292
2293 /* Starting with release 3.0, the Sun acc compiler,
c5aa993b
JM
2294 under SunOS4, puts out functions with N_FUN and a value
2295 of zero. This gets relocated to the start of the text
2296 segment of the module, which is no good either.
2297 Under SunOS4 we can deal with this as N_SLINE and N_SO
2298 entries contain valid absolute addresses.
2299 Release 3.0 acc also puts out N_OPT entries, which makes
2300 it possible to discern acc from cc or gcc. */
c906108c
SS
2301
2302 if (type == N_GSYM || type == N_STSYM
2303 || (type == N_FUN
2304 && n_opt_found && !block_address_function_relative))
2305 {
2306 struct minimal_symbol *m;
2307 int l = colon_pos - name;
2308
2309 m = lookup_minimal_symbol_by_pc (last_pc_address);
2310 if (m && STREQN (SYMBOL_NAME (m), name, l)
c5aa993b 2311 && SYMBOL_NAME (m)[l] == '\0')
c906108c
SS
2312 /* last_pc_address was in this function */
2313 valu = SYMBOL_VALUE (m);
c5aa993b
JM
2314 else if (m && SYMBOL_NAME (m + 1)
2315 && STREQN (SYMBOL_NAME (m + 1), name, l)
2316 && SYMBOL_NAME (m + 1)[l] == '\0')
c906108c 2317 /* last_pc_address was in last function */
c5aa993b 2318 valu = SYMBOL_VALUE (m + 1);
c906108c
SS
2319 else
2320 /* Not found - use last_pc_address (for finish_block) */
2321 valu = last_pc_address;
2322 }
2323
2324 last_pc_address = valu; /* Save for SunOS bug circumcision */
2325#endif
2326
2327 if (block_address_function_relative)
2328 /* For Solaris 2.0 compilers, the block addresses and
2329 N_SLINE's are relative to the start of the
2330 function. On normal systems, and when using gcc on
2331 Solaris 2.0, these addresses are just absolute, or
2332 relative to the N_SO, depending on
2333 BLOCK_ADDRESS_ABSOLUTE. */
c5aa993b 2334 function_start_offset = valu;
c906108c
SS
2335
2336 within_function = 1;
c3f6f71d
JM
2337
2338 if (context_stack_depth > 1)
2339 {
2340 complain (&lbrac_unmatched_complaint, symnum);
2341 break;
2342 }
2343
c906108c
SS
2344 if (context_stack_depth > 0)
2345 {
2346 new = pop_context ();
2347 /* Make a block for the local symbols within. */
2348 finish_block (new->name, &local_symbols, new->old_blocks,
2349 new->start_addr, valu, objfile);
2350 }
c906108c
SS
2351
2352 new = push_context (0, valu);
2353 new->name = define_symbol (valu, name, desc, type, objfile);
2354 break;
2355
2356 default:
2357 define_symbol (valu, name, desc, type, objfile);
2358 break;
2359 }
2360 }
2361 break;
2362
c5aa993b
JM
2363 /* We use N_OPT to carry the gcc2_compiled flag. Sun uses it
2364 for a bunch of other flags, too. Someday we may parse their
2365 flags; for now we ignore theirs and hope they'll ignore ours. */
2366 case N_OPT: /* Solaris 2: Compiler options */
c906108c
SS
2367 if (name)
2368 {
2369 if (STREQ (name, GCC2_COMPILED_FLAG_SYMBOL))
2370 {
2371 processing_gcc_compilation = 2;
8052a17a
JM
2372#if 0 /* Works, but is experimental. -fnf */
2373 /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
2374 know whether it will use the old style or v3 mangling. */
c906108c
SS
2375 if (AUTO_DEMANGLING)
2376 {
2377 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
2378 }
2379#endif
2380 }
2381 else
2382 n_opt_found = 1;
2383 }
2384 break;
2385
51cc5b07
AC
2386 case N_MAIN: /* Name of main routine. */
2387 /* FIXME: If one has a symbol file with N_MAIN and then replaces
2388 it with a symbol file with "main" and without N_MAIN. I'm
2389 not sure exactly what rule to follow but probably something
2390 like: N_MAIN takes precedence over "main" no matter what
2391 objfile it is in; If there is more than one N_MAIN, choose
2392 the one in the symfile_objfile; If there is more than one
2393 N_MAIN within a given objfile, complain() and choose
2394 arbitrarily. (kingdon) */
2395 if (name != NULL)
2396 set_main_name (name);
2397 break;
2398
c5aa993b
JM
2399 /* The following symbol types can be ignored. */
2400 case N_OBJ: /* Solaris 2: Object file dir and name */
2401 /* N_UNDF: Solaris 2: file separator mark */
2402 /* N_UNDF: -- we will never encounter it, since we only process one
2403 file's symbols at once. */
c906108c 2404 case N_ENDM: /* Solaris 2: End of module */
c906108c
SS
2405 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
2406 break;
2407 }
2408
2409 /* '#' is a GNU C extension to allow one symbol to refer to another
2410 related symbol.
2411
2412 Generally this is used so that an alias can refer to its main
c5aa993b 2413 symbol. */
c906108c
SS
2414 if (name[0] == '#')
2415 {
2416 /* Initialize symbol reference names and determine if this is
2417 a definition. If symbol reference is being defined, go
2418 ahead and add it. Otherwise, just return sym. */
2419
2420 char *s = name;
2421 int refnum;
2422
2423 /* If this stab defines a new reference ID that is not on the
c5aa993b 2424 reference list, then put it on the reference list.
c906108c 2425
c5aa993b
JM
2426 We go ahead and advance NAME past the reference, even though
2427 it is not strictly necessary at this time. */
c906108c
SS
2428 refnum = symbol_reference_defined (&s);
2429 if (refnum >= 0)
2430 if (!ref_search (refnum))
2431 ref_add (refnum, 0, name, valu);
2432 name = s;
2433 }
2434
2435
2436 previous_stab_code = type;
2437}
2438\f
2439/* FIXME: The only difference between this and elfstab_build_psymtabs
2440 is the call to install_minimal_symbols for elf, and the support for
2441 split sections. If the differences are really that small, the code
2442 should be shared. */
2443
2444/* Scan and build partial symbols for an coff symbol file.
2445 The coff file has already been processed to get its minimal symbols.
2446
2447 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2448 rolled into one.
2449
2450 OBJFILE is the object file we are reading symbols from.
2451 ADDR is the address relative to which the symbols are (e.g.
2452 the base address of the text segment).
2453 MAINLINE is true if we are reading the main symbol
2454 table (as opposed to a shared lib or dynamically loaded file).
2455 TEXTADDR is the address of the text section.
2456 TEXTSIZE is the size of the text section.
2457 STABSECTS is the list of .stab sections in OBJFILE.
2458 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2459 .stabstr section exists.
2460
2461 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2462 adjusted for coff details. */
2463
2464void
fba45db2
KB
2465coffstab_build_psymtabs (struct objfile *objfile, int mainline,
2466 CORE_ADDR textaddr, unsigned int textsize,
2467 struct stab_section_list *stabsects,
2468 file_ptr stabstroffset, unsigned int stabstrsize)
c906108c
SS
2469{
2470 int val;
2471 bfd *sym_bfd = objfile->obfd;
2472 char *name = bfd_get_filename (sym_bfd);
2473 struct dbx_symfile_info *info;
2474 unsigned int stabsize;
2475
2476 /* There is already a dbx_symfile_info allocated by our caller.
2477 It might even contain some info from the coff symtab to help us. */
2478 info = objfile->sym_stab_info;
2479
2480 DBX_TEXT_ADDR (objfile) = textaddr;
2481 DBX_TEXT_SIZE (objfile) = textsize;
2482
2483#define COFF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
c5aa993b 2484 DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
c906108c 2485 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
c5aa993b 2486
c906108c
SS
2487 if (stabstrsize > bfd_get_size (sym_bfd))
2488 error ("ridiculous string table size: %d bytes", stabstrsize);
2489 DBX_STRINGTAB (objfile) = (char *)
c5aa993b
JM
2490 obstack_alloc (&objfile->psymbol_obstack, stabstrsize + 1);
2491 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
c906108c
SS
2492
2493 /* Now read in the string table in one big gulp. */
2494
2495 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
2496 if (val < 0)
2497 perror_with_name (name);
2498 val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2499 if (val != stabstrsize)
2500 perror_with_name (name);
2501
2502 stabsread_new_init ();
2503 buildsym_new_init ();
2504 free_header_files ();
2505 init_header_files ();
2506
2507 processing_acc_compilation = 1;
2508
2509 /* In a coff file, we've already installed the minimal symbols that came
2510 from the coff (non-stab) symbol table, so always act like an
2511 incremental load here. */
2512 if (stabsects->next == NULL)
2513 {
2514 stabsize = bfd_section_size (sym_bfd, stabsects->section);
2515 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
2516 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
2517 }
2518 else
2519 {
2520 struct stab_section_list *stabsect;
2521
2522 DBX_SYMCOUNT (objfile) = 0;
2523 for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
2524 {
2525 stabsize = bfd_section_size (sym_bfd, stabsect->section);
2526 DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
2527 }
2528
2529 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
2530
2531 symbuf_sections = stabsects->next;
2532 symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
2533 symbuf_read = 0;
2534 }
2535
96baa820 2536 dbx_symfile_read (objfile, 0);
c906108c
SS
2537}
2538\f
2539/* Scan and build partial symbols for an ELF symbol file.
2540 This ELF file has already been processed to get its minimal symbols,
2541 and any DWARF symbols that were in it.
2542
2543 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2544 rolled into one.
2545
2546 OBJFILE is the object file we are reading symbols from.
2547 ADDR is the address relative to which the symbols are (e.g.
2548 the base address of the text segment).
2549 MAINLINE is true if we are reading the main symbol
2550 table (as opposed to a shared lib or dynamically loaded file).
2551 STABOFFSET and STABSIZE define the location in OBJFILE where the .stab
2552 section exists.
2553 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2554 .stabstr section exists.
2555
2556 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2557 adjusted for elf details. */
2558
2559void
fba45db2
KB
2560elfstab_build_psymtabs (struct objfile *objfile, int mainline,
2561 file_ptr staboffset, unsigned int stabsize,
2562 file_ptr stabstroffset, unsigned int stabstrsize)
c906108c
SS
2563{
2564 int val;
2565 bfd *sym_bfd = objfile->obfd;
2566 char *name = bfd_get_filename (sym_bfd);
2567 struct dbx_symfile_info *info;
c906108c
SS
2568
2569 /* There is already a dbx_symfile_info allocated by our caller.
2570 It might even contain some info from the ELF symtab to help us. */
2571 info = objfile->sym_stab_info;
2572
7a292a7a
SS
2573 /* Find the first and last text address. dbx_symfile_read seems to
2574 want this. */
2575 find_text_range (sym_bfd, objfile);
c906108c
SS
2576
2577#define ELF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
c5aa993b
JM
2578 DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
2579 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
c906108c 2580 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
c5aa993b
JM
2581 DBX_SYMTAB_OFFSET (objfile) = staboffset;
2582
c906108c
SS
2583 if (stabstrsize > bfd_get_size (sym_bfd))
2584 error ("ridiculous string table size: %d bytes", stabstrsize);
2585 DBX_STRINGTAB (objfile) = (char *)
c5aa993b
JM
2586 obstack_alloc (&objfile->psymbol_obstack, stabstrsize + 1);
2587 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
c906108c
SS
2588
2589 /* Now read in the string table in one big gulp. */
2590
2591 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
2592 if (val < 0)
2593 perror_with_name (name);
2594 val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2595 if (val != stabstrsize)
2596 perror_with_name (name);
2597
2598 stabsread_new_init ();
2599 buildsym_new_init ();
2600 free_header_files ();
2601 init_header_files ();
2602 install_minimal_symbols (objfile);
2603
2604 processing_acc_compilation = 1;
2605
2606 /* In an elf file, we've already installed the minimal symbols that came
2607 from the elf (non-stab) symbol table, so always act like an
2608 incremental load here. */
96baa820 2609 dbx_symfile_read (objfile, 0);
c906108c
SS
2610}
2611\f
2612/* Scan and build partial symbols for a file with special sections for stabs
2613 and stabstrings. The file has already been processed to get its minimal
2614 symbols, and any other symbols that might be necessary to resolve GSYMs.
2615
2616 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2617 rolled into one.
2618
2619 OBJFILE is the object file we are reading symbols from.
2620 ADDR is the address relative to which the symbols are (e.g. the base address
c5aa993b 2621 of the text segment).
c906108c 2622 MAINLINE is true if we are reading the main symbol table (as opposed to a
c5aa993b 2623 shared lib or dynamically loaded file).
c906108c
SS
2624 STAB_NAME is the name of the section that contains the stabs.
2625 STABSTR_NAME is the name of the section that contains the stab strings.
2626
2627 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read. */
2628
2629void
fba45db2
KB
2630stabsect_build_psymtabs (struct objfile *objfile, int mainline, char *stab_name,
2631 char *stabstr_name, char *text_name)
c906108c
SS
2632{
2633 int val;
2634 bfd *sym_bfd = objfile->obfd;
2635 char *name = bfd_get_filename (sym_bfd);
2636 asection *stabsect;
2637 asection *stabstrsect;
2638 asection *text_sect;
2639
2640 stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
2641 stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
2642
2643 if (!stabsect)
2644 return;
2645
2646 if (!stabstrsect)
2647 error ("stabsect_build_psymtabs: Found stabs (%s), but not string section (%s)",
2648 stab_name, stabstr_name);
2649
2650 objfile->sym_stab_info = (struct dbx_symfile_info *)
2651 xmalloc (sizeof (struct dbx_symfile_info));
2652 memset (objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
2653
2654 text_sect = bfd_get_section_by_name (sym_bfd, text_name);
2655 if (!text_sect)
2656 error ("Can't find %s section in symbol file", text_name);
2657 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
2658 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
2659
c5aa993b
JM
2660 DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
2661 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
c906108c
SS
2662 / DBX_SYMBOL_SIZE (objfile);
2663 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
c5aa993b
JM
2664 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos; /* XXX - FIXME: POKING INSIDE BFD DATA STRUCTURES */
2665
c906108c
SS
2666 if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
2667 error ("ridiculous string table size: %d bytes", DBX_STRINGTAB_SIZE (objfile));
2668 DBX_STRINGTAB (objfile) = (char *)
2669 obstack_alloc (&objfile->psymbol_obstack, DBX_STRINGTAB_SIZE (objfile) + 1);
2670 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
2671
2672 /* Now read in the string table in one big gulp. */
2673
c5aa993b
JM
2674 val = bfd_get_section_contents (sym_bfd, /* bfd */
2675 stabstrsect, /* bfd section */
2676 DBX_STRINGTAB (objfile), /* input buffer */
2677 0, /* offset into section */
2678 DBX_STRINGTAB_SIZE (objfile)); /* amount to read */
c906108c
SS
2679
2680 if (!val)
2681 perror_with_name (name);
2682
2683 stabsread_new_init ();
2684 buildsym_new_init ();
2685 free_header_files ();
2686 init_header_files ();
2687 install_minimal_symbols (objfile);
2688
2689 /* Now, do an incremental load */
2690
2691 processing_acc_compilation = 1;
96baa820 2692 dbx_symfile_read (objfile, 0);
c906108c
SS
2693}
2694\f
2695static struct sym_fns aout_sym_fns =
2696{
2697 bfd_target_aout_flavour,
c5aa993b
JM
2698 dbx_new_init, /* sym_new_init: init anything gbl to entire symtab */
2699 dbx_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2700 dbx_symfile_read, /* sym_read: read a symbol file into symtab */
2701 dbx_symfile_finish, /* sym_finish: finished with file, cleanup */
96baa820 2702 default_symfile_offsets, /* sym_offsets: parse user's offsets to internal form */
c5aa993b 2703 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
2704};
2705
2706void
fba45db2 2707_initialize_dbxread (void)
c906108c 2708{
c5aa993b 2709 add_symtab_fns (&aout_sym_fns);
c906108c 2710}
This page took 0.279283 seconds and 4 git commands to generate.