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