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