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