Add and use definition for IN_SOLIB_TRAMPOLINE which allows wait_for_inferior
[deliverable/binutils-gdb.git] / gdb / dbxread.c
CommitLineData
bd5635a1 1/* Read dbx symbol tables and convert to internal format, for GDB.
c55e6167 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
c3a21801 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
c3a21801
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
c3a21801 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
c3a21801
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
9404978d
MT
19
20/* This module provides three functions: dbx_symfile_init,
21 which initializes to read a symbol file; dbx_new_init, which
22 discards existing cached information when all symbols are being
23 discarded; and dbx_symfile_read, which reads a symbol table
24 from a file.
25
26 dbx_symfile_read only does the minimum work necessary for letting the
27 user "name" things symbolically; it does not read the entire symtab.
28 Instead, it reads the external and static symbols and puts them in partial
29 symbol tables. When more extensive information is requested of a
30 file, the corresponding partial symbol table is mutated into a full
31 fledged symbol table by going back and reading the symbols
32 for real. dbx_psymtab_to_symtab() is the function that does this */
bd5635a1 33
bd5635a1 34#include "defs.h"
318bf84f 35#include <string.h>
bd5635a1
RP
36
37#ifdef USG
38#include <sys/types.h>
39#include <fcntl.h>
40#define L_SET 0
41#define L_INCR 1
42#endif
43
afe4ca15
JG
44#include <obstack.h>
45#include <sys/param.h>
021959e2 46#ifndef NO_SYS_FILE
afe4ca15 47#include <sys/file.h>
021959e2 48#endif
afe4ca15 49#include <sys/stat.h>
bd5635a1 50#include <ctype.h>
afe4ca15
JG
51#include "symtab.h"
52#include "breakpoint.h"
53#include "command.h"
54#include "target.h"
55#include "gdbcore.h" /* for bfd stuff */
56#include "libaout.h" /* FIXME Secret internal BFD stuff for a.out */
57#include "symfile.h"
c0302457 58#include "buildsym.h"
afe4ca15 59
7e258d18
PB
60#include "aout/aout64.h"
61#include "aout/stab_gnu.h" /* We always use GNU stabs, not native, now */
bd5635a1 62
c0302457
JG
63/* Information is passed among various dbxread routines for accessing
64 symbol files. A pointer to this structure is kept in the sym_private
65 field of the struct sym_fns passed in by symfile.h. */
66
bd5635a1
RP
67struct dbx_symfile_info {
68 asection *text_sect; /* Text section accessor */
69 int symcount; /* How many symbols are there in the file */
70 char *stringtab; /* The actual string table */
71 int stringtab_size; /* Its size */
72 off_t symtab_offset; /* Offset in file to symbol table */
bd5635a1
RP
73};
74
c0302457 75
4a35d6e9
FF
76/* Each partial symbol table entry contains a pointer to private data for the
77 read_symtab() function to use when expanding a partial symbol table entry
78 to a full symbol table entry.
79
80 For dbxread this structure contains the offset within the file symbol table
81 of first local symbol for this file, and length (in bytes) of the section
82 of the symbol table devoted to this file's symbols (actually, the section
83 bracketed may contain more than just this file's symbols). If ldsymlen is
84 0, the only reason for this thing's existence is the dependency list.
85 Nothing else will happen when it is read in. */
86
87#define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
88#define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
89
90struct symloc {
91 int ldsymoff;
92 int ldsymlen;
93};
94
bd5635a1
RP
95/* Macro to determine which symbols to ignore when reading the first symbol
96 of a file. Some machines override this definition. */
97#ifndef IGNORE_SYMBOL
98/* This code is used on Ultrix systems. Ignore it */
99#define IGNORE_SYMBOL(type) (type == (int)N_NSYMS)
100#endif
101
102/* Macro for name of symbol to indicate a file compiled with gcc. */
103#ifndef GCC_COMPILED_FLAG_SYMBOL
104#define GCC_COMPILED_FLAG_SYMBOL "gcc_compiled."
105#endif
106
0cf9329b
PB
107/* Macro for name of symbol to indicate a file compiled with gcc2. */
108#ifndef GCC2_COMPILED_FLAG_SYMBOL
109#define GCC2_COMPILED_FLAG_SYMBOL "gcc2_compiled."
110#endif
111
bd5635a1
RP
112/* Define this as 1 if a pcc declaration of a char or short argument
113 gives the correct address. Otherwise assume pcc gives the
114 address of the corresponding int, which is not the same on a
115 big-endian machine. */
116
117#ifndef BELIEVE_PCC_PROMOTION
118#define BELIEVE_PCC_PROMOTION 0
119#endif
c0302457 120
bd5635a1
RP
121/* Nonzero means give verbose info on gdb action. From main.c. */
122extern int info_verbose;
123
7d9884b9 124/* The BFD for this file -- implicit parameter to next_symbol_text. */
bd5635a1 125
c0302457 126static bfd *symfile_bfd;
bd5635a1 127
7d9884b9
JG
128/* The objfile for this file -- only good in process_one_symbol(). */
129
130static struct objfile *our_objfile;
131
bd5635a1
RP
132/* String table for the main symbol file. It is kept in memory
133 permanently, to speed up symbol reading. Other files' symbol tables
134 are read in on demand. FIXME, this should be cleaner. */
135
136static char *symfile_string_table;
137static int symfile_string_table_size;
138
afe4ca15
JG
139/* The size of each symbol in the symbol file (in external form).
140 This is set by dbx_symfile_read when building psymtabs, and by
141 dbx_psymtab_to_symtab when building symtabs. */
142
143static unsigned symbol_size;
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
RP
155
156struct complaint lbrac_rbrac_complaint =
157 {"block start larger than block end", 0, 0};
7d9884b9
JG
158
159struct complaint lbrac_unmatched_complaint =
160 {"unmatched N_LBRAC before symtab pos %d", 0, 0};
161
162struct complaint lbrac_mismatch_complaint =
163 {"N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", 0, 0};
bd5635a1 164\f
bd5635a1
RP
165/* During initial symbol readin, we need to have a structure to keep
166 track of which psymtabs have which bincls in them. This structure
167 is used during readin to setup the list of dependencies within each
168 partial symbol table. */
169
170struct header_file_location
171{
172 char *name; /* Name of header file */
173 int instance; /* See above */
174 struct partial_symtab *pst; /* Partial symtab that has the
175 BINCL/EINCL defs for this file */
176};
177
178/* The actual list and controling variables */
179static struct header_file_location *bincl_list, *next_bincl;
180static int bincls_allocated;
181
021959e2
JG
182/* Local function prototypes */
183
184static void
185free_and_init_header_files PARAMS ((void));
186
187static struct pending *
188copy_pending PARAMS ((struct pending *, int, struct pending *));
189
190static struct symtab *
191read_ofile_symtab PARAMS ((struct objfile *, char *, unsigned int, int, int,
192 CORE_ADDR, int, int));
193
194static void
195dbx_psymtab_to_symtab PARAMS ((struct partial_symtab *));
196
197static void
198psymtab_to_symtab_1 PARAMS ((struct partial_symtab *, char *, int, int));
199
200static void
201read_dbx_symtab PARAMS ((CORE_ADDR, struct objfile *, char *, long, int,
202 CORE_ADDR, int));
203
204static void
205free_bincl_list PARAMS ((struct objfile *));
206
207static struct partial_symtab *
208find_corresponding_bincl_psymtab PARAMS ((char *, int));
209
210static void
211add_bincl_to_list PARAMS ((struct partial_symtab *, char *, int));
212
213static void
214init_bincl_list PARAMS ((int, struct objfile *));
215
216static void
217init_psymbol_list PARAMS ((int, struct objfile *));
218
219static char *
220dbx_next_symbol_text PARAMS ((void));
221
222static void
223fill_symbuf PARAMS ((bfd *));
224
225static void
226dbx_symfile_init PARAMS ((struct sym_fns *));
227
228static void
229dbx_new_init PARAMS ((void));
230
231static void
232dbx_symfile_read PARAMS ((struct sym_fns *, CORE_ADDR, int));
233
234static void
235record_minimal_symbol PARAMS ((char *, CORE_ADDR, int, struct objfile *));
236
237static void
238add_new_header_file PARAMS ((char *, int));
239
240static void
241add_old_header_file PARAMS ((char *, int));
242
243static void
244add_this_object_header_file PARAMS ((int));
245
bd5635a1
RP
246/* Free up old header file tables, and allocate new ones.
247 We're reading a new symbol file now. */
248
021959e2 249static void
bd5635a1
RP
250free_and_init_header_files ()
251{
252 register int i;
253 for (i = 0; i < n_header_files; i++)
254 free (header_files[i].name);
255 if (header_files) /* First time null */
256 free (header_files);
257 if (this_object_header_files) /* First time null */
258 free (this_object_header_files);
259
260 n_allocated_header_files = 10;
261 header_files = (struct header_file *) xmalloc (10 * sizeof (struct header_file));
262 n_header_files = 0;
263
264 n_allocated_this_object_header_files = 10;
265 this_object_header_files = (int *) xmalloc (10 * sizeof (int));
266}
267
bd5635a1
RP
268/* Add header file number I for this object file
269 at the next successive FILENUM. */
270
271static void
272add_this_object_header_file (i)
273 int i;
274{
275 if (n_this_object_header_files == n_allocated_this_object_header_files)
276 {
277 n_allocated_this_object_header_files *= 2;
278 this_object_header_files
021959e2 279 = (int *) xrealloc ((char *) this_object_header_files,
bd5635a1
RP
280 n_allocated_this_object_header_files * sizeof (int));
281 }
282
283 this_object_header_files[n_this_object_header_files++] = i;
284}
285
286/* Add to this file an "old" header file, one already seen in
287 a previous object file. NAME is the header file's name.
288 INSTANCE is its instance code, to select among multiple
289 symbol tables for the same header file. */
290
291static void
292add_old_header_file (name, instance)
293 char *name;
294 int instance;
295{
296 register struct header_file *p = header_files;
297 register int i;
298
299 for (i = 0; i < n_header_files; i++)
300 if (!strcmp (p[i].name, name) && instance == p[i].instance)
301 {
302 add_this_object_header_file (i);
303 return;
304 }
305 error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
306 symnum);
307}
308
309/* Add to this file a "new" header file: definitions for its types follow.
310 NAME is the header file's name.
311 Most often this happens only once for each distinct header file,
312 but not necessarily. If it happens more than once, INSTANCE has
313 a different value each time, and references to the header file
314 use INSTANCE values to select among them.
315
316 dbx output contains "begin" and "end" markers for each new header file,
317 but at this level we just need to know which files there have been;
318 so we record the file when its "begin" is seen and ignore the "end". */
319
320static void
321add_new_header_file (name, instance)
322 char *name;
323 int instance;
324{
325 register int i;
326 header_file_prev_index = -1;
327
328 /* Make sure there is room for one more header file. */
329
330 if (n_header_files == n_allocated_header_files)
331 {
332 n_allocated_header_files *= 2;
333 header_files = (struct header_file *)
021959e2
JG
334 xrealloc ((char *) header_files,
335 (n_allocated_header_files * sizeof (struct header_file)));
bd5635a1
RP
336 }
337
338 /* Create an entry for this header file. */
339
340 i = n_header_files++;
341 header_files[i].name = savestring (name, strlen(name));
342 header_files[i].instance = instance;
343 header_files[i].length = 10;
344 header_files[i].vector
345 = (struct type **) xmalloc (10 * sizeof (struct type *));
346 bzero (header_files[i].vector, 10 * sizeof (struct type *));
347
348 add_this_object_header_file (i);
349}
350
bd5635a1
RP
351#if 0
352static struct type **
353explicit_lookup_type (real_filenum, index)
354 int real_filenum, index;
355{
356 register struct header_file *f = &header_files[real_filenum];
357
358 if (index >= f->length)
359 {
360 f->length *= 2;
361 f->vector = (struct type **)
362 xrealloc (f->vector, f->length * sizeof (struct type *));
363 bzero (&f->vector[f->length / 2],
364 f->length * sizeof (struct type *) / 2);
365 }
366 return &f->vector[index];
367}
368#endif
369\f
9bba3334 370static void
021959e2 371record_minimal_symbol (name, address, type, objfile)
bd5635a1
RP
372 char *name;
373 CORE_ADDR address;
374 int type;
021959e2 375 struct objfile *objfile;
bd5635a1 376{
021959e2 377 enum minimal_symbol_type ms_type;
0c4d2cc2
JG
378
379 switch (type &~ N_EXT) {
021959e2
JG
380 case N_TEXT: ms_type = mst_text; break;
381 case N_DATA: ms_type = mst_data; break;
382 case N_BSS: ms_type = mst_bss; break;
383 case N_ABS: ms_type = mst_abs; break;
0c4d2cc2 384#ifdef N_SETV
021959e2 385 case N_SETV: ms_type = mst_data; break;
0c4d2cc2 386#endif
021959e2 387 default: ms_type = mst_unknown; break;
0c4d2cc2 388 }
bd5635a1 389
021959e2
JG
390 prim_record_minimal_symbol (obsavestring (name, strlen (name), &objfile -> symbol_obstack),
391 address, ms_type);
bd5635a1
RP
392}
393\f
394/* Scan and build partial symbols for a symbol file.
395 We have been initialized by a call to dbx_symfile_init, which
396 put all the relevant info into a "struct dbx_symfile_info"
397 hung off the struct sym_fns SF.
398
399 ADDR is the address relative to which the symbols in it are (e.g.
400 the base address of the text segment).
401 MAINLINE is true if we are reading the main symbol
402 table (as opposed to a shared lib or dynamically loaded file). */
403
9bba3334 404static void
bd5635a1
RP
405dbx_symfile_read (sf, addr, mainline)
406 struct sym_fns *sf;
407 CORE_ADDR addr;
408 int mainline; /* FIXME comments above */
409{
410 struct dbx_symfile_info *info = (struct dbx_symfile_info *) (sf->sym_private);
7d9884b9 411 bfd *sym_bfd = sf->objfile->obfd;
bd5635a1 412 int val;
bd5635a1 413
7d9884b9 414 val = bfd_seek (sf->objfile->obfd, info->symtab_offset, L_SET);
bd5635a1 415 if (val < 0)
7d9884b9 416 perror_with_name (sf->objfile->name);
bd5635a1
RP
417
418 /* If mainline, set global string table pointers, and reinitialize global
419 partial symbol list. */
420 if (mainline) {
421 symfile_string_table = info->stringtab;
422 symfile_string_table_size = info->stringtab_size;
bd5635a1
RP
423 }
424
66eeea27 425 /* If we are reinitializing, or if we have never loaded syms yet, init */
021959e2
JG
426 if (mainline || sf->objfile->global_psymbols.size == 0 || sf->objfile->static_psymbols.size == 0)
427 init_psymbol_list (info->symcount, sf->objfile);
66eeea27 428
afe4ca15
JG
429 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
430 symbol_size = obj_symbol_entry_size (sym_bfd);
431
bd5635a1
RP
432 pending_blocks = 0;
433 make_cleanup (really_free_pendings, 0);
434
021959e2
JG
435 init_minimal_symbol_collection ();
436 make_cleanup (discard_minimal_symbols, 0);
bd5635a1
RP
437
438 /* Now that the symbol table data of the executable file are all in core,
439 process them and define symbols accordingly. */
440
7d9884b9
JG
441 read_dbx_symtab (addr - bfd_section_vma (sym_bfd, info->text_sect), /*offset*/
442 sf->objfile, info->stringtab, info->stringtab_size,
bd5635a1
RP
443 info->symcount,
444 bfd_section_vma (sym_bfd, info->text_sect),
445 bfd_section_size (sym_bfd, info->text_sect));
446
021959e2
JG
447 /* Install any minimal symbols that have been collected as the current
448 minimal symbols for this objfile. */
bd5635a1 449
021959e2 450 install_minimal_symbols (sf -> objfile);
bd5635a1
RP
451
452 /* Free up any memory we allocated for ourselves. */
453
454 if (!mainline) {
318bf84f 455 mfree (sf->objfile->md, info->stringtab); /* Stringtab is only saved for mainline */
bd5635a1 456 }
318bf84f 457 mfree (sf->objfile->md, info);
021959e2 458 sf->sym_private = NULL; /* Zap pointer to our (now gone) info struct */
bd5635a1 459
021959e2 460 if (!have_partial_symbols ()) {
9404978d
MT
461 wrap_here ("");
462 printf_filtered ("(no debugging symbols found)...");
463 wrap_here ("");
464 }
bd5635a1
RP
465}
466
9404978d
MT
467/* Initialize anything that needs initializing when a completely new
468 symbol file is specified (not just adding some symbols from another
469 file, e.g. a shared library). */
bd5635a1 470
9bba3334 471static void
9404978d 472dbx_new_init ()
bd5635a1 473{
c0302457 474 buildsym_new_init ();
bd5635a1 475
bd5635a1 476 /* Don't put these on the cleanup chain; they need to stick around
9404978d 477 until the next call to dbx_new_init. *Then* we'll free them. */
bd5635a1
RP
478 if (symfile_string_table)
479 {
480 free (symfile_string_table);
481 symfile_string_table = 0;
482 symfile_string_table_size = 0;
483 }
484 free_and_init_header_files ();
485}
486
487
488/* dbx_symfile_init ()
489 is the dbx-specific initialization routine for reading symbols.
490 It is passed a struct sym_fns which contains, among other things,
491 the BFD for the file whose symbols are being read, and a slot for a pointer
492 to "private data" which we fill with goodies.
493
494 We read the string table into malloc'd space and stash a pointer to it.
495
496 Since BFD doesn't know how to read debug symbols in a format-independent
497 way (and may never do so...), we have to do it ourselves. We will never
498 be called unless this is an a.out (or very similar) file.
499 FIXME, there should be a cleaner peephole into the BFD environment here. */
500
9bba3334 501static void
bd5635a1
RP
502dbx_symfile_init (sf)
503 struct sym_fns *sf;
504{
505 int val;
7d9884b9 506 bfd *sym_bfd = sf->objfile->obfd;
bd5635a1
RP
507 char *name = bfd_get_filename (sym_bfd);
508 struct dbx_symfile_info *info;
509 unsigned char size_temp[4];
510
511 /* Allocate struct to keep track of the symfile */
318bf84f 512 sf->sym_private = xmmalloc (sf->objfile->md, sizeof (*info));
bd5635a1
RP
513 info = (struct dbx_symfile_info *)sf->sym_private;
514
515 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
bd5635a1
RP
516#define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
517#define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
518 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
519
bd5635a1
RP
520 info->text_sect = bfd_get_section_by_name (sym_bfd, ".text");
521 if (!info->text_sect)
522 abort();
63989338 523 info->symcount = bfd_get_symcount (sym_bfd);
bd5635a1
RP
524
525 /* Read the string table size and check it for bogosity. */
7d9884b9 526 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
bd5635a1
RP
527 if (val < 0)
528 perror_with_name (name);
bd5635a1 529
7d9884b9 530 val = bfd_read (size_temp, sizeof (long), 1, sym_bfd);
bd5635a1
RP
531 if (val < 0)
532 perror_with_name (name);
dcc35536 533 info->stringtab_size = bfd_h_get_32 (sym_bfd, size_temp);
bd5635a1 534
7d9884b9 535 if (info->stringtab_size >= 0)
bd5635a1 536 {
021959e2 537 /* Yes, this should be malloc, not xmalloc. We check its result. */
318bf84f 538 info->stringtab = (char *) mmalloc (sf->objfile->md, info->stringtab_size);
bd5635a1
RP
539 /* Caller is responsible for freeing the string table. No cleanup. */
540 }
541 else
542 info->stringtab = NULL;
543 if (info->stringtab == NULL && info->stringtab_size != 0)
544 error ("ridiculous string table size: %d bytes", info->stringtab_size);
545
546 /* Now read in the string table in one big gulp. */
547
7d9884b9 548 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
bd5635a1
RP
549 if (val < 0)
550 perror_with_name (name);
7d9884b9
JG
551 val = bfd_read (info->stringtab, info->stringtab_size, 1, sym_bfd);
552 if (val != info->stringtab_size)
bd5635a1
RP
553 perror_with_name (name);
554
555 /* Record the position of the symbol table for later use. */
556
557 info->symtab_offset = SYMBOL_TABLE_OFFSET;
558}
559\f
560/* Buffer for reading the symbol table entries. */
afe4ca15 561static struct internal_nlist symbuf[4096];
bd5635a1
RP
562static int symbuf_idx;
563static int symbuf_end;
564
bd5635a1
RP
565/* The address in memory of the string table of the object file we are
566 reading (which might not be the "main" object file, but might be a
567 shared library or some other dynamically loaded thing). This is set
568 by read_dbx_symtab when building psymtabs, and by read_ofile_symtab
569 when building symtabs, and is used only by next_symbol_text. */
570static char *stringtab_global;
571
572/* Refill the symbol table input buffer
573 and set the variables that control fetching entries from it.
574 Reports an error if no data available.
575 This function can read past the end of the symbol table
576 (into the string table) but this does no harm. */
577
7d9884b9
JG
578static void
579fill_symbuf (sym_bfd)
580 bfd *sym_bfd;
bd5635a1 581{
7d9884b9 582 int nbytes = bfd_read (symbuf, sizeof (symbuf), 1, sym_bfd);
bd5635a1 583 if (nbytes < 0)
7d9884b9 584 perror_with_name (bfd_get_filename (sym_bfd));
bd5635a1
RP
585 else if (nbytes == 0)
586 error ("Premature end of file reading symbol table");
afe4ca15 587 symbuf_end = nbytes / symbol_size;
bd5635a1 588 symbuf_idx = 0;
bd5635a1
RP
589}
590
7d9884b9 591#define SWAP_SYMBOL(symp, abfd) \
bd5635a1 592 { \
7d9884b9 593 (symp)->n_strx = bfd_h_get_32(abfd, \
afe4ca15 594 (unsigned char *)&(symp)->n_strx); \
7d9884b9 595 (symp)->n_desc = bfd_h_get_16 (abfd, \
bd5635a1 596 (unsigned char *)&(symp)->n_desc); \
7d9884b9 597 (symp)->n_value = bfd_h_get_32 (abfd, \
bd5635a1
RP
598 (unsigned char *)&(symp)->n_value); \
599 }
600
601/* Invariant: The symbol pointed to by symbuf_idx is the first one
602 that hasn't been swapped. Swap the symbol at the same time
603 that symbuf_idx is incremented. */
604
605/* dbx allows the text of a symbol name to be continued into the
606 next symbol name! When such a continuation is encountered
607 (a \ at the end of the text of a name)
608 call this function to get the continuation. */
609
021959e2 610static char *
aab77d5f 611dbx_next_symbol_text ()
bd5635a1
RP
612{
613 if (symbuf_idx == symbuf_end)
7d9884b9 614 fill_symbuf (symfile_bfd);
bd5635a1 615 symnum++;
7d9884b9 616 SWAP_SYMBOL(&symbuf[symbuf_idx], symfile_bfd);
afe4ca15 617 return symbuf[symbuf_idx++].n_strx + stringtab_global;
bd5635a1
RP
618}
619\f
620/* Initializes storage for all of the partial symbols that will be
621 created by read_dbx_symtab and subsidiaries. */
622
623static void
021959e2 624init_psymbol_list (total_symbols, objfile)
bd5635a1 625 int total_symbols;
021959e2 626 struct objfile *objfile;
bd5635a1
RP
627{
628 /* Free any previously allocated psymbol lists. */
021959e2 629 if (objfile -> global_psymbols.list)
318bf84f 630 mfree (objfile -> md, objfile -> global_psymbols.list);
021959e2 631 if (objfile -> static_psymbols.list)
318bf84f 632 mfree (objfile -> md, objfile -> static_psymbols.list);
bd5635a1
RP
633
634 /* Current best guess is that there are approximately a twentieth
635 of the total symbols (in a debugging file) are global or static
636 oriented symbols */
021959e2
JG
637 objfile -> global_psymbols.size = total_symbols / 10;
638 objfile -> static_psymbols.size = total_symbols / 10;
639 objfile -> global_psymbols.next = objfile -> global_psymbols.list = (struct partial_symbol *)
318bf84f 640 xmmalloc (objfile -> md, objfile -> global_psymbols.size * sizeof (struct partial_symbol));
021959e2 641 objfile -> static_psymbols.next = objfile -> static_psymbols.list = (struct partial_symbol *)
318bf84f 642 xmmalloc (objfile -> md, objfile -> static_psymbols.size * sizeof (struct partial_symbol));
bd5635a1
RP
643}
644
645/* Initialize the list of bincls to contain none and have some
646 allocated. */
647
648static void
021959e2 649init_bincl_list (number, objfile)
bd5635a1 650 int number;
021959e2 651 struct objfile *objfile;
bd5635a1
RP
652{
653 bincls_allocated = number;
654 next_bincl = bincl_list = (struct header_file_location *)
318bf84f 655 xmmalloc (objfile -> md, bincls_allocated * sizeof(struct header_file_location));
bd5635a1
RP
656}
657
658/* Add a bincl to the list. */
659
660static void
661add_bincl_to_list (pst, name, instance)
662 struct partial_symtab *pst;
663 char *name;
664 int instance;
665{
666 if (next_bincl >= bincl_list + bincls_allocated)
667 {
668 int offset = next_bincl - bincl_list;
669 bincls_allocated *= 2;
670 bincl_list = (struct header_file_location *)
318bf84f 671 xmrealloc (pst->objfile->md, (char *)bincl_list,
bd5635a1
RP
672 bincls_allocated * sizeof (struct header_file_location));
673 next_bincl = bincl_list + offset;
674 }
675 next_bincl->pst = pst;
676 next_bincl->instance = instance;
677 next_bincl++->name = name;
678}
679
680/* Given a name, value pair, find the corresponding
681 bincl in the list. Return the partial symtab associated
682 with that header_file_location. */
683
9bba3334 684static struct partial_symtab *
bd5635a1
RP
685find_corresponding_bincl_psymtab (name, instance)
686 char *name;
687 int instance;
688{
689 struct header_file_location *bincl;
690
691 for (bincl = bincl_list; bincl < next_bincl; bincl++)
692 if (bincl->instance == instance
693 && !strcmp (name, bincl->name))
694 return bincl->pst;
695
696 return (struct partial_symtab *) 0;
697}
698
699/* Free the storage allocated for the bincl list. */
700
701static void
021959e2
JG
702free_bincl_list (objfile)
703 struct objfile *objfile;
bd5635a1 704{
318bf84f 705 mfree (objfile -> md, bincl_list);
bd5635a1
RP
706 bincls_allocated = 0;
707}
708
bd5635a1
RP
709/* Given pointers to an a.out symbol table in core containing dbx
710 style data, setup partial_symtab's describing each source file for
711 which debugging information is available. NLISTLEN is the number
712 of symbols in the symbol table. All symbol names are given as
713 offsets relative to STRINGTAB. STRINGTAB_SIZE is the size of
714 STRINGTAB. SYMFILE_NAME is the name of the file we are reading from
715 and ADDR is its relocated address (if incremental) or 0 (if not). */
716
717static void
7d9884b9 718read_dbx_symtab (addr, objfile, stringtab, stringtab_size, nlistlen,
bd5635a1 719 text_addr, text_size)
bd5635a1 720 CORE_ADDR addr;
7d9884b9 721 struct objfile *objfile;
bd5635a1
RP
722 register char *stringtab;
723 register long stringtab_size;
724 register int nlistlen;
725 CORE_ADDR text_addr;
726 int text_size;
727{
afe4ca15 728 register struct internal_nlist *bufp;
bd5635a1 729 register char *namestring;
bd5635a1
RP
730 int nsl;
731 int past_first_source_file = 0;
732 CORE_ADDR last_o_file_start = 0;
733 struct cleanup *old_chain;
7d9884b9 734 bfd *abfd;
bd5635a1
RP
735
736 /* End of the text segment of the executable file. */
737 CORE_ADDR end_of_text_addr;
738
739 /* Current partial symtab */
740 struct partial_symtab *pst;
741
742 /* List of current psymtab's include files */
743 char **psymtab_include_list;
744 int includes_allocated;
745 int includes_used;
746
747 /* Index within current psymtab dependency list */
748 struct partial_symtab **dependency_list;
749 int dependencies_used, dependencies_allocated;
750
751 stringtab_global = stringtab;
752
753 pst = (struct partial_symtab *) 0;
754
755 includes_allocated = 30;
756 includes_used = 0;
757 psymtab_include_list = (char **) alloca (includes_allocated *
758 sizeof (char *));
759
760 dependencies_allocated = 30;
761 dependencies_used = 0;
762 dependency_list =
763 (struct partial_symtab **) alloca (dependencies_allocated *
764 sizeof (struct partial_symtab *));
765
7d9884b9 766 old_chain = make_cleanup (free_objfile, objfile);
bd5635a1
RP
767
768 /* Init bincl list */
021959e2
JG
769 init_bincl_list (20, objfile);
770 make_cleanup (free_bincl_list, objfile);
bd5635a1
RP
771
772 last_source_file = 0;
773
774#ifdef END_OF_TEXT_DEFAULT
775 end_of_text_addr = END_OF_TEXT_DEFAULT;
776#else
5bc757e2 777 end_of_text_addr = text_addr + addr + text_size; /* Relocate */
bd5635a1
RP
778#endif
779
7d9884b9
JG
780 symfile_bfd = objfile->obfd; /* For next_text_symbol */
781 abfd = objfile->obfd;
bd5635a1 782 symbuf_end = symbuf_idx = 0;
aab77d5f 783 next_symbol_text_func = dbx_next_symbol_text;
bd5635a1
RP
784
785 for (symnum = 0; symnum < nlistlen; symnum++)
786 {
787 /* Get the symbol for this run and pull out some info */
788 QUIT; /* allow this to be interruptable */
789 if (symbuf_idx == symbuf_end)
7d9884b9 790 fill_symbuf (abfd);
bd5635a1
RP
791 bufp = &symbuf[symbuf_idx++];
792
793 /*
794 * Special case to speed up readin.
795 */
796 if (bufp->n_type == (unsigned char)N_SLINE) continue;
797
7d9884b9 798 SWAP_SYMBOL (bufp, abfd);
bd5635a1
RP
799
800 /* Ok. There is a lot of code duplicated in the rest of this
801 switch statement (for efficiency reasons). Since I don't
802 like duplicating code, I will do my penance here, and
803 describe the code which is duplicated:
804
805 *) The assignment to namestring.
806 *) The call to strchr.
807 *) The addition of a partial symbol the the two partial
808 symbol lists. This last is a large section of code, so
809 I've imbedded it in the following macro.
810 */
811
812/* Set namestring based on bufp. If the string table index is invalid,
813 give a fake name, and print a single error message per symbol file read,
814 rather than abort the symbol reading or flood the user with messages. */
815#define SET_NAMESTRING()\
7d9884b9 816 if (((unsigned)bufp->n_strx) >= stringtab_size) { \
021959e2 817 complain (&string_table_offset_complaint, (char *) symnum); \
bd5635a1
RP
818 namestring = "foo"; \
819 } else \
afe4ca15 820 namestring = bufp->n_strx + stringtab
bd5635a1 821
7e258d18
PB
822#define CUR_SYMBOL_TYPE bufp->n_type
823#define CUR_SYMBOL_VALUE bufp->n_value
824#define DBXREAD_ONLY
7e258d18
PB
825#define START_PSYMTAB(ofile,addr,fname,low,symoff,global_syms,static_syms)\
826 start_psymtab(ofile, addr, fname, low, symoff, global_syms, static_syms)
827#define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)\
828 end_psymtab(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)
aab77d5f 829
7e258d18 830#include "partial-stab.h"
bd5635a1
RP
831 }
832
833 /* If there's stuff to be cleaned up, clean it up. */
63989338
JG
834 if (nlistlen > 0 /* We have some syms */
835 && entry_point < bufp->n_value
bd5635a1
RP
836 && entry_point >= last_o_file_start)
837 {
838 startup_file_start = last_o_file_start;
839 startup_file_end = bufp->n_value;
840 }
841
842 if (pst)
843 {
844 end_psymtab (pst, psymtab_include_list, includes_used,
afe4ca15 845 symnum * symbol_size, end_of_text_addr,
7e258d18 846 dependency_list, dependencies_used);
bd5635a1
RP
847 }
848
021959e2 849 free_bincl_list (objfile);
bd5635a1
RP
850 discard_cleanups (old_chain);
851}
852
4a35d6e9
FF
853/* Allocate and partially fill a partial symtab. It will be
854 completely filled at the end of the symbol list.
855
856 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
857 is the address relative to which its symbols are (incremental) or 0
858 (normal). */
859
bd5635a1 860
7e258d18 861struct partial_symtab *
7d9884b9 862start_psymtab (objfile, addr,
bd5635a1 863 filename, textlow, ldsymoff, global_syms, static_syms)
7d9884b9 864 struct objfile *objfile;
bd5635a1
RP
865 CORE_ADDR addr;
866 char *filename;
867 CORE_ADDR textlow;
868 int ldsymoff;
869 struct partial_symbol *global_syms;
870 struct partial_symbol *static_syms;
871{
872 struct partial_symtab *result =
021959e2
JG
873 start_psymtab_common(objfile, addr,
874 filename, textlow, global_syms, static_syms);
bd5635a1 875
021959e2
JG
876 result->read_symtab_private = (char *)
877 obstack_alloc (&objfile -> psymbol_obstack, sizeof (struct symloc));
878 LDSYMOFF(result) = ldsymoff;
bd5635a1
RP
879 result->read_symtab = dbx_psymtab_to_symtab;
880
bd5635a1
RP
881 return result;
882}
883
bd5635a1
RP
884/* Close off the current usage of a partial_symbol table entry. This
885 involves setting the correct number of includes (with a realloc),
886 setting the high text mark, setting the symbol length in the
887 executable, and setting the length of the global and static lists
888 of psymbols.
889
890 The global symbols and static symbols are then seperately sorted.
891
892 Then the partial symtab is put on the global list.
893 *** List variables and peculiarities of same. ***
894 */
021959e2 895
7e258d18 896void
bd5635a1 897end_psymtab (pst, include_list, num_includes, capping_symbol_offset,
7e258d18 898 capping_text, dependency_list, number_dependencies)
bd5635a1
RP
899 struct partial_symtab *pst;
900 char **include_list;
901 int num_includes;
902 int capping_symbol_offset;
903 CORE_ADDR capping_text;
904 struct partial_symtab **dependency_list;
905 int number_dependencies;
7e258d18 906/* struct partial_symbol *capping_global, *capping_static;*/
bd5635a1
RP
907{
908 int i;
021959e2 909 struct objfile *objfile = pst -> objfile;
bd5635a1 910
7e258d18
PB
911 if (capping_symbol_offset != -1)
912 LDSYMLEN(pst) = capping_symbol_offset - LDSYMOFF(pst);
bd5635a1
RP
913 pst->texthigh = capping_text;
914
915 pst->n_global_syms =
021959e2 916 objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
bd5635a1 917 pst->n_static_syms =
021959e2 918 objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
bd5635a1
RP
919
920 pst->number_of_dependencies = number_dependencies;
921 if (number_dependencies)
922 {
923 pst->dependencies = (struct partial_symtab **)
021959e2 924 obstack_alloc (&objfile->psymbol_obstack,
bd5635a1 925 number_dependencies * sizeof (struct partial_symtab *));
7e258d18 926 memcpy (pst->dependencies, dependency_list,
bd5635a1
RP
927 number_dependencies * sizeof (struct partial_symtab *));
928 }
929 else
930 pst->dependencies = 0;
931
932 for (i = 0; i < num_includes; i++)
933 {
bd5635a1 934 struct partial_symtab *subpst =
021959e2 935 allocate_psymtab (include_list[i], objfile);
7d9884b9 936
bd5635a1 937 subpst->addr = pst->addr;
021959e2
JG
938 subpst->read_symtab_private =
939 (char *) obstack_alloc (&objfile->psymbol_obstack,
940 sizeof (struct symloc));
4a35d6e9
FF
941 LDSYMOFF(subpst) =
942 LDSYMLEN(subpst) =
bd5635a1
RP
943 subpst->textlow =
944 subpst->texthigh = 0;
945
3f83182d
JG
946 /* We could save slight bits of space by only making one of these,
947 shared by the entire set of include files. FIXME-someday. */
bd5635a1 948 subpst->dependencies = (struct partial_symtab **)
021959e2 949 obstack_alloc (&objfile->psymbol_obstack,
bd5635a1
RP
950 sizeof (struct partial_symtab *));
951 subpst->dependencies[0] = pst;
952 subpst->number_of_dependencies = 1;
953
954 subpst->globals_offset =
955 subpst->n_global_syms =
956 subpst->statics_offset =
957 subpst->n_static_syms = 0;
958
959 subpst->readin = 0;
9a822037 960 subpst->symtab = 0;
bd5635a1 961 subpst->read_symtab = dbx_psymtab_to_symtab;
bd5635a1
RP
962 }
963
021959e2 964 sort_pst_symbols (pst);
bd5635a1 965
f9623881
JG
966 /* If there is already a psymtab or symtab for a file of this name, remove it.
967 (If there is a symtab, more drastic things also happen.)
968 This happens in VxWorks. */
969 free_named_symtabs (pst->filename);
970
7d9884b9
JG
971 if (num_includes == 0
972 && number_dependencies == 0
973 && pst->n_global_syms == 0
974 && pst->n_static_syms == 0) {
975 /* Throw away this psymtab, it's empty. We can't deallocate it, since
976 it is on the obstack, but we can forget to chain it on the list. */
318bf84f
FF
977 struct partial_symtab *prev_pst;
978
979 /* First, snip it out of the psymtab chain */
980
981 if (pst->objfile->psymtabs == pst)
982 pst->objfile->psymtabs = pst->next;
983 else
984 for (prev_pst = pst->objfile->psymtabs; prev_pst; prev_pst = pst->next)
985 if (prev_pst->next == pst)
986 prev_pst->next = pst->next;
987
988 /* Next, put it on a free list for recycling */
989
990 pst->next = pst->objfile->free_psymtabs;
991 pst->objfile->free_psymtabs = pst;
7d9884b9 992 }
bd5635a1
RP
993}
994\f
995static void
7d9884b9 996psymtab_to_symtab_1 (pst, stringtab, stringtab_size, sym_offset)
bd5635a1 997 struct partial_symtab *pst;
bd5635a1
RP
998 char *stringtab;
999 int stringtab_size;
1000 int sym_offset;
1001{
1002 struct cleanup *old_chain;
1003 int i;
1004
1005 if (!pst)
1006 return;
1007
1008 if (pst->readin)
1009 {
1010 fprintf (stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1011 pst->filename);
1012 return;
1013 }
1014
afe4ca15 1015 /* Read in all partial symtabs on which this one is dependent */
bd5635a1
RP
1016 for (i = 0; i < pst->number_of_dependencies; i++)
1017 if (!pst->dependencies[i]->readin)
1018 {
1019 /* Inform about additional files that need to be read in. */
1020 if (info_verbose)
1021 {
1022 fputs_filtered (" ", stdout);
1023 wrap_here ("");
1024 fputs_filtered ("and ", stdout);
1025 wrap_here ("");
1026 printf_filtered ("%s...", pst->dependencies[i]->filename);
1027 wrap_here (""); /* Flush output */
1028 fflush (stdout);
1029 }
7d9884b9 1030 psymtab_to_symtab_1 (pst->dependencies[i],
bd5635a1
RP
1031 stringtab, stringtab_size, sym_offset);
1032 }
1033
4a35d6e9 1034 if (LDSYMLEN(pst)) /* Otherwise it's a dummy */
bd5635a1
RP
1035 {
1036 /* Init stuff necessary for reading in symbols */
c0302457 1037 buildsym_init ();
bd5635a1
RP
1038 old_chain = make_cleanup (really_free_pendings, 0);
1039
1040 /* Read in this files symbols */
7d9884b9 1041 bfd_seek (pst->objfile->obfd, sym_offset, L_SET);
9404978d 1042 pst->symtab =
7d9884b9 1043 read_ofile_symtab (pst->objfile, stringtab, stringtab_size,
4a35d6e9
FF
1044 LDSYMOFF(pst),
1045 LDSYMLEN(pst), pst->textlow,
9404978d
MT
1046 pst->texthigh - pst->textlow, pst->addr);
1047 sort_symtab_syms (pst->symtab);
bd5635a1
RP
1048
1049 do_cleanups (old_chain);
1050 }
1051
1052 pst->readin = 1;
1053}
1054
1055/*
1056 * Read in all of the symbols for a given psymtab for real.
1057 * Be verbose about it if the user wants that.
1058 */
1059static void
1060dbx_psymtab_to_symtab (pst)
1061 struct partial_symtab *pst;
1062{
bd5635a1
RP
1063 char *stringtab;
1064 int stsize, val;
bd5635a1
RP
1065 bfd *sym_bfd;
1066 long st_temp;
1067
1068 if (!pst)
1069 return;
1070
1071 if (pst->readin)
1072 {
1073 fprintf (stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1074 pst->filename);
1075 return;
1076 }
1077
4a35d6e9 1078 if (LDSYMLEN(pst) || pst->number_of_dependencies)
bd5635a1
RP
1079 {
1080 /* Print the message now, before reading the string table,
1081 to avoid disconcerting pauses. */
1082 if (info_verbose)
1083 {
1084 printf_filtered ("Reading in symbols for %s...", pst->filename);
1085 fflush (stdout);
1086 }
1087
7d9884b9 1088 sym_bfd = pst->objfile->obfd;
bd5635a1 1089
7d9884b9 1090 /* We keep the string table for the main symfile resident in memory, but
bd5635a1 1091 not the string table for any other symbol files. */
7d9884b9 1092 if (symfile_objfile != pst->objfile)
bd5635a1
RP
1093 {
1094 /* Read in the string table */
1095
1096 /* FIXME, this uses internal BFD variables. See above in
1097 dbx_symbol_file_open where the macro is defined! */
7d9884b9 1098 bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
bd5635a1 1099
7d9884b9 1100 val = bfd_read (&st_temp, sizeof st_temp, 1, sym_bfd);
bd5635a1 1101 if (val < 0)
7d9884b9 1102 perror_with_name (pst->objfile->name);
dcc35536 1103 stsize = bfd_h_get_32 (sym_bfd, (unsigned char *)&st_temp);
7d9884b9
JG
1104#if 0
1105 /* BFD doesn't provide a way to know the total file size, sigh */
1106 struct stat statbuf;
bd5635a1 1107 if (fstat (desc, &statbuf) < 0)
7d9884b9 1108 perror_with_name (pst->objfile->name);
bd5635a1
RP
1109
1110 if (stsize >= 0 && stsize < statbuf.st_size)
7d9884b9
JG
1111#else
1112 if (stsize >= 0)
1113#endif
bd5635a1
RP
1114 {
1115#ifdef BROKEN_LARGE_ALLOCA
318bf84f
FF
1116 stringtab = (char *) xmalloc (stsize);
1117 make_cleanup (free, stringtab);
bd5635a1
RP
1118#else
1119 stringtab = (char *) alloca (stsize);
1120#endif
1121 }
1122 else
1123 stringtab = NULL;
1124 if (stringtab == NULL && stsize != 0)
1125 error ("ridiculous string table size: %d bytes", stsize);
1126
1127 /* FIXME, this uses internal BFD variables. See above in
1128 dbx_symbol_file_open where the macro is defined! */
7d9884b9 1129 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
bd5635a1 1130 if (val < 0)
7d9884b9
JG
1131 perror_with_name (pst->objfile->name);
1132 val = bfd_read (stringtab, stsize, 1, sym_bfd);
bd5635a1 1133 if (val < 0)
7d9884b9 1134 perror_with_name (pst->objfile->name);
bd5635a1
RP
1135 }
1136 else
1137 {
1138 stringtab = symfile_string_table;
1139 stsize = symfile_string_table_size;
1140 }
1141
afe4ca15
JG
1142 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
1143 symbol_size = obj_symbol_entry_size (sym_bfd);
bd5635a1 1144
aab77d5f
PB
1145 next_symbol_text_func = dbx_next_symbol_text;
1146
bd5635a1
RP
1147 /* FIXME, this uses internal BFD variables. See above in
1148 dbx_symbol_file_open where the macro is defined! */
7d9884b9 1149 psymtab_to_symtab_1 (pst, stringtab, stsize,
bd5635a1
RP
1150 SYMBOL_TABLE_OFFSET);
1151
1152 /* Match with global symbols. This only needs to be done once,
1153 after all of the symtabs and dependencies have been read in. */
021959e2 1154 scan_file_globals (pst->objfile);
bd5635a1 1155
bd5635a1
RP
1156 /* Finish up the debug error message. */
1157 if (info_verbose)
1158 printf_filtered ("done.\n");
1159 }
1160}
1161
bd5635a1
RP
1162/*
1163 * Read in a defined section of a specific object file's symbols.
1164 *
1165 * DESC is the file descriptor for the file, positioned at the
1166 * beginning of the symtab
1167 * STRINGTAB is a pointer to the files string
1168 * table, already read in
1169 * SYM_OFFSET is the offset within the file of
1170 * the beginning of the symbols we want to read, NUM_SUMBOLS is the
1171 * number of symbols to read
1172 * TEXT_OFFSET is the beginning of the text segment we are reading symbols for
1173 * TEXT_SIZE is the size of the text segment read in.
1174 * OFFSET is a relocation offset which gets added to each symbol
1175 */
1176
9404978d 1177static struct symtab *
7d9884b9 1178read_ofile_symtab (objfile, stringtab, stringtab_size, sym_offset,
bd5635a1 1179 sym_size, text_offset, text_size, offset)
7d9884b9 1180 struct objfile *objfile;
bd5635a1
RP
1181 register char *stringtab;
1182 unsigned int stringtab_size;
1183 int sym_offset;
1184 int sym_size;
1185 CORE_ADDR text_offset;
1186 int text_size;
1187 int offset;
1188{
1189 register char *namestring;
7d9884b9 1190 register struct internal_nlist *bufp;
bd5635a1 1191 unsigned char type;
afe4ca15 1192 unsigned max_symnum;
7d9884b9
JG
1193 register bfd *abfd;
1194
021959e2 1195 current_objfile = objfile;
bd5635a1
RP
1196 subfile_stack = 0;
1197
1198 stringtab_global = stringtab;
1199 last_source_file = 0;
1200
7d9884b9
JG
1201 abfd = objfile->obfd;
1202 symfile_bfd = objfile->obfd; /* Implicit param to next_text_symbol */
1203 our_objfile = objfile; /* For end_symtab calls in process_one_symbol */
bd5635a1
RP
1204 symbuf_end = symbuf_idx = 0;
1205
1206 /* It is necessary to actually read one symbol *before* the start
1207 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
1208 occurs before the N_SO symbol.
1209
1210 Detecting this in read_dbx_symtab
1211 would slow down initial readin, so we look for it here instead. */
afe4ca15 1212 if (sym_offset >= (int)symbol_size)
bd5635a1 1213 {
7d9884b9
JG
1214 bfd_seek (symfile_bfd, sym_offset - symbol_size, L_INCR);
1215 fill_symbuf (abfd);
bd5635a1 1216 bufp = &symbuf[symbuf_idx++];
7d9884b9 1217 SWAP_SYMBOL (bufp, abfd);
bd5635a1 1218
afe4ca15 1219 SET_NAMESTRING ();
bd5635a1
RP
1220
1221 processing_gcc_compilation =
1222 (bufp->n_type == N_TEXT
0cf9329b
PB
1223 && (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0
1224 || strcmp(namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0));
bd5635a1
RP
1225 }
1226 else
1227 {
1228 /* The N_SO starting this symtab is the first symbol, so we
1229 better not check the symbol before it. I'm not this can
1230 happen, but it doesn't hurt to check for it. */
7d9884b9 1231 bfd_seek (symfile_bfd, sym_offset, L_INCR);
bd5635a1
RP
1232 processing_gcc_compilation = 0;
1233 }
1234
1235 if (symbuf_idx == symbuf_end)
7d9884b9 1236 fill_symbuf (abfd);
bd5635a1
RP
1237 bufp = &symbuf[symbuf_idx];
1238 if (bufp->n_type != (unsigned char)N_SO)
1239 error("First symbol in segment of executable not a source symbol");
1240
afe4ca15
JG
1241 max_symnum = sym_size / symbol_size;
1242
bd5635a1 1243 for (symnum = 0;
afe4ca15 1244 symnum < max_symnum;
bd5635a1
RP
1245 symnum++)
1246 {
1247 QUIT; /* Allow this to be interruptable */
1248 if (symbuf_idx == symbuf_end)
7d9884b9 1249 fill_symbuf(abfd);
bd5635a1 1250 bufp = &symbuf[symbuf_idx++];
7d9884b9 1251 SWAP_SYMBOL (bufp, abfd);
bd5635a1 1252
c0302457 1253 type = bufp->n_type;
bd5635a1
RP
1254 if (type == (unsigned char)N_CATCH)
1255 {
1256 /* N_CATCH is not fixed up by the linker, and unfortunately,
1257 there's no other place to put it in the .stab map. */
c55e6167 1258 bufp->n_value += text_offset - offset;
bd5635a1 1259 }
bd5635a1 1260
afe4ca15 1261 SET_NAMESTRING ();
bd5635a1 1262
7d9884b9 1263 if (type & N_STAB) {
c55e6167
JG
1264 process_one_symbol (type, bufp->n_desc, bufp->n_value,
1265 namestring, offset);
1266 /* our_objfile is an implicit parameter. */
7d9884b9 1267 }
bd5635a1
RP
1268 /* We skip checking for a new .o or -l file; that should never
1269 happen in this routine. */
1270 else if (type == N_TEXT
0cf9329b
PB
1271 && (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0
1272 || strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0))
bd5635a1
RP
1273 /* I don't think this code will ever be executed, because
1274 the GCC_COMPILED_FLAG_SYMBOL usually is right before
1275 the N_SO symbol which starts this source file.
1276 However, there is no reason not to accept
1277 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
1278 processing_gcc_compilation = 1;
1279 else if (type & N_EXT || type == (unsigned char)N_TEXT
1280 || type == (unsigned char)N_NBTEXT
0c4d2cc2 1281 ) {
bd5635a1
RP
1282 /* Global symbol: see if we came across a dbx defintion for
1283 a corresponding symbol. If so, store the value. Remove
1284 syms from the chain when their values are stored, but
1285 search the whole chain, as there may be several syms from
1286 different files with the same name. */
1287 /* This is probably not true. Since the files will be read
1288 in one at a time, each reference to a global symbol will
1289 be satisfied in each file as it appears. So we skip this
1290 section. */
1291 ;
0c4d2cc2 1292 }
bd5635a1 1293 }
9404978d 1294
021959e2
JG
1295 current_objfile = NULL;
1296 return (end_symtab (text_offset + text_size, 0, 0, objfile));
bd5635a1 1297}
bd5635a1 1298\f
c55e6167
JG
1299/* This handles a single symbol from the symbol-file, building symbols
1300 into a GDB symtab. It takes these arguments and an implicit argument.
1301
1302 TYPE is the type field of the ".stab" symbol entry.
1303 DESC is the desc field of the ".stab" entry.
1304 VALU is the value field of the ".stab" entry.
1305 NAME is the symbol name, in our address space.
1306 OFFSET is the amount by which this object file was relocated
1307 when it was loaded into memory. All symbols that refer
1308 to memory locations need to be offset by this amount.
1309
1310 The implicit argument is:
1311 OUR_OBJFILE is the object file from which we are reading symbols.
1312 It is used in end_symtab. */
1313
7e258d18 1314void
c55e6167 1315process_one_symbol (type, desc, valu, name, offset)
bd5635a1
RP
1316 int type, desc;
1317 CORE_ADDR valu;
1318 char *name;
c55e6167 1319 int offset;
bd5635a1
RP
1320{
1321#ifndef SUN_FIXED_LBRAC_BUG
0cf9329b 1322 /* This records the last pc address we've seen. We depend on there being
bd5635a1
RP
1323 an SLINE or FUN or SO before the first LBRAC, since the variable does
1324 not get reset in between reads of different symbol files. */
1325 static CORE_ADDR last_pc_address;
1326#endif
1327 register struct context_stack *new;
1328 char *colon_pos;
1329
1330 /* Something is wrong if we see real data before
1331 seeing a source file name. */
1332
1333 if (last_source_file == 0 && type != (unsigned char)N_SO)
1334 {
1335 /* Currently this ignores N_ENTRY on Gould machines, N_NSYM on machines
1336 where that code is defined. */
1337 if (IGNORE_SYMBOL (type))
1338 return;
1339
1340 /* FIXME, this should not be an error, since it precludes extending
1341 the symbol table information in this way... */
1342 error ("Invalid symbol data: does not start by identifying a source file.");
1343 }
1344
1345 switch (type)
1346 {
1347 case N_FUN:
1348 case N_FNAME:
0bd83fd7 1349#if 0
3c03b5de
SG
1350/* It seems that the Sun ANSI C compiler (acc) replaces N_FUN with N_GSYM and
1351 N_STSYM with a type code of f or F. Can't enable this until we get some
0bd83fd7
SG
1352 stuff straightened out with psymtabs. */
1353
3c03b5de
SG
1354 case N_GSYM:
1355 case N_STSYM:
0bd83fd7 1356#endif /* 0 */
3c03b5de 1357
c55e6167
JG
1358 valu += offset; /* Relocate for dynamic loading */
1359
bd5635a1
RP
1360 /* Either of these types of symbols indicates the start of
1361 a new function. We must process its "name" normally for dbx,
1362 but also record the start of a new lexical context, and possibly
1363 also the end of the lexical context for the previous function. */
1364 /* This is not always true. This type of symbol may indicate a
1365 text segment variable. */
1366
bd5635a1
RP
1367 colon_pos = strchr (name, ':');
1368 if (!colon_pos++
1369 || (*colon_pos != 'f' && *colon_pos != 'F'))
1370 {
021959e2 1371 define_symbol (valu, name, desc, type, our_objfile);
bd5635a1
RP
1372 break;
1373 }
1374
3c03b5de
SG
1375#ifndef SUN_FIXED_LBRAC_BUG
1376 last_pc_address = valu; /* Save for SunOS bug circumcision */
1377#endif
1378
bd5635a1
RP
1379 within_function = 1;
1380 if (context_stack_depth > 0)
1381 {
7d9884b9 1382 new = pop_context ();
bd5635a1
RP
1383 /* Make a block for the local symbols within. */
1384 finish_block (new->name, &local_symbols, new->old_blocks,
021959e2 1385 new->start_addr, valu, our_objfile);
bd5635a1
RP
1386 }
1387 /* Stack must be empty now. */
1388 if (context_stack_depth != 0)
021959e2 1389 complain (&lbrac_unmatched_complaint, (char *) symnum);
bd5635a1 1390
7d9884b9 1391 new = push_context (0, valu);
021959e2 1392 new->name = define_symbol (valu, name, desc, type, our_objfile);
bd5635a1
RP
1393 break;
1394
1395 case N_CATCH:
1396 /* Record the address at which this catch takes place. */
021959e2 1397 define_symbol (valu+offset, name, desc, type, our_objfile);
bd5635a1
RP
1398 break;
1399
1400 case N_LBRAC:
1401 /* This "symbol" just indicates the start of an inner lexical
1402 context within a function. */
1403
c55e6167
JG
1404#if defined (BLOCK_ADDRESS_ABSOLUTE)
1405 valu += offset; /* Relocate for dynamic loading */
1406#else
bd5635a1
RP
1407 /* On most machines, the block addresses are relative to the
1408 N_SO, the linker did not relocate them (sigh). */
1409 valu += last_source_start_addr;
1410#endif
1411
1412#ifndef SUN_FIXED_LBRAC_BUG
1413 if (valu < last_pc_address) {
1414 /* Patch current LBRAC pc value to match last handy pc value */
1415 complain (&lbrac_complaint, 0);
1416 valu = last_pc_address;
1417 }
1418#endif
7d9884b9 1419 new = push_context (desc, valu);
bd5635a1
RP
1420 break;
1421
1422 case N_RBRAC:
1423 /* This "symbol" just indicates the end of an inner lexical
1424 context that was started with N_LBRAC. */
1425
c55e6167
JG
1426#if defined (BLOCK_ADDRESS_ABSOLUTE)
1427 valu += offset; /* Relocate for dynamic loading */
1428#else
bd5635a1
RP
1429 /* On most machines, the block addresses are relative to the
1430 N_SO, the linker did not relocate them (sigh). */
1431 valu += last_source_start_addr;
1432#endif
1433
7d9884b9 1434 new = pop_context();
bd5635a1 1435 if (desc != new->depth)
021959e2 1436 complain (&lbrac_mismatch_complaint, (char *) symnum);
bd5635a1
RP
1437
1438 /* Some compilers put the variable decls inside of an
1439 LBRAC/RBRAC block. This macro should be nonzero if this
1440 is true. DESC is N_DESC from the N_RBRAC symbol.
0cf9329b
PB
1441 GCC_P is true if we've detected the GCC_COMPILED_SYMBOL
1442 or the GCC2_COMPILED_SYMBOL. */
bd5635a1
RP
1443#if !defined (VARIABLES_INSIDE_BLOCK)
1444#define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
1445#endif
1446
1447 /* Can only use new->locals as local symbols here if we're in
1448 gcc or on a machine that puts them before the lbrack. */
1449 if (!VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1450 local_symbols = new->locals;
1451
1452 /* If this is not the outermost LBRAC...RBRAC pair in the
1453 function, its local symbols preceded it, and are the ones
1454 just recovered from the context stack. Defined the block for them.
1455
1456 If this is the outermost LBRAC...RBRAC pair, there is no
1457 need to do anything; leave the symbols that preceded it
1458 to be attached to the function's own block. However, if
1459 it is so, we need to indicate that we just moved outside
1460 of the function. */
1461 if (local_symbols
1462 && (context_stack_depth
1463 > !VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation)))
1464 {
1465 /* FIXME Muzzle a compiler bug that makes end < start. */
1466 if (new->start_addr > valu)
1467 {
1468 complain(&lbrac_rbrac_complaint, 0);
1469 new->start_addr = valu;
1470 }
1471 /* Make a block for the local symbols within. */
1472 finish_block (0, &local_symbols, new->old_blocks,
021959e2 1473 new->start_addr, valu, our_objfile);
bd5635a1
RP
1474 }
1475 else
1476 {
1477 within_function = 0;
1478 }
1479 if (VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1480 /* Now pop locals of block just finished. */
1481 local_symbols = new->locals;
1482 break;
1483
9bb30452 1484 case N_FN:
6150cc73 1485 case N_FN_SEQ:
9bb30452 1486 /* This kind of symbol indicates the start of an object file. */
c55e6167 1487 valu += offset; /* Relocate for dynamic loading */
bd5635a1
RP
1488 break;
1489
1490 case N_SO:
1491 /* This type of symbol indicates the start of data
1492 for one source file.
1493 Finish the symbol table of the previous source file
1494 (if any) and start accumulating a new symbol table. */
c55e6167
JG
1495 valu += offset; /* Relocate for dynamic loading */
1496
bd5635a1
RP
1497#ifndef SUN_FIXED_LBRAC_BUG
1498 last_pc_address = valu; /* Save for SunOS bug circumcision */
1499#endif
1500
1501#ifdef PCC_SOL_BROKEN
1502 /* pcc bug, occasionally puts out SO for SOL. */
1503 if (context_stack_depth > 0)
1504 {
1505 start_subfile (name, NULL);
1506 break;
1507 }
1508#endif
1509 if (last_source_file)
7e258d18
PB
1510 {
1511 /* Check if previous symbol was also an N_SO (with some
1512 sanity checks). If so, that one was actually the directory
1513 name, and the current one is the real file name.
1514 Patch things up. */
1515 if (previous_stab_code == N_SO
1516 && current_subfile && current_subfile->dirname == NULL
1517 && current_subfile->name != NULL
1518 && current_subfile->name[strlen(current_subfile->name)-1] == '/')
1519 {
1520 current_subfile->dirname = current_subfile->name;
021959e2
JG
1521 current_subfile->name =
1522 obsavestring (name, strlen (name),
1523 &our_objfile -> symbol_obstack);
7e258d18
PB
1524 break;
1525 }
021959e2 1526 (void) end_symtab (valu, 0, 0, our_objfile);
7e258d18 1527 }
bd5635a1
RP
1528 start_symtab (name, NULL, valu);
1529 break;
1530
c55e6167 1531
bd5635a1
RP
1532 case N_SOL:
1533 /* This type of symbol indicates the start of data for
1534 a sub-source-file, one whose contents were copied or
1535 included in the compilation of the main source file
1536 (whose name was given in the N_SO symbol.) */
c55e6167 1537 valu += offset; /* Relocate for dynamic loading */
bd5635a1
RP
1538 start_subfile (name, NULL);
1539 break;
1540
1541 case N_BINCL:
1542 push_subfile ();
1543 add_new_header_file (name, valu);
1544 start_subfile (name, NULL);
1545 break;
1546
1547 case N_EINCL:
1548 start_subfile (pop_subfile (), NULL);
1549 break;
1550
1551 case N_EXCL:
1552 add_old_header_file (name, valu);
1553 break;
1554
1555 case N_SLINE:
1556 /* This type of "symbol" really just records
1557 one line-number -- core-address correspondence.
1558 Enter it in the line list for this symbol table. */
c55e6167 1559 valu += offset; /* Relocate for dynamic loading */
bd5635a1
RP
1560#ifndef SUN_FIXED_LBRAC_BUG
1561 last_pc_address = valu; /* Save for SunOS bug circumcision */
1562#endif
4137c5fc 1563 record_line (current_subfile, desc, valu);
bd5635a1
RP
1564 break;
1565
1566 case N_BCOMM:
1567 if (common_block)
1568 error ("Invalid symbol data: common within common at symtab pos %d",
1569 symnum);
1570 common_block = local_symbols;
1571 common_block_i = local_symbols ? local_symbols->nsyms : 0;
1572 break;
1573
1574 case N_ECOMM:
1575 /* Symbols declared since the BCOMM are to have the common block
1576 start address added in when we know it. common_block points to
1577 the first symbol after the BCOMM in the local_symbols list;
1578 copy the list and hang it off the symbol for the common block name
1579 for later fixup. */
1580 {
1581 int i;
1582 struct symbol *sym =
318bf84f 1583 (struct symbol *) xmmalloc (our_objfile -> md, sizeof (struct symbol));
bd5635a1
RP
1584 bzero (sym, sizeof *sym);
1585 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1586 SYMBOL_CLASS (sym) = LOC_BLOCK;
1587 SYMBOL_NAMESPACE (sym) = (enum namespace)((long)
1588 copy_pending (local_symbols, common_block_i, common_block));
1589 i = hashname (SYMBOL_NAME (sym));
1590 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1591 global_sym_chain[i] = sym;
1592 common_block = 0;
1593 break;
1594 }
1595
c55e6167
JG
1596 /* The following symbol types need to have the offset added to their
1597 value; then we process symbol definitions in the name. */
1598 case N_STSYM: /* Global symbol */
1599 case N_LCSYM: /* Local symbol */
1600 case N_DSLINE: /* Source line number, data seg */
1601 case N_BSLINE: /* Source line number, bss seg */
1602 /* N_BROWS: overlaps with N_BSLINE */
1603 case N_ENTRY: /* Alternate entry point */
1604 valu += offset; /* Relocate for dynamic loading */
1605 /* FALL THROUGH */
1606
1607 /* The following symbol types don't need the address field relocated,
1608 since it is either unused, or is absolute. */
1609 case N_GSYM: /* Global variable */
1610 case N_NSYMS: /* Number of symbols (ultrix) */
1611 case N_NOMAP: /* No map? (ultrix) */
1612 case N_RSYM: /* Register variable */
1613 case N_DEFD: /* Modula-2 GNU module dependency */
1614 case N_SSYM: /* Struct or union element */
1615 case N_LSYM: /* Local symbol in stack */
1616 case N_PSYM: /* Parameter variable */
1617 case N_LENG: /* Length of preceding symbol type */
1618 if (name)
021959e2 1619 define_symbol (valu, name, desc, type, our_objfile);
bd5635a1
RP
1620 break;
1621
c55e6167
JG
1622 /* The following symbol types we don't know how to process. Handle
1623 them in a "default" way, but complain to people who care. */
bd5635a1 1624 default:
c55e6167
JG
1625 case N_EHDECL: /* Exception handler name */
1626 case N_MAIN: /* Name of main routine (not used in C) */
1627 case N_PC: /* Global symbol in Pascal */
1628 case N_M2C: /* Modula-2 compilation unit */
1629 /* N_MOD2: overlaps with N_EHDECL */
1630 case N_SCOPE: /* Modula-2 scope information */
1631 case N_ECOML: /* End common (local name) */
1632 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
1633 case N_NBDATA:
1634 case N_NBBSS:
1635 case N_NBSTS:
1636 case N_NBLCS:
1637 complain (&unknown_symtype_complaint, local_hex_string(type));
bd5635a1 1638 if (name)
021959e2 1639 define_symbol (valu, name, desc, type, our_objfile);
bd5635a1 1640 }
7e258d18
PB
1641
1642 previous_stab_code = type;
bd5635a1
RP
1643}
1644\f
bd5635a1
RP
1645/* Copy a pending list, used to record the contents of a common
1646 block for later fixup. */
1647static struct pending *
1648copy_pending (beg, begi, end)
021959e2 1649 struct pending *beg;
bd5635a1 1650 int begi;
021959e2 1651 struct pending *end;
bd5635a1
RP
1652{
1653 struct pending *new = 0;
1654 struct pending *next;
1655
1656 for (next = beg; next != 0 && (next != end || begi < end->nsyms);
1657 next = next->next, begi = 0)
1658 {
1659 register int j;
1660 for (j = begi; j < next->nsyms; j++)
1661 add_symbol_to_list (next->symbol[j], &new);
1662 }
1663 return new;
1664}
bd5635a1
RP
1665\f
1666/* Register our willingness to decode symbols for SunOS and a.out and
1667 b.out files handled by BFD... */
1668static struct sym_fns sunos_sym_fns = {"sunOs", 6,
9404978d 1669 dbx_new_init, dbx_symfile_init, dbx_symfile_read};
bd5635a1
RP
1670
1671static struct sym_fns aout_sym_fns = {"a.out", 5,
9404978d 1672 dbx_new_init, dbx_symfile_init, dbx_symfile_read};
bd5635a1
RP
1673
1674static struct sym_fns bout_sym_fns = {"b.out", 5,
9404978d 1675 dbx_new_init, dbx_symfile_init, dbx_symfile_read};
bd5635a1
RP
1676
1677void
1678_initialize_dbxread ()
1679{
1680 add_symtab_fns(&sunos_sym_fns);
1681 add_symtab_fns(&aout_sym_fns);
1682 add_symtab_fns(&bout_sym_fns);
bd5635a1 1683}
This page took 0.147135 seconds and 4 git commands to generate.