* main.c (complete_command): New command, from Rick Sladkey
[deliverable/binutils-gdb.git] / gdb / dbxread.c
CommitLineData
bd5635a1 1/* Read dbx symbol tables and convert to internal format, for GDB.
65ce5df4
JG
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993
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
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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"
318bf84f 36#include <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
afe4ca15
JG
43#include <obstack.h>
44#include <sys/param.h>
021959e2 45#ifndef NO_SYS_FILE
afe4ca15 46#include <sys/file.h>
021959e2 47#endif
afe4ca15 48#include <sys/stat.h>
bd5635a1 49#include <ctype.h>
afe4ca15
JG
50#include "symtab.h"
51#include "breakpoint.h"
52#include "command.h"
53#include "target.h"
54#include "gdbcore.h" /* for bfd stuff */
ac88ca20 55#include "libbfd.h" /* FIXME Secret internal BFD stuff (bfd_read) */
afe4ca15
JG
56#include "libaout.h" /* FIXME Secret internal BFD stuff for a.out */
57#include "symfile.h"
3624c875 58#include "objfiles.h"
c0302457 59#include "buildsym.h"
3416d90b 60#include "stabsread.h"
2af231b8 61#include "gdb-stabs.h"
3416d90b 62#include "demangle.h"
51b80b00
FF
63#include "language.h" /* Needed inside partial-stab.h */
64#include "complaints.h"
afe4ca15 65
7e258d18
PB
66#include "aout/aout64.h"
67#include "aout/stab_gnu.h" /* We always use GNU stabs, not native, now */
bd5635a1 68
2c7ab4ca
JK
69#if !defined (SEEK_SET)
70#define SEEK_SET 0
71#define SEEK_CUR 1
72#endif
73
4a35d6e9
FF
74/* Each partial symbol table entry contains a pointer to private data for the
75 read_symtab() function to use when expanding a partial symbol table entry
76 to a full symbol table entry.
77
78 For dbxread this structure contains the offset within the file symbol table
79 of first local symbol for this file, and length (in bytes) of the section
80 of the symbol table devoted to this file's symbols (actually, the section
9342ecb9
JG
81 bracketed may contain more than just this file's symbols). It also contains
82 further information needed to locate the symbols if they are in an ELF file.
83
84 If ldsymlen is 0, the only reason for this thing's existence is the
85 dependency list. Nothing else will happen when it is read in. */
4a35d6e9
FF
86
87#define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
88#define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
9342ecb9
JG
89#define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
90#define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
91#define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
92#define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
93#define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
4a35d6e9
FF
94
95struct symloc {
96 int ldsymoff;
97 int ldsymlen;
9342ecb9
JG
98 int symbol_size;
99 int symbol_offset;
100 int string_offset;
101 int file_string_offset;
4a35d6e9
FF
102};
103
bd5635a1
RP
104/* Macro to determine which symbols to ignore when reading the first symbol
105 of a file. Some machines override this definition. */
106#ifndef IGNORE_SYMBOL
107/* This code is used on Ultrix systems. Ignore it */
108#define IGNORE_SYMBOL(type) (type == (int)N_NSYMS)
109#endif
110
2e4964ad
FF
111/* Remember what we deduced to be the source language of this psymtab. */
112
113static enum language psymtab_language = language_unknown;
114
bd5635a1
RP
115/* Nonzero means give verbose info on gdb action. From main.c. */
116extern int info_verbose;
117
7d9884b9 118/* The BFD for this file -- implicit parameter to next_symbol_text. */
bd5635a1 119
c0302457 120static bfd *symfile_bfd;
bd5635a1 121
afe4ca15
JG
122/* The size of each symbol in the symbol file (in external form).
123 This is set by dbx_symfile_read when building psymtabs, and by
124 dbx_psymtab_to_symtab when building symtabs. */
125
126static unsigned symbol_size;
127
9342ecb9
JG
128/* This is the offset of the symbol table in the executable file */
129static unsigned symbol_table_offset;
130
131/* This is the offset of the string table in the executable file */
132static unsigned string_table_offset;
133
134/* For elf+stab executables, the n_strx field is not a simple index
135 into the string table. Instead, each .o file has a base offset
136 in the string table, and the associated symbols contain offsets
137 from this base. The following two variables contain the base
138 offset for the current and next .o files. */
139static unsigned int file_string_table_offset;
140static unsigned int next_file_string_table_offset;
9d2b8d50
JK
141\f
142/* This is the lowest text address we have yet encountered. */
143static CORE_ADDR lowest_text_address;
9342ecb9 144
bd5635a1
RP
145/* Complaints about the symbols we have encountered. */
146
bd5635a1
RP
147struct complaint lbrac_complaint =
148 {"bad block start address patched", 0, 0};
149
bd5635a1
RP
150struct complaint string_table_offset_complaint =
151 {"bad string table offset in symbol %d", 0, 0};
152
153struct complaint unknown_symtype_complaint =
0c4d2cc2 154 {"unknown symbol type %s", 0, 0};
bd5635a1 155
65ce5df4 156struct complaint unknown_symchar_complaint =
b30c81b6 157 {"unknown symbol descriptor `%c'", 0, 0};
65ce5df4 158
bd5635a1
RP
159struct complaint lbrac_rbrac_complaint =
160 {"block start larger than block end", 0, 0};
7d9884b9
JG
161
162struct complaint lbrac_unmatched_complaint =
163 {"unmatched N_LBRAC before symtab pos %d", 0, 0};
164
165struct complaint lbrac_mismatch_complaint =
166 {"N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", 0, 0};
9342ecb9
JG
167
168struct complaint repeated_header_complaint =
169 {"\"repeated\" header file not previously seen, at symtab pos %d", 0, 0};
170
171struct complaint repeated_header_name_complaint =
172 {"\"repeated\" header file not previously seen, named %s", 0, 0};
bd5635a1 173\f
bd5635a1
RP
174/* During initial symbol readin, we need to have a structure to keep
175 track of which psymtabs have which bincls in them. This structure
176 is used during readin to setup the list of dependencies within each
177 partial symbol table. */
178
179struct header_file_location
180{
181 char *name; /* Name of header file */
182 int instance; /* See above */
183 struct partial_symtab *pst; /* Partial symtab that has the
184 BINCL/EINCL defs for this file */
185};
186
187/* The actual list and controling variables */
188static struct header_file_location *bincl_list, *next_bincl;
189static int bincls_allocated;
190
021959e2
JG
191/* Local function prototypes */
192
193static void
80d68b1d
FF
194free_header_files PARAMS ((void));
195
196static void
197init_header_files PARAMS ((void));
021959e2 198
574dac8e
JK
199static void
200read_ofile_symtab PARAMS ((struct partial_symtab *));
021959e2
JG
201
202static void
203dbx_psymtab_to_symtab PARAMS ((struct partial_symtab *));
204
205static void
4c07f28d 206dbx_psymtab_to_symtab_1 PARAMS ((struct partial_symtab *));
021959e2
JG
207
208static void
2af231b8
JG
209read_dbx_symtab PARAMS ((struct section_offsets *, struct objfile *,
210 CORE_ADDR, int));
021959e2
JG
211
212static void
213free_bincl_list PARAMS ((struct objfile *));
214
215static struct partial_symtab *
216find_corresponding_bincl_psymtab PARAMS ((char *, int));
217
218static void
219add_bincl_to_list PARAMS ((struct partial_symtab *, char *, int));
220
221static void
222init_bincl_list PARAMS ((int, struct objfile *));
223
224static void
3624c875 225init_psymbol_list PARAMS ((struct objfile *));
021959e2
JG
226
227static char *
228dbx_next_symbol_text PARAMS ((void));
229
230static void
231fill_symbuf PARAMS ((bfd *));
232
233static void
80d68b1d
FF
234dbx_symfile_init PARAMS ((struct objfile *));
235
236static void
237dbx_new_init PARAMS ((struct objfile *));
021959e2
JG
238
239static void
2af231b8 240dbx_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
021959e2
JG
241
242static void
80d68b1d 243dbx_symfile_finish PARAMS ((struct objfile *));
021959e2
JG
244
245static void
246record_minimal_symbol PARAMS ((char *, CORE_ADDR, int, struct objfile *));
247
248static void
249add_new_header_file PARAMS ((char *, int));
250
251static void
252add_old_header_file PARAMS ((char *, int));
253
254static void
255add_this_object_header_file PARAMS ((int));
256
80d68b1d 257/* Free up old header file tables */
bd5635a1 258
021959e2 259static void
80d68b1d 260free_header_files ()
bd5635a1
RP
261{
262 register int i;
bd5635a1 263
80d68b1d
FF
264 if (header_files != NULL)
265 {
266 for (i = 0; i < n_header_files; i++)
267 {
268 free (header_files[i].name);
269 }
ac88ca20 270 free ((PTR)header_files);
80d68b1d
FF
271 header_files = NULL;
272 n_header_files = 0;
273 }
274 if (this_object_header_files)
275 {
ac88ca20 276 free ((PTR)this_object_header_files);
80d68b1d
FF
277 this_object_header_files = NULL;
278 }
279 n_allocated_header_files = 0;
280 n_allocated_this_object_header_files = 0;
281}
282
283/* Allocate new header file tables */
284
285static void
286init_header_files ()
287{
bd5635a1 288 n_header_files = 0;
80d68b1d
FF
289 n_allocated_header_files = 10;
290 header_files = (struct header_file *)
291 xmalloc (10 * sizeof (struct header_file));
bd5635a1
RP
292
293 n_allocated_this_object_header_files = 10;
294 this_object_header_files = (int *) xmalloc (10 * sizeof (int));
295}
296
bd5635a1
RP
297/* Add header file number I for this object file
298 at the next successive FILENUM. */
299
300static void
301add_this_object_header_file (i)
302 int i;
303{
304 if (n_this_object_header_files == n_allocated_this_object_header_files)
305 {
306 n_allocated_this_object_header_files *= 2;
307 this_object_header_files
021959e2 308 = (int *) xrealloc ((char *) this_object_header_files,
bd5635a1
RP
309 n_allocated_this_object_header_files * sizeof (int));
310 }
311
312 this_object_header_files[n_this_object_header_files++] = i;
313}
314
315/* Add to this file an "old" header file, one already seen in
316 a previous object file. NAME is the header file's name.
317 INSTANCE is its instance code, to select among multiple
318 symbol tables for the same header file. */
319
320static void
321add_old_header_file (name, instance)
322 char *name;
323 int instance;
324{
325 register struct header_file *p = header_files;
326 register int i;
327
328 for (i = 0; i < n_header_files; i++)
2e4964ad 329 if (STREQ (p[i].name, name) && instance == p[i].instance)
bd5635a1
RP
330 {
331 add_this_object_header_file (i);
332 return;
333 }
51b80b00 334 complain (&repeated_header_complaint, symnum);
9342ecb9 335 complain (&repeated_header_name_complaint, name);
bd5635a1
RP
336}
337
338/* Add to this file a "new" header file: definitions for its types follow.
339 NAME is the header file's name.
340 Most often this happens only once for each distinct header file,
341 but not necessarily. If it happens more than once, INSTANCE has
342 a different value each time, and references to the header file
343 use INSTANCE values to select among them.
344
345 dbx output contains "begin" and "end" markers for each new header file,
346 but at this level we just need to know which files there have been;
347 so we record the file when its "begin" is seen and ignore the "end". */
348
349static void
350add_new_header_file (name, instance)
351 char *name;
352 int instance;
353{
354 register int i;
bd5635a1
RP
355
356 /* Make sure there is room for one more header file. */
357
358 if (n_header_files == n_allocated_header_files)
359 {
360 n_allocated_header_files *= 2;
361 header_files = (struct header_file *)
021959e2
JG
362 xrealloc ((char *) header_files,
363 (n_allocated_header_files * sizeof (struct header_file)));
bd5635a1
RP
364 }
365
366 /* Create an entry for this header file. */
367
368 i = n_header_files++;
369 header_files[i].name = savestring (name, strlen(name));
370 header_files[i].instance = instance;
371 header_files[i].length = 10;
372 header_files[i].vector
373 = (struct type **) xmalloc (10 * sizeof (struct type *));
4ed3a9ea 374 memset (header_files[i].vector, 0, 10 * sizeof (struct type *));
bd5635a1
RP
375
376 add_this_object_header_file (i);
377}
378
bd5635a1
RP
379#if 0
380static struct type **
381explicit_lookup_type (real_filenum, index)
382 int real_filenum, index;
383{
384 register struct header_file *f = &header_files[real_filenum];
385
386 if (index >= f->length)
387 {
388 f->length *= 2;
389 f->vector = (struct type **)
390 xrealloc (f->vector, f->length * sizeof (struct type *));
4ed97c9a
RP
391 memset (&f->vector[f->length / 2],
392 '\0', f->length * sizeof (struct type *) / 2);
bd5635a1
RP
393 }
394 return &f->vector[index];
395}
396#endif
397\f
9bba3334 398static void
021959e2 399record_minimal_symbol (name, address, type, objfile)
bd5635a1
RP
400 char *name;
401 CORE_ADDR address;
402 int type;
021959e2 403 struct objfile *objfile;
bd5635a1 404{
021959e2 405 enum minimal_symbol_type ms_type;
0c4d2cc2 406
b8ec9a79
JK
407 switch (type)
408 {
409 case N_TEXT | N_EXT: ms_type = mst_text; break;
410 case N_DATA | N_EXT: ms_type = mst_data; break;
411 case N_BSS | N_EXT: ms_type = mst_bss; break;
412 case N_ABS | N_EXT: ms_type = mst_abs; break;
0c4d2cc2 413#ifdef N_SETV
b8ec9a79
JK
414 case N_SETV | N_EXT: ms_type = mst_data; break;
415 case N_SETV:
416 /* I don't think this type actually exists; since a N_SETV is the result
417 of going over many .o files, it doesn't make sense to have one
418 file local. */
419 ms_type = mst_file_data;
420 break;
0c4d2cc2 421#endif
05c81f45 422 case N_TEXT:
b8ec9a79
JK
423 case N_NBTEXT:
424 case N_FN:
425 case N_FN_SEQ:
b8ec9a79
JK
426 ms_type = mst_file_text;
427 break;
428
429 case N_DATA:
430 ms_type = mst_file_data;
431
432 /* Check for __DYNAMIC, which is used by Sun shared libraries.
433 Record it as global even if it's local, not global, so
05c81f45
SEF
434 lookup_minimal_symbol can find it. We don't check symbol_leading_char
435 because for SunOS4 it always is '_'. */
b8ec9a79
JK
436 if (name[8] == 'C' && STREQ ("__DYNAMIC", name))
437 ms_type = mst_data;
438
439 /* Same with virtual function tables, both global and static. */
440 {
441 char *tempstring = name;
442 if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
443 ++tempstring;
444 if (VTBL_PREFIX_P ((tempstring)))
445 ms_type = mst_data;
446 }
447 break;
448
449 case N_BSS:
450 ms_type = mst_file_bss;
451 break;
452
021959e2 453 default: ms_type = mst_unknown; break;
0c4d2cc2 454 }
bd5635a1 455
9d2b8d50
JK
456 if (ms_type == mst_file_text || ms_type == mst_text
457 && address < lowest_text_address)
458 lowest_text_address = address;
459
b8ec9a79
JK
460 prim_record_minimal_symbol
461 (obsavestring (name, strlen (name), &objfile -> symbol_obstack),
462 address,
6545c6a0
JK
463 ms_type,
464 objfile);
bd5635a1
RP
465}
466\f
467/* Scan and build partial symbols for a symbol file.
468 We have been initialized by a call to dbx_symfile_init, which
3624c875
FF
469 put all the relevant info into a "struct dbx_symfile_info",
470 hung off the objfile structure.
bd5635a1 471
2af231b8
JG
472 SECTION_OFFSETS contains offsets relative to which the symbols in the
473 various sections are (depending where the sections were actually loaded).
bd5635a1
RP
474 MAINLINE is true if we are reading the main symbol
475 table (as opposed to a shared lib or dynamically loaded file). */
476
9bba3334 477static void
2af231b8 478dbx_symfile_read (objfile, section_offsets, mainline)
80d68b1d 479 struct objfile *objfile;
2af231b8 480 struct section_offsets *section_offsets;
bd5635a1
RP
481 int mainline; /* FIXME comments above */
482{
80d68b1d 483 bfd *sym_bfd;
bd5635a1 484 int val;
0eb22669 485 struct cleanup *back_to;
bd5635a1 486
80d68b1d 487 sym_bfd = objfile->obfd;
2c7ab4ca 488 val = bfd_seek (objfile->obfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
bd5635a1 489 if (val < 0)
80d68b1d 490 perror_with_name (objfile->name);
bd5635a1 491
66eeea27 492 /* If we are reinitializing, or if we have never loaded syms yet, init */
80d68b1d 493 if (mainline || objfile->global_psymbols.size == 0 || objfile->static_psymbols.size == 0)
3624c875 494 init_psymbol_list (objfile);
66eeea27 495
9342ecb9
JG
496 symbol_size = DBX_SYMBOL_SIZE (objfile);
497 symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
afe4ca15 498
bd5635a1 499 pending_blocks = 0;
0eb22669 500 back_to = make_cleanup (really_free_pendings, 0);
bd5635a1 501
021959e2
JG
502 init_minimal_symbol_collection ();
503 make_cleanup (discard_minimal_symbols, 0);
bd5635a1
RP
504
505 /* Now that the symbol table data of the executable file are all in core,
506 process them and define symbols accordingly. */
507
2af231b8 508 read_dbx_symtab (section_offsets, objfile,
3624c875
FF
509 bfd_section_vma (sym_bfd, DBX_TEXT_SECT (objfile)),
510 bfd_section_size (sym_bfd, DBX_TEXT_SECT (objfile)));
bd5635a1 511
021959e2
JG
512 /* Install any minimal symbols that have been collected as the current
513 minimal symbols for this objfile. */
bd5635a1 514
80d68b1d 515 install_minimal_symbols (objfile);
bd5635a1 516
021959e2 517 if (!have_partial_symbols ()) {
9404978d
MT
518 wrap_here ("");
519 printf_filtered ("(no debugging symbols found)...");
520 wrap_here ("");
521 }
0eb22669
PS
522
523 do_cleanups (back_to);
bd5635a1
RP
524}
525
9404978d
MT
526/* Initialize anything that needs initializing when a completely new
527 symbol file is specified (not just adding some symbols from another
528 file, e.g. a shared library). */
bd5635a1 529
9bba3334 530static void
ac88ca20
JG
531dbx_new_init (ignore)
532 struct objfile *ignore;
bd5635a1 533{
3416d90b 534 stabsread_new_init ();
c0302457 535 buildsym_new_init ();
80d68b1d 536 init_header_files ();
bd5635a1
RP
537}
538
539
540/* dbx_symfile_init ()
541 is the dbx-specific initialization routine for reading symbols.
80d68b1d 542 It is passed a struct objfile which contains, among other things,
bd5635a1
RP
543 the BFD for the file whose symbols are being read, and a slot for a pointer
544 to "private data" which we fill with goodies.
545
546 We read the string table into malloc'd space and stash a pointer to it.
547
548 Since BFD doesn't know how to read debug symbols in a format-independent
549 way (and may never do so...), we have to do it ourselves. We will never
550 be called unless this is an a.out (or very similar) file.
551 FIXME, there should be a cleaner peephole into the BFD environment here. */
552
69a272c4
FF
553#define DBX_STRINGTAB_SIZE_SIZE sizeof(long) /* FIXME */
554
9bba3334 555static void
80d68b1d
FF
556dbx_symfile_init (objfile)
557 struct objfile *objfile;
bd5635a1
RP
558{
559 int val;
80d68b1d 560 bfd *sym_bfd = objfile->obfd;
bd5635a1 561 char *name = bfd_get_filename (sym_bfd);
69a272c4 562 unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
bd5635a1
RP
563
564 /* Allocate struct to keep track of the symfile */
965a5c32 565 objfile->sym_stab_info = (PTR)
3624c875 566 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
bd5635a1
RP
567
568 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
bd5635a1
RP
569#define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
570#define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
040b9597 571
bd5635a1
RP
572 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
573
784fd92b 574 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
3624c875
FF
575 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
576 if (!DBX_TEXT_SECT (objfile))
9342ecb9
JG
577 error ("Can't find .text section in symbol file");
578
bf18ac80 579 DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
7da1e27d 580 DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
9342ecb9 581 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
3624c875
FF
582
583 /* Read the string table and stash it away in the psymbol_obstack. It is
584 only needed as long as we need to expand psymbols into full symbols,
585 so when we blow away the psymbol the string table goes away as well.
586 Note that gdb used to use the results of attempting to malloc the
587 string table, based on the size it read, as a form of sanity check
588 for botched byte swapping, on the theory that a byte swapped string
589 table size would be so totally bogus that the malloc would fail. Now
590 that we put in on the psymbol_obstack, we can't do this since gdb gets
591 a fatal error (out of virtual memory) if the size is bogus. We can
69a272c4
FF
592 however at least check to see if the size is less than the size of
593 the size field itself, or larger than the size of the entire file.
594 Note that all valid string tables have a size greater than zero, since
595 the bytes used to hold the size are included in the count. */
3624c875 596
69a272c4
FF
597 if (STRING_TABLE_OFFSET == 0)
598 {
65ce5df4
JG
599 /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
600 will never be zero, even when there is no string table. This
601 would appear to be a bug in bfd. */
69a272c4
FF
602 DBX_STRINGTAB_SIZE (objfile) = 0;
603 DBX_STRINGTAB (objfile) = NULL;
604 }
605 else
606 {
2c7ab4ca 607 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
69a272c4
FF
608 if (val < 0)
609 perror_with_name (name);
610
611 memset ((PTR) size_temp, 0, sizeof (size_temp));
612 val = bfd_read ((PTR) size_temp, sizeof (size_temp), 1, sym_bfd);
613 if (val < 0)
65ce5df4
JG
614 {
615 perror_with_name (name);
616 }
617 else if (val == 0)
618 {
619 /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
620 EOF if there is no string table, and attempting to read the size
621 from EOF will read zero bytes. */
622 DBX_STRINGTAB_SIZE (objfile) = 0;
623 DBX_STRINGTAB (objfile) = NULL;
624 }
625 else
626 {
627 /* Read some data that would appear to be the string table size.
628 If there really is a string table, then it is probably the right
629 size. Byteswap if necessary and validate the size. Note that
630 the minimum is DBX_STRINGTAB_SIZE_SIZE. If we just read some
631 random data that happened to be at STRING_TABLE_OFFSET, because
632 bfd can't tell us there is no string table, the sanity checks may
633 or may not catch this. */
634 DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
635
636 if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
637 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
638 error ("ridiculous string table size (%d bytes).",
639 DBX_STRINGTAB_SIZE (objfile));
640
641 DBX_STRINGTAB (objfile) =
642 (char *) obstack_alloc (&objfile -> psymbol_obstack,
643 DBX_STRINGTAB_SIZE (objfile));
644
645 /* Now read in the string table in one big gulp. */
646
2c7ab4ca 647 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
65ce5df4
JG
648 if (val < 0)
649 perror_with_name (name);
650 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
651 sym_bfd);
652 if (val != DBX_STRINGTAB_SIZE (objfile))
653 perror_with_name (name);
654 }
69a272c4 655 }
bd5635a1 656}
80d68b1d
FF
657
658/* Perform any local cleanups required when we are done with a particular
659 objfile. I.E, we are in the process of discarding all symbol information
660 for an objfile, freeing up all memory held for it, and unlinking the
661 objfile struct from the global list of known objfiles. */
662
663static void
664dbx_symfile_finish (objfile)
665 struct objfile *objfile;
666{
965a5c32 667 if (objfile->sym_stab_info != NULL)
80d68b1d 668 {
965a5c32 669 mfree (objfile -> md, objfile->sym_stab_info);
80d68b1d
FF
670 }
671 free_header_files ();
672}
673
bd5635a1
RP
674\f
675/* Buffer for reading the symbol table entries. */
afe4ca15 676static struct internal_nlist symbuf[4096];
bd5635a1
RP
677static int symbuf_idx;
678static int symbuf_end;
679
9342ecb9
JG
680/* Name of last function encountered. Used in Solaris to approximate
681 object file boundaries. */
682static char *last_function_name;
683
bd5635a1
RP
684/* The address in memory of the string table of the object file we are
685 reading (which might not be the "main" object file, but might be a
686 shared library or some other dynamically loaded thing). This is set
687 by read_dbx_symtab when building psymtabs, and by read_ofile_symtab
688 when building symtabs, and is used only by next_symbol_text. */
689static char *stringtab_global;
690
691/* Refill the symbol table input buffer
692 and set the variables that control fetching entries from it.
693 Reports an error if no data available.
694 This function can read past the end of the symbol table
695 (into the string table) but this does no harm. */
696
7d9884b9
JG
697static void
698fill_symbuf (sym_bfd)
699 bfd *sym_bfd;
bd5635a1 700{
ac88ca20 701 int nbytes = bfd_read ((PTR)symbuf, sizeof (symbuf), 1, sym_bfd);
bd5635a1 702 if (nbytes < 0)
7d9884b9 703 perror_with_name (bfd_get_filename (sym_bfd));
bd5635a1
RP
704 else if (nbytes == 0)
705 error ("Premature end of file reading symbol table");
afe4ca15 706 symbuf_end = nbytes / symbol_size;
bd5635a1 707 symbuf_idx = 0;
bd5635a1
RP
708}
709
7d9884b9 710#define SWAP_SYMBOL(symp, abfd) \
bd5635a1 711 { \
7d9884b9 712 (symp)->n_strx = bfd_h_get_32(abfd, \
afe4ca15 713 (unsigned char *)&(symp)->n_strx); \
7d9884b9 714 (symp)->n_desc = bfd_h_get_16 (abfd, \
bd5635a1 715 (unsigned char *)&(symp)->n_desc); \
7d9884b9 716 (symp)->n_value = bfd_h_get_32 (abfd, \
bd5635a1
RP
717 (unsigned char *)&(symp)->n_value); \
718 }
719
720/* Invariant: The symbol pointed to by symbuf_idx is the first one
721 that hasn't been swapped. Swap the symbol at the same time
722 that symbuf_idx is incremented. */
723
724/* dbx allows the text of a symbol name to be continued into the
725 next symbol name! When such a continuation is encountered
726 (a \ at the end of the text of a name)
727 call this function to get the continuation. */
728
021959e2 729static char *
aab77d5f 730dbx_next_symbol_text ()
bd5635a1
RP
731{
732 if (symbuf_idx == symbuf_end)
7d9884b9 733 fill_symbuf (symfile_bfd);
bd5635a1 734 symnum++;
7d9884b9 735 SWAP_SYMBOL(&symbuf[symbuf_idx], symfile_bfd);
9342ecb9
JG
736 return symbuf[symbuf_idx++].n_strx + stringtab_global
737 + file_string_table_offset;
bd5635a1
RP
738}
739\f
740/* Initializes storage for all of the partial symbols that will be
741 created by read_dbx_symtab and subsidiaries. */
742
743static void
3624c875 744init_psymbol_list (objfile)
021959e2 745 struct objfile *objfile;
bd5635a1
RP
746{
747 /* Free any previously allocated psymbol lists. */
021959e2 748 if (objfile -> global_psymbols.list)
ac88ca20 749 mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
021959e2 750 if (objfile -> static_psymbols.list)
ac88ca20 751 mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
bd5635a1
RP
752
753 /* Current best guess is that there are approximately a twentieth
754 of the total symbols (in a debugging file) are global or static
755 oriented symbols */
3624c875
FF
756 objfile -> global_psymbols.size = DBX_SYMCOUNT (objfile) / 10;
757 objfile -> static_psymbols.size = DBX_SYMCOUNT (objfile) / 10;
021959e2 758 objfile -> global_psymbols.next = objfile -> global_psymbols.list = (struct partial_symbol *)
318bf84f 759 xmmalloc (objfile -> md, objfile -> global_psymbols.size * sizeof (struct partial_symbol));
021959e2 760 objfile -> static_psymbols.next = objfile -> static_psymbols.list = (struct partial_symbol *)
318bf84f 761 xmmalloc (objfile -> md, objfile -> static_psymbols.size * sizeof (struct partial_symbol));
bd5635a1
RP
762}
763
764/* Initialize the list of bincls to contain none and have some
765 allocated. */
766
767static void
021959e2 768init_bincl_list (number, objfile)
bd5635a1 769 int number;
021959e2 770 struct objfile *objfile;
bd5635a1
RP
771{
772 bincls_allocated = number;
773 next_bincl = bincl_list = (struct header_file_location *)
318bf84f 774 xmmalloc (objfile -> md, bincls_allocated * sizeof(struct header_file_location));
bd5635a1
RP
775}
776
777/* Add a bincl to the list. */
778
779static void
780add_bincl_to_list (pst, name, instance)
781 struct partial_symtab *pst;
782 char *name;
783 int instance;
784{
785 if (next_bincl >= bincl_list + bincls_allocated)
786 {
787 int offset = next_bincl - bincl_list;
788 bincls_allocated *= 2;
789 bincl_list = (struct header_file_location *)
318bf84f 790 xmrealloc (pst->objfile->md, (char *)bincl_list,
bd5635a1
RP
791 bincls_allocated * sizeof (struct header_file_location));
792 next_bincl = bincl_list + offset;
793 }
794 next_bincl->pst = pst;
795 next_bincl->instance = instance;
796 next_bincl++->name = name;
797}
798
799/* Given a name, value pair, find the corresponding
800 bincl in the list. Return the partial symtab associated
801 with that header_file_location. */
802
9bba3334 803static struct partial_symtab *
bd5635a1
RP
804find_corresponding_bincl_psymtab (name, instance)
805 char *name;
806 int instance;
807{
808 struct header_file_location *bincl;
809
810 for (bincl = bincl_list; bincl < next_bincl; bincl++)
811 if (bincl->instance == instance
2e4964ad 812 && STREQ (name, bincl->name))
bd5635a1
RP
813 return bincl->pst;
814
815 return (struct partial_symtab *) 0;
816}
817
818/* Free the storage allocated for the bincl list. */
819
820static void
021959e2
JG
821free_bincl_list (objfile)
822 struct objfile *objfile;
bd5635a1 823{
ac88ca20 824 mfree (objfile -> md, (PTR)bincl_list);
bd5635a1
RP
825 bincls_allocated = 0;
826}
827
bd5635a1
RP
828/* Given pointers to an a.out symbol table in core containing dbx
829 style data, setup partial_symtab's describing each source file for
3624c875
FF
830 which debugging information is available.
831 SYMFILE_NAME is the name of the file we are reading from
2af231b8
JG
832 and SECTION_OFFSETS is the set of offsets for the various sections
833 of the file (a set of zeros if the mainline program). */
bd5635a1
RP
834
835static void
2af231b8
JG
836read_dbx_symtab (section_offsets, objfile, text_addr, text_size)
837 struct section_offsets *section_offsets;
7d9884b9 838 struct objfile *objfile;
bd5635a1
RP
839 CORE_ADDR text_addr;
840 int text_size;
841{
ac88ca20 842 register struct internal_nlist *bufp = 0; /* =0 avoids gcc -Wall glitch */
bd5635a1 843 register char *namestring;
bd5635a1
RP
844 int nsl;
845 int past_first_source_file = 0;
846 CORE_ADDR last_o_file_start = 0;
0eb22669 847 struct cleanup *back_to;
7d9884b9 848 bfd *abfd;
bd5635a1
RP
849
850 /* End of the text segment of the executable file. */
851 CORE_ADDR end_of_text_addr;
852
853 /* Current partial symtab */
854 struct partial_symtab *pst;
855
856 /* List of current psymtab's include files */
857 char **psymtab_include_list;
858 int includes_allocated;
859 int includes_used;
860
861 /* Index within current psymtab dependency list */
862 struct partial_symtab **dependency_list;
863 int dependencies_used, dependencies_allocated;
864
9342ecb9
JG
865 /* FIXME. We probably want to change stringtab_global rather than add this
866 while processing every symbol entry. FIXME. */
867 file_string_table_offset = 0;
868 next_file_string_table_offset = 0;
869
3624c875 870 stringtab_global = DBX_STRINGTAB (objfile);
bd5635a1
RP
871
872 pst = (struct partial_symtab *) 0;
873
874 includes_allocated = 30;
875 includes_used = 0;
876 psymtab_include_list = (char **) alloca (includes_allocated *
877 sizeof (char *));
878
879 dependencies_allocated = 30;
880 dependencies_used = 0;
881 dependency_list =
882 (struct partial_symtab **) alloca (dependencies_allocated *
883 sizeof (struct partial_symtab *));
884
bd5635a1 885 /* Init bincl list */
021959e2 886 init_bincl_list (20, objfile);
0eb22669 887 back_to = make_cleanup (free_bincl_list, objfile);
bd5635a1 888
3416d90b 889 last_source_file = NULL;
bd5635a1 890
9d2b8d50 891 lowest_text_address = (CORE_ADDR)-1;
bd5635a1 892
7d9884b9
JG
893 symfile_bfd = objfile->obfd; /* For next_text_symbol */
894 abfd = objfile->obfd;
bd5635a1 895 symbuf_end = symbuf_idx = 0;
aab77d5f 896 next_symbol_text_func = dbx_next_symbol_text;
bd5635a1 897
3624c875 898 for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
bd5635a1
RP
899 {
900 /* Get the symbol for this run and pull out some info */
901 QUIT; /* allow this to be interruptable */
902 if (symbuf_idx == symbuf_end)
7d9884b9 903 fill_symbuf (abfd);
bd5635a1
RP
904 bufp = &symbuf[symbuf_idx++];
905
906 /*
907 * Special case to speed up readin.
908 */
909 if (bufp->n_type == (unsigned char)N_SLINE) continue;
910
7d9884b9 911 SWAP_SYMBOL (bufp, abfd);
bd5635a1
RP
912
913 /* Ok. There is a lot of code duplicated in the rest of this
914 switch statement (for efficiency reasons). Since I don't
915 like duplicating code, I will do my penance here, and
916 describe the code which is duplicated:
917
918 *) The assignment to namestring.
919 *) The call to strchr.
920 *) The addition of a partial symbol the the two partial
921 symbol lists. This last is a large section of code, so
922 I've imbedded it in the following macro.
923 */
924
925/* Set namestring based on bufp. If the string table index is invalid,
926 give a fake name, and print a single error message per symbol file read,
927 rather than abort the symbol reading or flood the user with messages. */
9342ecb9
JG
928
929/*FIXME: Too many adds and indirections in here for the inner loop. */
bd5635a1 930#define SET_NAMESTRING()\
9342ecb9
JG
931 if (((unsigned)bufp->n_strx + file_string_table_offset) >= \
932 DBX_STRINGTAB_SIZE (objfile)) { \
51b80b00 933 complain (&string_table_offset_complaint, symnum); \
bd5635a1
RP
934 namestring = "foo"; \
935 } else \
9342ecb9
JG
936 namestring = bufp->n_strx + file_string_table_offset + \
937 DBX_STRINGTAB (objfile)
bd5635a1 938
7e258d18
PB
939#define CUR_SYMBOL_TYPE bufp->n_type
940#define CUR_SYMBOL_VALUE bufp->n_value
941#define DBXREAD_ONLY
2af231b8
JG
942#define START_PSYMTAB(ofile,secoff,fname,low,symoff,global_syms,static_syms)\
943 start_psymtab(ofile, secoff, fname, low, symoff, global_syms, static_syms)
7e258d18
PB
944#define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)\
945 end_psymtab(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)
aab77d5f 946
7e258d18 947#include "partial-stab.h"
bd5635a1
RP
948 }
949
950 /* If there's stuff to be cleaned up, clean it up. */
3624c875 951 if (DBX_SYMCOUNT (objfile) > 0 /* We have some syms */
9342ecb9
JG
952/*FIXME, does this have a bug at start address 0? */
953 && last_o_file_start
3624c875
FF
954 && objfile -> ei.entry_point < bufp->n_value
955 && objfile -> ei.entry_point >= last_o_file_start)
bd5635a1 956 {
3624c875
FF
957 objfile -> ei.entry_file_lowpc = last_o_file_start;
958 objfile -> ei.entry_file_highpc = bufp->n_value;
bd5635a1
RP
959 }
960
961 if (pst)
962 {
963 end_psymtab (pst, psymtab_include_list, includes_used,
9d2b8d50
JK
964 symnum * symbol_size,
965 (lowest_text_address == (CORE_ADDR)-1
966 ? text_addr : lowest_text_address)
967 + text_size,
7e258d18 968 dependency_list, dependencies_used);
bd5635a1
RP
969 }
970
0eb22669 971 do_cleanups (back_to);
bd5635a1
RP
972}
973
4a35d6e9
FF
974/* Allocate and partially fill a partial symtab. It will be
975 completely filled at the end of the symbol list.
976
977 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
978 is the address relative to which its symbols are (incremental) or 0
979 (normal). */
980
bd5635a1 981
7e258d18 982struct partial_symtab *
2af231b8 983start_psymtab (objfile, section_offsets,
bd5635a1 984 filename, textlow, ldsymoff, global_syms, static_syms)
7d9884b9 985 struct objfile *objfile;
2af231b8 986 struct section_offsets *section_offsets;
bd5635a1
RP
987 char *filename;
988 CORE_ADDR textlow;
989 int ldsymoff;
990 struct partial_symbol *global_syms;
991 struct partial_symbol *static_syms;
992{
993 struct partial_symtab *result =
2af231b8 994 start_psymtab_common(objfile, section_offsets,
021959e2 995 filename, textlow, global_syms, static_syms);
bd5635a1 996
021959e2
JG
997 result->read_symtab_private = (char *)
998 obstack_alloc (&objfile -> psymbol_obstack, sizeof (struct symloc));
999 LDSYMOFF(result) = ldsymoff;
bd5635a1 1000 result->read_symtab = dbx_psymtab_to_symtab;
9342ecb9
JG
1001 SYMBOL_SIZE(result) = symbol_size;
1002 SYMBOL_OFFSET(result) = symbol_table_offset;
1003 STRING_OFFSET(result) = string_table_offset;
1004 FILE_STRING_OFFSET(result) = file_string_table_offset;
bd5635a1 1005
2af231b8
JG
1006 /* If we're handling an ELF file, drag some section-relocation info
1007 for this source file out of the ELF symbol table, to compensate for
1008 Sun brain death. This replaces the section_offsets in this psymtab,
1009 if successful. */
1010 elfstab_offset_sections (objfile, result);
1011
2e4964ad
FF
1012 /* Deduce the source language from the filename for this psymtab. */
1013 psymtab_language = deduce_language_from_filename (filename);
1014
bd5635a1
RP
1015 return result;
1016}
1017
cbba020f
PS
1018/* Close off the current usage of PST.
1019 Returns PST or NULL if the partial symtab was empty and thrown away.
bd5635a1 1020
cbba020f 1021 FIXME: List variables and peculiarities of same. */
bd5635a1 1022
cbba020f 1023struct partial_symtab *
bd5635a1 1024end_psymtab (pst, include_list, num_includes, capping_symbol_offset,
7e258d18 1025 capping_text, dependency_list, number_dependencies)
bd5635a1
RP
1026 struct partial_symtab *pst;
1027 char **include_list;
1028 int num_includes;
1029 int capping_symbol_offset;
1030 CORE_ADDR capping_text;
1031 struct partial_symtab **dependency_list;
1032 int number_dependencies;
bd5635a1
RP
1033{
1034 int i;
9342ecb9 1035 struct partial_symtab *p1;
021959e2 1036 struct objfile *objfile = pst -> objfile;
bd5635a1 1037
7e258d18
PB
1038 if (capping_symbol_offset != -1)
1039 LDSYMLEN(pst) = capping_symbol_offset - LDSYMOFF(pst);
bd5635a1
RP
1040 pst->texthigh = capping_text;
1041
6545c6a0 1042#ifdef N_SO_ADDRESS_MAYBE_MISSING
9342ecb9
JG
1043 /* Under Solaris, the N_SO symbols always have a value of 0,
1044 instead of the usual address of the .o file. Therefore,
1045 we have to do some tricks to fill in texthigh and textlow.
1046 The first trick is in partial-stab.h: if we see a static
1047 or global function, and the textlow for the current pst
1048 is still 0, then we use that function's address for
1049 the textlow of the pst.
1050
1051 Now, to fill in texthigh, we remember the last function seen
1052 in the .o file (also in partial-stab.h). Also, there's a hack in
1053 bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
1054 to here via the misc_info field. Therefore, we can fill in
1055 a reliable texthigh by taking the address plus size of the
1056 last function in the file.
1057
1058 Unfortunately, that does not cover the case where the last function
1059 in the file is static. See the paragraph below for more comments
1060 on this situation.
1061
1062 Finally, if we have a valid textlow for the current file, we run
1063 down the partial_symtab_list filling in previous texthighs that
1064 are still unknown. */
1065
bcbf9559 1066 if (pst->texthigh == 0 && last_function_name) {
9342ecb9
JG
1067 char *p;
1068 int n;
1069 struct minimal_symbol *minsym;
1070
1071 p = strchr (last_function_name, ':');
1072 if (p == NULL)
1073 p = last_function_name;
1074 n = p - last_function_name;
1075 p = alloca (n + 1);
1076 strncpy (p, last_function_name, n);
1077 p[n] = 0;
1078
1079 minsym = lookup_minimal_symbol (p, objfile);
1080
1081 if (minsym) {
2e4964ad 1082 pst->texthigh = SYMBOL_VALUE_ADDRESS (minsym) +
5573d7d4 1083 (long) MSYMBOL_INFO (minsym);
9342ecb9
JG
1084 } else {
1085 /* This file ends with a static function, and it's
1086 difficult to imagine how hard it would be to track down
1087 the elf symbol. Luckily, most of the time no one will notice,
1088 since the next file will likely be compiled with -g, so
1089 the code below will copy the first fuction's start address
1090 back to our texthigh variable. (Also, if this file is the
1091 last one in a dynamically linked program, texthigh already
1092 has the right value.) If the next file isn't compiled
1093 with -g, then the last function in this file winds up owning
1094 all of the text space up to the next -g file, or the end (minus
1095 shared libraries). This only matters for single stepping,
1096 and even then it will still work, except that it will single
1097 step through all of the covered functions, instead of setting
1098 breakpoints around them as it usualy does. This makes it
1099 pretty slow, but at least it doesn't fail.
1100
1101 We can fix this with a fairly big change to bfd, but we need
1102 to coordinate better with Cygnus if we want to do that. FIXME. */
1103 }
1104 last_function_name = NULL;
1105 }
1106
1107 /* this test will be true if the last .o file is only data */
1108 if (pst->textlow == 0)
6545c6a0
JK
1109 /* This loses if the text section really starts at address zero
1110 (generally true when we are debugging a .o file, for example).
9d2b8d50 1111 That is why this whole thing is inside N_SO_ADDRESS_MAYBE_MISSING. */
9342ecb9
JG
1112 pst->textlow = pst->texthigh;
1113
bcbf9559
JG
1114 /* If we know our own starting text address, then walk through all other
1115 psymtabs for this objfile, and if any didn't know their ending text
1116 address, set it to our starting address. Take care to not set our
1117 own ending address to our starting address, nor to set addresses on
1118 `dependency' files that have both textlow and texthigh zero. */
9342ecb9
JG
1119 if (pst->textlow) {
1120 ALL_OBJFILE_PSYMTABS (objfile, p1) {
bcbf9559 1121 if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst) {
9342ecb9
JG
1122 p1->texthigh = pst->textlow;
1123 /* if this file has only data, then make textlow match texthigh */
1124 if (p1->textlow == 0)
1125 p1->textlow = p1->texthigh;
1126 }
1127 }
1128 }
1129
1130 /* End of kludge for patching Solaris textlow and texthigh. */
9d2b8d50 1131#endif /* N_SO_ADDRESS_MAYBE_MISSING. */
9342ecb9 1132
bd5635a1 1133 pst->n_global_syms =
021959e2 1134 objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
bd5635a1 1135 pst->n_static_syms =
021959e2 1136 objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
bd5635a1
RP
1137
1138 pst->number_of_dependencies = number_dependencies;
1139 if (number_dependencies)
1140 {
1141 pst->dependencies = (struct partial_symtab **)
021959e2 1142 obstack_alloc (&objfile->psymbol_obstack,
bd5635a1 1143 number_dependencies * sizeof (struct partial_symtab *));
7e258d18 1144 memcpy (pst->dependencies, dependency_list,
bd5635a1
RP
1145 number_dependencies * sizeof (struct partial_symtab *));
1146 }
1147 else
1148 pst->dependencies = 0;
1149
1150 for (i = 0; i < num_includes; i++)
1151 {
bd5635a1 1152 struct partial_symtab *subpst =
021959e2 1153 allocate_psymtab (include_list[i], objfile);
7d9884b9 1154
2af231b8 1155 subpst->section_offsets = pst->section_offsets;
021959e2
JG
1156 subpst->read_symtab_private =
1157 (char *) obstack_alloc (&objfile->psymbol_obstack,
1158 sizeof (struct symloc));
4a35d6e9
FF
1159 LDSYMOFF(subpst) =
1160 LDSYMLEN(subpst) =
bd5635a1
RP
1161 subpst->textlow =
1162 subpst->texthigh = 0;
1163
3f83182d
JG
1164 /* We could save slight bits of space by only making one of these,
1165 shared by the entire set of include files. FIXME-someday. */
bd5635a1 1166 subpst->dependencies = (struct partial_symtab **)
021959e2 1167 obstack_alloc (&objfile->psymbol_obstack,
bd5635a1
RP
1168 sizeof (struct partial_symtab *));
1169 subpst->dependencies[0] = pst;
1170 subpst->number_of_dependencies = 1;
1171
1172 subpst->globals_offset =
1173 subpst->n_global_syms =
1174 subpst->statics_offset =
1175 subpst->n_static_syms = 0;
1176
1177 subpst->readin = 0;
9a822037 1178 subpst->symtab = 0;
2707b48a 1179 subpst->read_symtab = pst->read_symtab;
bd5635a1
RP
1180 }
1181
021959e2 1182 sort_pst_symbols (pst);
bd5635a1 1183
f9623881
JG
1184 /* If there is already a psymtab or symtab for a file of this name, remove it.
1185 (If there is a symtab, more drastic things also happen.)
1186 This happens in VxWorks. */
1187 free_named_symtabs (pst->filename);
1188
7d9884b9
JG
1189 if (num_includes == 0
1190 && number_dependencies == 0
1191 && pst->n_global_syms == 0
1192 && pst->n_static_syms == 0) {
1193 /* Throw away this psymtab, it's empty. We can't deallocate it, since
1194 it is on the obstack, but we can forget to chain it on the list. */
318bf84f
FF
1195 struct partial_symtab *prev_pst;
1196
1197 /* First, snip it out of the psymtab chain */
1198
1199 if (pst->objfile->psymtabs == pst)
1200 pst->objfile->psymtabs = pst->next;
1201 else
1202 for (prev_pst = pst->objfile->psymtabs; prev_pst; prev_pst = pst->next)
1203 if (prev_pst->next == pst)
1204 prev_pst->next = pst->next;
1205
1206 /* Next, put it on a free list for recycling */
1207
1208 pst->next = pst->objfile->free_psymtabs;
1209 pst->objfile->free_psymtabs = pst;
cbba020f
PS
1210
1211 /* Indicate that psymtab was thrown away. */
1212 pst = (struct partial_symtab *)NULL;
7d9884b9 1213 }
cbba020f 1214 return pst;
bd5635a1
RP
1215}
1216\f
1217static void
4c07f28d 1218dbx_psymtab_to_symtab_1 (pst)
bd5635a1 1219 struct partial_symtab *pst;
bd5635a1
RP
1220{
1221 struct cleanup *old_chain;
1222 int i;
1223
1224 if (!pst)
1225 return;
1226
1227 if (pst->readin)
1228 {
199b2450 1229 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
bd5635a1
RP
1230 pst->filename);
1231 return;
1232 }
1233
afe4ca15 1234 /* Read in all partial symtabs on which this one is dependent */
bd5635a1
RP
1235 for (i = 0; i < pst->number_of_dependencies; i++)
1236 if (!pst->dependencies[i]->readin)
1237 {
1238 /* Inform about additional files that need to be read in. */
1239 if (info_verbose)
1240 {
199b2450 1241 fputs_filtered (" ", gdb_stdout);
bd5635a1 1242 wrap_here ("");
199b2450 1243 fputs_filtered ("and ", gdb_stdout);
bd5635a1
RP
1244 wrap_here ("");
1245 printf_filtered ("%s...", pst->dependencies[i]->filename);
1246 wrap_here (""); /* Flush output */
199b2450 1247 gdb_flush (gdb_stdout);
bd5635a1 1248 }
4c07f28d 1249 dbx_psymtab_to_symtab_1 (pst->dependencies[i]);
bd5635a1
RP
1250 }
1251
4a35d6e9 1252 if (LDSYMLEN(pst)) /* Otherwise it's a dummy */
bd5635a1
RP
1253 {
1254 /* Init stuff necessary for reading in symbols */
3416d90b 1255 stabsread_init ();
c0302457 1256 buildsym_init ();
bd5635a1 1257 old_chain = make_cleanup (really_free_pendings, 0);
9342ecb9 1258 file_string_table_offset = FILE_STRING_OFFSET (pst);
4c07f28d
FF
1259 symbol_size = SYMBOL_SIZE (pst);
1260
1261 /* Read in this file's symbols */
2c7ab4ca 1262 bfd_seek (pst->objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
574dac8e 1263 read_ofile_symtab (pst);
9404978d 1264 sort_symtab_syms (pst->symtab);
bd5635a1
RP
1265
1266 do_cleanups (old_chain);
1267 }
1268
1269 pst->readin = 1;
1270}
1271
ac88ca20
JG
1272/* Read in all of the symbols for a given psymtab for real.
1273 Be verbose about it if the user wants that. */
1274
bd5635a1
RP
1275static void
1276dbx_psymtab_to_symtab (pst)
1277 struct partial_symtab *pst;
1278{
bd5635a1 1279 bfd *sym_bfd;
bd5635a1
RP
1280
1281 if (!pst)
1282 return;
1283
1284 if (pst->readin)
1285 {
199b2450 1286 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
bd5635a1
RP
1287 pst->filename);
1288 return;
1289 }
1290
4a35d6e9 1291 if (LDSYMLEN(pst) || pst->number_of_dependencies)
bd5635a1
RP
1292 {
1293 /* Print the message now, before reading the string table,
1294 to avoid disconcerting pauses. */
1295 if (info_verbose)
1296 {
1297 printf_filtered ("Reading in symbols for %s...", pst->filename);
199b2450 1298 gdb_flush (gdb_stdout);
bd5635a1
RP
1299 }
1300
7d9884b9 1301 sym_bfd = pst->objfile->obfd;
bd5635a1 1302
aab77d5f
PB
1303 next_symbol_text_func = dbx_next_symbol_text;
1304
4c07f28d 1305 dbx_psymtab_to_symtab_1 (pst);
bd5635a1
RP
1306
1307 /* Match with global symbols. This only needs to be done once,
1308 after all of the symtabs and dependencies have been read in. */
021959e2 1309 scan_file_globals (pst->objfile);
bd5635a1 1310
bd5635a1
RP
1311 /* Finish up the debug error message. */
1312 if (info_verbose)
1313 printf_filtered ("done.\n");
1314 }
1315}
1316
574dac8e 1317/* Read in a defined section of a specific object file's symbols. */
9342ecb9 1318
574dac8e
JK
1319static void
1320read_ofile_symtab (pst)
1321 struct partial_symtab *pst;
bd5635a1
RP
1322{
1323 register char *namestring;
7d9884b9 1324 register struct internal_nlist *bufp;
bd5635a1 1325 unsigned char type;
afe4ca15 1326 unsigned max_symnum;
7d9884b9 1327 register bfd *abfd;
574dac8e
JK
1328 struct objfile *objfile;
1329 int sym_offset; /* Offset to start of symbols to read */
1330 int sym_size; /* Size of symbols to read */
1331 CORE_ADDR text_offset; /* Start of text segment for symbols */
1332 int text_size; /* Size of text segment for symbols */
1333 struct section_offsets *section_offsets;
1334
1335 objfile = pst->objfile;
1336 sym_offset = LDSYMOFF(pst);
1337 sym_size = LDSYMLEN(pst);
1338 text_offset = pst->textlow;
1339 text_size = pst->texthigh - pst->textlow;
1340 section_offsets = pst->section_offsets;
7d9884b9 1341
021959e2 1342 current_objfile = objfile;
3416d90b 1343 subfile_stack = NULL;
bd5635a1 1344
3624c875 1345 stringtab_global = DBX_STRINGTAB (objfile);
3416d90b 1346 last_source_file = NULL;
bd5635a1 1347
7d9884b9
JG
1348 abfd = objfile->obfd;
1349 symfile_bfd = objfile->obfd; /* Implicit param to next_text_symbol */
bd5635a1
RP
1350 symbuf_end = symbuf_idx = 0;
1351
1352 /* It is necessary to actually read one symbol *before* the start
1353 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
1354 occurs before the N_SO symbol.
1355
1356 Detecting this in read_dbx_symtab
1357 would slow down initial readin, so we look for it here instead. */
9342ecb9 1358 if (!processing_acc_compilation && sym_offset >= (int)symbol_size)
bd5635a1 1359 {
2c7ab4ca 1360 bfd_seek (symfile_bfd, sym_offset - symbol_size, SEEK_CUR);
7d9884b9 1361 fill_symbuf (abfd);
bd5635a1 1362 bufp = &symbuf[symbuf_idx++];
7d9884b9 1363 SWAP_SYMBOL (bufp, abfd);
bd5635a1 1364
afe4ca15 1365 SET_NAMESTRING ();
bd5635a1 1366
1aed6766
SG
1367 processing_gcc_compilation = 0;
1368 if (bufp->n_type == N_TEXT)
1369 {
2e4964ad 1370 if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1aed6766 1371 processing_gcc_compilation = 1;
2e4964ad 1372 else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1aed6766
SG
1373 processing_gcc_compilation = 2;
1374 }
3416d90b
FF
1375
1376 /* Try to select a C++ demangling based on the compilation unit
1377 producer. */
1378
1379 if (processing_gcc_compilation)
1380 {
1aed6766 1381 if (AUTO_DEMANGLING)
3416d90b
FF
1382 {
1383 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1384 }
3416d90b 1385 }
bd5635a1
RP
1386 }
1387 else
1388 {
1389 /* The N_SO starting this symtab is the first symbol, so we
1390 better not check the symbol before it. I'm not this can
1391 happen, but it doesn't hurt to check for it. */
2c7ab4ca 1392 bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
bd5635a1
RP
1393 processing_gcc_compilation = 0;
1394 }
1395
1396 if (symbuf_idx == symbuf_end)
7d9884b9 1397 fill_symbuf (abfd);
bd5635a1
RP
1398 bufp = &symbuf[symbuf_idx];
1399 if (bufp->n_type != (unsigned char)N_SO)
1400 error("First symbol in segment of executable not a source symbol");
1401
afe4ca15
JG
1402 max_symnum = sym_size / symbol_size;
1403
bd5635a1 1404 for (symnum = 0;
afe4ca15 1405 symnum < max_symnum;
bd5635a1
RP
1406 symnum++)
1407 {
1408 QUIT; /* Allow this to be interruptable */
1409 if (symbuf_idx == symbuf_end)
7d9884b9 1410 fill_symbuf(abfd);
bd5635a1 1411 bufp = &symbuf[symbuf_idx++];
7d9884b9 1412 SWAP_SYMBOL (bufp, abfd);
bd5635a1 1413
c0302457 1414 type = bufp->n_type;
bd5635a1 1415
afe4ca15 1416 SET_NAMESTRING ();
bd5635a1 1417
7d9884b9 1418 if (type & N_STAB) {
c55e6167 1419 process_one_symbol (type, bufp->n_desc, bufp->n_value,
2af231b8 1420 namestring, section_offsets, objfile);
7d9884b9 1421 }
bd5635a1
RP
1422 /* We skip checking for a new .o or -l file; that should never
1423 happen in this routine. */
1aed6766 1424 else if (type == N_TEXT)
3416d90b
FF
1425 {
1426 /* I don't think this code will ever be executed, because
1427 the GCC_COMPILED_FLAG_SYMBOL usually is right before
1428 the N_SO symbol which starts this source file.
1429 However, there is no reason not to accept
1430 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
1aed6766 1431
2e4964ad 1432 if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1aed6766 1433 processing_gcc_compilation = 1;
2e4964ad 1434 else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1aed6766
SG
1435 processing_gcc_compilation = 2;
1436
1aed6766 1437 if (AUTO_DEMANGLING)
3416d90b
FF
1438 {
1439 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1440 }
3416d90b 1441 }
bd5635a1
RP
1442 else if (type & N_EXT || type == (unsigned char)N_TEXT
1443 || type == (unsigned char)N_NBTEXT
0c4d2cc2 1444 ) {
bd5635a1
RP
1445 /* Global symbol: see if we came across a dbx defintion for
1446 a corresponding symbol. If so, store the value. Remove
1447 syms from the chain when their values are stored, but
1448 search the whole chain, as there may be several syms from
1449 different files with the same name. */
1450 /* This is probably not true. Since the files will be read
1451 in one at a time, each reference to a global symbol will
1452 be satisfied in each file as it appears. So we skip this
1453 section. */
1454 ;
0c4d2cc2 1455 }
bd5635a1 1456 }
9404978d 1457
021959e2 1458 current_objfile = NULL;
9342ecb9
JG
1459
1460 /* In a Solaris elf file, this variable, which comes from the
1461 value of the N_SO symbol, will still be 0. Luckily, text_offset,
1462 which comes from pst->textlow is correct. */
1463 if (last_source_start_addr == 0)
1464 last_source_start_addr = text_offset;
1465
574dac8e
JK
1466 pst->symtab = end_symtab (text_offset + text_size, 0, 0, objfile,
1467 SECT_OFF_TEXT);
3416d90b 1468 end_stabs ();
bd5635a1 1469}
574dac8e 1470
bd5635a1 1471\f
c55e6167
JG
1472/* This handles a single symbol from the symbol-file, building symbols
1473 into a GDB symtab. It takes these arguments and an implicit argument.
1474
1475 TYPE is the type field of the ".stab" symbol entry.
1476 DESC is the desc field of the ".stab" entry.
1477 VALU is the value field of the ".stab" entry.
1478 NAME is the symbol name, in our address space.
2af231b8
JG
1479 SECTION_OFFSETS is a set of amounts by which the sections of this object
1480 file were relocated when it was loaded into memory.
1481 All symbols that refer
1482 to memory locations need to be offset by these amounts.
9342ecb9 1483 OBJFILE is the object file from which we are reading symbols.
c55e6167
JG
1484 It is used in end_symtab. */
1485
7e258d18 1486void
2af231b8 1487process_one_symbol (type, desc, valu, name, section_offsets, objfile)
bd5635a1
RP
1488 int type, desc;
1489 CORE_ADDR valu;
1490 char *name;
2af231b8 1491 struct section_offsets *section_offsets;
9342ecb9 1492 struct objfile *objfile;
bd5635a1 1493{
a5e6391b
JK
1494#ifdef SUN_FIXED_LBRAC_BUG
1495 /* If SUN_FIXED_LBRAC_BUG is defined, then it tells us whether we need
1496 to correct the address of N_LBRAC's. If it is not defined, then
1497 we never need to correct the addresses. */
1498
0cf9329b 1499 /* This records the last pc address we've seen. We depend on there being
bd5635a1
RP
1500 an SLINE or FUN or SO before the first LBRAC, since the variable does
1501 not get reset in between reads of different symbol files. */
1502 static CORE_ADDR last_pc_address;
a5e6391b 1503#endif
8357834f 1504
bd5635a1 1505 register struct context_stack *new;
9342ecb9
JG
1506 /* This remembers the address of the start of a function. It is used
1507 because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries are
1508 relative to the current function's start address. On systems
2af231b8
JG
1509 other than Solaris 2, this just holds the SECT_OFF_TEXT value, and is
1510 used to relocate these symbol types rather than SECTION_OFFSETS. */
9342ecb9 1511 static CORE_ADDR function_start_offset;
bd5635a1 1512
574dac8e
JK
1513 /* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are relative
1514 to the function start address. */
1515 int block_address_function_relative;
1516
8357834f 1517 /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this source
b8ec9a79 1518 file. Used to detect the SunPRO solaris compiler. */
4d57c599 1519 static int n_opt_found;
8357834f 1520
b8ec9a79
JK
1521 /* The stab type used for the definition of the last function.
1522 N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers. */
1523 static int function_stab_type = 0;
1524
9d2b8d50
JK
1525 /* This is true for Solaris (and all other systems which put stabs
1526 in sections, hopefully, since it would be silly to do things
1527 differently from Solaris), and false for SunOS4 and other a.out
1528 file formats. */
574dac8e 1529 block_address_function_relative =
9d2b8d50
JK
1530 ((0 == strncmp (bfd_get_target (objfile->obfd), "elf", 3))
1531 || (0 == strncmp (bfd_get_target (objfile->obfd), "som", 3))
1532 || (0 == strncmp (bfd_get_target (objfile->obfd), "coff", 4)));
574dac8e
JK
1533
1534 if (!block_address_function_relative)
1535 /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
1536 function start address, so just use the text offset. */
1537 function_start_offset = ANOFFSET (section_offsets, SECT_OFF_TEXT);
51b80b00 1538
bd5635a1
RP
1539 /* Something is wrong if we see real data before
1540 seeing a source file name. */
1541
3416d90b 1542 if (last_source_file == NULL && type != (unsigned char)N_SO)
bd5635a1 1543 {
a5e6391b
JK
1544 /* Ignore any symbols which appear before an N_SO symbol. Currently
1545 no one puts symbols there, but we should deal gracefully with the
1546 case. A complain()t might be in order (if !IGNORE_SYMBOL (type)),
1547 but this should not be an error (). */
1548 return;
bd5635a1
RP
1549 }
1550
1551 switch (type)
1552 {
1553 case N_FUN:
1554 case N_FNAME:
2af231b8
JG
1555 /* Relocate for dynamic loading */
1556 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
b8ec9a79 1557 goto define_a_symbol;
bd5635a1 1558
bd5635a1
RP
1559 case N_LBRAC:
1560 /* This "symbol" just indicates the start of an inner lexical
1561 context within a function. */
1562
574dac8e
JK
1563#if defined(BLOCK_ADDRESS_ABSOLUTE)
1564 /* Relocate for dynamic loading (?). */
9342ecb9 1565 valu += function_start_offset;
c55e6167 1566#else
574dac8e
JK
1567 if (block_address_function_relative)
1568 /* Relocate for Sun ELF acc fn-relative syms. */
1569 valu += function_start_offset;
1570 else
1571 /* On most machines, the block addresses are relative to the
1572 N_SO, the linker did not relocate them (sigh). */
1573 valu += last_source_start_addr;
bd5635a1
RP
1574#endif
1575
a5e6391b 1576#ifdef SUN_FIXED_LBRAC_BUG
8357834f 1577 if (!SUN_FIXED_LBRAC_BUG && valu < last_pc_address) {
bd5635a1 1578 /* Patch current LBRAC pc value to match last handy pc value */
51b80b00 1579 complain (&lbrac_complaint);
bd5635a1
RP
1580 valu = last_pc_address;
1581 }
a5e6391b 1582#endif
7d9884b9 1583 new = push_context (desc, valu);
bd5635a1
RP
1584 break;
1585
1586 case N_RBRAC:
1587 /* This "symbol" just indicates the end of an inner lexical
1588 context that was started with N_LBRAC. */
1589
574dac8e
JK
1590#if defined(BLOCK_ADDRESS_ABSOLUTE)
1591 /* Relocate for dynamic loading (?). */
9342ecb9 1592 valu += function_start_offset;
c55e6167 1593#else
574dac8e
JK
1594 if (block_address_function_relative)
1595 /* Relocate for Sun ELF acc fn-relative syms. */
1596 valu += function_start_offset;
1597 else
1598 /* On most machines, the block addresses are relative to the
1599 N_SO, the linker did not relocate them (sigh). */
1600 valu += last_source_start_addr;
bd5635a1
RP
1601#endif
1602
7d9884b9 1603 new = pop_context();
bd5635a1 1604 if (desc != new->depth)
51b80b00 1605 complain (&lbrac_mismatch_complaint, symnum);
bd5635a1
RP
1606
1607 /* Some compilers put the variable decls inside of an
1608 LBRAC/RBRAC block. This macro should be nonzero if this
1609 is true. DESC is N_DESC from the N_RBRAC symbol.
0cf9329b
PB
1610 GCC_P is true if we've detected the GCC_COMPILED_SYMBOL
1611 or the GCC2_COMPILED_SYMBOL. */
bd5635a1
RP
1612#if !defined (VARIABLES_INSIDE_BLOCK)
1613#define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
1614#endif
1615
1616 /* Can only use new->locals as local symbols here if we're in
1617 gcc or on a machine that puts them before the lbrack. */
1618 if (!VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1619 local_symbols = new->locals;
1620
2f8c3639
JL
1621 if (context_stack_depth
1622 > !VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
bd5635a1 1623 {
2f8c3639
JL
1624 /* This is not the outermost LBRAC...RBRAC pair in the function,
1625 its local symbols preceded it, and are the ones just recovered
1626 from the context stack. Define the block for them (but don't
1627 bother if the block contains no symbols. Should we complain
1628 on blocks without symbols? I can't think of any useful purpose
1629 for them). */
1630 if (local_symbols != NULL)
bd5635a1 1631 {
2f8c3639
JL
1632 /* Muzzle a compiler bug that makes end < start. (which
1633 compilers? Is this ever harmful?). */
1634 if (new->start_addr > valu)
1635 {
1636 complain (&lbrac_rbrac_complaint);
1637 new->start_addr = valu;
1638 }
1639 /* Make a block for the local symbols within. */
1640 finish_block (0, &local_symbols, new->old_blocks,
1641 new->start_addr, valu, objfile);
bd5635a1 1642 }
bd5635a1
RP
1643 }
1644 else
1645 {
2f8c3639
JL
1646 /* This is the outermost LBRAC...RBRAC pair. There is no
1647 need to do anything; leave the symbols that preceded it
1648 to be attached to the function's own block. We need to
1649 indicate that we just moved outside of the function. */
bd5635a1
RP
1650 within_function = 0;
1651 }
2f8c3639 1652
bd5635a1
RP
1653 if (VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1654 /* Now pop locals of block just finished. */
1655 local_symbols = new->locals;
1656 break;
1657
9bb30452 1658 case N_FN:
6150cc73 1659 case N_FN_SEQ:
9bb30452 1660 /* This kind of symbol indicates the start of an object file. */
2af231b8
JG
1661 /* Relocate for dynamic loading */
1662 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
bd5635a1
RP
1663 break;
1664
1665 case N_SO:
1666 /* This type of symbol indicates the start of data
1667 for one source file.
1668 Finish the symbol table of the previous source file
1669 (if any) and start accumulating a new symbol table. */
2af231b8
JG
1670 /* Relocate for dynamic loading */
1671 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
c55e6167 1672
8357834f
JK
1673 n_opt_found = 0;
1674
a5e6391b 1675#ifdef SUN_FIXED_LBRAC_BUG
bd5635a1 1676 last_pc_address = valu; /* Save for SunOS bug circumcision */
a5e6391b 1677#endif
8357834f 1678
bd5635a1
RP
1679#ifdef PCC_SOL_BROKEN
1680 /* pcc bug, occasionally puts out SO for SOL. */
1681 if (context_stack_depth > 0)
1682 {
1683 start_subfile (name, NULL);
1684 break;
1685 }
1686#endif
1687 if (last_source_file)
7e258d18
PB
1688 {
1689 /* Check if previous symbol was also an N_SO (with some
1690 sanity checks). If so, that one was actually the directory
1691 name, and the current one is the real file name.
1692 Patch things up. */
6985bc54 1693 if (previous_stab_code == (unsigned char) N_SO)
7e258d18 1694 {
3416d90b 1695 patch_subfile_names (current_subfile, name);
c72af089 1696 break; /* Ignore repeated SOs */
7e258d18 1697 }
65ce5df4 1698 end_symtab (valu, 0, 0, objfile, SECT_OFF_TEXT);
3416d90b 1699 end_stabs ();
7e258d18 1700 }
3416d90b 1701 start_stabs ();
bd5635a1
RP
1702 start_symtab (name, NULL, valu);
1703 break;
1704
c55e6167 1705
bd5635a1
RP
1706 case N_SOL:
1707 /* This type of symbol indicates the start of data for
1708 a sub-source-file, one whose contents were copied or
1709 included in the compilation of the main source file
1710 (whose name was given in the N_SO symbol.) */
2af231b8
JG
1711 /* Relocate for dynamic loading */
1712 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
784fd92b 1713 start_subfile (name, current_subfile->dirname);
bd5635a1
RP
1714 break;
1715
1716 case N_BINCL:
1717 push_subfile ();
1718 add_new_header_file (name, valu);
784fd92b 1719 start_subfile (name, current_subfile->dirname);
bd5635a1
RP
1720 break;
1721
1722 case N_EINCL:
784fd92b 1723 start_subfile (pop_subfile (), current_subfile->dirname);
bd5635a1
RP
1724 break;
1725
1726 case N_EXCL:
1727 add_old_header_file (name, valu);
1728 break;
1729
1730 case N_SLINE:
1731 /* This type of "symbol" really just records
1732 one line-number -- core-address correspondence.
1733 Enter it in the line list for this symbol table. */
9342ecb9
JG
1734 /* Relocate for dynamic loading and for ELF acc fn-relative syms. */
1735 valu += function_start_offset;
a5e6391b 1736#ifdef SUN_FIXED_LBRAC_BUG
bd5635a1 1737 last_pc_address = valu; /* Save for SunOS bug circumcision */
a5e6391b 1738#endif
4137c5fc 1739 record_line (current_subfile, desc, valu);
bd5635a1
RP
1740 break;
1741
1742 case N_BCOMM:
4d57c599 1743 common_block_start (name, objfile);
bd5635a1
RP
1744 break;
1745
1746 case N_ECOMM:
4d57c599
JK
1747 common_block_end (objfile);
1748 break;
bd5635a1 1749
2af231b8
JG
1750 /* The following symbol types need to have the appropriate offset added
1751 to their value; then we process symbol definitions in the name. */
1752
1753 case N_STSYM: /* Static symbol in data seg */
1754 case N_LCSYM: /* Static symbol in BSS seg */
1755 case N_ROSYM: /* Static symbol in Read-only data seg */
4d57c599
JK
1756 /* HORRID HACK DEPT. However, it's Sun's furgin' fault.
1757 Solaris2's stabs-in-elf makes *most* symbols relative
1758 but leaves a few absolute (at least for Solaris 2.1 and version
1759 2.0.1 of the SunPRO compiler). N_STSYM and friends sit on the fence.
2af231b8
JG
1760 .stab "foo:S...",N_STSYM is absolute (ld relocates it)
1761 .stab "foo:V...",N_STSYM is relative (section base subtracted).
1762 This leaves us no choice but to search for the 'S' or 'V'...
1763 (or pass the whole section_offsets stuff down ONE MORE function
4d57c599 1764 call level, which we really don't want to do). */
2af231b8
JG
1765 {
1766 char *p;
1767 p = strchr (name, ':');
1768 if (p != 0 && p[1] == 'S')
1769 {
c7d4c4c8
JK
1770 /* The linker relocated it. We don't want to add an
1771 elfstab_offset_sections-type offset, but we *do* want
1772 to add whatever solib.c passed to symbol_file_add as
1773 addr (this is known to affect SunOS4, and I suspect ELF
1774 too). Since elfstab_offset_sections currently does not
1775 muck with the text offset (there is no Ttext.text
1776 symbol), we can get addr from the text offset. If
1777 elfstab_offset_sections ever starts dealing with the
1778 text offset, and we still need to do this, we need to
1779 invent a SECT_OFF_ADDR_KLUDGE or something. */
1780 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
2af231b8
JG
1781 goto define_a_symbol;
1782 }
1783 /* Since it's not the kludge case, re-dispatch to the right handler. */
1784 switch (type) {
1785 case N_STSYM: goto case_N_STSYM;
1786 case N_LCSYM: goto case_N_LCSYM;
1787 case N_ROSYM: goto case_N_ROSYM;
1788 default: abort();
1789 }
1790 }
1791
1792 case_N_STSYM: /* Static symbol in data seg */
c55e6167 1793 case N_DSLINE: /* Source line number, data seg */
2af231b8
JG
1794 valu += ANOFFSET (section_offsets, SECT_OFF_DATA);
1795 goto define_a_symbol;
1796
1797 case_N_LCSYM: /* Static symbol in BSS seg */
c55e6167
JG
1798 case N_BSLINE: /* Source line number, bss seg */
1799 /* N_BROWS: overlaps with N_BSLINE */
2af231b8
JG
1800 valu += ANOFFSET (section_offsets, SECT_OFF_BSS);
1801 goto define_a_symbol;
1802
1803 case_N_ROSYM: /* Static symbol in Read-only data seg */
1804 valu += ANOFFSET (section_offsets, SECT_OFF_RODATA);
1805 goto define_a_symbol;
1806
c55e6167 1807 case N_ENTRY: /* Alternate entry point */
2af231b8
JG
1808 /* Relocate for dynamic loading */
1809 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1810 goto define_a_symbol;
c55e6167 1811
4f470205
JK
1812 /* The following symbol types we don't know how to process. Handle
1813 them in a "default" way, but complain to people who care. */
1814 default:
1815 case N_CATCH: /* Exception handler catcher */
1816 case N_EHDECL: /* Exception handler name */
1817 case N_PC: /* Global symbol in Pascal */
1818 case N_M2C: /* Modula-2 compilation unit */
1819 /* N_MOD2: overlaps with N_EHDECL */
1820 case N_SCOPE: /* Modula-2 scope information */
1821 case N_ECOML: /* End common (local name) */
1822 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
1823 case N_NBDATA:
1824 case N_NBBSS:
1825 case N_NBSTS:
1826 case N_NBLCS:
9d2b8d50 1827 complain (&unknown_symtype_complaint, local_hex_string (type));
4f470205
JK
1828 /* FALLTHROUGH */
1829
c55e6167
JG
1830 /* The following symbol types don't need the address field relocated,
1831 since it is either unused, or is absolute. */
2af231b8 1832 define_a_symbol:
c55e6167
JG
1833 case N_GSYM: /* Global variable */
1834 case N_NSYMS: /* Number of symbols (ultrix) */
1835 case N_NOMAP: /* No map? (ultrix) */
1836 case N_RSYM: /* Register variable */
1837 case N_DEFD: /* Modula-2 GNU module dependency */
1838 case N_SSYM: /* Struct or union element */
1839 case N_LSYM: /* Local symbol in stack */
1840 case N_PSYM: /* Parameter variable */
1841 case N_LENG: /* Length of preceding symbol type */
1842 if (name)
4f470205 1843 {
b8ec9a79
JK
1844 int deftype;
1845 char *colon_pos = strchr (name, ':');
1846 if (colon_pos == NULL)
1847 deftype = '\0';
1848 else
1849 deftype = colon_pos[1];
1850
1851 switch (deftype)
4f470205 1852 {
b8ec9a79
JK
1853 case 'f':
1854 case 'F':
1855 function_stab_type = type;
1856
3ef0fc8c 1857#ifdef SUN_FIXED_LBRAC_BUG
b8ec9a79
JK
1858 /* The Sun acc compiler, under SunOS4, puts out
1859 functions with N_GSYM or N_STSYM. The problem is
1860 that the address of the symbol is no good (for N_GSYM
1861 it doesn't even attept an address; for N_STSYM it
1862 puts out an address but then it gets relocated
1863 relative to the data segment, not the text segment).
1864 Currently we can't fix this up later as we do for
1865 some types of symbol in scan_file_globals.
1866 Fortunately we do have a way of finding the address -
1867 we know that the value in last_pc_address is either
1868 the one we want (if we're dealing with the first
1869 function in an object file), or somewhere in the
1870 previous function. This means that we can use the
1871 minimal symbol table to get the address. */
1872
8adcfb97
JK
1873 /* On solaris up to 2.2, the N_FUN stab gets relocated.
1874 On Solaris 2.3, ld no longer relocates stabs (which
1875 is good), and the N_FUN's value is now always zero.
5573d7d4
JK
1876 The following code can't deal with this, because
1877 last_pc_address depends on getting the address from a
1878 N_SLINE or some such and in Solaris those are function
1879 relative. Best fix is probably to create a Ttext.text symbol
1880 and handle this like Ddata.data and so on. */
8adcfb97 1881
5573d7d4 1882 if (type == N_GSYM || type == N_STSYM)
b8ec9a79
JK
1883 {
1884 struct minimal_symbol *m;
1885 int l = colon_pos - name;
1886
1887 m = lookup_minimal_symbol_by_pc (last_pc_address);
1888 if (m && STREQN (SYMBOL_NAME (m), name, l))
1889 /* last_pc_address was in this function */
1890 valu = SYMBOL_VALUE (m);
3c7d3064
JK
1891 else if (m && STREQN (SYMBOL_NAME (m+1), name, l))
1892 /* last_pc_address was in last function */
1893 valu = SYMBOL_VALUE (m+1);
b8ec9a79 1894 else
3c7d3064
JK
1895 /* Not found - use last_pc_address (for finish_block) */
1896 valu = last_pc_address;
b8ec9a79
JK
1897 }
1898
b8ec9a79
JK
1899 last_pc_address = valu; /* Save for SunOS bug circumcision */
1900#endif
1901
1902 if (block_address_function_relative)
1903 /* For Solaris 2.0 compilers, the block addresses and
1904 N_SLINE's are relative to the start of the
1905 function. On normal systems, and when using gcc on
1906 Solaris 2.0, these addresses are just absolute, or
1907 relative to the N_SO, depending on
1908 BLOCK_ADDRESS_ABSOLUTE. */
1909 function_start_offset = valu;
1910
1911 within_function = 1;
1912 if (context_stack_depth > 0)
1913 {
1914 new = pop_context ();
1915 /* Make a block for the local symbols within. */
1916 finish_block (new->name, &local_symbols, new->old_blocks,
1917 new->start_addr, valu, objfile);
1918 }
1919 /* Stack must be empty now. */
1920 if (context_stack_depth != 0)
1921 complain (&lbrac_unmatched_complaint, symnum);
1922
1923 new = push_context (0, valu);
1924 new->name = define_symbol (valu, name, desc, type, objfile);
1925 break;
1926
1927 default:
1928 define_symbol (valu, name, desc, type, objfile);
1929 break;
4f470205
JK
1930 }
1931 }
bd5635a1
RP
1932 break;
1933
ec8ceca3
JG
1934 /* We use N_OPT to carry the gcc2_compiled flag. Sun uses it
1935 for a bunch of other flags, too. Someday we may parse their
1936 flags; for now we ignore theirs and hope they'll ignore ours. */
1937 case N_OPT: /* Solaris 2: Compiler options */
1938 if (name)
1939 {
2e4964ad 1940 if (STREQ (name, GCC2_COMPILED_FLAG_SYMBOL))
3416d90b 1941 {
1aed6766 1942 processing_gcc_compilation = 2;
3416d90b 1943#if 1 /* Works, but is experimental. -fnf */
1aed6766 1944 if (AUTO_DEMANGLING)
3416d90b
FF
1945 {
1946 set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1947 }
1948#endif
1949 }
8357834f
JK
1950 else
1951 n_opt_found = 1;
ec8ceca3
JG
1952 }
1953 break;
1954
bcbf9559
JG
1955 /* The following symbol types can be ignored. */
1956 case N_OBJ: /* Solaris 2: Object file dir and name */
bcbf9559
JG
1957 /* N_UNDF: Solaris 2: file separator mark */
1958 /* N_UNDF: -- we will never encounter it, since we only process one
1959 file's symbols at once. */
4c7c6bab
JG
1960 case N_ENDM: /* Solaris 2: End of module */
1961 case N_MAIN: /* Name of main routine. */
9342ecb9 1962 break;
bd5635a1 1963 }
7e258d18
PB
1964
1965 previous_stab_code = type;
bd5635a1
RP
1966}
1967\f
965a5c32
SS
1968/* FIXME: The only difference between this and elfstab_build_psymtabs is
1969 the call to install_minimal_symbols for elf. If the differences are
1970 really that small, the code should be shared. */
1971
b5b186a2
SS
1972/* Scan and build partial symbols for an coff symbol file.
1973 The coff file has already been processed to get its minimal symbols.
1974
1975 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
1976 rolled into one.
1977
1978 OBJFILE is the object file we are reading symbols from.
1979 ADDR is the address relative to which the symbols are (e.g.
1980 the base address of the text segment).
1981 MAINLINE is true if we are reading the main symbol
1982 table (as opposed to a shared lib or dynamically loaded file).
1983 STABOFFSET and STABSIZE define the location in OBJFILE where the .stab
1984 section exists.
1985 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
1986 .stabstr section exists.
1987
1988 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
1989 adjusted for coff details. */
1990
1991void
1992coffstab_build_psymtabs (objfile, section_offsets, mainline,
1993 staboffset, stabsize,
1994 stabstroffset, stabstrsize)
1995 struct objfile *objfile;
1996 struct section_offsets *section_offsets;
1997 int mainline;
1998 file_ptr staboffset;
1999 unsigned int stabsize;
2000 file_ptr stabstroffset;
2001 unsigned int stabstrsize;
2002{
2003 int val;
2004 bfd *sym_bfd = objfile->obfd;
2005 char *name = bfd_get_filename (sym_bfd);
2006 struct dbx_symfile_info *info;
2007
2008 /* There is already a dbx_symfile_info allocated by our caller.
2009 It might even contain some info from the coff symtab to help us. */
965a5c32 2010 info = (struct dbx_symfile_info *) objfile->sym_stab_info;
b5b186a2
SS
2011
2012 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
2013 if (!DBX_TEXT_SECT (objfile))
2014 error ("Can't find .text section in symbol file");
2015
2016#define COFF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
2017 DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
2018 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
2019 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
2020 DBX_SYMTAB_OFFSET (objfile) = staboffset;
2021
2022 if (stabstrsize > bfd_get_size (sym_bfd))
2023 error ("ridiculous string table size: %d bytes", stabstrsize);
2024 DBX_STRINGTAB (objfile) = (char *)
2025 obstack_alloc (&objfile->psymbol_obstack, stabstrsize+1);
2026
2027 /* Now read in the string table in one big gulp. */
2028
2029 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
2030 if (val < 0)
2031 perror_with_name (name);
2032 val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2033 if (val != stabstrsize)
2034 perror_with_name (name);
2035
2036 stabsread_new_init ();
2037 buildsym_new_init ();
2038 free_header_files ();
2039 init_header_files ();
2040
2041 processing_acc_compilation = 1;
2042
2043 /* In a coff file, we've already installed the minimal symbols that came
2044 from the coff (non-stab) symbol table, so always act like an
2045 incremental load here. */
2046 dbx_symfile_read (objfile, section_offsets, 0);
2047}
2048\f
9342ecb9
JG
2049/* Scan and build partial symbols for an ELF symbol file.
2050 This ELF file has already been processed to get its minimal symbols,
2051 and any DWARF symbols that were in it.
2052
2053 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2054 rolled into one.
2055
2056 OBJFILE is the object file we are reading symbols from.
2057 ADDR is the address relative to which the symbols are (e.g.
2058 the base address of the text segment).
2059 MAINLINE is true if we are reading the main symbol
2060 table (as opposed to a shared lib or dynamically loaded file).
2061 STABOFFSET and STABSIZE define the location in OBJFILE where the .stab
2062 section exists.
2063 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2064 .stabstr section exists.
2065
2066 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2067 adjusted for elf details. */
2068
2069void
1aed6766 2070elfstab_build_psymtabs (objfile, section_offsets, mainline,
9342ecb9 2071 staboffset, stabsize,
1aed6766
SG
2072 stabstroffset, stabstrsize)
2073 struct objfile *objfile;
2074 struct section_offsets *section_offsets;
2075 int mainline;
51b80b00 2076 file_ptr staboffset;
1aed6766 2077 unsigned int stabsize;
51b80b00 2078 file_ptr stabstroffset;
1aed6766 2079 unsigned int stabstrsize;
9342ecb9
JG
2080{
2081 int val;
2082 bfd *sym_bfd = objfile->obfd;
2083 char *name = bfd_get_filename (sym_bfd);
2084 struct dbx_symfile_info *info;
2085
2af231b8
JG
2086 /* There is already a dbx_symfile_info allocated by our caller.
2087 It might even contain some info from the ELF symtab to help us. */
965a5c32 2088 info = (struct dbx_symfile_info *) objfile->sym_stab_info;
9342ecb9
JG
2089
2090 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
2091 if (!DBX_TEXT_SECT (objfile))
2092 error ("Can't find .text section in symbol file");
2093
2094#define ELF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
2095 DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
2096 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
2097 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
2098 DBX_SYMTAB_OFFSET (objfile) = staboffset;
2099
996ccb30 2100 if (stabstrsize > bfd_get_size (sym_bfd))
9342ecb9
JG
2101 error ("ridiculous string table size: %d bytes", stabstrsize);
2102 DBX_STRINGTAB (objfile) = (char *)
2103 obstack_alloc (&objfile->psymbol_obstack, stabstrsize+1);
2104
2105 /* Now read in the string table in one big gulp. */
2106
2c7ab4ca 2107 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
9342ecb9
JG
2108 if (val < 0)
2109 perror_with_name (name);
2110 val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2111 if (val != stabstrsize)
2112 perror_with_name (name);
2113
3416d90b 2114 stabsread_new_init ();
9342ecb9
JG
2115 buildsym_new_init ();
2116 free_header_files ();
2117 init_header_files ();
2118 install_minimal_symbols (objfile);
2119
2120 processing_acc_compilation = 1;
2121
2122 /* In an elf file, we've already installed the minimal symbols that came
2123 from the elf (non-stab) symbol table, so always act like an
2124 incremental load here. */
2af231b8
JG
2125 dbx_symfile_read (objfile, section_offsets, 0);
2126}
2127\f
040b9597
RP
2128/* Scan and build partial symbols for a PA symbol file.
2129 This PA file has already been processed to get its minimal symbols.
2130
2131 OBJFILE is the object file we are reading symbols from.
2132 ADDR is the address relative to which the symbols are (e.g.
2133 the base address of the text segment).
2134 MAINLINE is true if we are reading the main symbol
2135 table (as opposed to a shared lib or dynamically loaded file).
2136
2137 */
2138
2139void
2140pastab_build_psymtabs (objfile, section_offsets, mainline)
2141 struct objfile *objfile;
2142 struct section_offsets *section_offsets;
2143 int mainline;
2144{
2145 free_header_files ();
2146 init_header_files ();
2147
2f8c3639
JL
2148 /* This is needed to debug objects assembled with gas2. */
2149 processing_acc_compilation = 1;
2150
040b9597
RP
2151 /* In a PA file, we've already installed the minimal symbols that came
2152 from the PA (non-stab) symbol table, so always act like an
2153 incremental load here. */
2154
2155 dbx_symfile_read (objfile, section_offsets, mainline);
2156}
2157\f
2af231b8
JG
2158/* Parse the user's idea of an offset for dynamic linking, into our idea
2159 of how to represent it for fast symbol reading. */
2160
040b9597 2161static struct section_offsets *
2af231b8
JG
2162dbx_symfile_offsets (objfile, addr)
2163 struct objfile *objfile;
2164 CORE_ADDR addr;
2165{
2166 struct section_offsets *section_offsets;
2167 int i;
4d57c599
JK
2168
2169 objfile->num_sections = SECT_OFF_MAX;
2af231b8
JG
2170 section_offsets = (struct section_offsets *)
2171 obstack_alloc (&objfile -> psymbol_obstack,
4d57c599
JK
2172 sizeof (struct section_offsets)
2173 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
2af231b8
JG
2174
2175 for (i = 0; i < SECT_OFF_MAX; i++)
2176 ANOFFSET (section_offsets, i) = addr;
2177
2178 return section_offsets;
9342ecb9
JG
2179}
2180\f
80d68b1d
FF
2181static struct sym_fns aout_sym_fns =
2182{
0eed42de 2183 bfd_target_aout_flavour,
80d68b1d
FF
2184 dbx_new_init, /* sym_new_init: init anything gbl to entire symtab */
2185 dbx_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2186 dbx_symfile_read, /* sym_read: read a symbol file into symtab */
2187 dbx_symfile_finish, /* sym_finish: finished with file, cleanup */
2af231b8 2188 dbx_symfile_offsets, /* sym_offsets: parse user's offsets to internal form */
80d68b1d
FF
2189 NULL /* next: pointer to next struct sym_fns */
2190};
bd5635a1
RP
2191
2192void
2193_initialize_dbxread ()
2194{
bd5635a1 2195 add_symtab_fns(&aout_sym_fns);
bd5635a1 2196}
This page took 0.268522 seconds and 4 git commands to generate.