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