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