daily update
[deliverable/binutils-gdb.git] / gdb / dbxread.c
CommitLineData
c906108c 1/* Read dbx symbol tables and convert to internal format, for GDB.
197e01b6 2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9ab9195f 3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004.
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21/* This module provides three functions: dbx_symfile_init,
22 which initializes to read a symbol file; dbx_new_init, which
23 discards existing cached information when all symbols are being
24 discarded; and dbx_symfile_read, which reads a symbol table
25 from a file.
26
27 dbx_symfile_read only does the minimum work necessary for letting the
28 user "name" things symbolically; it does not read the entire symtab.
29 Instead, it reads the external and static symbols and puts them in partial
30 symbol tables. When more extensive information is requested of a
31 file, the corresponding partial symbol table is mutated into a full
32 fledged symbol table by going back and reading the symbols
33 for real. dbx_psymtab_to_symtab() is the function that does this */
34
35#include "defs.h"
36#include "gdb_string.h"
37
1f04aa62 38#if defined(__CYGNUSCLIB__)
c906108c
SS
39#include <sys/types.h>
40#include <fcntl.h>
41#endif
42
04ea0df1 43#include "gdb_obstack.h"
c906108c 44#include "gdb_stat.h"
c906108c
SS
45#include "symtab.h"
46#include "breakpoint.h"
c906108c
SS
47#include "target.h"
48#include "gdbcore.h" /* for bfd stuff */
c5aa993b 49#include "libaout.h" /* FIXME Secret internal BFD stuff for a.out */
c906108c
SS
50#include "objfiles.h"
51#include "buildsym.h"
52#include "stabsread.h"
53#include "gdb-stabs.h"
54#include "demangle.h"
c906108c 55#include "complaints.h"
015a42b4 56#include "cp-abi.h"
91f54576 57
7c8a5605 58#include "gdb_assert.h"
91f54576 59#include "gdb_string.h"
c906108c
SS
60
61#include "aout/aout64.h"
62#include "aout/stab_gnu.h" /* We always use GNU stabs, not native, now */
c906108c 63\f
c5aa993b 64
c906108c
SS
65/* We put a pointer to this structure in the read_symtab_private field
66 of the psymtab. */
67
c5aa993b
JM
68struct symloc
69 {
c5aa993b
JM
70 /* Offset within the file symbol table of first local symbol for this
71 file. */
c906108c 72
c5aa993b 73 int ldsymoff;
c906108c 74
c5aa993b
JM
75 /* Length (in bytes) of the section of the symbol table devoted to
76 this file's symbols (actually, the section bracketed may contain
77 more than just this file's symbols). If ldsymlen is 0, the only
78 reason for this thing's existence is the dependency list. Nothing
79 else will happen when it is read in. */
c906108c 80
c5aa993b 81 int ldsymlen;
c906108c 82
c5aa993b 83 /* The size of each symbol in the symbol file (in external form). */
c906108c 84
c5aa993b 85 int symbol_size;
c906108c 86
c5aa993b
JM
87 /* Further information needed to locate the symbols if they are in
88 an ELF file. */
c906108c 89
c5aa993b
JM
90 int symbol_offset;
91 int string_offset;
92 int file_string_offset;
93 };
c906108c
SS
94
95#define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
96#define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
97#define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
98#define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
99#define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
100#define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
101#define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
c906108c 102\f
c5aa993b 103
c906108c
SS
104/* Remember what we deduced to be the source language of this psymtab. */
105
106static enum language psymtab_language = language_unknown;
107
c906108c
SS
108/* The BFD for this file -- implicit parameter to next_symbol_text. */
109
110static bfd *symfile_bfd;
111
112/* The size of each symbol in the symbol file (in external form).
113 This is set by dbx_symfile_read when building psymtabs, and by
114 dbx_psymtab_to_symtab when building symtabs. */
115
116static unsigned symbol_size;
117
118/* This is the offset of the symbol table in the executable file. */
119
120static unsigned symbol_table_offset;
121
122/* This is the offset of the string table in the executable file. */
123
124static unsigned string_table_offset;
125
126/* For elf+stab executables, the n_strx field is not a simple index
127 into the string table. Instead, each .o file has a base offset in
128 the string table, and the associated symbols contain offsets from
129 this base. The following two variables contain the base offset for
130 the current and next .o files. */
131
132static unsigned int file_string_table_offset;
133static unsigned int next_file_string_table_offset;
134
135/* .o and NLM files contain unrelocated addresses which are based at
136 0. When non-zero, this flag disables some of the special cases for
137 Solaris elf+stab text addresses at location 0. */
138
139static int symfile_relocatable = 0;
140
141/* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are
142 relative to the function start address. */
143
144static int block_address_function_relative = 0;
145\f
146/* The lowest text address we have yet encountered. This is needed
147 because in an a.out file, there is no header field which tells us
148 what address the program is actually going to be loaded at, so we
149 need to make guesses based on the symbols (which *are* relocated to
150 reflect the address it will be loaded at). */
151
152static CORE_ADDR lowest_text_address;
153
154/* Non-zero if there is any line number info in the objfile. Prevents
155 end_psymtab from discarding an otherwise empty psymtab. */
156
157static int has_line_numbers;
158
159/* Complaints about the symbols we have encountered. */
160
23136709
KB
161static void
162unknown_symtype_complaint (const char *arg1)
163{
e2e0b3e5 164 complaint (&symfile_complaints, _("unknown symbol type %s"), arg1);
23136709 165}
c906108c 166
23136709
KB
167static void
168lbrac_mismatch_complaint (int arg1)
169{
170 complaint (&symfile_complaints,
e2e0b3e5 171 _("N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d"), arg1);
23136709 172}
c906108c 173
23136709
KB
174static void
175repeated_header_complaint (const char *arg1, int arg2)
176{
177 complaint (&symfile_complaints,
063e58ba
MD
178 _("\"repeated\" header file %s not previously seen, at symtab \
179pos %d"),
23136709
KB
180 arg1, arg2);
181}
1f077a3e 182
7a292a7a
SS
183/* find_text_range --- find start and end of loadable code sections
184
185 The find_text_range function finds the shortest address range that
186 encloses all sections containing executable code, and stores it in
187 objfile's text_addr and text_size members.
188
189 dbx_symfile_read will use this to finish off the partial symbol
190 table, in some cases. */
191
192static void
c5aa993b 193find_text_range (bfd * sym_bfd, struct objfile *objfile)
7a292a7a
SS
194{
195 asection *sec;
196 int found_any = 0;
b9179dbc
EZ
197 CORE_ADDR start = 0;
198 CORE_ADDR end = 0;
c5aa993b 199
7a292a7a
SS
200 for (sec = sym_bfd->sections; sec; sec = sec->next)
201 if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
202 {
203 CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
204 CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
205
206 if (found_any)
207 {
c5aa993b
JM
208 if (sec_start < start)
209 start = sec_start;
210 if (sec_end > end)
211 end = sec_end;
7a292a7a
SS
212 }
213 else
214 {
215 start = sec_start;
216 end = sec_end;
217 }
218
219 found_any = 1;
220 }
221
c5aa993b 222 if (!found_any)
8a3fe4f8 223 error (_("Can't find any code sections in symbol file"));
7a292a7a
SS
224
225 DBX_TEXT_ADDR (objfile) = start;
226 DBX_TEXT_SIZE (objfile) = end - start;
227}
c5aa993b 228\f
7a292a7a
SS
229
230
c906108c
SS
231/* During initial symbol readin, we need to have a structure to keep
232 track of which psymtabs have which bincls in them. This structure
233 is used during readin to setup the list of dependencies within each
234 partial symbol table. */
235
236struct header_file_location
237{
238 char *name; /* Name of header file */
239 int instance; /* See above */
240 struct partial_symtab *pst; /* Partial symtab that has the
241 BINCL/EINCL defs for this file */
242};
243
244/* The actual list and controling variables */
245static struct header_file_location *bincl_list, *next_bincl;
246static int bincls_allocated;
247
248/* Local function prototypes */
249
a14ed312 250extern void _initialize_dbxread (void);
392a587b 251
a14ed312 252static void read_ofile_symtab (struct partial_symtab *);
c906108c 253
a14ed312 254static void dbx_psymtab_to_symtab (struct partial_symtab *);
c906108c 255
a14ed312 256static void dbx_psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 257
a14ed312 258static void read_dbx_dynamic_symtab (struct objfile *objfile);
c906108c 259
a14ed312 260static void read_dbx_symtab (struct objfile *);
c906108c 261
a14ed312 262static void free_bincl_list (struct objfile *);
c906108c 263
a14ed312 264static struct partial_symtab *find_corresponding_bincl_psymtab (char *, int);
c906108c 265
a14ed312 266static void add_bincl_to_list (struct partial_symtab *, char *, int);
c906108c 267
a14ed312 268static void init_bincl_list (int, struct objfile *);
c906108c 269
a14ed312 270static char *dbx_next_symbol_text (struct objfile *);
c906108c 271
a14ed312 272static void fill_symbuf (bfd *);
c906108c 273
a14ed312 274static void dbx_symfile_init (struct objfile *);
c906108c 275
a14ed312 276static void dbx_new_init (struct objfile *);
c906108c 277
a14ed312 278static void dbx_symfile_read (struct objfile *, int);
c906108c 279
a14ed312 280static void dbx_symfile_finish (struct objfile *);
c906108c 281
a14ed312 282static void record_minimal_symbol (char *, CORE_ADDR, int, struct objfile *);
c906108c 283
a14ed312 284static void add_new_header_file (char *, int);
c906108c 285
a14ed312 286static void add_old_header_file (char *, int);
c906108c 287
a14ed312 288static void add_this_object_header_file (int);
c906108c 289
a14ed312
KB
290static struct partial_symtab *start_psymtab (struct objfile *, char *,
291 CORE_ADDR, int,
292 struct partial_symbol **,
293 struct partial_symbol **);
d4f3574e 294
c906108c
SS
295/* Free up old header file tables */
296
d3d55eeb 297void
fba45db2 298free_header_files (void)
c906108c
SS
299{
300 if (this_object_header_files)
301 {
b8c9b27d 302 xfree (this_object_header_files);
c906108c
SS
303 this_object_header_files = NULL;
304 }
305 n_allocated_this_object_header_files = 0;
306}
307
308/* Allocate new header file tables */
309
d3d55eeb 310void
fba45db2 311init_header_files (void)
c906108c
SS
312{
313 n_allocated_this_object_header_files = 10;
314 this_object_header_files = (int *) xmalloc (10 * sizeof (int));
315}
316
317/* Add header file number I for this object file
318 at the next successive FILENUM. */
319
320static void
fba45db2 321add_this_object_header_file (int i)
c906108c
SS
322{
323 if (n_this_object_header_files == n_allocated_this_object_header_files)
324 {
325 n_allocated_this_object_header_files *= 2;
326 this_object_header_files
327 = (int *) xrealloc ((char *) this_object_header_files,
c5aa993b 328 n_allocated_this_object_header_files * sizeof (int));
c906108c
SS
329 }
330
331 this_object_header_files[n_this_object_header_files++] = i;
332}
333
334/* Add to this file an "old" header file, one already seen in
335 a previous object file. NAME is the header file's name.
336 INSTANCE is its instance code, to select among multiple
337 symbol tables for the same header file. */
338
339static void
fba45db2 340add_old_header_file (char *name, int instance)
c906108c 341{
52f0bd74
AC
342 struct header_file *p = HEADER_FILES (current_objfile);
343 int i;
c906108c
SS
344
345 for (i = 0; i < N_HEADER_FILES (current_objfile); i++)
6314a349 346 if (strcmp (p[i].name, name) == 0 && instance == p[i].instance)
c906108c
SS
347 {
348 add_this_object_header_file (i);
349 return;
350 }
23136709 351 repeated_header_complaint (name, symnum);
c906108c
SS
352}
353
354/* Add to this file a "new" header file: definitions for its types follow.
355 NAME is the header file's name.
356 Most often this happens only once for each distinct header file,
357 but not necessarily. If it happens more than once, INSTANCE has
358 a different value each time, and references to the header file
359 use INSTANCE values to select among them.
360
361 dbx output contains "begin" and "end" markers for each new header file,
362 but at this level we just need to know which files there have been;
363 so we record the file when its "begin" is seen and ignore the "end". */
364
365static void
fba45db2 366add_new_header_file (char *name, int instance)
c906108c 367{
52f0bd74
AC
368 int i;
369 struct header_file *hfile;
c906108c
SS
370
371 /* Make sure there is room for one more header file. */
372
373 i = N_ALLOCATED_HEADER_FILES (current_objfile);
374
375 if (N_HEADER_FILES (current_objfile) == i)
376 {
377 if (i == 0)
378 {
379 N_ALLOCATED_HEADER_FILES (current_objfile) = 10;
380 HEADER_FILES (current_objfile) = (struct header_file *)
381 xmalloc (10 * sizeof (struct header_file));
382 }
383 else
384 {
385 i *= 2;
386 N_ALLOCATED_HEADER_FILES (current_objfile) = i;
387 HEADER_FILES (current_objfile) = (struct header_file *)
388 xrealloc ((char *) HEADER_FILES (current_objfile),
389 (i * sizeof (struct header_file)));
390 }
391 }
392
393 /* Create an entry for this header file. */
394
395 i = N_HEADER_FILES (current_objfile)++;
396 hfile = HEADER_FILES (current_objfile) + i;
c5aa993b 397 hfile->name = savestring (name, strlen (name));
c906108c
SS
398 hfile->instance = instance;
399 hfile->length = 10;
400 hfile->vector
401 = (struct type **) xmalloc (10 * sizeof (struct type *));
402 memset (hfile->vector, 0, 10 * sizeof (struct type *));
403
404 add_this_object_header_file (i);
405}
406
407#if 0
408static struct type **
fba45db2 409explicit_lookup_type (int real_filenum, int index)
c906108c 410{
52f0bd74 411 struct header_file *f = &HEADER_FILES (current_objfile)[real_filenum];
c906108c
SS
412
413 if (index >= f->length)
414 {
415 f->length *= 2;
416 f->vector = (struct type **)
417 xrealloc (f->vector, f->length * sizeof (struct type *));
418 memset (&f->vector[f->length / 2],
c5aa993b 419 '\0', f->length * sizeof (struct type *) / 2);
c906108c
SS
420 }
421 return &f->vector[index];
422}
423#endif
424\f
425static void
fba45db2
KB
426record_minimal_symbol (char *name, CORE_ADDR address, int type,
427 struct objfile *objfile)
c906108c
SS
428{
429 enum minimal_symbol_type ms_type;
430 int section;
431 asection *bfd_section;
432
433 switch (type)
434 {
435 case N_TEXT | N_EXT:
436 ms_type = mst_text;
b8fbeb18 437 section = SECT_OFF_TEXT (objfile);
c906108c
SS
438 bfd_section = DBX_TEXT_SECTION (objfile);
439 break;
440 case N_DATA | N_EXT:
441 ms_type = mst_data;
b8fbeb18 442 section = SECT_OFF_DATA (objfile);
c906108c
SS
443 bfd_section = DBX_DATA_SECTION (objfile);
444 break;
445 case N_BSS | N_EXT:
446 ms_type = mst_bss;
b8fbeb18 447 section = SECT_OFF_BSS (objfile);
c906108c
SS
448 bfd_section = DBX_BSS_SECTION (objfile);
449 break;
450 case N_ABS | N_EXT:
451 ms_type = mst_abs;
452 section = -1;
453 bfd_section = NULL;
454 break;
455#ifdef N_SETV
456 case N_SETV | N_EXT:
457 ms_type = mst_data;
b8fbeb18 458 section = SECT_OFF_DATA (objfile);
c906108c
SS
459 bfd_section = DBX_DATA_SECTION (objfile);
460 break;
461 case N_SETV:
462 /* I don't think this type actually exists; since a N_SETV is the result
c5aa993b
JM
463 of going over many .o files, it doesn't make sense to have one
464 file local. */
c906108c 465 ms_type = mst_file_data;
b8fbeb18 466 section = SECT_OFF_DATA (objfile);
c906108c
SS
467 bfd_section = DBX_DATA_SECTION (objfile);
468 break;
469#endif
470 case N_TEXT:
471 case N_NBTEXT:
472 case N_FN:
473 case N_FN_SEQ:
474 ms_type = mst_file_text;
b8fbeb18 475 section = SECT_OFF_TEXT (objfile);
c906108c
SS
476 bfd_section = DBX_TEXT_SECTION (objfile);
477 break;
478 case N_DATA:
479 ms_type = mst_file_data;
480
481 /* Check for __DYNAMIC, which is used by Sun shared libraries.
c5aa993b
JM
482 Record it as global even if it's local, not global, so
483 lookup_minimal_symbol can find it. We don't check symbol_leading_char
484 because for SunOS4 it always is '_'. */
91f54576 485 if (name[8] == 'C' && strcmp ("__DYNAMIC", name) == 0)
c906108c
SS
486 ms_type = mst_data;
487
488 /* Same with virtual function tables, both global and static. */
489 {
490 char *tempstring = name;
491 if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
492 ++tempstring;
015a42b4 493 if (is_vtable_name (tempstring))
c906108c
SS
494 ms_type = mst_data;
495 }
b8fbeb18 496 section = SECT_OFF_DATA (objfile);
c906108c
SS
497 bfd_section = DBX_DATA_SECTION (objfile);
498 break;
499 case N_BSS:
500 ms_type = mst_file_bss;
b8fbeb18 501 section = SECT_OFF_BSS (objfile);
c906108c
SS
502 bfd_section = DBX_BSS_SECTION (objfile);
503 break;
504 default:
505 ms_type = mst_unknown;
506 section = -1;
507 bfd_section = NULL;
508 break;
c5aa993b 509 }
c906108c
SS
510
511 if ((ms_type == mst_file_text || ms_type == mst_text)
512 && address < lowest_text_address)
513 lowest_text_address = address;
514
515 prim_record_minimal_symbol_and_info
516 (name, address, ms_type, NULL, section, bfd_section, objfile);
517}
518\f
519/* Scan and build partial symbols for a symbol file.
520 We have been initialized by a call to dbx_symfile_init, which
521 put all the relevant info into a "struct dbx_symfile_info",
522 hung off the objfile structure.
523
c906108c
SS
524 MAINLINE is true if we are reading the main symbol
525 table (as opposed to a shared lib or dynamically loaded file). */
526
527static void
9df3df99 528dbx_symfile_read (struct objfile *objfile, int mainline)
c906108c
SS
529{
530 bfd *sym_bfd;
531 int val;
532 struct cleanup *back_to;
533
c906108c
SS
534 sym_bfd = objfile->obfd;
535
536 /* .o and .nlm files are relocatables with text, data and bss segs based at
537 0. This flag disables special (Solaris stabs-in-elf only) fixups for
538 symbols with a value of 0. */
539
540 symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
541
542 /* This is true for Solaris (and all other systems which put stabs
543 in sections, hopefully, since it would be silly to do things
544 differently from Solaris), and false for SunOS4 and other a.out
545 file formats. */
546 block_address_function_relative =
547 ((0 == strncmp (bfd_get_target (sym_bfd), "elf", 3))
548 || (0 == strncmp (bfd_get_target (sym_bfd), "som", 3))
549 || (0 == strncmp (bfd_get_target (sym_bfd), "coff", 4))
550 || (0 == strncmp (bfd_get_target (sym_bfd), "pe", 2))
c2d11a7d 551 || (0 == strncmp (bfd_get_target (sym_bfd), "epoc-pe", 7))
c906108c
SS
552 || (0 == strncmp (bfd_get_target (sym_bfd), "nlm", 3)));
553
554 val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
555 if (val < 0)
556 perror_with_name (objfile->name);
557
558 /* If we are reinitializing, or if we have never loaded syms yet, init */
559 if (mainline
ef96bde8
EZ
560 || (objfile->global_psymbols.size == 0
561 && objfile->static_psymbols.size == 0))
c906108c
SS
562 init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
563
564 symbol_size = DBX_SYMBOL_SIZE (objfile);
565 symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
566
567 free_pending_blocks ();
a0b3c4fd 568 back_to = make_cleanup (really_free_pendings, 0);
c906108c
SS
569
570 init_minimal_symbol_collection ();
56e290f4 571 make_cleanup_discard_minimal_symbols ();
c906108c 572
d4f3574e 573 /* Read stabs data from executable file and define symbols. */
c906108c 574
d4f3574e 575 read_dbx_symtab (objfile);
c906108c
SS
576
577 /* Add the dynamic symbols. */
578
96baa820 579 read_dbx_dynamic_symtab (objfile);
c906108c
SS
580
581 /* Install any minimal symbols that have been collected as the current
582 minimal symbols for this objfile. */
583
584 install_minimal_symbols (objfile);
585
586 do_cleanups (back_to);
587}
588
589/* Initialize anything that needs initializing when a completely new
590 symbol file is specified (not just adding some symbols from another
591 file, e.g. a shared library). */
592
593static void
fba45db2 594dbx_new_init (struct objfile *ignore)
c906108c
SS
595{
596 stabsread_new_init ();
597 buildsym_new_init ();
598 init_header_files ();
599}
600
601
602/* dbx_symfile_init ()
603 is the dbx-specific initialization routine for reading symbols.
604 It is passed a struct objfile which contains, among other things,
605 the BFD for the file whose symbols are being read, and a slot for a pointer
606 to "private data" which we fill with goodies.
607
608 We read the string table into malloc'd space and stash a pointer to it.
609
610 Since BFD doesn't know how to read debug symbols in a format-independent
611 way (and may never do so...), we have to do it ourselves. We will never
612 be called unless this is an a.out (or very similar) file.
613 FIXME, there should be a cleaner peephole into the BFD environment here. */
614
c5aa993b 615#define DBX_STRINGTAB_SIZE_SIZE sizeof(long) /* FIXME */
c906108c
SS
616
617static void
fba45db2 618dbx_symfile_init (struct objfile *objfile)
c906108c
SS
619{
620 int val;
621 bfd *sym_bfd = objfile->obfd;
622 char *name = bfd_get_filename (sym_bfd);
623 asection *text_sect;
624 unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
625
626 /* Allocate struct to keep track of the symfile */
0a6ddd08 627 objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *)
7936743b 628 xmalloc (sizeof (struct dbx_symfile_info));
063e58ba
MD
629 memset (objfile->deprecated_sym_stab_info, 0,
630 sizeof (struct dbx_symfile_info));
c906108c
SS
631
632 DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
633 DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
634 DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
635
636 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
637#define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
638#define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
639
640 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
641
642 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
c5aa993b 643
c906108c
SS
644 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
645 if (!text_sect)
8a3fe4f8 646 error (_("Can't find .text section in symbol file"));
c906108c
SS
647 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
648 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
649
650 DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
651 DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
652 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
653
8b92e4d5
EZ
654 /* Read the string table and stash it away in the objfile_obstack.
655 When we blow away the objfile the string table goes away as well.
c906108c
SS
656 Note that gdb used to use the results of attempting to malloc the
657 string table, based on the size it read, as a form of sanity check
658 for botched byte swapping, on the theory that a byte swapped string
659 table size would be so totally bogus that the malloc would fail. Now
8b92e4d5 660 that we put in on the objfile_obstack, we can't do this since gdb gets
c906108c
SS
661 a fatal error (out of virtual memory) if the size is bogus. We can
662 however at least check to see if the size is less than the size of
663 the size field itself, or larger than the size of the entire file.
664 Note that all valid string tables have a size greater than zero, since
665 the bytes used to hold the size are included in the count. */
666
667 if (STRING_TABLE_OFFSET == 0)
668 {
669 /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
c5aa993b
JM
670 will never be zero, even when there is no string table. This
671 would appear to be a bug in bfd. */
c906108c
SS
672 DBX_STRINGTAB_SIZE (objfile) = 0;
673 DBX_STRINGTAB (objfile) = NULL;
674 }
675 else
676 {
677 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
678 if (val < 0)
679 perror_with_name (name);
c5aa993b 680
4efb68b1
AC
681 memset (size_temp, 0, sizeof (size_temp));
682 val = bfd_bread (size_temp, sizeof (size_temp), sym_bfd);
c906108c
SS
683 if (val < 0)
684 {
685 perror_with_name (name);
686 }
687 else if (val == 0)
688 {
689 /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
690 EOF if there is no string table, and attempting to read the size
691 from EOF will read zero bytes. */
692 DBX_STRINGTAB_SIZE (objfile) = 0;
693 DBX_STRINGTAB (objfile) = NULL;
694 }
695 else
696 {
697 /* Read some data that would appear to be the string table size.
698 If there really is a string table, then it is probably the right
699 size. Byteswap if necessary and validate the size. Note that
700 the minimum is DBX_STRINGTAB_SIZE_SIZE. If we just read some
701 random data that happened to be at STRING_TABLE_OFFSET, because
702 bfd can't tell us there is no string table, the sanity checks may
703 or may not catch this. */
704 DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
c5aa993b 705
c906108c
SS
706 if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
707 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
8a3fe4f8 708 error (_("ridiculous string table size (%d bytes)."),
c906108c 709 DBX_STRINGTAB_SIZE (objfile));
c5aa993b 710
c906108c 711 DBX_STRINGTAB (objfile) =
8b92e4d5 712 (char *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
713 DBX_STRINGTAB_SIZE (objfile));
714 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
c5aa993b 715
c906108c 716 /* Now read in the string table in one big gulp. */
c5aa993b 717
c906108c
SS
718 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
719 if (val < 0)
720 perror_with_name (name);
3a42e9d0
AM
721 val = bfd_bread (DBX_STRINGTAB (objfile),
722 DBX_STRINGTAB_SIZE (objfile),
723 sym_bfd);
c906108c
SS
724 if (val != DBX_STRINGTAB_SIZE (objfile))
725 perror_with_name (name);
726 }
727 }
728}
729
730/* Perform any local cleanups required when we are done with a particular
731 objfile. I.E, we are in the process of discarding all symbol information
732 for an objfile, freeing up all memory held for it, and unlinking the
733 objfile struct from the global list of known objfiles. */
734
735static void
fba45db2 736dbx_symfile_finish (struct objfile *objfile)
c906108c 737{
0a6ddd08 738 if (objfile->deprecated_sym_stab_info != NULL)
c906108c
SS
739 {
740 if (HEADER_FILES (objfile) != NULL)
741 {
aa1ee363
AC
742 int i = N_HEADER_FILES (objfile);
743 struct header_file *hfiles = HEADER_FILES (objfile);
c906108c
SS
744
745 while (--i >= 0)
746 {
b8c9b27d
KB
747 xfree (hfiles[i].name);
748 xfree (hfiles[i].vector);
c906108c 749 }
b8c9b27d 750 xfree (hfiles);
c906108c 751 }
0a6ddd08 752 xfree (objfile->deprecated_sym_stab_info);
c906108c
SS
753 }
754 free_header_files ();
755}
c906108c 756\f
c5aa993b 757
c906108c
SS
758/* Buffer for reading the symbol table entries. */
759static struct external_nlist symbuf[4096];
760static int symbuf_idx;
761static int symbuf_end;
762
c906108c
SS
763/* Name of last function encountered. Used in Solaris to approximate
764 object file boundaries. */
765static char *last_function_name;
766
767/* The address in memory of the string table of the object file we are
768 reading (which might not be the "main" object file, but might be a
769 shared library or some other dynamically loaded thing). This is
770 set by read_dbx_symtab when building psymtabs, and by
771 read_ofile_symtab when building symtabs, and is used only by
772 next_symbol_text. FIXME: If that is true, we don't need it when
773 building psymtabs, right? */
774static char *stringtab_global;
775
776/* These variables are used to control fill_symbuf when the stabs
777 symbols are not contiguous (as may be the case when a COFF file is
778 linked using --split-by-reloc). */
779static struct stab_section_list *symbuf_sections;
780static unsigned int symbuf_left;
781static unsigned int symbuf_read;
782
086df311
DJ
783/* This variable stores a global stabs buffer, if we read stabs into
784 memory in one chunk in order to process relocations. */
785static bfd_byte *stabs_data;
786
c906108c
SS
787/* Refill the symbol table input buffer
788 and set the variables that control fetching entries from it.
789 Reports an error if no data available.
790 This function can read past the end of the symbol table
791 (into the string table) but this does no harm. */
792
793static void
fba45db2 794fill_symbuf (bfd *sym_bfd)
c906108c
SS
795{
796 unsigned int count;
797 int nbytes;
798
086df311
DJ
799 if (stabs_data)
800 {
801 nbytes = sizeof (symbuf);
802 if (nbytes > symbuf_left)
803 nbytes = symbuf_left;
804 memcpy (symbuf, stabs_data + symbuf_read, nbytes);
805 }
806 else if (symbuf_sections == NULL)
807 {
808 count = sizeof (symbuf);
809 nbytes = bfd_bread (symbuf, count, sym_bfd);
810 }
c906108c
SS
811 else
812 {
813 if (symbuf_left <= 0)
814 {
815 file_ptr filepos = symbuf_sections->section->filepos;
816 if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
817 perror_with_name (bfd_get_filename (sym_bfd));
818 symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
819 symbol_table_offset = filepos - symbuf_read;
820 symbuf_sections = symbuf_sections->next;
821 }
822
823 count = symbuf_left;
824 if (count > sizeof (symbuf))
825 count = sizeof (symbuf);
086df311 826 nbytes = bfd_bread (symbuf, count, sym_bfd);
c906108c
SS
827 }
828
c906108c
SS
829 if (nbytes < 0)
830 perror_with_name (bfd_get_filename (sym_bfd));
831 else if (nbytes == 0)
8a3fe4f8 832 error (_("Premature end of file reading symbol table"));
c906108c
SS
833 symbuf_end = nbytes / symbol_size;
834 symbuf_idx = 0;
835 symbuf_left -= nbytes;
836 symbuf_read += nbytes;
837}
838
086df311
DJ
839static void
840stabs_seek (int sym_offset)
841{
842 if (stabs_data)
843 {
844 symbuf_read += sym_offset;
845 symbuf_left -= sym_offset;
846 }
847 else
848 bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
849}
850
c906108c
SS
851#define INTERNALIZE_SYMBOL(intern, extern, abfd) \
852 { \
853 (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type); \
854 (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx); \
855 (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc); \
40b3352b
L
856 if (bfd_get_sign_extend_vma (abfd)) \
857 (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value); \
858 else \
859 (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value); \
c906108c
SS
860 }
861
862/* Invariant: The symbol pointed to by symbuf_idx is the first one
863 that hasn't been swapped. Swap the symbol at the same time
864 that symbuf_idx is incremented. */
865
866/* dbx allows the text of a symbol name to be continued into the
867 next symbol name! When such a continuation is encountered
868 (a \ at the end of the text of a name)
869 call this function to get the continuation. */
870
871static char *
fba45db2 872dbx_next_symbol_text (struct objfile *objfile)
c906108c
SS
873{
874 struct internal_nlist nlist;
875
876 if (symbuf_idx == symbuf_end)
877 fill_symbuf (symfile_bfd);
878
879 symnum++;
c5aa993b 880 INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
c906108c
SS
881 OBJSTAT (objfile, n_stabs++);
882
883 symbuf_idx++;
884
885 return nlist.n_strx + stringtab_global + file_string_table_offset;
886}
887\f
888/* Initialize the list of bincls to contain none and have some
889 allocated. */
890
891static void
fba45db2 892init_bincl_list (int number, struct objfile *objfile)
c906108c
SS
893{
894 bincls_allocated = number;
895 next_bincl = bincl_list = (struct header_file_location *)
7936743b 896 xmalloc (bincls_allocated * sizeof (struct header_file_location));
c906108c
SS
897}
898
899/* Add a bincl to the list. */
900
901static void
fba45db2 902add_bincl_to_list (struct partial_symtab *pst, char *name, int instance)
c906108c
SS
903{
904 if (next_bincl >= bincl_list + bincls_allocated)
905 {
906 int offset = next_bincl - bincl_list;
907 bincls_allocated *= 2;
908 bincl_list = (struct header_file_location *)
0efffb96
AC
909 xrealloc ((char *) bincl_list,
910 bincls_allocated * sizeof (struct header_file_location));
c906108c
SS
911 next_bincl = bincl_list + offset;
912 }
913 next_bincl->pst = pst;
914 next_bincl->instance = instance;
915 next_bincl++->name = name;
916}
917
918/* Given a name, value pair, find the corresponding
919 bincl in the list. Return the partial symtab associated
920 with that header_file_location. */
921
922static struct partial_symtab *
fba45db2 923find_corresponding_bincl_psymtab (char *name, int instance)
c906108c
SS
924{
925 struct header_file_location *bincl;
926
927 for (bincl = bincl_list; bincl < next_bincl; bincl++)
928 if (bincl->instance == instance
6314a349 929 && strcmp (name, bincl->name) == 0)
c906108c
SS
930 return bincl->pst;
931
23136709 932 repeated_header_complaint (name, symnum);
c906108c
SS
933 return (struct partial_symtab *) 0;
934}
935
936/* Free the storage allocated for the bincl list. */
937
938static void
fba45db2 939free_bincl_list (struct objfile *objfile)
c906108c 940{
2dc74dc1 941 xfree (bincl_list);
c906108c
SS
942 bincls_allocated = 0;
943}
944
74b7792f
AC
945static void
946do_free_bincl_list_cleanup (void *objfile)
947{
948 free_bincl_list (objfile);
949}
950
951static struct cleanup *
952make_cleanup_free_bincl_list (struct objfile *objfile)
953{
954 return make_cleanup (do_free_bincl_list_cleanup, objfile);
955}
956
6a34fd2f
EZ
957/* Set namestring based on nlist. If the string table index is invalid,
958 give a fake name, and print a single error message per symbol file read,
959 rather than abort the symbol reading or flood the user with messages. */
960
961static char *
962set_namestring (struct objfile *objfile, struct internal_nlist nlist)
963{
964 char *namestring;
965
966 if (((unsigned) nlist.n_strx + file_string_table_offset) >=
967 DBX_STRINGTAB_SIZE (objfile))
968 {
e2e0b3e5 969 complaint (&symfile_complaints, _("bad string table offset in symbol %d"),
23136709 970 symnum);
6a34fd2f
EZ
971 namestring = "<bad string table offset>";
972 }
973 else
974 namestring = nlist.n_strx + file_string_table_offset +
975 DBX_STRINGTAB (objfile);
976 return namestring;
977}
978
c906108c
SS
979/* Scan a SunOs dynamic symbol table for symbols of interest and
980 add them to the minimal symbol table. */
981
982static void
fba45db2 983read_dbx_dynamic_symtab (struct objfile *objfile)
c906108c
SS
984{
985 bfd *abfd = objfile->obfd;
986 struct cleanup *back_to;
987 int counter;
988 long dynsym_size;
989 long dynsym_count;
990 asymbol **dynsyms;
991 asymbol **symptr;
992 arelent **relptr;
993 long dynrel_size;
994 long dynrel_count;
995 arelent **dynrels;
996 CORE_ADDR sym_value;
997 char *name;
998
999 /* Check that the symbol file has dynamic symbols that we know about.
1000 bfd_arch_unknown can happen if we are reading a sun3 symbol file
1001 on a sun4 host (and vice versa) and bfd is not configured
1002 --with-target=all. This would trigger an assertion in bfd/sunos.c,
1003 so we ignore the dynamic symbols in this case. */
1004 if (bfd_get_flavour (abfd) != bfd_target_aout_flavour
1005 || (bfd_get_file_flags (abfd) & DYNAMIC) == 0
1006 || bfd_get_arch (abfd) == bfd_arch_unknown)
1007 return;
1008
1009 dynsym_size = bfd_get_dynamic_symtab_upper_bound (abfd);
1010 if (dynsym_size < 0)
1011 return;
1012
1013 dynsyms = (asymbol **) xmalloc (dynsym_size);
b8c9b27d 1014 back_to = make_cleanup (xfree, dynsyms);
c906108c
SS
1015
1016 dynsym_count = bfd_canonicalize_dynamic_symtab (abfd, dynsyms);
1017 if (dynsym_count < 0)
1018 {
1019 do_cleanups (back_to);
1020 return;
1021 }
1022
1023 /* Enter dynamic symbols into the minimal symbol table
1024 if this is a stripped executable. */
1025 if (bfd_get_symcount (abfd) <= 0)
1026 {
1027 symptr = dynsyms;
1028 for (counter = 0; counter < dynsym_count; counter++, symptr++)
1029 {
1030 asymbol *sym = *symptr;
1031 asection *sec;
1032 int type;
1033
1034 sec = bfd_get_section (sym);
1035
1036 /* BFD symbols are section relative. */
1037 sym_value = sym->value + sec->vma;
1038
1039 if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
1040 {
063e58ba
MD
1041 sym_value += ANOFFSET (objfile->section_offsets,
1042 SECT_OFF_TEXT (objfile));
c906108c
SS
1043 type = N_TEXT;
1044 }
1045 else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
1046 {
063e58ba
MD
1047 sym_value += ANOFFSET (objfile->section_offsets,
1048 SECT_OFF_DATA (objfile));
c906108c
SS
1049 type = N_DATA;
1050 }
1051 else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
1052 {
063e58ba
MD
1053 sym_value += ANOFFSET (objfile->section_offsets,
1054 SECT_OFF_BSS (objfile));
c906108c
SS
1055 type = N_BSS;
1056 }
1057 else
1058 continue;
1059
1060 if (sym->flags & BSF_GLOBAL)
1061 type |= N_EXT;
1062
1063 record_minimal_symbol ((char *) bfd_asymbol_name (sym), sym_value,
1064 type, objfile);
1065 }
1066 }
1067
1068 /* Symbols from shared libraries have a dynamic relocation entry
1069 that points to the associated slot in the procedure linkage table.
1070 We make a mininal symbol table entry with type mst_solib_trampoline
1071 at the address in the procedure linkage table. */
1072 dynrel_size = bfd_get_dynamic_reloc_upper_bound (abfd);
1073 if (dynrel_size < 0)
1074 {
1075 do_cleanups (back_to);
1076 return;
1077 }
c5aa993b 1078
c906108c 1079 dynrels = (arelent **) xmalloc (dynrel_size);
b8c9b27d 1080 make_cleanup (xfree, dynrels);
c906108c
SS
1081
1082 dynrel_count = bfd_canonicalize_dynamic_reloc (abfd, dynrels, dynsyms);
1083 if (dynrel_count < 0)
1084 {
1085 do_cleanups (back_to);
1086 return;
1087 }
1088
1089 for (counter = 0, relptr = dynrels;
1090 counter < dynrel_count;
1091 counter++, relptr++)
1092 {
1093 arelent *rel = *relptr;
1094 CORE_ADDR address =
063e58ba
MD
1095 rel->address + ANOFFSET (objfile->section_offsets,
1096 SECT_OFF_DATA (objfile));
c906108c
SS
1097
1098 switch (bfd_get_arch (abfd))
1099 {
1100 case bfd_arch_sparc:
1101 if (rel->howto->type != RELOC_JMP_SLOT)
1102 continue;
1103 break;
1104 case bfd_arch_m68k:
1105 /* `16' is the type BFD produces for a jump table relocation. */
1106 if (rel->howto->type != 16)
1107 continue;
1108
1109 /* Adjust address in the jump table to point to
1110 the start of the bsr instruction. */
1111 address -= 2;
1112 break;
1113 default:
1114 continue;
1115 }
1116
1117 name = (char *) bfd_asymbol_name (*rel->sym_ptr_ptr);
1118 prim_record_minimal_symbol (name, address, mst_solib_trampoline,
1119 objfile);
1120 }
1121
1122 do_cleanups (back_to);
1123}
1124
a78f21af 1125static CORE_ADDR
4867e41e
DM
1126find_stab_function_addr (char *namestring, char *filename,
1127 struct objfile *objfile)
1128{
1129 struct minimal_symbol *msym;
1130 char *p;
1131 int n;
1132
1133 p = strchr (namestring, ':');
1134 if (p == NULL)
1135 p = namestring;
1136 n = p - namestring;
1137 p = alloca (n + 2);
1138 strncpy (p, namestring, n);
1139 p[n] = 0;
1140
1141 msym = lookup_minimal_symbol (p, filename, objfile);
1142 if (msym == NULL)
1143 {
1144 /* Sun Fortran appends an underscore to the minimal symbol name,
1145 try again with an appended underscore if the minimal symbol
1146 was not found. */
1147 p[n] = '_';
1148 p[n + 1] = 0;
1149 msym = lookup_minimal_symbol (p, filename, objfile);
1150 }
1151
1152 if (msym == NULL && filename != NULL)
1153 {
1154 /* Try again without the filename. */
1155 p[n] = 0;
1156 msym = lookup_minimal_symbol (p, NULL, objfile);
1157 }
1158 if (msym == NULL && filename != NULL)
1159 {
1160 /* And try again for Sun Fortran, but without the filename. */
1161 p[n] = '_';
1162 p[n + 1] = 0;
1163 msym = lookup_minimal_symbol (p, NULL, objfile);
1164 }
1165
1166 return msym == NULL ? 0 : SYMBOL_VALUE_ADDRESS (msym);
1167}
4867e41e 1168
23136709
KB
1169static void
1170function_outside_compilation_unit_complaint (const char *arg1)
1171{
1172 complaint (&symfile_complaints,
063e58ba
MD
1173 _("function `%s' appears to be defined outside of all compilation \
1174units"),
23136709
KB
1175 arg1);
1176}
1177
d4f3574e
SS
1178/* Setup partial_symtab's describing each source file for which
1179 debugging information is available. */
c906108c
SS
1180
1181static void
fba45db2 1182read_dbx_symtab (struct objfile *objfile)
c906108c 1183{
52f0bd74 1184 struct external_nlist *bufp = 0; /* =0 avoids gcc -Wall glitch */
c906108c 1185 struct internal_nlist nlist;
d4f3574e
SS
1186 CORE_ADDR text_addr;
1187 int text_size;
c906108c 1188
52f0bd74 1189 char *namestring;
c906108c
SS
1190 int nsl;
1191 int past_first_source_file = 0;
1192 CORE_ADDR last_o_file_start = 0;
1193 CORE_ADDR last_function_start = 0;
1194 struct cleanup *back_to;
1195 bfd *abfd;
1196 int textlow_not_set;
7c8a5605 1197 int data_sect_index;
c906108c
SS
1198
1199 /* Current partial symtab */
1200 struct partial_symtab *pst;
1201
1202 /* List of current psymtab's include files */
1203 char **psymtab_include_list;
1204 int includes_allocated;
1205 int includes_used;
1206
1207 /* Index within current psymtab dependency list */
1208 struct partial_symtab **dependency_list;
1209 int dependencies_used, dependencies_allocated;
1210
d4f3574e
SS
1211 text_addr = DBX_TEXT_ADDR (objfile);
1212 text_size = DBX_TEXT_SIZE (objfile);
1213
c906108c
SS
1214 /* FIXME. We probably want to change stringtab_global rather than add this
1215 while processing every symbol entry. FIXME. */
1216 file_string_table_offset = 0;
1217 next_file_string_table_offset = 0;
1218
1219 stringtab_global = DBX_STRINGTAB (objfile);
c5aa993b 1220
c906108c
SS
1221 pst = (struct partial_symtab *) 0;
1222
1223 includes_allocated = 30;
1224 includes_used = 0;
1225 psymtab_include_list = (char **) alloca (includes_allocated *
1226 sizeof (char *));
1227
1228 dependencies_allocated = 30;
1229 dependencies_used = 0;
1230 dependency_list =
1231 (struct partial_symtab **) alloca (dependencies_allocated *
1232 sizeof (struct partial_symtab *));
1233
1234 /* Init bincl list */
1235 init_bincl_list (20, objfile);
74b7792f 1236 back_to = make_cleanup_free_bincl_list (objfile);
c906108c
SS
1237
1238 last_source_file = NULL;
1239
96baa820 1240 lowest_text_address = (CORE_ADDR) -1;
c906108c
SS
1241
1242 symfile_bfd = objfile->obfd; /* For next_text_symbol */
1243 abfd = objfile->obfd;
1244 symbuf_end = symbuf_idx = 0;
1245 next_symbol_text_func = dbx_next_symbol_text;
1246 textlow_not_set = 1;
1247 has_line_numbers = 0;
1248
4f49b26b
JB
1249 /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
1250 to global and static variables. The stab for a global or static
1251 variable doesn't give us any indication of which section it's in,
1252 so we can't tell immediately which offset in
1253 objfile->section_offsets we should apply to the variable's
1254 address.
1255
1256 We could certainly find out which section contains the variable
1257 by looking up the variable's unrelocated address with
1258 find_pc_section, but that would be expensive; this is the
1259 function that constructs the partial symbol tables by examining
1260 every symbol in the entire executable, and it's
1261 performance-critical. So that expense would not be welcome. I'm
1262 not sure what to do about this at the moment.
1263
1264 What we have done for years is to simply assume that the .data
1265 section's offset is appropriate for all global and static
1266 variables. Recently, this was expanded to fall back to the .bss
1267 section's offset if there is no .data section, and then to the
1268 .rodata section's offset. */
7c8a5605
JB
1269 data_sect_index = objfile->sect_index_data;
1270 if (data_sect_index == -1)
1271 data_sect_index = SECT_OFF_BSS (objfile);
2a13f9bc
JB
1272 if (data_sect_index == -1)
1273 data_sect_index = SECT_OFF_RODATA (objfile);
d646061f
JB
1274
1275 /* If data_sect_index is still -1, that's okay. It's perfectly fine
1276 for the file to have no .data, no .bss, and no .text at all, if
1277 it also has no global or static variables. If it does, we will
1278 get an internal error from an ANOFFSET macro below when we try to
1279 use data_sect_index. */
7c8a5605 1280
c906108c
SS
1281 for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1282 {
1283 /* Get the symbol for this run and pull out some info */
c5aa993b 1284 QUIT; /* allow this to be interruptable */
c906108c
SS
1285 if (symbuf_idx == symbuf_end)
1286 fill_symbuf (abfd);
1287 bufp = &symbuf[symbuf_idx++];
1288
1289 /*
1290 * Special case to speed up readin.
1291 */
1292 if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
1293 {
1294 has_line_numbers = 1;
1295 continue;
1296 }
1297
1298 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1299 OBJSTAT (objfile, n_stabs++);
1300
1301 /* Ok. There is a lot of code duplicated in the rest of this
1302 switch statement (for efficiency reasons). Since I don't
1303 like duplicating code, I will do my penance here, and
1304 describe the code which is duplicated:
1305
c5aa993b
JM
1306 *) The assignment to namestring.
1307 *) The call to strchr.
1308 *) The addition of a partial symbol the the two partial
1309 symbol lists. This last is a large section of code, so
1310 I've imbedded it in the following macro.
6a34fd2f 1311 */
c5aa993b 1312
6a34fd2f
EZ
1313 switch (nlist.n_type)
1314 {
6a34fd2f
EZ
1315 char *p;
1316 /*
1317 * Standard, external, non-debugger, symbols
1318 */
1319
1320 case N_TEXT | N_EXT:
1321 case N_NBTEXT | N_EXT:
063e58ba
MD
1322 nlist.n_value += ANOFFSET (objfile->section_offsets,
1323 SECT_OFF_TEXT (objfile));
6a34fd2f
EZ
1324 goto record_it;
1325
1326 case N_DATA | N_EXT:
1327 case N_NBDATA | N_EXT:
063e58ba
MD
1328 nlist.n_value += ANOFFSET (objfile->section_offsets,
1329 SECT_OFF_DATA (objfile));
6a34fd2f
EZ
1330 goto record_it;
1331
1332 case N_BSS:
1333 case N_BSS | N_EXT:
1334 case N_NBBSS | N_EXT:
1335 case N_SETV | N_EXT: /* FIXME, is this in BSS? */
063e58ba
MD
1336 nlist.n_value += ANOFFSET (objfile->section_offsets,
1337 SECT_OFF_BSS (objfile));
6a34fd2f
EZ
1338 goto record_it;
1339
1340 case N_ABS | N_EXT:
1341 record_it:
1342 namestring = set_namestring (objfile, nlist);
1343
1344 bss_ext_symbol:
1345 record_minimal_symbol (namestring, nlist.n_value,
1346 nlist.n_type, objfile); /* Always */
1347 continue;
1348
1349 /* Standard, local, non-debugger, symbols */
1350
1351 case N_NBTEXT:
1352
1353 /* We need to be able to deal with both N_FN or N_TEXT,
1354 because we have no way of knowing whether the sys-supplied ld
1355 or GNU ld was used to make the executable. Sequents throw
1356 in another wrinkle -- they renumbered N_FN. */
1357
1358 case N_FN:
1359 case N_FN_SEQ:
1360 case N_TEXT:
063e58ba
MD
1361 nlist.n_value += ANOFFSET (objfile->section_offsets,
1362 SECT_OFF_TEXT (objfile));
6a34fd2f
EZ
1363 namestring = set_namestring (objfile, nlist);
1364
1365 if ((namestring[0] == '-' && namestring[1] == 'l')
1366 || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
1367 && namestring[nsl - 2] == '.'))
1368 {
6a34fd2f
EZ
1369 if (past_first_source_file && pst
1370 /* The gould NP1 uses low values for .o and -l symbols
1371 which are not the address. */
5afc051b 1372 && nlist.n_value >= pst->textlow)
6a34fd2f
EZ
1373 {
1374 end_psymtab (pst, psymtab_include_list, includes_used,
1375 symnum * symbol_size,
5afc051b
JB
1376 nlist.n_value > pst->texthigh
1377 ? nlist.n_value : pst->texthigh,
063e58ba
MD
1378 dependency_list, dependencies_used,
1379 textlow_not_set);
6a34fd2f
EZ
1380 pst = (struct partial_symtab *) 0;
1381 includes_used = 0;
1382 dependencies_used = 0;
1383 }
1384 else
1385 past_first_source_file = 1;
1386 last_o_file_start = nlist.n_value;
1387 }
1388 else
1389 goto record_it;
1390 continue;
1391
1392 case N_DATA:
063e58ba
MD
1393 nlist.n_value += ANOFFSET (objfile->section_offsets,
1394 SECT_OFF_DATA (objfile));
6a34fd2f
EZ
1395 goto record_it;
1396
1397 case N_UNDF | N_EXT:
1398 if (nlist.n_value != 0)
1399 {
1400 /* This is a "Fortran COMMON" symbol. See if the target
1401 environment knows where it has been relocated to. */
1402
1403 CORE_ADDR reladdr;
1404
1405 namestring = set_namestring (objfile, nlist);
1406 if (target_lookup_symbol (namestring, &reladdr))
1407 {
063e58ba 1408 continue; /* Error in lookup; ignore symbol for now. */
6a34fd2f
EZ
1409 }
1410 nlist.n_type ^= (N_BSS ^ N_UNDF); /* Define it as a bss-symbol */
1411 nlist.n_value = reladdr;
1412 goto bss_ext_symbol;
1413 }
1414 continue; /* Just undefined, not COMMON */
1415
1416 case N_UNDF:
1417 if (processing_acc_compilation && nlist.n_strx == 1)
1418 {
1419 /* Deal with relative offsets in the string table
1420 used in ELF+STAB under Solaris. If we want to use the
1421 n_strx field, which contains the name of the file,
1422 we must adjust file_string_table_offset *before* calling
1423 set_namestring(). */
1424 past_first_source_file = 1;
1425 file_string_table_offset = next_file_string_table_offset;
1426 next_file_string_table_offset =
1427 file_string_table_offset + nlist.n_value;
1428 if (next_file_string_table_offset < file_string_table_offset)
8a3fe4f8 1429 error (_("string table offset backs up at %d"), symnum);
6a34fd2f
EZ
1430 /* FIXME -- replace error() with complaint. */
1431 continue;
1432 }
1433 continue;
1434
1435 /* Lots of symbol types we can just ignore. */
1436
1437 case N_ABS:
1438 case N_NBDATA:
1439 case N_NBBSS:
1440 continue;
1441
1442 /* Keep going . . . */
1443
1444 /*
1445 * Special symbol types for GNU
1446 */
1447 case N_INDR:
1448 case N_INDR | N_EXT:
1449 case N_SETA:
1450 case N_SETA | N_EXT:
1451 case N_SETT:
1452 case N_SETT | N_EXT:
1453 case N_SETD:
1454 case N_SETD | N_EXT:
1455 case N_SETB:
1456 case N_SETB | N_EXT:
1457 case N_SETV:
1458 continue;
1459
1460 /*
1461 * Debugger symbols
1462 */
1463
1464 case N_SO:
1465 {
1466 CORE_ADDR valu;
1467 static int prev_so_symnum = -10;
1468 static int first_so_symnum;
1469 char *p;
57c22c6c 1470 static char *dirname_nso;
6a34fd2f
EZ
1471 int prev_textlow_not_set;
1472
063e58ba
MD
1473 valu = nlist.n_value + ANOFFSET (objfile->section_offsets,
1474 SECT_OFF_TEXT (objfile));
6a34fd2f
EZ
1475
1476 prev_textlow_not_set = textlow_not_set;
1477
6a34fd2f
EZ
1478 /* A zero value is probably an indication for the SunPRO 3.0
1479 compiler. end_psymtab explicitly tests for zero, so
1480 don't relocate it. */
1481
203c3895
UW
1482 if (nlist.n_value == 0
1483 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f
EZ
1484 {
1485 textlow_not_set = 1;
1486 valu = 0;
1487 }
1488 else
1489 textlow_not_set = 0;
203c3895 1490
6a34fd2f
EZ
1491 past_first_source_file = 1;
1492
1493 if (prev_so_symnum != symnum - 1)
1494 { /* Here if prev stab wasn't N_SO */
1495 first_so_symnum = symnum;
1496
1497 if (pst)
1498 {
1499 end_psymtab (pst, psymtab_include_list, includes_used,
1500 symnum * symbol_size,
5afc051b 1501 valu > pst->texthigh ? valu : pst->texthigh,
6a34fd2f
EZ
1502 dependency_list, dependencies_used,
1503 prev_textlow_not_set);
1504 pst = (struct partial_symtab *) 0;
1505 includes_used = 0;
1506 dependencies_used = 0;
1507 }
1508 }
1509
1510 prev_so_symnum = symnum;
1511
1512 /* End the current partial symtab and start a new one */
1513
1514 namestring = set_namestring (objfile, nlist);
1515
1516 /* Null name means end of .o file. Don't start a new one. */
1517 if (*namestring == '\000')
1518 continue;
1519
1520 /* Some compilers (including gcc) emit a pair of initial N_SOs.
1521 The first one is a directory name; the second the file name.
1522 If pst exists, is empty, and has a filename ending in '/',
1523 we assume the previous N_SO was a directory name. */
1524
1525 p = strrchr (namestring, '/');
1526 if (p && *(p + 1) == '\000')
57c22c6c
BR
1527 {
1528 /* Save the directory name SOs locally, then save it into
1529 the psymtab when it's created below. */
1530 dirname_nso = namestring;
1531 continue;
1532 }
6a34fd2f
EZ
1533
1534 /* Some other compilers (C++ ones in particular) emit useless
1535 SOs for non-existant .c files. We ignore all subsequent SOs that
1536 immediately follow the first. */
1537
1538 if (!pst)
57c22c6c 1539 {
6a34fd2f
EZ
1540 pst = start_psymtab (objfile,
1541 namestring, valu,
1542 first_so_symnum * symbol_size,
1543 objfile->global_psymbols.next,
1544 objfile->static_psymbols.next);
57c22c6c
BR
1545 pst->dirname = dirname_nso;
1546 dirname_nso = NULL;
1547 }
6a34fd2f
EZ
1548 continue;
1549 }
1550
1551 case N_BINCL:
1552 {
1553 enum language tmp_language;
1554 /* Add this bincl to the bincl_list for future EXCLs. No
1555 need to save the string; it'll be around until
1556 read_dbx_symtab function returns */
1557
1558 namestring = set_namestring (objfile, nlist);
1559 tmp_language = deduce_language_from_filename (namestring);
1560
1561 /* Only change the psymtab's language if we've learned
1562 something useful (eg. tmp_language is not language_unknown).
1563 In addition, to match what start_subfile does, never change
1564 from C++ to C. */
1565 if (tmp_language != language_unknown
1566 && (tmp_language != language_c
1567 || psymtab_language != language_cplus))
d7f98cce 1568 psymtab_language = tmp_language;
6a34fd2f
EZ
1569
1570 if (pst == NULL)
1571 {
1572 /* FIXME: we should not get here without a PST to work on.
1573 Attempt to recover. */
23136709 1574 complaint (&symfile_complaints,
063e58ba
MD
1575 _("N_BINCL %s not in entries for any file, at symtab \
1576pos %d"),
23136709 1577 namestring, symnum);
6a34fd2f
EZ
1578 continue;
1579 }
1580 add_bincl_to_list (pst, namestring, nlist.n_value);
1581
1582 /* Mark down an include file in the current psymtab */
1583
1584 goto record_include_file;
1585 }
1586
1587 case N_SOL:
1588 {
1589 enum language tmp_language;
1590 /* Mark down an include file in the current psymtab */
1591
1592 namestring = set_namestring (objfile, nlist);
1593 tmp_language = deduce_language_from_filename (namestring);
1594
1595 /* Only change the psymtab's language if we've learned
1596 something useful (eg. tmp_language is not language_unknown).
1597 In addition, to match what start_subfile does, never change
1598 from C++ to C. */
1599 if (tmp_language != language_unknown
1600 && (tmp_language != language_c
1601 || psymtab_language != language_cplus))
d7f98cce 1602 psymtab_language = tmp_language;
6a34fd2f
EZ
1603
1604 /* In C++, one may expect the same filename to come round many
1605 times, when code is coming alternately from the main file
1606 and from inline functions in other files. So I check to see
1607 if this is a file we've seen before -- either the main
1608 source file, or a previously included file.
1609
1610 This seems to be a lot of time to be spending on N_SOL, but
1611 things like "break c-exp.y:435" need to work (I
1612 suppose the psymtab_include_list could be hashed or put
1613 in a binary tree, if profiling shows this is a major hog). */
6314a349 1614 if (pst && strcmp (namestring, pst->filename) == 0)
6a34fd2f
EZ
1615 continue;
1616 {
aa1ee363 1617 int i;
6a34fd2f 1618 for (i = 0; i < includes_used; i++)
6314a349 1619 if (strcmp (namestring, psymtab_include_list[i]) == 0)
6a34fd2f
EZ
1620 {
1621 i = -1;
1622 break;
1623 }
1624 if (i == -1)
1625 continue;
1626 }
1627
1628 record_include_file:
1629
1630 psymtab_include_list[includes_used++] = namestring;
1631 if (includes_used >= includes_allocated)
1632 {
1633 char **orig = psymtab_include_list;
1634
1635 psymtab_include_list = (char **)
1636 alloca ((includes_allocated *= 2) *
1637 sizeof (char *));
4efb68b1 1638 memcpy (psymtab_include_list, orig,
6a34fd2f
EZ
1639 includes_used * sizeof (char *));
1640 }
1641 continue;
1642 }
063e58ba 1643 case N_LSYM: /* Typedef or automatic variable. */
6a34fd2f
EZ
1644 case N_STSYM: /* Data seg var -- static */
1645 case N_LCSYM: /* BSS " */
1646 case N_ROSYM: /* Read-only data seg var -- static. */
1647 case N_NBSTS: /* Gould nobase. */
1648 case N_NBLCS: /* symbols. */
1649 case N_FUN:
063e58ba
MD
1650 case N_GSYM: /* Global (extern) variable; can be
1651 data or bss (sigh FIXME). */
6a34fd2f
EZ
1652
1653 /* Following may probably be ignored; I'll leave them here
1654 for now (until I do Pascal and Modula 2 extensions). */
1655
063e58ba
MD
1656 case N_PC: /* I may or may not need this; I
1657 suspect not. */
1658 case N_M2C: /* I suspect that I can ignore this here. */
6a34fd2f
EZ
1659 case N_SCOPE: /* Same. */
1660
1661 namestring = set_namestring (objfile, nlist);
1662
1663 /* See if this is an end of function stab. */
1664 if (pst && nlist.n_type == N_FUN && *namestring == '\000')
1665 {
1666 CORE_ADDR valu;
1667
1668 /* It's value is the size (in bytes) of the function for
1669 function relative stabs, or the address of the function's
1670 end for old style stabs. */
1671 valu = nlist.n_value + last_function_start;
5afc051b
JB
1672 if (pst->texthigh == 0 || valu > pst->texthigh)
1673 pst->texthigh = valu;
6a34fd2f
EZ
1674 break;
1675 }
1676
1677 p = (char *) strchr (namestring, ':');
1678 if (!p)
1679 continue; /* Not a debugging symbol. */
1680
1681
1682
1683 /* Main processing section for debugging symbols which
1684 the initial read through the symbol tables needs to worry
1685 about. If we reach this point, the symbol which we are
1686 considering is definitely one we are interested in.
1687 p must also contain the (valid) index into the namestring
1688 which indicates the debugging type symbol. */
1689
1690 switch (p[1])
1691 {
1692 case 'S':
063e58ba
MD
1693 nlist.n_value += ANOFFSET (objfile->section_offsets,
1694 data_sect_index);
149ad273
UW
1695
1696 if (gdbarch_static_transform_name_p (current_gdbarch))
1697 namestring = gdbarch_static_transform_name
1698 (current_gdbarch, namestring);
1699
6a34fd2f 1700 add_psymbol_to_list (namestring, p - namestring,
176620f1 1701 VAR_DOMAIN, LOC_STATIC,
6a34fd2f
EZ
1702 &objfile->static_psymbols,
1703 0, nlist.n_value,
1704 psymtab_language, objfile);
1705 continue;
1706 case 'G':
063e58ba
MD
1707 nlist.n_value += ANOFFSET (objfile->section_offsets,
1708 data_sect_index);
6a34fd2f
EZ
1709 /* The addresses in these entries are reported to be
1710 wrong. See the code that reads 'G's for symtabs. */
1711 add_psymbol_to_list (namestring, p - namestring,
176620f1 1712 VAR_DOMAIN, LOC_STATIC,
6a34fd2f
EZ
1713 &objfile->global_psymbols,
1714 0, nlist.n_value,
1715 psymtab_language, objfile);
1716 continue;
1717
1718 case 'T':
1719 /* When a 'T' entry is defining an anonymous enum, it
1720 may have a name which is the empty string, or a
1721 single space. Since they're not really defining a
1722 symbol, those shouldn't go in the partial symbol
1723 table. We do pick up the elements of such enums at
1724 'check_enum:', below. */
1725 if (p >= namestring + 2
1726 || (p == namestring + 1
1727 && namestring[0] != ' '))
1728 {
1729 add_psymbol_to_list (namestring, p - namestring,
176620f1 1730 STRUCT_DOMAIN, LOC_TYPEDEF,
6a34fd2f
EZ
1731 &objfile->static_psymbols,
1732 nlist.n_value, 0,
1733 psymtab_language, objfile);
1734 if (p[2] == 't')
1735 {
1736 /* Also a typedef with the same name. */
1737 add_psymbol_to_list (namestring, p - namestring,
176620f1 1738 VAR_DOMAIN, LOC_TYPEDEF,
6a34fd2f
EZ
1739 &objfile->static_psymbols,
1740 nlist.n_value, 0,
1741 psymtab_language, objfile);
1742 p += 1;
1743 }
6a34fd2f
EZ
1744 }
1745 goto check_enum;
1746 case 't':
1747 if (p != namestring) /* a name is there, not just :T... */
1748 {
1749 add_psymbol_to_list (namestring, p - namestring,
176620f1 1750 VAR_DOMAIN, LOC_TYPEDEF,
6a34fd2f
EZ
1751 &objfile->static_psymbols,
1752 nlist.n_value, 0,
1753 psymtab_language, objfile);
1754 }
1755 check_enum:
1756 /* If this is an enumerated type, we need to
1757 add all the enum constants to the partial symbol
1758 table. This does not cover enums without names, e.g.
1759 "enum {a, b} c;" in C, but fortunately those are
1760 rare. There is no way for GDB to find those from the
1761 enum type without spending too much time on it. Thus
1762 to solve this problem, the compiler needs to put out the
1763 enum in a nameless type. GCC2 does this. */
1764
1765 /* We are looking for something of the form
1766 <name> ":" ("t" | "T") [<number> "="] "e"
1767 {<constant> ":" <value> ","} ";". */
1768
1769 /* Skip over the colon and the 't' or 'T'. */
1770 p += 2;
1771 /* This type may be given a number. Also, numbers can come
1772 in pairs like (0,26). Skip over it. */
1773 while ((*p >= '0' && *p <= '9')
1774 || *p == '(' || *p == ',' || *p == ')'
1775 || *p == '=')
1776 p++;
1777
1778 if (*p++ == 'e')
1779 {
1780 /* The aix4 compiler emits extra crud before the members. */
1781 if (*p == '-')
1782 {
1783 /* Skip over the type (?). */
1784 while (*p != ':')
1785 p++;
1786
1787 /* Skip over the colon. */
1788 p++;
1789 }
1790
1791 /* We have found an enumerated type. */
1792 /* According to comments in read_enum_type
1793 a comma could end it instead of a semicolon.
1794 I don't know where that happens.
1795 Accept either. */
1796 while (*p && *p != ';' && *p != ',')
1797 {
1798 char *q;
1799
1800 /* Check for and handle cretinous dbx symbol name
1801 continuation! */
1802 if (*p == '\\' || (*p == '?' && p[1] == '\0'))
1803 p = next_symbol_text (objfile);
1804
1805 /* Point to the character after the name
1806 of the enum constant. */
1807 for (q = p; *q && *q != ':'; q++)
1808 ;
1809 /* Note that the value doesn't matter for
1810 enum constants in psymtabs, just in symtabs. */
1811 add_psymbol_to_list (p, q - p,
176620f1 1812 VAR_DOMAIN, LOC_CONST,
6a34fd2f
EZ
1813 &objfile->static_psymbols, 0,
1814 0, psymtab_language, objfile);
1815 /* Point past the name. */
1816 p = q;
1817 /* Skip over the value. */
1818 while (*p && *p != ',')
1819 p++;
1820 /* Advance past the comma. */
1821 if (*p)
1822 p++;
1823 }
1824 }
1825 continue;
1826 case 'c':
1827 /* Constant, e.g. from "const" in Pascal. */
1828 add_psymbol_to_list (namestring, p - namestring,
176620f1 1829 VAR_DOMAIN, LOC_CONST,
6a34fd2f
EZ
1830 &objfile->static_psymbols, nlist.n_value,
1831 0, psymtab_language, objfile);
1832 continue;
1833
1834 case 'f':
1835 if (! pst)
1836 {
1837 int name_len = p - namestring;
1838 char *name = xmalloc (name_len + 1);
1839 memcpy (name, namestring, name_len);
1840 name[name_len] = '\0';
23136709 1841 function_outside_compilation_unit_complaint (name);
6a34fd2f
EZ
1842 xfree (name);
1843 }
6b2d86bd
MS
1844 nlist.n_value += ANOFFSET (objfile->section_offsets,
1845 SECT_OFF_TEXT (objfile));
6a34fd2f
EZ
1846 /* Kludges for ELF/STABS with Sun ACC */
1847 last_function_name = namestring;
6a34fd2f
EZ
1848 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1849 value for the bottom of the text seg in those cases. */
1850 if (nlist.n_value == ANOFFSET (objfile->section_offsets,
203c3895
UW
1851 SECT_OFF_TEXT (objfile))
1852 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f
EZ
1853 {
1854 CORE_ADDR minsym_valu =
6b2d86bd
MS
1855 find_stab_function_addr (namestring,
1856 pst ? pst->filename : NULL,
1857 objfile);
6a34fd2f
EZ
1858 /* find_stab_function_addr will return 0 if the minimal
1859 symbol wasn't found. (Unfortunately, this might also
1860 be a valid address.) Anyway, if it *does* return 0,
1861 it is likely that the value was set correctly to begin
1862 with... */
1863 if (minsym_valu != 0)
1864 nlist.n_value = minsym_valu;
1865 }
203c3895
UW
1866 if (pst && textlow_not_set
1867 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f 1868 {
5afc051b 1869 pst->textlow = nlist.n_value;
6a34fd2f
EZ
1870 textlow_not_set = 0;
1871 }
6a34fd2f
EZ
1872 /* End kludge. */
1873
1874 /* Keep track of the start of the last function so we
1875 can handle end of function symbols. */
1876 last_function_start = nlist.n_value;
1877
1878 /* In reordered executables this function may lie outside
1879 the bounds created by N_SO symbols. If that's the case
1880 use the address of this function as the low bound for
1881 the partial symbol table. */
1882 if (pst
1883 && (textlow_not_set
5afc051b 1884 || (nlist.n_value < pst->textlow
6a34fd2f
EZ
1885 && (nlist.n_value
1886 != ANOFFSET (objfile->section_offsets,
1887 SECT_OFF_TEXT (objfile))))))
1888 {
5afc051b 1889 pst->textlow = nlist.n_value;
6a34fd2f
EZ
1890 textlow_not_set = 0;
1891 }
1892 add_psymbol_to_list (namestring, p - namestring,
176620f1 1893 VAR_DOMAIN, LOC_BLOCK,
6a34fd2f
EZ
1894 &objfile->static_psymbols,
1895 0, nlist.n_value,
1896 psymtab_language, objfile);
1897 continue;
1898
1899 /* Global functions were ignored here, but now they
1900 are put into the global psymtab like one would expect.
1901 They're also in the minimal symbol table. */
1902 case 'F':
1903 if (! pst)
1904 {
1905 int name_len = p - namestring;
1906 char *name = xmalloc (name_len + 1);
1907 memcpy (name, namestring, name_len);
1908 name[name_len] = '\0';
23136709 1909 function_outside_compilation_unit_complaint (name);
6a34fd2f
EZ
1910 xfree (name);
1911 }
6b2d86bd
MS
1912 nlist.n_value += ANOFFSET (objfile->section_offsets,
1913 SECT_OFF_TEXT (objfile));
6a34fd2f
EZ
1914 /* Kludges for ELF/STABS with Sun ACC */
1915 last_function_name = namestring;
6a34fd2f
EZ
1916 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1917 value for the bottom of the text seg in those cases. */
1918 if (nlist.n_value == ANOFFSET (objfile->section_offsets,
203c3895
UW
1919 SECT_OFF_TEXT (objfile))
1920 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f
EZ
1921 {
1922 CORE_ADDR minsym_valu =
6b2d86bd
MS
1923 find_stab_function_addr (namestring,
1924 pst ? pst->filename : NULL,
1925 objfile);
6a34fd2f
EZ
1926 /* find_stab_function_addr will return 0 if the minimal
1927 symbol wasn't found. (Unfortunately, this might also
1928 be a valid address.) Anyway, if it *does* return 0,
1929 it is likely that the value was set correctly to begin
1930 with... */
1931 if (minsym_valu != 0)
1932 nlist.n_value = minsym_valu;
1933 }
203c3895
UW
1934 if (pst && textlow_not_set
1935 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f 1936 {
5afc051b 1937 pst->textlow = nlist.n_value;
6a34fd2f
EZ
1938 textlow_not_set = 0;
1939 }
6a34fd2f
EZ
1940 /* End kludge. */
1941
1942 /* Keep track of the start of the last function so we
1943 can handle end of function symbols. */
1944 last_function_start = nlist.n_value;
1945
1946 /* In reordered executables this function may lie outside
1947 the bounds created by N_SO symbols. If that's the case
1948 use the address of this function as the low bound for
1949 the partial symbol table. */
1950 if (pst
1951 && (textlow_not_set
5afc051b 1952 || (nlist.n_value < pst->textlow
6a34fd2f
EZ
1953 && (nlist.n_value
1954 != ANOFFSET (objfile->section_offsets,
1955 SECT_OFF_TEXT (objfile))))))
1956 {
5afc051b 1957 pst->textlow = nlist.n_value;
6a34fd2f
EZ
1958 textlow_not_set = 0;
1959 }
1960 add_psymbol_to_list (namestring, p - namestring,
176620f1 1961 VAR_DOMAIN, LOC_BLOCK,
6a34fd2f
EZ
1962 &objfile->global_psymbols,
1963 0, nlist.n_value,
1964 psymtab_language, objfile);
1965 continue;
1966
1967 /* Two things show up here (hopefully); static symbols of
1968 local scope (static used inside braces) or extensions
1969 of structure symbols. We can ignore both. */
1970 case 'V':
1971 case '(':
1972 case '0':
1973 case '1':
1974 case '2':
1975 case '3':
1976 case '4':
1977 case '5':
1978 case '6':
1979 case '7':
1980 case '8':
1981 case '9':
1982 case '-':
063e58ba 1983 case '#': /* for symbol identification (used in live ranges) */
6a34fd2f
EZ
1984 continue;
1985
1986 case ':':
1987 /* It is a C++ nested symbol. We don't need to record it
1988 (I don't think); if we try to look up foo::bar::baz,
1989 then symbols for the symtab containing foo should get
1990 read in, I think. */
1991 /* Someone says sun cc puts out symbols like
1992 /foo/baz/maclib::/usr/local/bin/maclib,
1993 which would get here with a symbol type of ':'. */
1994 continue;
1995
1996 default:
1997 /* Unexpected symbol descriptor. The second and subsequent stabs
1998 of a continued stab can show up here. The question is
1999 whether they ever can mimic a normal stab--it would be
2000 nice if not, since we certainly don't want to spend the
2001 time searching to the end of every string looking for
2002 a backslash. */
2003
e2e0b3e5 2004 complaint (&symfile_complaints, _("unknown symbol descriptor `%c'"),
23136709 2005 p[1]);
6a34fd2f
EZ
2006
2007 /* Ignore it; perhaps it is an extension that we don't
2008 know about. */
2009 continue;
2010 }
2011
2012 case N_EXCL:
2013
2014 namestring = set_namestring (objfile, nlist);
2015
2016 /* Find the corresponding bincl and mark that psymtab on the
2017 psymtab dependency list */
2018 {
2019 struct partial_symtab *needed_pst =
2020 find_corresponding_bincl_psymtab (namestring, nlist.n_value);
2021
2022 /* If this include file was defined earlier in this file,
2023 leave it alone. */
2024 if (needed_pst == pst)
2025 continue;
2026
2027 if (needed_pst)
2028 {
2029 int i;
2030 int found = 0;
2031
2032 for (i = 0; i < dependencies_used; i++)
2033 if (dependency_list[i] == needed_pst)
2034 {
2035 found = 1;
2036 break;
2037 }
2038
2039 /* If it's already in the list, skip the rest. */
2040 if (found)
2041 continue;
2042
2043 dependency_list[dependencies_used++] = needed_pst;
2044 if (dependencies_used >= dependencies_allocated)
2045 {
2046 struct partial_symtab **orig = dependency_list;
2047 dependency_list =
2048 (struct partial_symtab **)
2049 alloca ((dependencies_allocated *= 2)
2050 * sizeof (struct partial_symtab *));
4efb68b1 2051 memcpy (dependency_list, orig,
6a34fd2f
EZ
2052 (dependencies_used
2053 * sizeof (struct partial_symtab *)));
2054#ifdef DEBUG_INFO
063e58ba
MD
2055 fprintf_unfiltered (gdb_stderr,
2056 "Had to reallocate dependency list.\n");
2057 fprintf_unfiltered (gdb_stderr,
2058 "New dependencies allocated: %d\n",
6a34fd2f
EZ
2059 dependencies_allocated);
2060#endif
2061 }
2062 }
2063 }
2064 continue;
2065
2066 case N_ENDM:
6a34fd2f 2067 /* Solaris 2 end of module, finish current partial symbol table.
5afc051b 2068 end_psymtab will set pst->texthigh to the proper value, which
6a34fd2f
EZ
2069 is necessary if a module compiled without debugging info
2070 follows this module. */
203c3895 2071 if (pst && gdbarch_sofun_address_maybe_missing (current_gdbarch))
6a34fd2f
EZ
2072 {
2073 end_psymtab (pst, psymtab_include_list, includes_used,
2074 symnum * symbol_size,
2075 (CORE_ADDR) 0,
2076 dependency_list, dependencies_used, textlow_not_set);
2077 pst = (struct partial_symtab *) 0;
2078 includes_used = 0;
2079 dependencies_used = 0;
2080 }
6a34fd2f 2081 continue;
c906108c 2082
6a34fd2f
EZ
2083 case N_RBRAC:
2084#ifdef HANDLE_RBRAC
2085 HANDLE_RBRAC (nlist.n_value);
2086 continue;
2087#endif
2088 case N_EINCL:
2089 case N_DSLINE:
2090 case N_BSLINE:
063e58ba
MD
2091 case N_SSYM: /* Claim: Structure or union element.
2092 Hopefully, I can ignore this. */
6a34fd2f 2093 case N_ENTRY: /* Alternate entry point; can ignore. */
063e58ba 2094 case N_MAIN: /* Can definitely ignore this. */
6a34fd2f 2095 case N_CATCH: /* These are GNU C++ extensions */
063e58ba 2096 case N_EHDECL: /* that can safely be ignored here. */
6a34fd2f
EZ
2097 case N_LENG:
2098 case N_BCOMM:
2099 case N_ECOMM:
2100 case N_ECOML:
2101 case N_FNAME:
2102 case N_SLINE:
2103 case N_RSYM:
2104 case N_PSYM:
2105 case N_LBRAC:
2106 case N_NSYMS: /* Ultrix 4.0: symbol count */
063e58ba 2107 case N_DEFD: /* GNU Modula-2 */
6a34fd2f
EZ
2108 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
2109
063e58ba 2110 case N_OBJ: /* useless types from Solaris */
6a34fd2f 2111 case N_OPT:
c77c642a 2112 case N_PATCH:
6a34fd2f
EZ
2113 /* These symbols aren't interesting; don't worry about them */
2114
2115 continue;
2116
2117 default:
2118 /* If we haven't found it yet, ignore it. It's probably some
2119 new type we don't know about yet. */
bb599908 2120 unknown_symtype_complaint (hex_string (nlist.n_type));
6a34fd2f
EZ
2121 continue;
2122 }
c906108c
SS
2123 }
2124
2125 /* If there's stuff to be cleaned up, clean it up. */
c906108c
SS
2126 if (pst)
2127 {
2128 /* Don't set pst->texthigh lower than it already is. */
2129 CORE_ADDR text_end =
6a34fd2f 2130 (lowest_text_address == (CORE_ADDR) -1
063e58ba
MD
2131 ? (text_addr + ANOFFSET (objfile->section_offsets,
2132 SECT_OFF_TEXT (objfile)))
6a34fd2f
EZ
2133 : lowest_text_address)
2134 + text_size;
c906108c
SS
2135
2136 end_psymtab (pst, psymtab_include_list, includes_used,
2137 symnum * symbol_size,
5afc051b 2138 text_end > pst->texthigh ? text_end : pst->texthigh,
c906108c
SS
2139 dependency_list, dependencies_used, textlow_not_set);
2140 }
2141
2142 do_cleanups (back_to);
2143}
2144
2145/* Allocate and partially fill a partial symtab. It will be
2146 completely filled at the end of the symbol list.
2147
2148 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
2149 is the address relative to which its symbols are (incremental) or 0
2150 (normal). */
2151
2152
d4f3574e 2153static struct partial_symtab *
fba45db2
KB
2154start_psymtab (struct objfile *objfile, char *filename, CORE_ADDR textlow,
2155 int ldsymoff, struct partial_symbol **global_syms,
2156 struct partial_symbol **static_syms)
c906108c
SS
2157{
2158 struct partial_symtab *result =
d4f3574e 2159 start_psymtab_common (objfile, objfile->section_offsets,
c5aa993b 2160 filename, textlow, global_syms, static_syms);
c906108c
SS
2161
2162 result->read_symtab_private = (char *)
8b92e4d5 2163 obstack_alloc (&objfile->objfile_obstack, sizeof (struct symloc));
c5aa993b 2164 LDSYMOFF (result) = ldsymoff;
c906108c 2165 result->read_symtab = dbx_psymtab_to_symtab;
c5aa993b
JM
2166 SYMBOL_SIZE (result) = symbol_size;
2167 SYMBOL_OFFSET (result) = symbol_table_offset;
2168 STRING_OFFSET (result) = string_table_offset;
2169 FILE_STRING_OFFSET (result) = file_string_table_offset;
c906108c 2170
31d99776 2171#ifdef HAVE_ELF
c906108c
SS
2172 /* If we're handling an ELF file, drag some section-relocation info
2173 for this source file out of the ELF symbol table, to compensate for
2174 Sun brain death. This replaces the section_offsets in this psymtab,
2175 if successful. */
2176 elfstab_offset_sections (objfile, result);
31d99776 2177#endif
c906108c
SS
2178
2179 /* Deduce the source language from the filename for this psymtab. */
2180 psymtab_language = deduce_language_from_filename (filename);
2181
2182 return result;
2183}
2184
2185/* Close off the current usage of PST.
2186 Returns PST or NULL if the partial symtab was empty and thrown away.
2187
2188 FIXME: List variables and peculiarities of same. */
2189
2190struct partial_symtab *
fba45db2
KB
2191end_psymtab (struct partial_symtab *pst, char **include_list, int num_includes,
2192 int capping_symbol_offset, CORE_ADDR capping_text,
2193 struct partial_symtab **dependency_list, int number_dependencies,
2194 int textlow_not_set)
c906108c
SS
2195{
2196 int i;
c5aa993b 2197 struct objfile *objfile = pst->objfile;
c906108c
SS
2198
2199 if (capping_symbol_offset != -1)
c5aa993b 2200 LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
5afc051b 2201 pst->texthigh = capping_text;
c906108c 2202
c906108c
SS
2203 /* Under Solaris, the N_SO symbols always have a value of 0,
2204 instead of the usual address of the .o file. Therefore,
2205 we have to do some tricks to fill in texthigh and textlow.
6a34fd2f 2206 The first trick is: if we see a static
c906108c
SS
2207 or global function, and the textlow for the current pst
2208 is not set (ie: textlow_not_set), then we use that function's
2209 address for the textlow of the pst. */
2210
2211 /* Now, to fill in texthigh, we remember the last function seen
6a34fd2f 2212 in the .o file. Also, there's a hack in
c906108c
SS
2213 bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
2214 to here via the misc_info field. Therefore, we can fill in
2215 a reliable texthigh by taking the address plus size of the
2216 last function in the file. */
2217
203c3895
UW
2218 if (pst->texthigh == 0 && last_function_name
2219 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
c906108c
SS
2220 {
2221 char *p;
2222 int n;
2223 struct minimal_symbol *minsym;
2224
2225 p = strchr (last_function_name, ':');
2226 if (p == NULL)
2227 p = last_function_name;
2228 n = p - last_function_name;
2229 p = alloca (n + 2);
2230 strncpy (p, last_function_name, n);
2231 p[n] = 0;
c5aa993b 2232
c906108c
SS
2233 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2234 if (minsym == NULL)
2235 {
2236 /* Sun Fortran appends an underscore to the minimal symbol name,
2237 try again with an appended underscore if the minimal symbol
2238 was not found. */
2239 p[n] = '_';
2240 p[n + 1] = 0;
2241 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2242 }
2243
2244 if (minsym)
5afc051b 2245 pst->texthigh = SYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym);
c906108c
SS
2246
2247 last_function_name = NULL;
2248 }
2249
203c3895
UW
2250 if (!gdbarch_sofun_address_maybe_missing (current_gdbarch))
2251 ;
c906108c 2252 /* this test will be true if the last .o file is only data */
203c3895 2253 else if (textlow_not_set)
5afc051b 2254 pst->textlow = pst->texthigh;
c906108c
SS
2255 else
2256 {
2257 struct partial_symtab *p1;
2258
2259 /* If we know our own starting text address, then walk through all other
c5aa993b
JM
2260 psymtabs for this objfile, and if any didn't know their ending text
2261 address, set it to our starting address. Take care to not set our
2262 own ending address to our starting address, nor to set addresses on
2263 `dependency' files that have both textlow and texthigh zero. */
c906108c
SS
2264
2265 ALL_OBJFILE_PSYMTABS (objfile, p1)
c5aa993b 2266 {
5afc051b 2267 if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
c5aa993b 2268 {
5afc051b 2269 p1->texthigh = pst->textlow;
c5aa993b 2270 /* if this file has only data, then make textlow match texthigh */
5afc051b
JB
2271 if (p1->textlow == 0)
2272 p1->textlow = p1->texthigh;
c5aa993b
JM
2273 }
2274 }
c906108c
SS
2275 }
2276
2277 /* End of kludge for patching Solaris textlow and texthigh. */
c906108c
SS
2278
2279 pst->n_global_syms =
063e58ba
MD
2280 objfile->global_psymbols.next - (objfile->global_psymbols.list
2281 + pst->globals_offset);
c906108c 2282 pst->n_static_syms =
063e58ba
MD
2283 objfile->static_psymbols.next - (objfile->static_psymbols.list
2284 + pst->statics_offset);
c906108c
SS
2285
2286 pst->number_of_dependencies = number_dependencies;
2287 if (number_dependencies)
2288 {
2289 pst->dependencies = (struct partial_symtab **)
8b92e4d5 2290 obstack_alloc (&objfile->objfile_obstack,
c5aa993b 2291 number_dependencies * sizeof (struct partial_symtab *));
c906108c 2292 memcpy (pst->dependencies, dependency_list,
c5aa993b 2293 number_dependencies * sizeof (struct partial_symtab *));
c906108c
SS
2294 }
2295 else
2296 pst->dependencies = 0;
2297
2298 for (i = 0; i < num_includes; i++)
2299 {
2300 struct partial_symtab *subpst =
c5aa993b 2301 allocate_psymtab (include_list[i], objfile);
c906108c 2302
b8fbeb18 2303 /* Copy the sesction_offsets array from the main psymtab. */
c906108c
SS
2304 subpst->section_offsets = pst->section_offsets;
2305 subpst->read_symtab_private =
8b92e4d5 2306 (char *) obstack_alloc (&objfile->objfile_obstack,
c5aa993b
JM
2307 sizeof (struct symloc));
2308 LDSYMOFF (subpst) =
2309 LDSYMLEN (subpst) =
5afc051b
JB
2310 subpst->textlow =
2311 subpst->texthigh = 0;
c906108c
SS
2312
2313 /* We could save slight bits of space by only making one of these,
c5aa993b 2314 shared by the entire set of include files. FIXME-someday. */
c906108c 2315 subpst->dependencies = (struct partial_symtab **)
8b92e4d5 2316 obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
2317 sizeof (struct partial_symtab *));
2318 subpst->dependencies[0] = pst;
2319 subpst->number_of_dependencies = 1;
2320
2321 subpst->globals_offset =
2322 subpst->n_global_syms =
c5aa993b
JM
2323 subpst->statics_offset =
2324 subpst->n_static_syms = 0;
c906108c
SS
2325
2326 subpst->readin = 0;
2327 subpst->symtab = 0;
2328 subpst->read_symtab = pst->read_symtab;
2329 }
2330
2331 sort_pst_symbols (pst);
2332
2333 /* If there is already a psymtab or symtab for a file of this name, remove it.
2334 (If there is a symtab, more drastic things also happen.)
2335 This happens in VxWorks. */
2336 free_named_symtabs (pst->filename);
2337
2338 if (num_includes == 0
2339 && number_dependencies == 0
2340 && pst->n_global_syms == 0
2341 && pst->n_static_syms == 0
2342 && has_line_numbers == 0)
2343 {
2344 /* Throw away this psymtab, it's empty. We can't deallocate it, since
c5aa993b 2345 it is on the obstack, but we can forget to chain it on the list. */
c906108c 2346 /* Empty psymtabs happen as a result of header files which don't have
c5aa993b
JM
2347 any symbols in them. There can be a lot of them. But this check
2348 is wrong, in that a psymtab with N_SLINE entries but nothing else
2349 is not empty, but we don't realize that. Fixing that without slowing
2350 things down might be tricky. */
c906108c
SS
2351
2352 discard_psymtab (pst);
2353
2354 /* Indicate that psymtab was thrown away. */
c5aa993b 2355 pst = (struct partial_symtab *) NULL;
c906108c
SS
2356 }
2357 return pst;
2358}
2359\f
2360static void
fba45db2 2361dbx_psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c
SS
2362{
2363 struct cleanup *old_chain;
2364 int i;
c5aa993b 2365
c906108c
SS
2366 if (!pst)
2367 return;
2368
2369 if (pst->readin)
2370 {
063e58ba
MD
2371 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. \
2372Shouldn't happen.\n",
c5aa993b 2373 pst->filename);
c906108c
SS
2374 return;
2375 }
2376
2377 /* Read in all partial symtabs on which this one is dependent */
2378 for (i = 0; i < pst->number_of_dependencies; i++)
2379 if (!pst->dependencies[i]->readin)
2380 {
2381 /* Inform about additional files that need to be read in. */
2382 if (info_verbose)
2383 {
2384 fputs_filtered (" ", gdb_stdout);
2385 wrap_here ("");
2386 fputs_filtered ("and ", gdb_stdout);
2387 wrap_here ("");
2388 printf_filtered ("%s...", pst->dependencies[i]->filename);
c5aa993b 2389 wrap_here (""); /* Flush output */
c906108c
SS
2390 gdb_flush (gdb_stdout);
2391 }
2392 dbx_psymtab_to_symtab_1 (pst->dependencies[i]);
2393 }
2394
c5aa993b 2395 if (LDSYMLEN (pst)) /* Otherwise it's a dummy */
c906108c
SS
2396 {
2397 /* Init stuff necessary for reading in symbols */
2398 stabsread_init ();
2399 buildsym_init ();
a0b3c4fd 2400 old_chain = make_cleanup (really_free_pendings, 0);
c906108c
SS
2401 file_string_table_offset = FILE_STRING_OFFSET (pst);
2402 symbol_size = SYMBOL_SIZE (pst);
2403
2404 /* Read in this file's symbols */
2405 bfd_seek (pst->objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
2406 read_ofile_symtab (pst);
c906108c
SS
2407
2408 do_cleanups (old_chain);
2409 }
2410
2411 pst->readin = 1;
2412}
2413
2414/* Read in all of the symbols for a given psymtab for real.
2415 Be verbose about it if the user wants that. */
2416
2417static void
fba45db2 2418dbx_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
2419{
2420 bfd *sym_bfd;
086df311 2421 struct cleanup *back_to = NULL;
c906108c
SS
2422
2423 if (!pst)
2424 return;
2425
2426 if (pst->readin)
2427 {
063e58ba
MD
2428 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. \
2429Shouldn't happen.\n",
c5aa993b 2430 pst->filename);
c906108c
SS
2431 return;
2432 }
2433
c5aa993b 2434 if (LDSYMLEN (pst) || pst->number_of_dependencies)
c906108c
SS
2435 {
2436 /* Print the message now, before reading the string table,
c5aa993b 2437 to avoid disconcerting pauses. */
c906108c
SS
2438 if (info_verbose)
2439 {
2440 printf_filtered ("Reading in symbols for %s...", pst->filename);
2441 gdb_flush (gdb_stdout);
2442 }
2443
2444 sym_bfd = pst->objfile->obfd;
2445
2446 next_symbol_text_func = dbx_next_symbol_text;
2447
086df311
DJ
2448 if (DBX_STAB_SECTION (pst->objfile))
2449 {
2450 stabs_data
2451 = symfile_relocate_debug_section (pst->objfile->obfd,
2452 DBX_STAB_SECTION (pst->objfile),
2453 NULL);
2454 if (stabs_data)
063e58ba
MD
2455 back_to = make_cleanup (free_current_contents,
2456 (void *) &stabs_data);
086df311
DJ
2457 }
2458
c906108c
SS
2459 dbx_psymtab_to_symtab_1 (pst);
2460
086df311
DJ
2461 if (back_to)
2462 do_cleanups (back_to);
2463
c906108c
SS
2464 /* Match with global symbols. This only needs to be done once,
2465 after all of the symtabs and dependencies have been read in. */
2466 scan_file_globals (pst->objfile);
2467
2468 /* Finish up the debug error message. */
2469 if (info_verbose)
2470 printf_filtered ("done.\n");
2471 }
2472}
2473
2474/* Read in a defined section of a specific object file's symbols. */
c5aa993b 2475
c906108c 2476static void
fba45db2 2477read_ofile_symtab (struct partial_symtab *pst)
c906108c 2478{
52f0bd74
AC
2479 char *namestring;
2480 struct external_nlist *bufp;
c906108c
SS
2481 struct internal_nlist nlist;
2482 unsigned char type;
2483 unsigned max_symnum;
52f0bd74 2484 bfd *abfd;
c906108c
SS
2485 struct objfile *objfile;
2486 int sym_offset; /* Offset to start of symbols to read */
2487 int sym_size; /* Size of symbols to read */
2488 CORE_ADDR text_offset; /* Start of text segment for symbols */
2489 int text_size; /* Size of text segment for symbols */
2490 struct section_offsets *section_offsets;
2491
2492 objfile = pst->objfile;
c5aa993b
JM
2493 sym_offset = LDSYMOFF (pst);
2494 sym_size = LDSYMLEN (pst);
5afc051b
JB
2495 text_offset = pst->textlow;
2496 text_size = pst->texthigh - pst->textlow;
b8fbeb18
EZ
2497 /* This cannot be simply objfile->section_offsets because of
2498 elfstab_offset_sections() which initializes the psymtab section
2499 offsets information in a special way, and that is different from
2500 objfile->section_offsets. */
c906108c
SS
2501 section_offsets = pst->section_offsets;
2502
2503 current_objfile = objfile;
2504 subfile_stack = NULL;
2505
2506 stringtab_global = DBX_STRINGTAB (objfile);
2507 last_source_file = NULL;
2508
2509 abfd = objfile->obfd;
2510 symfile_bfd = objfile->obfd; /* Implicit param to next_text_symbol */
2511 symbuf_end = symbuf_idx = 0;
086df311
DJ
2512 symbuf_read = 0;
2513 symbuf_left = sym_offset + sym_size;
c906108c
SS
2514
2515 /* It is necessary to actually read one symbol *before* the start
2516 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
2517 occurs before the N_SO symbol.
2518
2519 Detecting this in read_dbx_symtab
2520 would slow down initial readin, so we look for it here instead. */
c5aa993b 2521 if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
c906108c 2522 {
086df311 2523 stabs_seek (sym_offset - symbol_size);
c906108c
SS
2524 fill_symbuf (abfd);
2525 bufp = &symbuf[symbuf_idx++];
2526 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2527 OBJSTAT (objfile, n_stabs++);
2528
6a34fd2f 2529 namestring = set_namestring (objfile, nlist);
c906108c
SS
2530
2531 processing_gcc_compilation = 0;
2532 if (nlist.n_type == N_TEXT)
2533 {
2534 const char *tempstring = namestring;
2535
91f54576 2536 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
c906108c 2537 processing_gcc_compilation = 1;
91f54576 2538 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
c906108c
SS
2539 processing_gcc_compilation = 2;
2540 if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
2541 ++tempstring;
91f54576 2542 if (strncmp (tempstring, "__gnu_compiled", 14) == 0)
c906108c
SS
2543 processing_gcc_compilation = 2;
2544 }
2545
2546 /* Try to select a C++ demangling based on the compilation unit
c5aa993b 2547 producer. */
c906108c 2548
8052a17a
JM
2549#if 0
2550 /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
2551 know whether it will use the old style or v3 mangling. */
c906108c
SS
2552 if (processing_gcc_compilation)
2553 {
2554 if (AUTO_DEMANGLING)
2555 {
2556 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
2557 }
2558 }
8052a17a 2559#endif
c906108c
SS
2560 }
2561 else
2562 {
2563 /* The N_SO starting this symtab is the first symbol, so we
c5aa993b
JM
2564 better not check the symbol before it. I'm not this can
2565 happen, but it doesn't hurt to check for it. */
086df311 2566 stabs_seek (sym_offset);
c906108c
SS
2567 processing_gcc_compilation = 0;
2568 }
2569
2570 if (symbuf_idx == symbuf_end)
2571 fill_symbuf (abfd);
2572 bufp = &symbuf[symbuf_idx];
2573 if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
8a3fe4f8 2574 error (_("First symbol in segment of executable not a source symbol"));
c906108c
SS
2575
2576 max_symnum = sym_size / symbol_size;
2577
2578 for (symnum = 0;
2579 symnum < max_symnum;
2580 symnum++)
2581 {
2582 QUIT; /* Allow this to be interruptable */
2583 if (symbuf_idx == symbuf_end)
c5aa993b 2584 fill_symbuf (abfd);
c906108c
SS
2585 bufp = &symbuf[symbuf_idx++];
2586 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2587 OBJSTAT (objfile, n_stabs++);
2588
2589 type = bfd_h_get_8 (abfd, bufp->e_type);
2590
6a34fd2f 2591 namestring = set_namestring (objfile, nlist);
c906108c 2592
c5aa993b
JM
2593 if (type & N_STAB)
2594 {
c906108c
SS
2595 process_one_symbol (type, nlist.n_desc, nlist.n_value,
2596 namestring, section_offsets, objfile);
c5aa993b 2597 }
c906108c
SS
2598 /* We skip checking for a new .o or -l file; that should never
2599 happen in this routine. */
2600 else if (type == N_TEXT)
2601 {
2602 /* I don't think this code will ever be executed, because
2603 the GCC_COMPILED_FLAG_SYMBOL usually is right before
2604 the N_SO symbol which starts this source file.
2605 However, there is no reason not to accept
2606 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
2607
91f54576 2608 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
c906108c 2609 processing_gcc_compilation = 1;
91f54576 2610 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
c906108c
SS
2611 processing_gcc_compilation = 2;
2612
8052a17a
JM
2613#if 0
2614 /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
2615 know whether it will use the old style or v3 mangling. */
c906108c
SS
2616 if (AUTO_DEMANGLING)
2617 {
2618 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
2619 }
8052a17a 2620#endif
c906108c 2621 }
c5aa993b
JM
2622 else if (type & N_EXT || type == (unsigned char) N_TEXT
2623 || type == (unsigned char) N_NBTEXT
2624 )
2625 {
c906108c
SS
2626 /* Global symbol: see if we came across a dbx defintion for
2627 a corresponding symbol. If so, store the value. Remove
2628 syms from the chain when their values are stored, but
2629 search the whole chain, as there may be several syms from
2630 different files with the same name. */
2631 /* This is probably not true. Since the files will be read
2632 in one at a time, each reference to a global symbol will
2633 be satisfied in each file as it appears. So we skip this
2634 section. */
2635 ;
c5aa993b 2636 }
c906108c
SS
2637 }
2638
c906108c
SS
2639 /* In a Solaris elf file, this variable, which comes from the
2640 value of the N_SO symbol, will still be 0. Luckily, text_offset,
5afc051b 2641 which comes from pst->textlow is correct. */
c906108c
SS
2642 if (last_source_start_addr == 0)
2643 last_source_start_addr = text_offset;
2644
2645 /* In reordered executables last_source_start_addr may not be the
2646 lower bound for this symtab, instead use text_offset which comes
5afc051b 2647 from pst->textlow which is correct. */
c906108c
SS
2648 if (last_source_start_addr > text_offset)
2649 last_source_start_addr = text_offset;
2650
063e58ba
MD
2651 pst->symtab = end_symtab (text_offset + text_size, objfile,
2652 SECT_OFF_TEXT (objfile));
c906108c 2653
c906108c 2654 end_stabs ();
e9112110
PA
2655
2656 current_objfile = NULL;
c906108c 2657}
c906108c 2658\f
c5aa993b 2659
c906108c
SS
2660/* This handles a single symbol from the symbol-file, building symbols
2661 into a GDB symtab. It takes these arguments and an implicit argument.
2662
2663 TYPE is the type field of the ".stab" symbol entry.
2664 DESC is the desc field of the ".stab" entry.
2665 VALU is the value field of the ".stab" entry.
2666 NAME is the symbol name, in our address space.
e31272c3
MK
2667 SECTION_OFFSETS is a set of amounts by which the sections of this
2668 object file were relocated when it was loaded into memory. Note
2669 that these section_offsets are not the objfile->section_offsets but
2670 the pst->section_offsets. All symbols that refer to memory
2671 locations need to be offset by these amounts.
2672 OBJFILE is the object file from which we are reading symbols. It
2673 is used in end_symtab. */
c906108c
SS
2674
2675void
fba45db2
KB
2676process_one_symbol (int type, int desc, CORE_ADDR valu, char *name,
2677 struct section_offsets *section_offsets,
2678 struct objfile *objfile)
c906108c 2679{
52f0bd74 2680 struct context_stack *new;
e31272c3
MK
2681 /* This remembers the address of the start of a function. It is
2682 used because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries
2683 are relative to the current function's start address. On systems
2684 other than Solaris 2, this just holds the SECT_OFF_TEXT value,
2685 and is used to relocate these symbol types rather than
2686 SECTION_OFFSETS. */
c906108c
SS
2687 static CORE_ADDR function_start_offset;
2688
e31272c3
MK
2689 /* This holds the address of the start of a function, without the
2690 system peculiarities of function_start_offset. */
a1b9830c
DJ
2691 static CORE_ADDR last_function_start;
2692
54c7009d
EZ
2693 /* If this is nonzero, we've seen an N_SLINE since the start of the
2694 current function. We use this to tell us to move the first sline
2695 to the beginning of the function regardless of what its given
2696 value is. */
a1b9830c
DJ
2697 static int sline_found_in_function = 1;
2698
e31272c3
MK
2699 /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this
2700 source file. Used to detect the SunPRO solaris compiler. */
c906108c
SS
2701 static int n_opt_found;
2702
2703 /* The stab type used for the definition of the last function.
2704 N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers. */
2705 static int function_stab_type = 0;
2706
2707 if (!block_address_function_relative)
e31272c3
MK
2708 {
2709 /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
2710 function start address, so just use the text offset. */
2711 function_start_offset =
2712 ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2713 }
c906108c 2714
e31272c3
MK
2715 /* Something is wrong if we see real data before seeing a source
2716 file name. */
c906108c 2717
c5aa993b 2718 if (last_source_file == NULL && type != (unsigned char) N_SO)
c906108c
SS
2719 {
2720 /* Ignore any symbols which appear before an N_SO symbol.
c5aa993b
JM
2721 Currently no one puts symbols there, but we should deal
2722 gracefully with the case. A complain()t might be in order,
2723 but this should not be an error (). */
c906108c
SS
2724 return;
2725 }
2726
2727 switch (type)
2728 {
2729 case N_FUN:
2730 case N_FNAME:
2731
2732 if (*name == '\000')
2733 {
e31272c3
MK
2734 /* This N_FUN marks the end of a function. This closes off
2735 the current block. */
0c5e171a
KD
2736
2737 if (context_stack_depth <= 0)
2738 {
23136709 2739 lbrac_mismatch_complaint (symnum);
0c5e171a
KD
2740 break;
2741 }
2742
6b37567a
JJ
2743 /* The following check is added before recording line 0 at
2744 end of function so as to handle hand-generated stabs
e31272c3
MK
2745 which may have an N_FUN stabs at the end of the function,
2746 but no N_SLINE stabs. */
6b37567a
JJ
2747 if (sline_found_in_function)
2748 record_line (current_subfile, 0, last_function_start + valu);
2749
c906108c
SS
2750 within_function = 0;
2751 new = pop_context ();
2752
2753 /* Make a block for the local symbols within. */
2754 finish_block (new->name, &local_symbols, new->old_blocks,
2755 new->start_addr, new->start_addr + valu,
2756 objfile);
2757
2758 /* May be switching to an assembler file which may not be using
2759 block relative stabs, so reset the offset. */
2760 if (block_address_function_relative)
2761 function_start_offset = 0;
2762
2763 break;
2764 }
2765
a1b9830c
DJ
2766 sline_found_in_function = 0;
2767
e31272c3 2768 /* Relocate for dynamic loading. */
b8fbeb18 2769 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
260edbc2 2770 valu = gdbarch_smash_text_address (current_gdbarch, valu);
a1b9830c
DJ
2771 last_function_start = valu;
2772
c906108c
SS
2773 goto define_a_symbol;
2774
2775 case N_LBRAC:
2776 /* This "symbol" just indicates the start of an inner lexical
c5aa993b 2777 context within a function. */
c906108c
SS
2778
2779 /* Ignore extra outermost context from SunPRO cc and acc. */
2780 if (n_opt_found && desc == 1)
2781 break;
2782
2783 if (block_address_function_relative)
2784 /* Relocate for Sun ELF acc fn-relative syms. */
2785 valu += function_start_offset;
2786 else
2787 /* On most machines, the block addresses are relative to the
2788 N_SO, the linker did not relocate them (sigh). */
2789 valu += last_source_start_addr;
2790
c906108c
SS
2791 new = push_context (desc, valu);
2792 break;
2793
2794 case N_RBRAC:
2795 /* This "symbol" just indicates the end of an inner lexical
c5aa993b 2796 context that was started with N_LBRAC. */
c906108c
SS
2797
2798 /* Ignore extra outermost context from SunPRO cc and acc. */
2799 if (n_opt_found && desc == 1)
2800 break;
2801
2802 if (block_address_function_relative)
2803 /* Relocate for Sun ELF acc fn-relative syms. */
2804 valu += function_start_offset;
2805 else
2806 /* On most machines, the block addresses are relative to the
2807 N_SO, the linker did not relocate them (sigh). */
2808 valu += last_source_start_addr;
2809
0c5e171a
KD
2810 if (context_stack_depth <= 0)
2811 {
23136709 2812 lbrac_mismatch_complaint (symnum);
0c5e171a
KD
2813 break;
2814 }
2815
c5aa993b 2816 new = pop_context ();
c906108c 2817 if (desc != new->depth)
23136709 2818 lbrac_mismatch_complaint (symnum);
c906108c 2819
35156bae 2820 if (local_symbols != NULL)
1f077a3e 2821 {
35156bae
DJ
2822 /* GCC development snapshots from March to December of
2823 2000 would output N_LSYM entries after N_LBRAC
2824 entries. As a consequence, these symbols are simply
2825 discarded. Complain if this is the case. */
2826 complaint (&symfile_complaints, _("\
e31272c3 2827misplaced N_LBRAC entry; discarding local symbols which have \
e2e0b3e5 2828no enclosing block"));
1f077a3e 2829 }
35156bae 2830 local_symbols = new->locals;
c906108c 2831
35156bae 2832 if (context_stack_depth > 1)
c906108c 2833 {
e31272c3
MK
2834 /* This is not the outermost LBRAC...RBRAC pair in the
2835 function, its local symbols preceded it, and are the ones
2836 just recovered from the context stack. Define the block
2837 for them (but don't bother if the block contains no
2838 symbols. Should we complain on blocks without symbols?
2839 I can't think of any useful purpose for them). */
c906108c
SS
2840 if (local_symbols != NULL)
2841 {
e31272c3
MK
2842 /* Muzzle a compiler bug that makes end < start.
2843
2844 ??? Which compilers? Is this ever harmful?. */
c906108c
SS
2845 if (new->start_addr > valu)
2846 {
23136709 2847 complaint (&symfile_complaints,
e2e0b3e5 2848 _("block start larger than block end"));
c906108c
SS
2849 new->start_addr = valu;
2850 }
2851 /* Make a block for the local symbols within. */
2852 finish_block (0, &local_symbols, new->old_blocks,
2853 new->start_addr, valu, objfile);
2854 }
2855 }
2856 else
2857 {
2858 /* This is the outermost LBRAC...RBRAC pair. There is no
2859 need to do anything; leave the symbols that preceded it
2860 to be attached to the function's own block. We need to
2861 indicate that we just moved outside of the function. */
2862 within_function = 0;
2863 }
2864
c906108c
SS
2865 break;
2866
2867 case N_FN:
2868 case N_FN_SEQ:
e31272c3
MK
2869 /* This kind of symbol indicates the start of an object file.
2870 Relocate for dynamic loading. */
b8fbeb18 2871 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2872 break;
2873
2874 case N_SO:
e31272c3
MK
2875 /* This type of symbol indicates the start of data for one
2876 source file. Finish the symbol table of the previous source
2877 file (if any) and start accumulating a new symbol table.
2878 Relocate for dynamic loading. */
b8fbeb18 2879 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2880
2881 n_opt_found = 0;
2882
c906108c
SS
2883 if (last_source_file)
2884 {
2885 /* Check if previous symbol was also an N_SO (with some
e31272c3
MK
2886 sanity checks). If so, that one was actually the
2887 directory name, and the current one is the real file
2888 name. Patch things up. */
c906108c
SS
2889 if (previous_stab_code == (unsigned char) N_SO)
2890 {
2891 patch_subfile_names (current_subfile, name);
2892 break; /* Ignore repeated SOs */
2893 }
b8fbeb18 2894 end_symtab (valu, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
2895 end_stabs ();
2896 }
2897
e31272c3
MK
2898 /* Null name means this just marks the end of text for this .o
2899 file. Don't start a new symtab in this case. */
c906108c
SS
2900 if (*name == '\000')
2901 break;
2902
2903 if (block_address_function_relative)
c5aa993b 2904 function_start_offset = 0;
c906108c
SS
2905
2906 start_stabs ();
2907 start_symtab (name, NULL, valu);
2908 record_debugformat ("stabs");
2909 break;
2910
2911 case N_SOL:
e31272c3
MK
2912 /* This type of symbol indicates the start of data for a
2913 sub-source-file, one whose contents were copied or included
2914 in the compilation of the main source file (whose name was
2915 given in the N_SO symbol). Relocate for dynamic loading. */
b8fbeb18 2916 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
2917 start_subfile (name, current_subfile->dirname);
2918 break;
2919
2920 case N_BINCL:
2921 push_subfile ();
2922 add_new_header_file (name, valu);
2923 start_subfile (name, current_subfile->dirname);
2924 break;
2925
2926 case N_EINCL:
2927 start_subfile (pop_subfile (), current_subfile->dirname);
2928 break;
2929
2930 case N_EXCL:
2931 add_old_header_file (name, valu);
2932 break;
2933
2934 case N_SLINE:
e31272c3
MK
2935 /* This type of "symbol" really just records one line-number --
2936 core-address correspondence. Enter it in the line list for
2937 this symbol table. */
c906108c 2938
e31272c3
MK
2939 /* Relocate for dynamic loading and for ELF acc
2940 function-relative symbols. */
c906108c
SS
2941 valu += function_start_offset;
2942
ceddaf06
MK
2943 /* GCC 2.95.3 emits the first N_SLINE stab somwehere in the
2944 middle of the prologue instead of right at the start of the
2945 function. To deal with this we record the address for the
2946 first N_SLINE stab to be the start of the function instead of
2947 the listed location. We really shouldn't to this. When
2948 compiling with optimization, this first N_SLINE stab might be
2949 optimized away. Other (non-GCC) compilers don't emit this
2950 stab at all. There is no real harm in having an extra
2951 numbered line, although it can be a bit annoying for the
2952 user. However, it totally screws up our testsuite.
2953
2954 So for now, keep adjusting the address of the first N_SLINE
2955 stab, but only for code compiled with GCC. */
2956
a1b9830c
DJ
2957 if (within_function && sline_found_in_function == 0)
2958 {
ceddaf06
MK
2959 if (processing_gcc_compilation == 2)
2960 record_line (current_subfile, desc, last_function_start);
2961 else
2962 record_line (current_subfile, desc, valu);
a1b9830c
DJ
2963 sline_found_in_function = 1;
2964 }
2965 else
2966 record_line (current_subfile, desc, valu);
c906108c
SS
2967 break;
2968
2969 case N_BCOMM:
2970 common_block_start (name, objfile);
2971 break;
2972
2973 case N_ECOMM:
2974 common_block_end (objfile);
2975 break;
2976
e31272c3
MK
2977 /* The following symbol types need to have the appropriate
2978 offset added to their value; then we process symbol
2979 definitions in the name. */
c906108c 2980
e31272c3
MK
2981 case N_STSYM: /* Static symbol in data segment. */
2982 case N_LCSYM: /* Static symbol in BSS segment. */
2983 case N_ROSYM: /* Static symbol in read-only data segment. */
c5aa993b 2984 /* HORRID HACK DEPT. However, it's Sun's furgin' fault.
e31272c3
MK
2985 Solaris 2's stabs-in-elf makes *most* symbols relative but
2986 leaves a few absolute (at least for Solaris 2.1 and version
2987 2.0.1 of the SunPRO compiler). N_STSYM and friends sit on
2988 the fence. .stab "foo:S...",N_STSYM is absolute (ld
2989 relocates it) .stab "foo:V...",N_STSYM is relative (section
2990 base subtracted). This leaves us no choice but to search for
2991 the 'S' or 'V'... (or pass the whole section_offsets stuff
2992 down ONE MORE function call level, which we really don't want
2993 to do). */
c906108c
SS
2994 {
2995 char *p;
2996
e31272c3
MK
2997 /* Normal object file and NLMs have non-zero text seg offsets,
2998 but don't need their static syms offset in this fashion.
2999 XXX - This is really a crock that should be fixed in the
3000 solib handling code so that I don't have to work around it
3001 here. */
c906108c
SS
3002
3003 if (!symfile_relocatable)
3004 {
3005 p = strchr (name, ':');
3006 if (p != 0 && p[1] == 'S')
3007 {
3008 /* The linker relocated it. We don't want to add an
e31272c3
MK
3009 elfstab_offset_sections-type offset, but we *do*
3010 want to add whatever solib.c passed to
3011 symbol_file_add as addr (this is known to affect
3012 SunOS 4, and I suspect ELF too). Since
3013 elfstab_offset_sections currently does not muck
3014 with the text offset (there is no Ttext.text
c906108c 3015 symbol), we can get addr from the text offset. If
e31272c3
MK
3016 elfstab_offset_sections ever starts dealing with
3017 the text offset, and we still need to do this, we
3018 need to invent a SECT_OFF_ADDR_KLUDGE or something. */
b8fbeb18 3019 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3020 goto define_a_symbol;
3021 }
3022 }
e31272c3
MK
3023 /* Since it's not the kludge case, re-dispatch to the right
3024 handler. */
c5aa993b
JM
3025 switch (type)
3026 {
3027 case N_STSYM:
3028 goto case_N_STSYM;
3029 case N_LCSYM:
3030 goto case_N_LCSYM;
3031 case N_ROSYM:
3032 goto case_N_ROSYM;
3033 default:
e31272c3 3034 internal_error (__FILE__, __LINE__,
e2e0b3e5 3035 _("failed internal consistency check"));
c5aa993b 3036 }
c906108c
SS
3037 }
3038
e31272c3
MK
3039 case_N_STSYM: /* Static symbol in data segment. */
3040 case N_DSLINE: /* Source line number, data segment. */
b8fbeb18 3041 valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
c906108c
SS
3042 goto define_a_symbol;
3043
e31272c3
MK
3044 case_N_LCSYM: /* Static symbol in BSS segment. */
3045 case N_BSLINE: /* Source line number, BSS segment. */
3046 /* N_BROWS: overlaps with N_BSLINE. */
b8fbeb18 3047 valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
c906108c
SS
3048 goto define_a_symbol;
3049
e31272c3 3050 case_N_ROSYM: /* Static symbol in read-only data segment. */
b8fbeb18 3051 valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
c906108c
SS
3052 goto define_a_symbol;
3053
e31272c3
MK
3054 case N_ENTRY: /* Alternate entry point. */
3055 /* Relocate for dynamic loading. */
b8fbeb18 3056 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3057 goto define_a_symbol;
3058
e31272c3
MK
3059 /* The following symbol types we don't know how to process.
3060 Handle them in a "default" way, but complain to people who
3061 care. */
c906108c 3062 default:
e31272c3
MK
3063 case N_CATCH: /* Exception handler catcher. */
3064 case N_EHDECL: /* Exception handler name. */
3065 case N_PC: /* Global symbol in Pascal. */
3066 case N_M2C: /* Modula-2 compilation unit. */
3067 /* N_MOD2: overlaps with N_EHDECL. */
3068 case N_SCOPE: /* Modula-2 scope information. */
3069 case N_ECOML: /* End common (local name). */
3070 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
c906108c
SS
3071 case N_NBDATA:
3072 case N_NBBSS:
3073 case N_NBSTS:
3074 case N_NBLCS:
bb599908 3075 unknown_symtype_complaint (hex_string (type));
c906108c
SS
3076 /* FALLTHROUGH */
3077
e31272c3
MK
3078 /* The following symbol types don't need the address field
3079 relocated, since it is either unused, or is absolute. */
c906108c 3080 define_a_symbol:
e31272c3
MK
3081 case N_GSYM: /* Global variable. */
3082 case N_NSYMS: /* Number of symbols (Ultrix). */
3083 case N_NOMAP: /* No map? (Ultrix). */
3084 case N_RSYM: /* Register variable. */
3085 case N_DEFD: /* Modula-2 GNU module dependency. */
3086 case N_SSYM: /* Struct or union element. */
3087 case N_LSYM: /* Local symbol in stack. */
3088 case N_PSYM: /* Parameter variable. */
3089 case N_LENG: /* Length of preceding symbol type. */
c906108c
SS
3090 if (name)
3091 {
3092 int deftype;
3093 char *colon_pos = strchr (name, ':');
3094 if (colon_pos == NULL)
3095 deftype = '\0';
3096 else
3097 deftype = colon_pos[1];
3098
3099 switch (deftype)
3100 {
3101 case 'f':
3102 case 'F':
3103 function_stab_type = type;
3104
e31272c3
MK
3105 /* Deal with the SunPRO 3.0 compiler which omits the
3106 address from N_FUN symbols. */
c906108c 3107 if (type == N_FUN
e31272c3 3108 && valu == ANOFFSET (section_offsets,
203c3895
UW
3109 SECT_OFF_TEXT (objfile))
3110 && gdbarch_sofun_address_maybe_missing (current_gdbarch))
9a058a09
KB
3111 {
3112 CORE_ADDR minsym_valu =
3113 find_stab_function_addr (name, last_source_file, objfile);
3114
e31272c3
MK
3115 /* The function find_stab_function_addr will return
3116 0 if the minimal symbol wasn't found.
3117 (Unfortunately, this might also be a valid
3118 address.) Anyway, if it *does* return 0, it is
3119 likely that the value was set correctly to begin
3120 with... */
9a058a09
KB
3121 if (minsym_valu != 0)
3122 valu = minsym_valu;
3123 }
c906108c 3124
c906108c 3125 if (block_address_function_relative)
e31272c3 3126 /* For Solaris 2 compilers, the block addresses and
c906108c 3127 N_SLINE's are relative to the start of the
e31272c3
MK
3128 function. On normal systems, and when using GCC on
3129 Solaris 2, these addresses are just absolute, or
c906108c
SS
3130 relative to the N_SO, depending on
3131 BLOCK_ADDRESS_ABSOLUTE. */
c5aa993b 3132 function_start_offset = valu;
c906108c
SS
3133
3134 within_function = 1;
c3f6f71d
JM
3135
3136 if (context_stack_depth > 1)
3137 {
23136709 3138 complaint (&symfile_complaints,
063e58ba
MD
3139 _("unmatched N_LBRAC before symtab pos %d"),
3140 symnum);
c3f6f71d
JM
3141 break;
3142 }
3143
c906108c
SS
3144 if (context_stack_depth > 0)
3145 {
3146 new = pop_context ();
3147 /* Make a block for the local symbols within. */
3148 finish_block (new->name, &local_symbols, new->old_blocks,
3149 new->start_addr, valu, objfile);
3150 }
c906108c
SS
3151
3152 new = push_context (0, valu);
3153 new->name = define_symbol (valu, name, desc, type, objfile);
3154 break;
3155
3156 default:
3157 define_symbol (valu, name, desc, type, objfile);
3158 break;
3159 }
3160 }
3161 break;
3162
c5aa993b
JM
3163 /* We use N_OPT to carry the gcc2_compiled flag. Sun uses it
3164 for a bunch of other flags, too. Someday we may parse their
3165 flags; for now we ignore theirs and hope they'll ignore ours. */
e31272c3 3166 case N_OPT: /* Solaris 2: Compiler options. */
c906108c
SS
3167 if (name)
3168 {
6314a349 3169 if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
c906108c
SS
3170 {
3171 processing_gcc_compilation = 2;
8052a17a 3172#if 0 /* Works, but is experimental. -fnf */
e31272c3
MK
3173 /* For now, stay with AUTO_DEMANGLING for g++ output, as
3174 we don't know whether it will use the old style or v3
3175 mangling. */
c906108c
SS
3176 if (AUTO_DEMANGLING)
3177 {
3178 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
3179 }
3180#endif
3181 }
3182 else
3183 n_opt_found = 1;
3184 }
3185 break;
3186
51cc5b07
AC
3187 case N_MAIN: /* Name of main routine. */
3188 /* FIXME: If one has a symbol file with N_MAIN and then replaces
3189 it with a symbol file with "main" and without N_MAIN. I'm
3190 not sure exactly what rule to follow but probably something
3191 like: N_MAIN takes precedence over "main" no matter what
3192 objfile it is in; If there is more than one N_MAIN, choose
3193 the one in the symfile_objfile; If there is more than one
3194 N_MAIN within a given objfile, complain() and choose
3195 arbitrarily. (kingdon) */
3196 if (name != NULL)
3197 set_main_name (name);
3198 break;
3199
c5aa993b 3200 /* The following symbol types can be ignored. */
e31272c3
MK
3201 case N_OBJ: /* Solaris 2: Object file dir and name. */
3202 case N_PATCH: /* Solaris 2: Patch Run Time Checker. */
3203 /* N_UNDF: Solaris 2: File separator mark. */
3204 /* N_UNDF: -- we will never encounter it, since we only process
3205 one file's symbols at once. */
3206 case N_ENDM: /* Solaris 2: End of module. */
c906108c
SS
3207 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
3208 break;
3209 }
3210
3211 /* '#' is a GNU C extension to allow one symbol to refer to another
3212 related symbol.
3213
3214 Generally this is used so that an alias can refer to its main
c5aa993b 3215 symbol. */
c906108c
SS
3216 if (name[0] == '#')
3217 {
e31272c3
MK
3218 /* Initialize symbol reference names and determine if this is a
3219 definition. If a symbol reference is being defined, go ahead
3220 and add it. Otherwise, just return. */
c906108c
SS
3221
3222 char *s = name;
3223 int refnum;
3224
3225 /* If this stab defines a new reference ID that is not on the
c5aa993b 3226 reference list, then put it on the reference list.
c906108c 3227
c5aa993b
JM
3228 We go ahead and advance NAME past the reference, even though
3229 it is not strictly necessary at this time. */
c906108c
SS
3230 refnum = symbol_reference_defined (&s);
3231 if (refnum >= 0)
3232 if (!ref_search (refnum))
3233 ref_add (refnum, 0, name, valu);
3234 name = s;
3235 }
3236
c906108c
SS
3237 previous_stab_code = type;
3238}
3239\f
3240/* FIXME: The only difference between this and elfstab_build_psymtabs
3241 is the call to install_minimal_symbols for elf, and the support for
3242 split sections. If the differences are really that small, the code
3243 should be shared. */
3244
3245/* Scan and build partial symbols for an coff symbol file.
3246 The coff file has already been processed to get its minimal symbols.
3247
3248 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3249 rolled into one.
3250
3251 OBJFILE is the object file we are reading symbols from.
3252 ADDR is the address relative to which the symbols are (e.g.
3253 the base address of the text segment).
3254 MAINLINE is true if we are reading the main symbol
3255 table (as opposed to a shared lib or dynamically loaded file).
3256 TEXTADDR is the address of the text section.
3257 TEXTSIZE is the size of the text section.
3258 STABSECTS is the list of .stab sections in OBJFILE.
3259 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
3260 .stabstr section exists.
3261
3262 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
3263 adjusted for coff details. */
3264
3265void
fba45db2
KB
3266coffstab_build_psymtabs (struct objfile *objfile, int mainline,
3267 CORE_ADDR textaddr, unsigned int textsize,
3268 struct stab_section_list *stabsects,
3269 file_ptr stabstroffset, unsigned int stabstrsize)
c906108c
SS
3270{
3271 int val;
3272 bfd *sym_bfd = objfile->obfd;
3273 char *name = bfd_get_filename (sym_bfd);
3274 struct dbx_symfile_info *info;
3275 unsigned int stabsize;
3276
3277 /* There is already a dbx_symfile_info allocated by our caller.
3278 It might even contain some info from the coff symtab to help us. */
0a6ddd08 3279 info = objfile->deprecated_sym_stab_info;
c906108c
SS
3280
3281 DBX_TEXT_ADDR (objfile) = textaddr;
3282 DBX_TEXT_SIZE (objfile) = textsize;
3283
3284#define COFF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
c5aa993b 3285 DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
c906108c 3286 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
c5aa993b 3287
c906108c 3288 if (stabstrsize > bfd_get_size (sym_bfd))
8a3fe4f8 3289 error (_("ridiculous string table size: %d bytes"), stabstrsize);
c906108c 3290 DBX_STRINGTAB (objfile) = (char *)
8b92e4d5 3291 obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
c5aa993b 3292 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
c906108c
SS
3293
3294 /* Now read in the string table in one big gulp. */
3295
3296 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3297 if (val < 0)
3298 perror_with_name (name);
3a42e9d0 3299 val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
c906108c
SS
3300 if (val != stabstrsize)
3301 perror_with_name (name);
3302
3303 stabsread_new_init ();
3304 buildsym_new_init ();
3305 free_header_files ();
3306 init_header_files ();
3307
3308 processing_acc_compilation = 1;
3309
3310 /* In a coff file, we've already installed the minimal symbols that came
3311 from the coff (non-stab) symbol table, so always act like an
3312 incremental load here. */
3313 if (stabsects->next == NULL)
3314 {
3315 stabsize = bfd_section_size (sym_bfd, stabsects->section);
3316 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
3317 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3318 }
3319 else
3320 {
3321 struct stab_section_list *stabsect;
3322
3323 DBX_SYMCOUNT (objfile) = 0;
3324 for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
3325 {
3326 stabsize = bfd_section_size (sym_bfd, stabsect->section);
3327 DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
3328 }
3329
3330 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3331
3332 symbuf_sections = stabsects->next;
3333 symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
3334 symbuf_read = 0;
3335 }
3336
96baa820 3337 dbx_symfile_read (objfile, 0);
c906108c
SS
3338}
3339\f
3340/* Scan and build partial symbols for an ELF symbol file.
fea25152 3341 This ELF file has already been processed to get its minimal symbols.
c906108c
SS
3342
3343 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3344 rolled into one.
3345
3346 OBJFILE is the object file we are reading symbols from.
3347 ADDR is the address relative to which the symbols are (e.g.
3348 the base address of the text segment).
3349 MAINLINE is true if we are reading the main symbol
3350 table (as opposed to a shared lib or dynamically loaded file).
086df311 3351 STABSECT is the BFD section information for the .stab section.
c906108c
SS
3352 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
3353 .stabstr section exists.
3354
3355 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
3356 adjusted for elf details. */
3357
3358void
fba45db2 3359elfstab_build_psymtabs (struct objfile *objfile, int mainline,
086df311 3360 asection *stabsect,
fba45db2 3361 file_ptr stabstroffset, unsigned int stabstrsize)
c906108c
SS
3362{
3363 int val;
3364 bfd *sym_bfd = objfile->obfd;
3365 char *name = bfd_get_filename (sym_bfd);
3366 struct dbx_symfile_info *info;
086df311 3367 struct cleanup *back_to = NULL;
c906108c
SS
3368
3369 /* There is already a dbx_symfile_info allocated by our caller.
3370 It might even contain some info from the ELF symtab to help us. */
0a6ddd08 3371 info = objfile->deprecated_sym_stab_info;
c906108c 3372
7a292a7a
SS
3373 /* Find the first and last text address. dbx_symfile_read seems to
3374 want this. */
3375 find_text_range (sym_bfd, objfile);
c906108c
SS
3376
3377#define ELF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
c5aa993b 3378 DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
086df311
DJ
3379 DBX_SYMCOUNT (objfile)
3380 = bfd_section_size (objfile->obfd, stabsect) / DBX_SYMBOL_SIZE (objfile);
c906108c 3381 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
086df311
DJ
3382 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
3383 DBX_STAB_SECTION (objfile) = stabsect;
c5aa993b 3384
c906108c 3385 if (stabstrsize > bfd_get_size (sym_bfd))
8a3fe4f8 3386 error (_("ridiculous string table size: %d bytes"), stabstrsize);
c906108c 3387 DBX_STRINGTAB (objfile) = (char *)
8b92e4d5 3388 obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
c5aa993b 3389 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
c906108c
SS
3390
3391 /* Now read in the string table in one big gulp. */
3392
3393 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3394 if (val < 0)
3395 perror_with_name (name);
3a42e9d0 3396 val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
c906108c
SS
3397 if (val != stabstrsize)
3398 perror_with_name (name);
3399
3400 stabsread_new_init ();
3401 buildsym_new_init ();
3402 free_header_files ();
3403 init_header_files ();
c906108c
SS
3404
3405 processing_acc_compilation = 1;
3406
086df311
DJ
3407 symbuf_read = 0;
3408 symbuf_left = bfd_section_size (objfile->obfd, stabsect);
3409 stabs_data = symfile_relocate_debug_section (objfile->obfd, stabsect, NULL);
3410 if (stabs_data)
3411 back_to = make_cleanup (free_current_contents, (void *) &stabs_data);
3412
c906108c
SS
3413 /* In an elf file, we've already installed the minimal symbols that came
3414 from the elf (non-stab) symbol table, so always act like an
7134143f
DJ
3415 incremental load here. dbx_symfile_read should not generate any new
3416 minimal symbols, since we will have already read the ELF dynamic symbol
3417 table and normal symbol entries won't be in the ".stab" section; but in
3418 case it does, it will install them itself. */
96baa820 3419 dbx_symfile_read (objfile, 0);
086df311
DJ
3420
3421 if (back_to)
3422 do_cleanups (back_to);
c906108c
SS
3423}
3424\f
3425/* Scan and build partial symbols for a file with special sections for stabs
3426 and stabstrings. The file has already been processed to get its minimal
3427 symbols, and any other symbols that might be necessary to resolve GSYMs.
3428
3429 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3430 rolled into one.
3431
3432 OBJFILE is the object file we are reading symbols from.
3433 ADDR is the address relative to which the symbols are (e.g. the base address
c5aa993b 3434 of the text segment).
c906108c 3435 MAINLINE is true if we are reading the main symbol table (as opposed to a
c5aa993b 3436 shared lib or dynamically loaded file).
c906108c
SS
3437 STAB_NAME is the name of the section that contains the stabs.
3438 STABSTR_NAME is the name of the section that contains the stab strings.
3439
3440 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read. */
3441
3442void
fba45db2
KB
3443stabsect_build_psymtabs (struct objfile *objfile, int mainline, char *stab_name,
3444 char *stabstr_name, char *text_name)
c906108c
SS
3445{
3446 int val;
3447 bfd *sym_bfd = objfile->obfd;
3448 char *name = bfd_get_filename (sym_bfd);
3449 asection *stabsect;
3450 asection *stabstrsect;
3451 asection *text_sect;
3452
3453 stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
3454 stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
3455
3456 if (!stabsect)
3457 return;
3458
3459 if (!stabstrsect)
063e58ba
MD
3460 error (_("stabsect_build_psymtabs: Found stabs (%s), but not string \
3461section (%s)"),
c906108c
SS
3462 stab_name, stabstr_name);
3463
0a6ddd08 3464 objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *)
c906108c 3465 xmalloc (sizeof (struct dbx_symfile_info));
063e58ba
MD
3466 memset (objfile->deprecated_sym_stab_info, 0,
3467 sizeof (struct dbx_symfile_info));
c906108c
SS
3468
3469 text_sect = bfd_get_section_by_name (sym_bfd, text_name);
3470 if (!text_sect)
8a3fe4f8 3471 error (_("Can't find %s section in symbol file"), text_name);
c906108c
SS
3472 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
3473 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
3474
c5aa993b
JM
3475 DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
3476 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
c906108c
SS
3477 / DBX_SYMBOL_SIZE (objfile);
3478 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
c5aa993b
JM
3479 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos; /* XXX - FIXME: POKING INSIDE BFD DATA STRUCTURES */
3480
c906108c 3481 if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
063e58ba
MD
3482 error (_("ridiculous string table size: %d bytes"),
3483 DBX_STRINGTAB_SIZE (objfile));
c906108c 3484 DBX_STRINGTAB (objfile) = (char *)
8b92e4d5 3485 obstack_alloc (&objfile->objfile_obstack, DBX_STRINGTAB_SIZE (objfile) + 1);
c906108c
SS
3486 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
3487
3488 /* Now read in the string table in one big gulp. */
3489
c5aa993b
JM
3490 val = bfd_get_section_contents (sym_bfd, /* bfd */
3491 stabstrsect, /* bfd section */
063e58ba 3492 DBX_STRINGTAB (objfile), /* input buffer */
c5aa993b 3493 0, /* offset into section */
063e58ba 3494 DBX_STRINGTAB_SIZE (objfile)); /* amount to read */
c906108c
SS
3495
3496 if (!val)
3497 perror_with_name (name);
3498
3499 stabsread_new_init ();
3500 buildsym_new_init ();
3501 free_header_files ();
3502 init_header_files ();
c906108c
SS
3503
3504 /* Now, do an incremental load */
3505
3506 processing_acc_compilation = 1;
96baa820 3507 dbx_symfile_read (objfile, 0);
c906108c
SS
3508}
3509\f
3510static struct sym_fns aout_sym_fns =
3511{
3512 bfd_target_aout_flavour,
063e58ba
MD
3513 dbx_new_init, /* sym_new_init: init anything gbl to entire symtab */
3514 dbx_symfile_init, /* sym_init: read initial info, setup for sym_read() */
c5aa993b
JM
3515 dbx_symfile_read, /* sym_read: read a symbol file into symtab */
3516 dbx_symfile_finish, /* sym_finish: finished with file, cleanup */
063e58ba
MD
3517 default_symfile_offsets, /* sym_offsets: parse user's offsets to
3518 internal form */
31d99776
DJ
3519 default_symfile_segments, /* sym_segments: Get segment information from
3520 a file. */
c295b2e5 3521 NULL, /* sym_read_linetable */
c5aa993b 3522 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
3523};
3524
3525void
fba45db2 3526_initialize_dbxread (void)
c906108c 3527{
c5aa993b 3528 add_symtab_fns (&aout_sym_fns);
c906108c 3529}
This page took 0.918051 seconds and 4 git commands to generate.