dbxread.c revision 1.7 1 /* Read dbx symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* This module provides three functions: dbx_symfile_init,
20 which initializes to read a symbol file; dbx_new_init, which
21 discards existing cached information when all symbols are being
22 discarded; and dbx_symfile_read, which reads a symbol table
23 from a file.
24
25 dbx_symfile_read only does the minimum work necessary for letting the
26 user "name" things symbolically; it does not read the entire symtab.
27 Instead, it reads the external and static symbols and puts them in partial
28 symbol tables. When more extensive information is requested of a
29 file, the corresponding partial symbol table is mutated into a full
30 fledged symbol table by going back and reading the symbols
31 for real. dbx_psymtab_to_symtab() is the function that does this */
32
33 #include "defs.h"
34 #if defined(__CYGNUSCLIB__)
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #endif
38
39 #include "gdb_obstack.h"
40 #include <sys/stat.h>
41 #include "symtab.h"
42 #include "breakpoint.h"
43 #include "target.h"
44 #include "gdbcore.h" /* for bfd stuff */
45 #include "libaout.h" /* FIXME Secret internal BFD stuff for a.out */
46 #include "filenames.h"
47 #include "objfiles.h"
48 #include "buildsym.h"
49 #include "stabsread.h"
50 #include "gdb-stabs.h"
51 #include "demangle.h"
52 #include "complaints.h"
53 #include "cp-abi.h"
54 #include "cp-support.h"
55 #include "psympriv.h"
56 #include "block.h"
57
58 #include "aout/aout64.h"
59 #include "aout/stab_gnu.h" /* We always use GNU stabs, not
60 native, now. */
61
62
64 /* Key for dbx-associated data. */
65
66 const struct objfile_data *dbx_objfile_data_key;
67
68 /* We put a pointer to this structure in the read_symtab_private field
69 of the psymtab. */
70
71 struct symloc
72 {
73 /* Offset within the file symbol table of first local symbol for this
74 file. */
75
76 int ldsymoff;
77
78 /* Length (in bytes) of the section of the symbol table devoted to
79 this file's symbols (actually, the section bracketed may contain
80 more than just this file's symbols). If ldsymlen is 0, the only
81 reason for this thing's existence is the dependency list. Nothing
82 else will happen when it is read in. */
83
84 int ldsymlen;
85
86 /* The size of each symbol in the symbol file (in external form). */
87
88 int symbol_size;
89
90 /* Further information needed to locate the symbols if they are in
91 an ELF file. */
92
93 int symbol_offset;
94 int string_offset;
95 int file_string_offset;
96 };
97
98 #define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
99 #define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
100 #define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
101 #define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
102 #define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
103 #define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
104 #define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
105
106
108 /* The objfile we are currently reading. */
109
110 static struct objfile *dbxread_objfile;
111
112 /* Remember what we deduced to be the source language of this psymtab. */
113
114 static enum language psymtab_language = language_unknown;
115
116 /* The BFD for this file -- implicit parameter to next_symbol_text. */
117
118 static bfd *symfile_bfd;
119
120 /* The size of each symbol in the symbol file (in external form).
121 This is set by dbx_symfile_read when building psymtabs, and by
122 dbx_psymtab_to_symtab when building symtabs. */
123
124 static unsigned symbol_size;
125
126 /* This is the offset of the symbol table in the executable file. */
127
128 static unsigned symbol_table_offset;
129
130 /* This is the offset of the string table in the executable file. */
131
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 in
136 the string table, and the associated symbols contain offsets from
137 this base. The following two variables contain the base offset for
138 the current and next .o files. */
139
140 static unsigned int file_string_table_offset;
141 static unsigned int next_file_string_table_offset;
142
143 /* .o and NLM files contain unrelocated addresses which are based at
144 0. When non-zero, this flag disables some of the special cases for
145 Solaris elf+stab text addresses at location 0. */
146
147 static int symfile_relocatable = 0;
148
149
150 /* The lowest text address we have yet encountered. This is needed
152 because in an a.out file, there is no header field which tells us
153 what address the program is actually going to be loaded at, so we
154 need to make guesses based on the symbols (which *are* relocated to
155 reflect the address it will be loaded at). */
156
157 static CORE_ADDR lowest_text_address;
158
159 /* Non-zero if there is any line number info in the objfile. Prevents
160 dbx_end_psymtab from discarding an otherwise empty psymtab. */
161
162 static int has_line_numbers;
163
164 /* Complaints about the symbols we have encountered. */
165
166 static void
167 unknown_symtype_complaint (const char *arg1)
168 {
169 complaint (&symfile_complaints, _("unknown symbol type %s"), arg1);
170 }
171
172 static void
173 lbrac_mismatch_complaint (int arg1)
174 {
175 complaint (&symfile_complaints,
176 _("N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d"), arg1);
177 }
178
179 static void
180 repeated_header_complaint (const char *arg1, int arg2)
181 {
182 complaint (&symfile_complaints,
183 _("\"repeated\" header file %s not "
184 "previously seen, at symtab pos %d"),
185 arg1, arg2);
186 }
187
188 /* find_text_range --- find start and end of loadable code sections
189
190 The find_text_range function finds the shortest address range that
191 encloses all sections containing executable code, and stores it in
192 objfile's text_addr and text_size members.
193
194 dbx_symfile_read will use this to finish off the partial symbol
195 table, in some cases. */
196
197 static void
198 find_text_range (bfd * sym_bfd, struct objfile *objfile)
199 {
200 asection *sec;
201 int found_any = 0;
202 CORE_ADDR start = 0;
203 CORE_ADDR end = 0;
204
205 for (sec = sym_bfd->sections; sec; sec = sec->next)
206 if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
207 {
208 CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
209 CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
210
211 if (found_any)
212 {
213 if (sec_start < start)
214 start = sec_start;
215 if (sec_end > end)
216 end = sec_end;
217 }
218 else
219 {
220 start = sec_start;
221 end = sec_end;
222 }
223
224 found_any = 1;
225 }
226
227 if (!found_any)
228 error (_("Can't find any code sections in symbol file"));
229
230 DBX_TEXT_ADDR (objfile) = start;
231 DBX_TEXT_SIZE (objfile) = end - start;
232 }
233
234
236
237 /* During initial symbol readin, we need to have a structure to keep
238 track of which psymtabs have which bincls in them. This structure
239 is used during readin to setup the list of dependencies within each
240 partial symbol table. */
241
242 struct header_file_location
243 {
244 const char *name; /* Name of header file */
245 int instance; /* See above */
246 struct partial_symtab *pst; /* Partial symtab that has the
247 BINCL/EINCL defs for this file. */
248 };
249
250 /* The actual list and controling variables. */
251 static struct header_file_location *bincl_list, *next_bincl;
252 static int bincls_allocated;
253
254 /* Local function prototypes. */
255
256 extern void _initialize_dbxread (void);
257
258 static void read_ofile_symtab (struct objfile *, struct partial_symtab *);
259
260 static void dbx_read_symtab (struct partial_symtab *self,
261 struct objfile *objfile);
262
263 static void dbx_psymtab_to_symtab_1 (struct objfile *, struct partial_symtab *);
264
265 static void read_dbx_symtab (minimal_symbol_reader &, struct objfile *);
266
267 static void free_bincl_list (struct objfile *);
268
269 static struct partial_symtab *find_corresponding_bincl_psymtab (const char *,
270 int);
271
272 static void add_bincl_to_list (struct partial_symtab *, const char *, int);
273
274 static void init_bincl_list (int, struct objfile *);
275
276 static const char *dbx_next_symbol_text (struct objfile *);
277
278 static void fill_symbuf (bfd *);
279
280 static void dbx_symfile_init (struct objfile *);
281
282 static void dbx_new_init (struct objfile *);
283
284 static void dbx_symfile_read (struct objfile *, symfile_add_flags);
285
286 static void dbx_symfile_finish (struct objfile *);
287
288 static void record_minimal_symbol (minimal_symbol_reader &,
289 const char *, CORE_ADDR, int,
290 struct objfile *);
291
292 static void add_new_header_file (const char *, int);
293
294 static void add_old_header_file (const char *, int);
295
296 static void add_this_object_header_file (int);
297
298 static struct partial_symtab *start_psymtab (struct objfile *, const char *,
299 CORE_ADDR, int,
300 struct partial_symbol **,
301 struct partial_symbol **);
302
303 /* Free up old header file tables. */
304
305 void
306 free_header_files (void)
307 {
308 if (this_object_header_files)
309 {
310 xfree (this_object_header_files);
311 this_object_header_files = NULL;
312 }
313 n_allocated_this_object_header_files = 0;
314 }
315
316 /* Allocate new header file tables. */
317
318 void
319 init_header_files (void)
320 {
321 n_allocated_this_object_header_files = 10;
322 this_object_header_files = XNEWVEC (int, 10);
323 }
324
325 /* Add header file number I for this object file
326 at the next successive FILENUM. */
327
328 static void
329 add_this_object_header_file (int i)
330 {
331 if (n_this_object_header_files == n_allocated_this_object_header_files)
332 {
333 n_allocated_this_object_header_files *= 2;
334 this_object_header_files
335 = (int *) xrealloc ((char *) this_object_header_files,
336 n_allocated_this_object_header_files * sizeof (int));
337 }
338
339 this_object_header_files[n_this_object_header_files++] = i;
340 }
341
342 /* Add to this file an "old" header file, one already seen in
343 a previous object file. NAME is the header file's name.
344 INSTANCE is its instance code, to select among multiple
345 symbol tables for the same header file. */
346
347 static void
348 add_old_header_file (const char *name, int instance)
349 {
350 struct header_file *p = HEADER_FILES (dbxread_objfile);
351 int i;
352
353 for (i = 0; i < N_HEADER_FILES (dbxread_objfile); i++)
354 if (filename_cmp (p[i].name, name) == 0 && instance == p[i].instance)
355 {
356 add_this_object_header_file (i);
357 return;
358 }
359 repeated_header_complaint (name, symnum);
360 }
361
362 /* Add to this file a "new" header file: definitions for its types follow.
363 NAME is the header file's name.
364 Most often this happens only once for each distinct header file,
365 but not necessarily. If it happens more than once, INSTANCE has
366 a different value each time, and references to the header file
367 use INSTANCE values to select among them.
368
369 dbx output contains "begin" and "end" markers for each new header file,
370 but at this level we just need to know which files there have been;
371 so we record the file when its "begin" is seen and ignore the "end". */
372
373 static void
374 add_new_header_file (const char *name, int instance)
375 {
376 int i;
377 struct header_file *hfile;
378
379 /* Make sure there is room for one more header file. */
380
381 i = N_ALLOCATED_HEADER_FILES (dbxread_objfile);
382
383 if (N_HEADER_FILES (dbxread_objfile) == i)
384 {
385 if (i == 0)
386 {
387 N_ALLOCATED_HEADER_FILES (dbxread_objfile) = 10;
388 HEADER_FILES (dbxread_objfile) = (struct header_file *)
389 xmalloc (10 * sizeof (struct header_file));
390 }
391 else
392 {
393 i *= 2;
394 N_ALLOCATED_HEADER_FILES (dbxread_objfile) = i;
395 HEADER_FILES (dbxread_objfile) = (struct header_file *)
396 xrealloc ((char *) HEADER_FILES (dbxread_objfile),
397 (i * sizeof (struct header_file)));
398 }
399 }
400
401 /* Create an entry for this header file. */
402
403 i = N_HEADER_FILES (dbxread_objfile)++;
404 hfile = HEADER_FILES (dbxread_objfile) + i;
405 hfile->name = xstrdup (name);
406 hfile->instance = instance;
407 hfile->length = 10;
408 hfile->vector = XCNEWVEC (struct type *, 10);
409
410 add_this_object_header_file (i);
411 }
412
413 #if 0
414 static struct type **
415 explicit_lookup_type (int real_filenum, int index)
416 {
417 struct header_file *f = &HEADER_FILES (dbxread_objfile)[real_filenum];
418
419 if (index >= f->length)
420 {
421 f->length *= 2;
422 f->vector = (struct type **)
423 xrealloc (f->vector, f->length * sizeof (struct type *));
424 memset (&f->vector[f->length / 2],
425 '\0', f->length * sizeof (struct type *) / 2);
426 }
427 return &f->vector[index];
428 }
429 #endif
430
431 static void
433 record_minimal_symbol (minimal_symbol_reader &reader,
434 const char *name, CORE_ADDR address, int type,
435 struct objfile *objfile)
436 {
437 enum minimal_symbol_type ms_type;
438 int section;
439
440 switch (type)
441 {
442 case N_TEXT | N_EXT:
443 ms_type = mst_text;
444 section = SECT_OFF_TEXT (objfile);
445 break;
446 case N_DATA | N_EXT:
447 ms_type = mst_data;
448 section = SECT_OFF_DATA (objfile);
449 break;
450 case N_BSS | N_EXT:
451 ms_type = mst_bss;
452 section = SECT_OFF_BSS (objfile);
453 break;
454 case N_ABS | N_EXT:
455 ms_type = mst_abs;
456 section = -1;
457 break;
458 #ifdef N_SETV
459 case N_SETV | N_EXT:
460 ms_type = mst_data;
461 section = SECT_OFF_DATA (objfile);
462 break;
463 case N_SETV:
464 /* I don't think this type actually exists; since a N_SETV is the result
465 of going over many .o files, it doesn't make sense to have one
466 file local. */
467 ms_type = mst_file_data;
468 section = SECT_OFF_DATA (objfile);
469 break;
470 #endif
471 case N_TEXT:
472 case N_NBTEXT:
473 case N_FN:
474 case N_FN_SEQ:
475 ms_type = mst_file_text;
476 section = SECT_OFF_TEXT (objfile);
477 break;
478 case N_DATA:
479 ms_type = mst_file_data;
480
481 /* Check for __DYNAMIC, which is used by Sun shared libraries.
482 Record it as global even if it's local, not global, so
483 lookup_minimal_symbol can find it. We don't check symbol_leading_char
484 because for SunOS4 it always is '_'. */
485 if (name[8] == 'C' && strcmp ("__DYNAMIC", name) == 0)
486 ms_type = mst_data;
487
488 /* Same with virtual function tables, both global and static. */
489 {
490 const char *tempstring = name;
491
492 if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
493 ++tempstring;
494 if (is_vtable_name (tempstring))
495 ms_type = mst_data;
496 }
497 section = SECT_OFF_DATA (objfile);
498 break;
499 case N_BSS:
500 ms_type = mst_file_bss;
501 section = SECT_OFF_BSS (objfile);
502 break;
503 default:
504 ms_type = mst_unknown;
505 section = -1;
506 break;
507 }
508
509 if ((ms_type == mst_file_text || ms_type == mst_text)
510 && address < lowest_text_address)
511 lowest_text_address = address;
512
513 reader.record_with_info (name, address, ms_type, section);
514 }
515
516 /* Scan and build partial symbols for a symbol file.
518 We have been initialized by a call to dbx_symfile_init, which
519 put all the relevant info into a "struct dbx_symfile_info",
520 hung off the objfile structure. */
521
522 static void
523 dbx_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
524 {
525 bfd *sym_bfd;
526 int val;
527 struct cleanup *back_to;
528
529 sym_bfd = objfile->obfd;
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. */
534
535 symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
536
537 val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
538 if (val < 0)
539 perror_with_name (objfile_name (objfile));
540
541 /* Size the symbol table. */
542 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
543 init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
544
545 symbol_size = DBX_SYMBOL_SIZE (objfile);
546 symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
547
548 free_pending_blocks ();
549 back_to = make_cleanup (really_free_pendings, 0);
550
551 minimal_symbol_reader reader (objfile);
552
553 /* Read stabs data from executable file and define symbols. */
554
555 read_dbx_symtab (reader, objfile);
556
557 /* Install any minimal symbols that have been collected as the current
558 minimal symbols for this objfile. */
559
560 reader.install ();
561
562 do_cleanups (back_to);
563 }
564
565 /* Initialize anything that needs initializing when a completely new
566 symbol file is specified (not just adding some symbols from another
567 file, e.g. a shared library). */
568
569 static void
570 dbx_new_init (struct objfile *ignore)
571 {
572 stabsread_new_init ();
573 buildsym_new_init ();
574 init_header_files ();
575 }
576
577
578 /* dbx_symfile_init ()
579 is the dbx-specific initialization routine for reading symbols.
580 It is passed a struct objfile which contains, among other things,
581 the BFD for the file whose symbols are being read, and a slot for a pointer
582 to "private data" which we fill with goodies.
583
584 We read the string table into malloc'd space and stash a pointer to it.
585
586 Since BFD doesn't know how to read debug symbols in a format-independent
587 way (and may never do so...), we have to do it ourselves. We will never
588 be called unless this is an a.out (or very similar) file.
589 FIXME, there should be a cleaner peephole into the BFD environment here. */
590
591 #define DBX_STRINGTAB_SIZE_SIZE sizeof(long) /* FIXME */
592
593 static void
594 dbx_symfile_init (struct objfile *objfile)
595 {
596 int val;
597 bfd *sym_bfd = objfile->obfd;
598 char *name = bfd_get_filename (sym_bfd);
599 asection *text_sect;
600 unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
601 struct dbx_symfile_info *dbx;
602
603 /* Allocate struct to keep track of the symfile. */
604 dbx = XCNEW (struct dbx_symfile_info);
605 set_objfile_data (objfile, dbx_objfile_data_key, dbx);
606
607 DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
608 DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
609 DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
610
611 /* FIXME POKING INSIDE BFD DATA STRUCTURES. */
612 #define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
613 #define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
614
615 /* FIXME POKING INSIDE BFD DATA STRUCTURES. */
616
617 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
618 if (!text_sect)
619 error (_("Can't find .text section in symbol file"));
620 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
621 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
622
623 DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
624 DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
625 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
626
627 /* Read the string table and stash it away in the objfile_obstack.
628 When we blow away the objfile the string table goes away as well.
629 Note that gdb used to use the results of attempting to malloc the
630 string table, based on the size it read, as a form of sanity check
631 for botched byte swapping, on the theory that a byte swapped string
632 table size would be so totally bogus that the malloc would fail. Now
633 that we put in on the objfile_obstack, we can't do this since gdb gets
634 a fatal error (out of virtual memory) if the size is bogus. We can
635 however at least check to see if the size is less than the size of
636 the size field itself, or larger than the size of the entire file.
637 Note that all valid string tables have a size greater than zero, since
638 the bytes used to hold the size are included in the count. */
639
640 if (STRING_TABLE_OFFSET == 0)
641 {
642 /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
643 will never be zero, even when there is no string table. This
644 would appear to be a bug in bfd. */
645 DBX_STRINGTAB_SIZE (objfile) = 0;
646 DBX_STRINGTAB (objfile) = NULL;
647 }
648 else
649 {
650 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
651 if (val < 0)
652 perror_with_name (name);
653
654 memset (size_temp, 0, sizeof (size_temp));
655 val = bfd_bread (size_temp, sizeof (size_temp), sym_bfd);
656 if (val < 0)
657 {
658 perror_with_name (name);
659 }
660 else if (val == 0)
661 {
662 /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
663 EOF if there is no string table, and attempting to read the size
664 from EOF will read zero bytes. */
665 DBX_STRINGTAB_SIZE (objfile) = 0;
666 DBX_STRINGTAB (objfile) = NULL;
667 }
668 else
669 {
670 /* Read some data that would appear to be the string table size.
671 If there really is a string table, then it is probably the right
672 size. Byteswap if necessary and validate the size. Note that
673 the minimum is DBX_STRINGTAB_SIZE_SIZE. If we just read some
674 random data that happened to be at STRING_TABLE_OFFSET, because
675 bfd can't tell us there is no string table, the sanity checks may
676 or may not catch this. */
677 DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
678
679 if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
680 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
681 error (_("ridiculous string table size (%d bytes)."),
682 DBX_STRINGTAB_SIZE (objfile));
683
684 DBX_STRINGTAB (objfile) =
685 (char *) obstack_alloc (&objfile->objfile_obstack,
686 DBX_STRINGTAB_SIZE (objfile));
687 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
688
689 /* Now read in the string table in one big gulp. */
690
691 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
692 if (val < 0)
693 perror_with_name (name);
694 val = bfd_bread (DBX_STRINGTAB (objfile),
695 DBX_STRINGTAB_SIZE (objfile),
696 sym_bfd);
697 if (val != DBX_STRINGTAB_SIZE (objfile))
698 perror_with_name (name);
699 }
700 }
701 }
702
703 /* Perform any local cleanups required when we are done with a particular
704 objfile. I.E, we are in the process of discarding all symbol information
705 for an objfile, freeing up all memory held for it, and unlinking the
706 objfile struct from the global list of known objfiles. */
707
708 static void
709 dbx_symfile_finish (struct objfile *objfile)
710 {
711 free_header_files ();
712 }
713
714 static void
715 dbx_free_symfile_info (struct objfile *objfile, void *arg)
716 {
717 struct dbx_symfile_info *dbx = (struct dbx_symfile_info *) arg;
718
719 if (dbx->header_files != NULL)
720 {
721 int i = dbx->n_header_files;
722 struct header_file *hfiles = dbx->header_files;
723
724 while (--i >= 0)
725 {
726 xfree (hfiles[i].name);
727 xfree (hfiles[i].vector);
728 }
729 xfree (hfiles);
730 }
731
732 xfree (dbx);
733 }
734
735
736
738 /* Buffer for reading the symbol table entries. */
739 static struct external_nlist symbuf[4096];
740 static int symbuf_idx;
741 static int symbuf_end;
742
743 /* Name of last function encountered. Used in Solaris to approximate
744 object file boundaries. */
745 static const char *last_function_name;
746
747 /* The address in memory of the string table of the object file we are
748 reading (which might not be the "main" object file, but might be a
749 shared library or some other dynamically loaded thing). This is
750 set by read_dbx_symtab when building psymtabs, and by
751 read_ofile_symtab when building symtabs, and is used only by
752 next_symbol_text. FIXME: If that is true, we don't need it when
753 building psymtabs, right? */
754 static char *stringtab_global;
755
756 /* These variables are used to control fill_symbuf when the stabs
757 symbols are not contiguous (as may be the case when a COFF file is
758 linked using --split-by-reloc). */
759 static struct stab_section_list *symbuf_sections;
760 static unsigned int symbuf_left;
761 static unsigned int symbuf_read;
762
763 /* This variable stores a global stabs buffer, if we read stabs into
764 memory in one chunk in order to process relocations. */
765 static bfd_byte *stabs_data;
766
767 /* Refill the symbol table input buffer
768 and set the variables that control fetching entries from it.
769 Reports an error if no data available.
770 This function can read past the end of the symbol table
771 (into the string table) but this does no harm. */
772
773 static void
774 fill_symbuf (bfd *sym_bfd)
775 {
776 unsigned int count;
777 int nbytes;
778
779 if (stabs_data)
780 {
781 nbytes = sizeof (symbuf);
782 if (nbytes > symbuf_left)
783 nbytes = symbuf_left;
784 memcpy (symbuf, stabs_data + symbuf_read, nbytes);
785 }
786 else if (symbuf_sections == NULL)
787 {
788 count = sizeof (symbuf);
789 nbytes = bfd_bread (symbuf, count, sym_bfd);
790 }
791 else
792 {
793 if (symbuf_left <= 0)
794 {
795 file_ptr filepos = symbuf_sections->section->filepos;
796
797 if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
798 perror_with_name (bfd_get_filename (sym_bfd));
799 symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
800 symbol_table_offset = filepos - symbuf_read;
801 symbuf_sections = symbuf_sections->next;
802 }
803
804 count = symbuf_left;
805 if (count > sizeof (symbuf))
806 count = sizeof (symbuf);
807 nbytes = bfd_bread (symbuf, count, sym_bfd);
808 }
809
810 if (nbytes < 0)
811 perror_with_name (bfd_get_filename (sym_bfd));
812 else if (nbytes == 0)
813 error (_("Premature end of file reading symbol table"));
814 symbuf_end = nbytes / symbol_size;
815 symbuf_idx = 0;
816 symbuf_left -= nbytes;
817 symbuf_read += nbytes;
818 }
819
820 static void
821 stabs_seek (int sym_offset)
822 {
823 if (stabs_data)
824 {
825 symbuf_read += sym_offset;
826 symbuf_left -= sym_offset;
827 }
828 else
829 bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
830 }
831
832 #define INTERNALIZE_SYMBOL(intern, extern, abfd) \
833 { \
834 (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx); \
835 (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type); \
836 (intern).n_other = 0; \
837 (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc); \
838 if (bfd_get_sign_extend_vma (abfd)) \
839 (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value); \
840 else \
841 (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value); \
842 }
843
844 /* Invariant: The symbol pointed to by symbuf_idx is the first one
845 that hasn't been swapped. Swap the symbol at the same time
846 that symbuf_idx is incremented. */
847
848 /* dbx allows the text of a symbol name to be continued into the
849 next symbol name! When such a continuation is encountered
850 (a \ at the end of the text of a name)
851 call this function to get the continuation. */
852
853 static const char *
854 dbx_next_symbol_text (struct objfile *objfile)
855 {
856 struct internal_nlist nlist;
857
858 if (symbuf_idx == symbuf_end)
859 fill_symbuf (symfile_bfd);
860
861 symnum++;
862 INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
863 OBJSTAT (objfile, n_stabs++);
864
865 symbuf_idx++;
866
867 return nlist.n_strx + stringtab_global + file_string_table_offset;
868 }
869
870 /* Initialize the list of bincls to contain none and have some
872 allocated. */
873
874 static void
875 init_bincl_list (int number, struct objfile *objfile)
876 {
877 bincls_allocated = number;
878 next_bincl = bincl_list = XNEWVEC (struct header_file_location,
879 bincls_allocated);
880 }
881
882 /* Add a bincl to the list. */
883
884 static void
885 add_bincl_to_list (struct partial_symtab *pst, const char *name, int instance)
886 {
887 if (next_bincl >= bincl_list + bincls_allocated)
888 {
889 int offset = next_bincl - bincl_list;
890
891 bincls_allocated *= 2;
892 bincl_list = (struct header_file_location *)
893 xrealloc ((char *) bincl_list,
894 bincls_allocated * sizeof (struct header_file_location));
895 next_bincl = bincl_list + offset;
896 }
897 next_bincl->pst = pst;
898 next_bincl->instance = instance;
899 next_bincl++->name = name;
900 }
901
902 /* Given a name, value pair, find the corresponding
903 bincl in the list. Return the partial symtab associated
904 with that header_file_location. */
905
906 static struct partial_symtab *
907 find_corresponding_bincl_psymtab (const char *name, int instance)
908 {
909 struct header_file_location *bincl;
910
911 for (bincl = bincl_list; bincl < next_bincl; bincl++)
912 if (bincl->instance == instance
913 && strcmp (name, bincl->name) == 0)
914 return bincl->pst;
915
916 repeated_header_complaint (name, symnum);
917 return (struct partial_symtab *) 0;
918 }
919
920 /* Free the storage allocated for the bincl list. */
921
922 static void
923 free_bincl_list (struct objfile *objfile)
924 {
925 xfree (bincl_list);
926 bincls_allocated = 0;
927 }
928
929 static void
930 do_free_bincl_list_cleanup (void *objfile)
931 {
932 free_bincl_list ((struct objfile *) objfile);
933 }
934
935 static struct cleanup *
936 make_cleanup_free_bincl_list (struct objfile *objfile)
937 {
938 return make_cleanup (do_free_bincl_list_cleanup, objfile);
939 }
940
941 /* Set namestring based on nlist. If the string table index is invalid,
942 give a fake name, and print a single error message per symbol file read,
943 rather than abort the symbol reading or flood the user with messages. */
944
945 static const char *
946 set_namestring (struct objfile *objfile, const struct internal_nlist *nlist)
947 {
948 const char *namestring;
949
950 if (nlist->n_strx + file_string_table_offset
951 >= DBX_STRINGTAB_SIZE (objfile)
952 || nlist->n_strx + file_string_table_offset < nlist->n_strx)
953 {
954 complaint (&symfile_complaints,
955 _("bad string table offset in symbol %d"),
956 symnum);
957 namestring = "<bad string table offset>";
958 }
959 else
960 namestring = (nlist->n_strx + file_string_table_offset
961 + DBX_STRINGTAB (objfile));
962 return namestring;
963 }
964
965 static CORE_ADDR
966 find_stab_function_addr (const char *namestring, const char *filename,
967 struct objfile *objfile)
968 {
969 struct bound_minimal_symbol msym;
970 int n;
971
972 const char *colon = strchr (namestring, ':');
973 if (colon == NULL)
974 n = 0;
975 else
976 n = colon - namestring;
977
978 char *p = (char *) alloca (n + 2);
979 strncpy (p, namestring, n);
980 p[n] = 0;
981
982 msym = lookup_minimal_symbol (p, filename, objfile);
983 if (msym.minsym == NULL)
984 {
985 /* Sun Fortran appends an underscore to the minimal symbol name,
986 try again with an appended underscore if the minimal symbol
987 was not found. */
988 p[n] = '_';
989 p[n + 1] = 0;
990 msym = lookup_minimal_symbol (p, filename, objfile);
991 }
992
993 if (msym.minsym == NULL && filename != NULL)
994 {
995 /* Try again without the filename. */
996 p[n] = 0;
997 msym = lookup_minimal_symbol (p, NULL, objfile);
998 }
999 if (msym.minsym == NULL && filename != NULL)
1000 {
1001 /* And try again for Sun Fortran, but without the filename. */
1002 p[n] = '_';
1003 p[n + 1] = 0;
1004 msym = lookup_minimal_symbol (p, NULL, objfile);
1005 }
1006
1007 return msym.minsym == NULL ? 0 : BMSYMBOL_VALUE_ADDRESS (msym);
1008 }
1009
1010 static void
1011 function_outside_compilation_unit_complaint (const char *arg1)
1012 {
1013 complaint (&symfile_complaints,
1014 _("function `%s' appears to be defined "
1015 "outside of all compilation units"),
1016 arg1);
1017 }
1018
1019 /* Setup partial_symtab's describing each source file for which
1020 debugging information is available. */
1021
1022 static void
1023 read_dbx_symtab (minimal_symbol_reader &reader, struct objfile *objfile)
1024 {
1025 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1026 struct external_nlist *bufp = 0; /* =0 avoids gcc -Wall glitch. */
1027 struct internal_nlist nlist;
1028 CORE_ADDR text_addr;
1029 int text_size;
1030 const char *sym_name;
1031 int sym_len;
1032
1033 const char *namestring;
1034 int nsl;
1035 int past_first_source_file = 0;
1036 CORE_ADDR last_function_start = 0;
1037 struct cleanup *back_to;
1038 bfd *abfd;
1039 int textlow_not_set;
1040 int data_sect_index;
1041
1042 /* Current partial symtab. */
1043 struct partial_symtab *pst;
1044
1045 /* List of current psymtab's include files. */
1046 const char **psymtab_include_list;
1047 int includes_allocated;
1048 int includes_used;
1049
1050 /* Index within current psymtab dependency list. */
1051 struct partial_symtab **dependency_list;
1052 int dependencies_used, dependencies_allocated;
1053
1054 text_addr = DBX_TEXT_ADDR (objfile);
1055 text_size = DBX_TEXT_SIZE (objfile);
1056
1057 /* FIXME. We probably want to change stringtab_global rather than add this
1058 while processing every symbol entry. FIXME. */
1059 file_string_table_offset = 0;
1060 next_file_string_table_offset = 0;
1061
1062 stringtab_global = DBX_STRINGTAB (objfile);
1063
1064 pst = (struct partial_symtab *) 0;
1065
1066 includes_allocated = 30;
1067 includes_used = 0;
1068 psymtab_include_list = (const char **) alloca (includes_allocated *
1069 sizeof (const char *));
1070
1071 dependencies_allocated = 30;
1072 dependencies_used = 0;
1073 dependency_list =
1074 (struct partial_symtab **) alloca (dependencies_allocated *
1075 sizeof (struct partial_symtab *));
1076
1077 /* Init bincl list */
1078 init_bincl_list (20, objfile);
1079 back_to = make_cleanup_free_bincl_list (objfile);
1080
1081 set_last_source_file (NULL);
1082
1083 lowest_text_address = (CORE_ADDR) -1;
1084
1085 symfile_bfd = objfile->obfd; /* For next_text_symbol. */
1086 abfd = objfile->obfd;
1087 symbuf_end = symbuf_idx = 0;
1088 next_symbol_text_func = dbx_next_symbol_text;
1089 textlow_not_set = 1;
1090 has_line_numbers = 0;
1091
1092 /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
1093 to global and static variables. The stab for a global or static
1094 variable doesn't give us any indication of which section it's in,
1095 so we can't tell immediately which offset in
1096 objfile->section_offsets we should apply to the variable's
1097 address.
1098
1099 We could certainly find out which section contains the variable
1100 by looking up the variable's unrelocated address with
1101 find_pc_section, but that would be expensive; this is the
1102 function that constructs the partial symbol tables by examining
1103 every symbol in the entire executable, and it's
1104 performance-critical. So that expense would not be welcome. I'm
1105 not sure what to do about this at the moment.
1106
1107 What we have done for years is to simply assume that the .data
1108 section's offset is appropriate for all global and static
1109 variables. Recently, this was expanded to fall back to the .bss
1110 section's offset if there is no .data section, and then to the
1111 .rodata section's offset. */
1112 data_sect_index = objfile->sect_index_data;
1113 if (data_sect_index == -1)
1114 data_sect_index = SECT_OFF_BSS (objfile);
1115 if (data_sect_index == -1)
1116 data_sect_index = SECT_OFF_RODATA (objfile);
1117
1118 /* If data_sect_index is still -1, that's okay. It's perfectly fine
1119 for the file to have no .data, no .bss, and no .text at all, if
1120 it also has no global or static variables. If it does, we will
1121 get an internal error from an ANOFFSET macro below when we try to
1122 use data_sect_index. */
1123
1124 for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1125 {
1126 /* Get the symbol for this run and pull out some info. */
1127 QUIT; /* Allow this to be interruptable. */
1128 if (symbuf_idx == symbuf_end)
1129 fill_symbuf (abfd);
1130 bufp = &symbuf[symbuf_idx++];
1131
1132 /*
1133 * Special case to speed up readin.
1134 */
1135 if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
1136 {
1137 has_line_numbers = 1;
1138 continue;
1139 }
1140
1141 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1142 OBJSTAT (objfile, n_stabs++);
1143
1144 /* Ok. There is a lot of code duplicated in the rest of this
1145 switch statement (for efficiency reasons). Since I don't
1146 like duplicating code, I will do my penance here, and
1147 describe the code which is duplicated:
1148
1149 *) The assignment to namestring.
1150 *) The call to strchr.
1151 *) The addition of a partial symbol the two partial
1152 symbol lists. This last is a large section of code, so
1153 I've imbedded it in the following macro. */
1154
1155 switch (nlist.n_type)
1156 {
1157 /*
1158 * Standard, external, non-debugger, symbols
1159 */
1160
1161 case N_TEXT | N_EXT:
1162 case N_NBTEXT | N_EXT:
1163 goto record_it;
1164
1165 case N_DATA | N_EXT:
1166 case N_NBDATA | N_EXT:
1167 goto record_it;
1168
1169 case N_BSS:
1170 case N_BSS | N_EXT:
1171 case N_NBBSS | N_EXT:
1172 case N_SETV | N_EXT: /* FIXME, is this in BSS? */
1173 goto record_it;
1174
1175 case N_ABS | N_EXT:
1176 record_it:
1177 namestring = set_namestring (objfile, &nlist);
1178
1179 record_minimal_symbol (reader, namestring, nlist.n_value,
1180 nlist.n_type, objfile); /* Always */
1181 continue;
1182
1183 /* Standard, local, non-debugger, symbols. */
1184
1185 case N_NBTEXT:
1186
1187 /* We need to be able to deal with both N_FN or N_TEXT,
1188 because we have no way of knowing whether the sys-supplied ld
1189 or GNU ld was used to make the executable. Sequents throw
1190 in another wrinkle -- they renumbered N_FN. */
1191
1192 case N_FN:
1193 case N_FN_SEQ:
1194 case N_TEXT:
1195 namestring = set_namestring (objfile, &nlist);
1196
1197 if ((namestring[0] == '-' && namestring[1] == 'l')
1198 || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
1199 && namestring[nsl - 2] == '.'))
1200 {
1201 nlist.n_value += ANOFFSET (objfile->section_offsets,
1202 SECT_OFF_TEXT (objfile));
1203
1204 if (past_first_source_file && pst
1205 /* The gould NP1 uses low values for .o and -l symbols
1206 which are not the address. */
1207 && nlist.n_value >= pst->textlow)
1208 {
1209 dbx_end_psymtab (objfile, pst, psymtab_include_list,
1210 includes_used, symnum * symbol_size,
1211 nlist.n_value > pst->texthigh
1212 ? nlist.n_value : pst->texthigh,
1213 dependency_list, dependencies_used,
1214 textlow_not_set);
1215 pst = (struct partial_symtab *) 0;
1216 includes_used = 0;
1217 dependencies_used = 0;
1218 has_line_numbers = 0;
1219 }
1220 else
1221 past_first_source_file = 1;
1222 }
1223 else
1224 goto record_it;
1225 continue;
1226
1227 case N_DATA:
1228 goto record_it;
1229
1230 case N_UNDF | N_EXT:
1231 /* The case (nlist.n_value != 0) is a "Fortran COMMON" symbol.
1232 We used to rely on the target to tell us whether it knows
1233 where the symbol has been relocated to, but none of the
1234 target implementations actually provided that operation.
1235 So we just ignore the symbol, the same way we would do if
1236 we had a target-side symbol lookup which returned no match.
1237
1238 All other symbols (with nlist.n_value == 0), are really
1239 undefined, and so we ignore them too. */
1240 continue;
1241
1242 case N_UNDF:
1243 if (processing_acc_compilation && nlist.n_strx == 1)
1244 {
1245 /* Deal with relative offsets in the string table
1246 used in ELF+STAB under Solaris. If we want to use the
1247 n_strx field, which contains the name of the file,
1248 we must adjust file_string_table_offset *before* calling
1249 set_namestring(). */
1250 past_first_source_file = 1;
1251 file_string_table_offset = next_file_string_table_offset;
1252 next_file_string_table_offset =
1253 file_string_table_offset + nlist.n_value;
1254 if (next_file_string_table_offset < file_string_table_offset)
1255 error (_("string table offset backs up at %d"), symnum);
1256 /* FIXME -- replace error() with complaint. */
1257 continue;
1258 }
1259 continue;
1260
1261 /* Lots of symbol types we can just ignore. */
1262
1263 case N_ABS:
1264 case N_NBDATA:
1265 case N_NBBSS:
1266 continue;
1267
1268 /* Keep going . . . */
1269
1270 /*
1271 * Special symbol types for GNU
1272 */
1273 case N_INDR:
1274 case N_INDR | N_EXT:
1275 case N_SETA:
1276 case N_SETA | N_EXT:
1277 case N_SETT:
1278 case N_SETT | N_EXT:
1279 case N_SETD:
1280 case N_SETD | N_EXT:
1281 case N_SETB:
1282 case N_SETB | N_EXT:
1283 case N_SETV:
1284 continue;
1285
1286 /*
1287 * Debugger symbols
1288 */
1289
1290 case N_SO:
1291 {
1292 CORE_ADDR valu;
1293 static int prev_so_symnum = -10;
1294 static int first_so_symnum;
1295 const char *p;
1296 static const char *dirname_nso;
1297 int prev_textlow_not_set;
1298
1299 valu = nlist.n_value + ANOFFSET (objfile->section_offsets,
1300 SECT_OFF_TEXT (objfile));
1301
1302 prev_textlow_not_set = textlow_not_set;
1303
1304 /* A zero value is probably an indication for the SunPRO 3.0
1305 compiler. dbx_end_psymtab explicitly tests for zero, so
1306 don't relocate it. */
1307
1308 if (nlist.n_value == 0
1309 && gdbarch_sofun_address_maybe_missing (gdbarch))
1310 {
1311 textlow_not_set = 1;
1312 valu = 0;
1313 }
1314 else
1315 textlow_not_set = 0;
1316
1317 past_first_source_file = 1;
1318
1319 if (prev_so_symnum != symnum - 1)
1320 { /* Here if prev stab wasn't N_SO. */
1321 first_so_symnum = symnum;
1322
1323 if (pst)
1324 {
1325 dbx_end_psymtab (objfile, pst, psymtab_include_list,
1326 includes_used, symnum * symbol_size,
1327 valu > pst->texthigh
1328 ? valu : pst->texthigh,
1329 dependency_list, dependencies_used,
1330 prev_textlow_not_set);
1331 pst = (struct partial_symtab *) 0;
1332 includes_used = 0;
1333 dependencies_used = 0;
1334 has_line_numbers = 0;
1335 }
1336 }
1337
1338 prev_so_symnum = symnum;
1339
1340 /* End the current partial symtab and start a new one. */
1341
1342 namestring = set_namestring (objfile, &nlist);
1343
1344 /* Null name means end of .o file. Don't start a new one. */
1345 if (*namestring == '\000')
1346 continue;
1347
1348 /* Some compilers (including gcc) emit a pair of initial N_SOs.
1349 The first one is a directory name; the second the file name.
1350 If pst exists, is empty, and has a filename ending in '/',
1351 we assume the previous N_SO was a directory name. */
1352
1353 p = lbasename (namestring);
1354 if (p != namestring && *p == '\000')
1355 {
1356 /* Save the directory name SOs locally, then save it into
1357 the psymtab when it's created below. */
1358 dirname_nso = namestring;
1359 continue;
1360 }
1361
1362 /* Some other compilers (C++ ones in particular) emit useless
1363 SOs for non-existant .c files. We ignore all subsequent SOs
1364 that immediately follow the first. */
1365
1366 if (!pst)
1367 {
1368 pst = start_psymtab (objfile,
1369 namestring, valu,
1370 first_so_symnum * symbol_size,
1371 objfile->global_psymbols.next,
1372 objfile->static_psymbols.next);
1373 pst->dirname = dirname_nso;
1374 dirname_nso = NULL;
1375 }
1376 continue;
1377 }
1378
1379 case N_BINCL:
1380 {
1381 enum language tmp_language;
1382
1383 /* Add this bincl to the bincl_list for future EXCLs. No
1384 need to save the string; it'll be around until
1385 read_dbx_symtab function returns. */
1386
1387 namestring = set_namestring (objfile, &nlist);
1388 tmp_language = deduce_language_from_filename (namestring);
1389
1390 /* Only change the psymtab's language if we've learned
1391 something useful (eg. tmp_language is not language_unknown).
1392 In addition, to match what start_subfile does, never change
1393 from C++ to C. */
1394 if (tmp_language != language_unknown
1395 && (tmp_language != language_c
1396 || psymtab_language != language_cplus))
1397 psymtab_language = tmp_language;
1398
1399 if (pst == NULL)
1400 {
1401 /* FIXME: we should not get here without a PST to work on.
1402 Attempt to recover. */
1403 complaint (&symfile_complaints,
1404 _("N_BINCL %s not in entries for "
1405 "any file, at symtab pos %d"),
1406 namestring, symnum);
1407 continue;
1408 }
1409 add_bincl_to_list (pst, namestring, nlist.n_value);
1410
1411 /* Mark down an include file in the current psymtab. */
1412
1413 goto record_include_file;
1414 }
1415
1416 case N_SOL:
1417 {
1418 enum language tmp_language;
1419
1420 /* Mark down an include file in the current psymtab. */
1421 namestring = set_namestring (objfile, &nlist);
1422 tmp_language = deduce_language_from_filename (namestring);
1423
1424 /* Only change the psymtab's language if we've learned
1425 something useful (eg. tmp_language is not language_unknown).
1426 In addition, to match what start_subfile does, never change
1427 from C++ to C. */
1428 if (tmp_language != language_unknown
1429 && (tmp_language != language_c
1430 || psymtab_language != language_cplus))
1431 psymtab_language = tmp_language;
1432
1433 /* In C++, one may expect the same filename to come round many
1434 times, when code is coming alternately from the main file
1435 and from inline functions in other files. So I check to see
1436 if this is a file we've seen before -- either the main
1437 source file, or a previously included file.
1438
1439 This seems to be a lot of time to be spending on N_SOL, but
1440 things like "break c-exp.y:435" need to work (I
1441 suppose the psymtab_include_list could be hashed or put
1442 in a binary tree, if profiling shows this is a major hog). */
1443 if (pst && filename_cmp (namestring, pst->filename) == 0)
1444 continue;
1445 {
1446 int i;
1447
1448 for (i = 0; i < includes_used; i++)
1449 if (filename_cmp (namestring, psymtab_include_list[i]) == 0)
1450 {
1451 i = -1;
1452 break;
1453 }
1454 if (i == -1)
1455 continue;
1456 }
1457
1458 record_include_file:
1459
1460 psymtab_include_list[includes_used++] = namestring;
1461 if (includes_used >= includes_allocated)
1462 {
1463 const char **orig = psymtab_include_list;
1464
1465 psymtab_include_list = (const char **)
1466 alloca ((includes_allocated *= 2) * sizeof (const char *));
1467 memcpy (psymtab_include_list, orig,
1468 includes_used * sizeof (const char *));
1469 }
1470 continue;
1471 }
1472 case N_LSYM: /* Typedef or automatic variable. */
1473 case N_STSYM: /* Data seg var -- static. */
1474 case N_LCSYM: /* BSS " */
1475 case N_ROSYM: /* Read-only data seg var -- static. */
1476 case N_NBSTS: /* Gould nobase. */
1477 case N_NBLCS: /* symbols. */
1478 case N_FUN:
1479 case N_GSYM: /* Global (extern) variable; can be
1480 data or bss (sigh FIXME). */
1481
1482 /* Following may probably be ignored; I'll leave them here
1483 for now (until I do Pascal and Modula 2 extensions). */
1484
1485 case N_PC: /* I may or may not need this; I
1486 suspect not. */
1487 case N_M2C: /* I suspect that I can ignore this here. */
1488 case N_SCOPE: /* Same. */
1489 {
1490 const char *p;
1491
1492 namestring = set_namestring (objfile, &nlist);
1493
1494 /* See if this is an end of function stab. */
1495 if (pst && nlist.n_type == N_FUN && *namestring == '\000')
1496 {
1497 CORE_ADDR valu;
1498
1499 /* It's value is the size (in bytes) of the function for
1500 function relative stabs, or the address of the function's
1501 end for old style stabs. */
1502 valu = nlist.n_value + last_function_start;
1503 if (pst->texthigh == 0 || valu > pst->texthigh)
1504 pst->texthigh = valu;
1505 break;
1506 }
1507
1508 p = (char *) strchr (namestring, ':');
1509 if (!p)
1510 continue; /* Not a debugging symbol. */
1511
1512 sym_len = 0;
1513 sym_name = NULL; /* pacify "gcc -Werror" */
1514 if (psymtab_language == language_cplus)
1515 {
1516 std::string name (namestring, p - namestring);
1517 std::string new_name = cp_canonicalize_string (name.c_str ());
1518 if (!new_name.empty ())
1519 {
1520 sym_len = new_name.length ();
1521 sym_name = (char *) obstack_copy0 (&objfile->objfile_obstack,
1522 new_name.c_str (),
1523 sym_len);
1524 }
1525 }
1526
1527 if (sym_len == 0)
1528 {
1529 sym_name = namestring;
1530 sym_len = p - namestring;
1531 }
1532
1533 /* Main processing section for debugging symbols which
1534 the initial read through the symbol tables needs to worry
1535 about. If we reach this point, the symbol which we are
1536 considering is definitely one we are interested in.
1537 p must also contain the (valid) index into the namestring
1538 which indicates the debugging type symbol. */
1539
1540 switch (p[1])
1541 {
1542 case 'S':
1543 nlist.n_value += ANOFFSET (objfile->section_offsets,
1544 data_sect_index);
1545
1546 if (gdbarch_static_transform_name_p (gdbarch))
1547 gdbarch_static_transform_name (gdbarch, namestring);
1548
1549 add_psymbol_to_list (sym_name, sym_len, 1,
1550 VAR_DOMAIN, LOC_STATIC,
1551 &objfile->static_psymbols,
1552 nlist.n_value, psymtab_language, objfile);
1553 continue;
1554
1555 case 'G':
1556 nlist.n_value += ANOFFSET (objfile->section_offsets,
1557 data_sect_index);
1558 /* The addresses in these entries are reported to be
1559 wrong. See the code that reads 'G's for symtabs. */
1560 add_psymbol_to_list (sym_name, sym_len, 1,
1561 VAR_DOMAIN, LOC_STATIC,
1562 &objfile->global_psymbols,
1563 nlist.n_value, psymtab_language, objfile);
1564 continue;
1565
1566 case 'T':
1567 /* When a 'T' entry is defining an anonymous enum, it
1568 may have a name which is the empty string, or a
1569 single space. Since they're not really defining a
1570 symbol, those shouldn't go in the partial symbol
1571 table. We do pick up the elements of such enums at
1572 'check_enum:', below. */
1573 if (p >= namestring + 2
1574 || (p == namestring + 1
1575 && namestring[0] != ' '))
1576 {
1577 add_psymbol_to_list (sym_name, sym_len, 1,
1578 STRUCT_DOMAIN, LOC_TYPEDEF,
1579 &objfile->static_psymbols,
1580 0, psymtab_language, objfile);
1581 if (p[2] == 't')
1582 {
1583 /* Also a typedef with the same name. */
1584 add_psymbol_to_list (sym_name, sym_len, 1,
1585 VAR_DOMAIN, LOC_TYPEDEF,
1586 &objfile->static_psymbols,
1587 0, psymtab_language, objfile);
1588 p += 1;
1589 }
1590 }
1591 goto check_enum;
1592
1593 case 't':
1594 if (p != namestring) /* a name is there, not just :T... */
1595 {
1596 add_psymbol_to_list (sym_name, sym_len, 1,
1597 VAR_DOMAIN, LOC_TYPEDEF,
1598 &objfile->static_psymbols,
1599 0, psymtab_language, objfile);
1600 }
1601 check_enum:
1602 /* If this is an enumerated type, we need to
1603 add all the enum constants to the partial symbol
1604 table. This does not cover enums without names, e.g.
1605 "enum {a, b} c;" in C, but fortunately those are
1606 rare. There is no way for GDB to find those from the
1607 enum type without spending too much time on it. Thus
1608 to solve this problem, the compiler needs to put out the
1609 enum in a nameless type. GCC2 does this. */
1610
1611 /* We are looking for something of the form
1612 <name> ":" ("t" | "T") [<number> "="] "e"
1613 {<constant> ":" <value> ","} ";". */
1614
1615 /* Skip over the colon and the 't' or 'T'. */
1616 p += 2;
1617 /* This type may be given a number. Also, numbers can come
1618 in pairs like (0,26). Skip over it. */
1619 while ((*p >= '0' && *p <= '9')
1620 || *p == '(' || *p == ',' || *p == ')'
1621 || *p == '=')
1622 p++;
1623
1624 if (*p++ == 'e')
1625 {
1626 /* The aix4 compiler emits extra crud before the members. */
1627 if (*p == '-')
1628 {
1629 /* Skip over the type (?). */
1630 while (*p != ':')
1631 p++;
1632
1633 /* Skip over the colon. */
1634 p++;
1635 }
1636
1637 /* We have found an enumerated type. */
1638 /* According to comments in read_enum_type
1639 a comma could end it instead of a semicolon.
1640 I don't know where that happens.
1641 Accept either. */
1642 while (*p && *p != ';' && *p != ',')
1643 {
1644 const char *q;
1645
1646 /* Check for and handle cretinous dbx symbol name
1647 continuation! */
1648 if (*p == '\\' || (*p == '?' && p[1] == '\0'))
1649 p = next_symbol_text (objfile);
1650
1651 /* Point to the character after the name
1652 of the enum constant. */
1653 for (q = p; *q && *q != ':'; q++)
1654 ;
1655 /* Note that the value doesn't matter for
1656 enum constants in psymtabs, just in symtabs. */
1657 add_psymbol_to_list (p, q - p, 1,
1658 VAR_DOMAIN, LOC_CONST,
1659 &objfile->static_psymbols, 0,
1660 psymtab_language, objfile);
1661 /* Point past the name. */
1662 p = q;
1663 /* Skip over the value. */
1664 while (*p && *p != ',')
1665 p++;
1666 /* Advance past the comma. */
1667 if (*p)
1668 p++;
1669 }
1670 }
1671 continue;
1672
1673 case 'c':
1674 /* Constant, e.g. from "const" in Pascal. */
1675 add_psymbol_to_list (sym_name, sym_len, 1,
1676 VAR_DOMAIN, LOC_CONST,
1677 &objfile->static_psymbols, 0,
1678 psymtab_language, objfile);
1679 continue;
1680
1681 case 'f':
1682 if (! pst)
1683 {
1684 int name_len = p - namestring;
1685 char *name = (char *) xmalloc (name_len + 1);
1686
1687 memcpy (name, namestring, name_len);
1688 name[name_len] = '\0';
1689 function_outside_compilation_unit_complaint (name);
1690 xfree (name);
1691 }
1692 nlist.n_value += ANOFFSET (objfile->section_offsets,
1693 SECT_OFF_TEXT (objfile));
1694 /* Kludges for ELF/STABS with Sun ACC. */
1695 last_function_name = namestring;
1696 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1697 value for the bottom of the text seg in those cases. */
1698 if (nlist.n_value == ANOFFSET (objfile->section_offsets,
1699 SECT_OFF_TEXT (objfile))
1700 && gdbarch_sofun_address_maybe_missing (gdbarch))
1701 {
1702 CORE_ADDR minsym_valu =
1703 find_stab_function_addr (namestring,
1704 pst ? pst->filename : NULL,
1705 objfile);
1706
1707 /* find_stab_function_addr will return 0 if the minimal
1708 symbol wasn't found. (Unfortunately, this might also
1709 be a valid address.) Anyway, if it *does* return 0,
1710 it is likely that the value was set correctly to begin
1711 with... */
1712 if (minsym_valu != 0)
1713 nlist.n_value = minsym_valu;
1714 }
1715 if (pst && textlow_not_set
1716 && gdbarch_sofun_address_maybe_missing (gdbarch))
1717 {
1718 pst->textlow = nlist.n_value;
1719 textlow_not_set = 0;
1720 }
1721 /* End kludge. */
1722
1723 /* Keep track of the start of the last function so we
1724 can handle end of function symbols. */
1725 last_function_start = nlist.n_value;
1726
1727 /* In reordered executables this function may lie outside
1728 the bounds created by N_SO symbols. If that's the case
1729 use the address of this function as the low bound for
1730 the partial symbol table. */
1731 if (pst
1732 && (textlow_not_set
1733 || (nlist.n_value < pst->textlow
1734 && (nlist.n_value
1735 != ANOFFSET (objfile->section_offsets,
1736 SECT_OFF_TEXT (objfile))))))
1737 {
1738 pst->textlow = nlist.n_value;
1739 textlow_not_set = 0;
1740 }
1741 add_psymbol_to_list (sym_name, sym_len, 1,
1742 VAR_DOMAIN, LOC_BLOCK,
1743 &objfile->static_psymbols,
1744 nlist.n_value, psymtab_language, objfile);
1745 continue;
1746
1747 /* Global functions were ignored here, but now they
1748 are put into the global psymtab like one would expect.
1749 They're also in the minimal symbol table. */
1750 case 'F':
1751 if (! pst)
1752 {
1753 int name_len = p - namestring;
1754 char *name = (char *) xmalloc (name_len + 1);
1755
1756 memcpy (name, namestring, name_len);
1757 name[name_len] = '\0';
1758 function_outside_compilation_unit_complaint (name);
1759 xfree (name);
1760 }
1761 nlist.n_value += ANOFFSET (objfile->section_offsets,
1762 SECT_OFF_TEXT (objfile));
1763 /* Kludges for ELF/STABS with Sun ACC. */
1764 last_function_name = namestring;
1765 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1766 value for the bottom of the text seg in those cases. */
1767 if (nlist.n_value == ANOFFSET (objfile->section_offsets,
1768 SECT_OFF_TEXT (objfile))
1769 && gdbarch_sofun_address_maybe_missing (gdbarch))
1770 {
1771 CORE_ADDR minsym_valu =
1772 find_stab_function_addr (namestring,
1773 pst ? pst->filename : NULL,
1774 objfile);
1775
1776 /* find_stab_function_addr will return 0 if the minimal
1777 symbol wasn't found. (Unfortunately, this might also
1778 be a valid address.) Anyway, if it *does* return 0,
1779 it is likely that the value was set correctly to begin
1780 with... */
1781 if (minsym_valu != 0)
1782 nlist.n_value = minsym_valu;
1783 }
1784 if (pst && textlow_not_set
1785 && gdbarch_sofun_address_maybe_missing (gdbarch))
1786 {
1787 pst->textlow = nlist.n_value;
1788 textlow_not_set = 0;
1789 }
1790 /* End kludge. */
1791
1792 /* Keep track of the start of the last function so we
1793 can handle end of function symbols. */
1794 last_function_start = nlist.n_value;
1795
1796 /* In reordered executables this function may lie outside
1797 the bounds created by N_SO symbols. If that's the case
1798 use the address of this function as the low bound for
1799 the partial symbol table. */
1800 if (pst
1801 && (textlow_not_set
1802 || (nlist.n_value < pst->textlow
1803 && (nlist.n_value
1804 != ANOFFSET (objfile->section_offsets,
1805 SECT_OFF_TEXT (objfile))))))
1806 {
1807 pst->textlow = nlist.n_value;
1808 textlow_not_set = 0;
1809 }
1810 add_psymbol_to_list (sym_name, sym_len, 1,
1811 VAR_DOMAIN, LOC_BLOCK,
1812 &objfile->global_psymbols,
1813 nlist.n_value, psymtab_language, objfile);
1814 continue;
1815
1816 /* Two things show up here (hopefully); static symbols of
1817 local scope (static used inside braces) or extensions
1818 of structure symbols. We can ignore both. */
1819 case 'V':
1820 case '(':
1821 case '0':
1822 case '1':
1823 case '2':
1824 case '3':
1825 case '4':
1826 case '5':
1827 case '6':
1828 case '7':
1829 case '8':
1830 case '9':
1831 case '-':
1832 case '#': /* For symbol identification (used in live ranges). */
1833 continue;
1834
1835 case ':':
1836 /* It is a C++ nested symbol. We don't need to record it
1837 (I don't think); if we try to look up foo::bar::baz,
1838 then symbols for the symtab containing foo should get
1839 read in, I think. */
1840 /* Someone says sun cc puts out symbols like
1841 /foo/baz/maclib::/usr/local/bin/maclib,
1842 which would get here with a symbol type of ':'. */
1843 continue;
1844
1845 default:
1846 /* Unexpected symbol descriptor. The second and subsequent stabs
1847 of a continued stab can show up here. The question is
1848 whether they ever can mimic a normal stab--it would be
1849 nice if not, since we certainly don't want to spend the
1850 time searching to the end of every string looking for
1851 a backslash. */
1852
1853 complaint (&symfile_complaints,
1854 _("unknown symbol descriptor `%c'"),
1855 p[1]);
1856
1857 /* Ignore it; perhaps it is an extension that we don't
1858 know about. */
1859 continue;
1860 }
1861 }
1862
1863 case N_EXCL:
1864
1865 namestring = set_namestring (objfile, &nlist);
1866
1867 /* Find the corresponding bincl and mark that psymtab on the
1868 psymtab dependency list. */
1869 {
1870 struct partial_symtab *needed_pst =
1871 find_corresponding_bincl_psymtab (namestring, nlist.n_value);
1872
1873 /* If this include file was defined earlier in this file,
1874 leave it alone. */
1875 if (needed_pst == pst)
1876 continue;
1877
1878 if (needed_pst)
1879 {
1880 int i;
1881 int found = 0;
1882
1883 for (i = 0; i < dependencies_used; i++)
1884 if (dependency_list[i] == needed_pst)
1885 {
1886 found = 1;
1887 break;
1888 }
1889
1890 /* If it's already in the list, skip the rest. */
1891 if (found)
1892 continue;
1893
1894 dependency_list[dependencies_used++] = needed_pst;
1895 if (dependencies_used >= dependencies_allocated)
1896 {
1897 struct partial_symtab **orig = dependency_list;
1898
1899 dependency_list =
1900 (struct partial_symtab **)
1901 alloca ((dependencies_allocated *= 2)
1902 * sizeof (struct partial_symtab *));
1903 memcpy (dependency_list, orig,
1904 (dependencies_used
1905 * sizeof (struct partial_symtab *)));
1906 #ifdef DEBUG_INFO
1907 fprintf_unfiltered (gdb_stderr,
1908 "Had to reallocate "
1909 "dependency list.\n");
1910 fprintf_unfiltered (gdb_stderr,
1911 "New dependencies allocated: %d\n",
1912 dependencies_allocated);
1913 #endif
1914 }
1915 }
1916 }
1917 continue;
1918
1919 case N_ENDM:
1920 /* Solaris 2 end of module, finish current partial symbol table.
1921 dbx_end_psymtab will set pst->texthigh to the proper value, which
1922 is necessary if a module compiled without debugging info
1923 follows this module. */
1924 if (pst && gdbarch_sofun_address_maybe_missing (gdbarch))
1925 {
1926 dbx_end_psymtab (objfile, pst,
1927 psymtab_include_list, includes_used,
1928 symnum * symbol_size,
1929 (CORE_ADDR) 0, dependency_list,
1930 dependencies_used, textlow_not_set);
1931 pst = (struct partial_symtab *) 0;
1932 includes_used = 0;
1933 dependencies_used = 0;
1934 has_line_numbers = 0;
1935 }
1936 continue;
1937
1938 case N_RBRAC:
1939 #ifdef HANDLE_RBRAC
1940 HANDLE_RBRAC (nlist.n_value);
1941 continue;
1942 #endif
1943 case N_EINCL:
1944 case N_DSLINE:
1945 case N_BSLINE:
1946 case N_SSYM: /* Claim: Structure or union element.
1947 Hopefully, I can ignore this. */
1948 case N_ENTRY: /* Alternate entry point; can ignore. */
1949 case N_MAIN: /* Can definitely ignore this. */
1950 case N_CATCH: /* These are GNU C++ extensions */
1951 case N_EHDECL: /* that can safely be ignored here. */
1952 case N_LENG:
1953 case N_BCOMM:
1954 case N_ECOMM:
1955 case N_ECOML:
1956 case N_FNAME:
1957 case N_SLINE:
1958 case N_RSYM:
1959 case N_PSYM:
1960 case N_BNSYM:
1961 case N_ENSYM:
1962 case N_LBRAC:
1963 case N_NSYMS: /* Ultrix 4.0: symbol count */
1964 case N_DEFD: /* GNU Modula-2 */
1965 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
1966
1967 case N_OBJ: /* Useless types from Solaris. */
1968 case N_OPT:
1969 case N_PATCH:
1970 /* These symbols aren't interesting; don't worry about them. */
1971 continue;
1972
1973 default:
1974 /* If we haven't found it yet, ignore it. It's probably some
1975 new type we don't know about yet. */
1976 unknown_symtype_complaint (hex_string (nlist.n_type));
1977 continue;
1978 }
1979 }
1980
1981 /* If there's stuff to be cleaned up, clean it up. */
1982 if (pst)
1983 {
1984 /* Don't set pst->texthigh lower than it already is. */
1985 CORE_ADDR text_end =
1986 (lowest_text_address == (CORE_ADDR) -1
1987 ? (text_addr + ANOFFSET (objfile->section_offsets,
1988 SECT_OFF_TEXT (objfile)))
1989 : lowest_text_address)
1990 + text_size;
1991
1992 dbx_end_psymtab (objfile, pst, psymtab_include_list, includes_used,
1993 symnum * symbol_size,
1994 text_end > pst->texthigh ? text_end : pst->texthigh,
1995 dependency_list, dependencies_used, textlow_not_set);
1996 }
1997
1998 do_cleanups (back_to);
1999 }
2000
2001 /* Allocate and partially fill a partial symtab. It will be
2002 completely filled at the end of the symbol list.
2003
2004 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
2005 is the address relative to which its symbols are (incremental) or 0
2006 (normal). */
2007
2008 static struct partial_symtab *
2009 start_psymtab (struct objfile *objfile, const char *filename, CORE_ADDR textlow,
2010 int ldsymoff, struct partial_symbol **global_syms,
2011 struct partial_symbol **static_syms)
2012 {
2013 struct partial_symtab *result =
2014 start_psymtab_common (objfile, filename, textlow,
2015 global_syms, static_syms);
2016
2017 result->read_symtab_private =
2018 XOBNEW (&objfile->objfile_obstack, struct symloc);
2019 LDSYMOFF (result) = ldsymoff;
2020 result->read_symtab = dbx_read_symtab;
2021 SYMBOL_SIZE (result) = symbol_size;
2022 SYMBOL_OFFSET (result) = symbol_table_offset;
2023 STRING_OFFSET (result) = string_table_offset;
2024 FILE_STRING_OFFSET (result) = file_string_table_offset;
2025
2026 /* Deduce the source language from the filename for this psymtab. */
2027 psymtab_language = deduce_language_from_filename (filename);
2028
2029 return result;
2030 }
2031
2032 /* Close off the current usage of PST.
2033 Returns PST or NULL if the partial symtab was empty and thrown away.
2034
2035 FIXME: List variables and peculiarities of same. */
2036
2037 struct partial_symtab *
2038 dbx_end_psymtab (struct objfile *objfile, struct partial_symtab *pst,
2039 const char **include_list, int num_includes,
2040 int capping_symbol_offset, CORE_ADDR capping_text,
2041 struct partial_symtab **dependency_list,
2042 int number_dependencies,
2043 int textlow_not_set)
2044 {
2045 int i;
2046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2047
2048 if (capping_symbol_offset != -1)
2049 LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
2050 pst->texthigh = capping_text;
2051
2052 /* Under Solaris, the N_SO symbols always have a value of 0,
2053 instead of the usual address of the .o file. Therefore,
2054 we have to do some tricks to fill in texthigh and textlow.
2055 The first trick is: if we see a static
2056 or global function, and the textlow for the current pst
2057 is not set (ie: textlow_not_set), then we use that function's
2058 address for the textlow of the pst. */
2059
2060 /* Now, to fill in texthigh, we remember the last function seen
2061 in the .o file. Also, there's a hack in
2062 bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
2063 to here via the misc_info field. Therefore, we can fill in
2064 a reliable texthigh by taking the address plus size of the
2065 last function in the file. */
2066
2067 if (pst->texthigh == 0 && last_function_name
2068 && gdbarch_sofun_address_maybe_missing (gdbarch))
2069 {
2070 int n;
2071 struct bound_minimal_symbol minsym;
2072
2073 const char *colon = strchr (last_function_name, ':');
2074 if (colon == NULL)
2075 n = 0;
2076 else
2077 n = colon - last_function_name;
2078 char *p = (char *) alloca (n + 2);
2079 strncpy (p, last_function_name, n);
2080 p[n] = 0;
2081
2082 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2083 if (minsym.minsym == NULL)
2084 {
2085 /* Sun Fortran appends an underscore to the minimal symbol name,
2086 try again with an appended underscore if the minimal symbol
2087 was not found. */
2088 p[n] = '_';
2089 p[n + 1] = 0;
2090 minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2091 }
2092
2093 if (minsym.minsym)
2094 pst->texthigh = (BMSYMBOL_VALUE_ADDRESS (minsym)
2095 + MSYMBOL_SIZE (minsym.minsym));
2096
2097 last_function_name = NULL;
2098 }
2099
2100 if (!gdbarch_sofun_address_maybe_missing (gdbarch))
2101 ;
2102 /* This test will be true if the last .o file is only data. */
2103 else if (textlow_not_set)
2104 pst->textlow = pst->texthigh;
2105 else
2106 {
2107 struct partial_symtab *p1;
2108
2109 /* If we know our own starting text address, then walk through all other
2110 psymtabs for this objfile, and if any didn't know their ending text
2111 address, set it to our starting address. Take care to not set our
2112 own ending address to our starting address, nor to set addresses on
2113 `dependency' files that have both textlow and texthigh zero. */
2114
2115 ALL_OBJFILE_PSYMTABS (objfile, p1)
2116 {
2117 if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
2118 {
2119 p1->texthigh = pst->textlow;
2120 /* If this file has only data, then make textlow match
2121 texthigh. */
2122 if (p1->textlow == 0)
2123 p1->textlow = p1->texthigh;
2124 }
2125 }
2126 }
2127
2128 /* End of kludge for patching Solaris textlow and texthigh. */
2129
2130 end_psymtab_common (objfile, pst);
2131
2132 pst->number_of_dependencies = number_dependencies;
2133 if (number_dependencies)
2134 {
2135 pst->dependencies = XOBNEWVEC (&objfile->objfile_obstack,
2136 struct partial_symtab *,
2137 number_dependencies);
2138 memcpy (pst->dependencies, dependency_list,
2139 number_dependencies * sizeof (struct partial_symtab *));
2140 }
2141 else
2142 pst->dependencies = 0;
2143
2144 for (i = 0; i < num_includes; i++)
2145 {
2146 struct partial_symtab *subpst =
2147 allocate_psymtab (include_list[i], objfile);
2148
2149 subpst->read_symtab_private =
2150 XOBNEW (&objfile->objfile_obstack, struct symloc);
2151 LDSYMOFF (subpst) =
2152 LDSYMLEN (subpst) =
2153 subpst->textlow =
2154 subpst->texthigh = 0;
2155
2156 /* We could save slight bits of space by only making one of these,
2157 shared by the entire set of include files. FIXME-someday. */
2158 subpst->dependencies =
2159 XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
2160 subpst->dependencies[0] = pst;
2161 subpst->number_of_dependencies = 1;
2162
2163 subpst->globals_offset =
2164 subpst->n_global_syms =
2165 subpst->statics_offset =
2166 subpst->n_static_syms = 0;
2167
2168 subpst->readin = 0;
2169 subpst->compunit_symtab = 0;
2170 subpst->read_symtab = pst->read_symtab;
2171 }
2172
2173 if (num_includes == 0
2174 && number_dependencies == 0
2175 && pst->n_global_syms == 0
2176 && pst->n_static_syms == 0
2177 && has_line_numbers == 0)
2178 {
2179 /* Throw away this psymtab, it's empty. We can't deallocate it, since
2180 it is on the obstack, but we can forget to chain it on the list. */
2181 /* Empty psymtabs happen as a result of header files which don't have
2182 any symbols in them. There can be a lot of them. But this check
2183 is wrong, in that a psymtab with N_SLINE entries but nothing else
2184 is not empty, but we don't realize that. Fixing that without slowing
2185 things down might be tricky. */
2186
2187 discard_psymtab (objfile, pst);
2188
2189 /* Indicate that psymtab was thrown away. */
2190 pst = NULL;
2191 }
2192 return pst;
2193 }
2194
2195 static void
2197 dbx_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
2198 {
2199 struct cleanup *old_chain;
2200 int i;
2201
2202 if (pst->readin)
2203 {
2204 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. "
2205 "Shouldn't happen.\n",
2206 pst->filename);
2207 return;
2208 }
2209
2210 /* Read in all partial symtabs on which this one is dependent. */
2211 for (i = 0; i < pst->number_of_dependencies; i++)
2212 if (!pst->dependencies[i]->readin)
2213 {
2214 /* Inform about additional files that need to be read in. */
2215 if (info_verbose)
2216 {
2217 fputs_filtered (" ", gdb_stdout);
2218 wrap_here ("");
2219 fputs_filtered ("and ", gdb_stdout);
2220 wrap_here ("");
2221 printf_filtered ("%s...", pst->dependencies[i]->filename);
2222 wrap_here (""); /* Flush output. */
2223 gdb_flush (gdb_stdout);
2224 }
2225 dbx_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
2226 }
2227
2228 if (LDSYMLEN (pst)) /* Otherwise it's a dummy. */
2229 {
2230 /* Init stuff necessary for reading in symbols */
2231 stabsread_init ();
2232 buildsym_init ();
2233 old_chain = make_cleanup (really_free_pendings, 0);
2234 file_string_table_offset = FILE_STRING_OFFSET (pst);
2235 symbol_size = SYMBOL_SIZE (pst);
2236
2237 /* Read in this file's symbols. */
2238 bfd_seek (objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
2239 read_ofile_symtab (objfile, pst);
2240
2241 do_cleanups (old_chain);
2242 }
2243
2244 pst->readin = 1;
2245 }
2246
2247 /* Read in all of the symbols for a given psymtab for real.
2248 Be verbose about it if the user wants that. SELF is not NULL. */
2249
2250 static void
2251 dbx_read_symtab (struct partial_symtab *self, struct objfile *objfile)
2252 {
2253 if (self->readin)
2254 {
2255 fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in. "
2256 "Shouldn't happen.\n",
2257 self->filename);
2258 return;
2259 }
2260
2261 if (LDSYMLEN (self) || self->number_of_dependencies)
2262 {
2263 struct cleanup *back_to;
2264
2265 /* Print the message now, before reading the string table,
2266 to avoid disconcerting pauses. */
2267 if (info_verbose)
2268 {
2269 printf_filtered ("Reading in symbols for %s...", self->filename);
2270 gdb_flush (gdb_stdout);
2271 }
2272
2273 next_symbol_text_func = dbx_next_symbol_text;
2274
2275 back_to = make_cleanup (null_cleanup, NULL);
2276
2277 if (DBX_STAB_SECTION (objfile))
2278 {
2279 stabs_data
2280 = symfile_relocate_debug_section (objfile,
2281 DBX_STAB_SECTION (objfile),
2282 NULL);
2283
2284 if (stabs_data)
2285 make_cleanup (free_current_contents, (void *) &stabs_data);
2286 }
2287
2288 dbx_psymtab_to_symtab_1 (objfile, self);
2289
2290 do_cleanups (back_to);
2291
2292 /* Match with global symbols. This only needs to be done once,
2293 after all of the symtabs and dependencies have been read in. */
2294 scan_file_globals (objfile);
2295
2296 /* Finish up the debug error message. */
2297 if (info_verbose)
2298 printf_filtered ("done.\n");
2299 }
2300 }
2301
2302 /* Read in a defined section of a specific object file's symbols. */
2303
2304 static void
2305 read_ofile_symtab (struct objfile *objfile, struct partial_symtab *pst)
2306 {
2307 const char *namestring;
2308 struct external_nlist *bufp;
2309 struct internal_nlist nlist;
2310 unsigned char type;
2311 unsigned max_symnum;
2312 bfd *abfd;
2313 int sym_offset; /* Offset to start of symbols to read */
2314 int sym_size; /* Size of symbols to read */
2315 CORE_ADDR text_offset; /* Start of text segment for symbols */
2316 int text_size; /* Size of text segment for symbols */
2317 struct section_offsets *section_offsets;
2318
2319 sym_offset = LDSYMOFF (pst);
2320 sym_size = LDSYMLEN (pst);
2321 text_offset = pst->textlow;
2322 text_size = pst->texthigh - pst->textlow;
2323 section_offsets = objfile->section_offsets;
2324
2325 dbxread_objfile = objfile;
2326
2327 stringtab_global = DBX_STRINGTAB (objfile);
2328 set_last_source_file (NULL);
2329
2330 abfd = objfile->obfd;
2331 symfile_bfd = objfile->obfd; /* Implicit param to next_text_symbol. */
2332 symbuf_end = symbuf_idx = 0;
2333 symbuf_read = 0;
2334 symbuf_left = sym_offset + sym_size;
2335
2336 /* It is necessary to actually read one symbol *before* the start
2337 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
2338 occurs before the N_SO symbol.
2339
2340 Detecting this in read_dbx_symtab
2341 would slow down initial readin, so we look for it here instead. */
2342 if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
2343 {
2344 stabs_seek (sym_offset - symbol_size);
2345 fill_symbuf (abfd);
2346 bufp = &symbuf[symbuf_idx++];
2347 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2348 OBJSTAT (objfile, n_stabs++);
2349
2350 namestring = set_namestring (objfile, &nlist);
2351
2352 processing_gcc_compilation = 0;
2353 if (nlist.n_type == N_TEXT)
2354 {
2355 const char *tempstring = namestring;
2356
2357 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
2358 processing_gcc_compilation = 1;
2359 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2360 processing_gcc_compilation = 2;
2361 if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
2362 ++tempstring;
2363 if (startswith (tempstring, "__gnu_compiled"))
2364 processing_gcc_compilation = 2;
2365 }
2366 }
2367 else
2368 {
2369 /* The N_SO starting this symtab is the first symbol, so we
2370 better not check the symbol before it. I'm not this can
2371 happen, but it doesn't hurt to check for it. */
2372 stabs_seek (sym_offset);
2373 processing_gcc_compilation = 0;
2374 }
2375
2376 if (symbuf_idx == symbuf_end)
2377 fill_symbuf (abfd);
2378 bufp = &symbuf[symbuf_idx];
2379 if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
2380 error (_("First symbol in segment of executable not a source symbol"));
2381
2382 max_symnum = sym_size / symbol_size;
2383
2384 for (symnum = 0;
2385 symnum < max_symnum;
2386 symnum++)
2387 {
2388 QUIT; /* Allow this to be interruptable. */
2389 if (symbuf_idx == symbuf_end)
2390 fill_symbuf (abfd);
2391 bufp = &symbuf[symbuf_idx++];
2392 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2393 OBJSTAT (objfile, n_stabs++);
2394
2395 type = bfd_h_get_8 (abfd, bufp->e_type);
2396
2397 namestring = set_namestring (objfile, &nlist);
2398
2399 if (type & N_STAB)
2400 {
2401 if (sizeof (nlist.n_value) > 4
2402 /* We are a 64-bit debugger debugging a 32-bit program. */
2403 && (type == N_LSYM || type == N_PSYM))
2404 /* We have to be careful with the n_value in the case of N_LSYM
2405 and N_PSYM entries, because they are signed offsets from frame
2406 pointer, but we actually read them as unsigned 32-bit values.
2407 This is not a problem for 32-bit debuggers, for which negative
2408 values end up being interpreted correctly (as negative
2409 offsets) due to integer overflow.
2410 But we need to sign-extend the value for 64-bit debuggers,
2411 or we'll end up interpreting negative values as very large
2412 positive offsets. */
2413 nlist.n_value = (nlist.n_value ^ 0x80000000) - 0x80000000;
2414 process_one_symbol (type, nlist.n_desc, nlist.n_value,
2415 namestring, section_offsets, objfile);
2416 }
2417 /* We skip checking for a new .o or -l file; that should never
2418 happen in this routine. */
2419 else if (type == N_TEXT)
2420 {
2421 /* I don't think this code will ever be executed, because
2422 the GCC_COMPILED_FLAG_SYMBOL usually is right before
2423 the N_SO symbol which starts this source file.
2424 However, there is no reason not to accept
2425 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
2426
2427 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
2428 processing_gcc_compilation = 1;
2429 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2430 processing_gcc_compilation = 2;
2431 }
2432 else if (type & N_EXT || type == (unsigned char) N_TEXT
2433 || type == (unsigned char) N_NBTEXT)
2434 {
2435 /* Global symbol: see if we came across a dbx defintion for
2436 a corresponding symbol. If so, store the value. Remove
2437 syms from the chain when their values are stored, but
2438 search the whole chain, as there may be several syms from
2439 different files with the same name. */
2440 /* This is probably not true. Since the files will be read
2441 in one at a time, each reference to a global symbol will
2442 be satisfied in each file as it appears. So we skip this
2443 section. */
2444 ;
2445 }
2446 }
2447
2448 /* In a Solaris elf file, this variable, which comes from the
2449 value of the N_SO symbol, will still be 0. Luckily, text_offset,
2450 which comes from pst->textlow is correct. */
2451 if (last_source_start_addr == 0)
2452 last_source_start_addr = text_offset;
2453
2454 /* In reordered executables last_source_start_addr may not be the
2455 lower bound for this symtab, instead use text_offset which comes
2456 from pst->textlow which is correct. */
2457 if (last_source_start_addr > text_offset)
2458 last_source_start_addr = text_offset;
2459
2460 pst->compunit_symtab = end_symtab (text_offset + text_size,
2461 SECT_OFF_TEXT (objfile));
2462
2463 end_stabs ();
2464
2465 dbxread_objfile = NULL;
2466 }
2467
2468
2470 /* Record the namespace that the function defined by SYMBOL was
2471 defined in, if necessary. BLOCK is the associated block; use
2472 OBSTACK for allocation. */
2473
2474 static void
2475 cp_set_block_scope (const struct symbol *symbol,
2476 struct block *block,
2477 struct obstack *obstack)
2478 {
2479 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
2480 {
2481 /* Try to figure out the appropriate namespace from the
2482 demangled name. */
2483
2484 /* FIXME: carlton/2003-04-15: If the function in question is
2485 a method of a class, the name will actually include the
2486 name of the class as well. This should be harmless, but
2487 is a little unfortunate. */
2488
2489 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
2490 unsigned int prefix_len = cp_entire_prefix_len (name);
2491
2492 block_set_scope (block,
2493 (const char *) obstack_copy0 (obstack, name, prefix_len),
2494 obstack);
2495 }
2496 }
2497
2498 /* This handles a single symbol from the symbol-file, building symbols
2499 into a GDB symtab. It takes these arguments and an implicit argument.
2500
2501 TYPE is the type field of the ".stab" symbol entry.
2502 DESC is the desc field of the ".stab" entry.
2503 VALU is the value field of the ".stab" entry.
2504 NAME is the symbol name, in our address space.
2505 SECTION_OFFSETS is a set of amounts by which the sections of this
2506 object file were relocated when it was loaded into memory. Note
2507 that these section_offsets are not the objfile->section_offsets but
2508 the pst->section_offsets. All symbols that refer to memory
2509 locations need to be offset by these amounts.
2510 OBJFILE is the object file from which we are reading symbols. It
2511 is used in end_symtab. */
2512
2513 void
2514 process_one_symbol (int type, int desc, CORE_ADDR valu, const char *name,
2515 const struct section_offsets *section_offsets,
2516 struct objfile *objfile)
2517 {
2518 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2519 struct context_stack *newobj;
2520 /* This remembers the address of the start of a function. It is
2521 used because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries
2522 are relative to the current function's start address. On systems
2523 other than Solaris 2, this just holds the SECT_OFF_TEXT value,
2524 and is used to relocate these symbol types rather than
2525 SECTION_OFFSETS. */
2526 static CORE_ADDR function_start_offset;
2527
2528 /* This holds the address of the start of a function, without the
2529 system peculiarities of function_start_offset. */
2530 static CORE_ADDR last_function_start;
2531
2532 /* If this is nonzero, we've seen an N_SLINE since the start of the
2533 current function. We use this to tell us to move the first sline
2534 to the beginning of the function regardless of what its given
2535 value is. */
2536 static int sline_found_in_function = 1;
2537
2538 /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this
2539 source file. Used to detect the SunPRO solaris compiler. */
2540 static int n_opt_found;
2541
2542 /* Something is wrong if we see real data before seeing a source
2543 file name. */
2544
2545 if (get_last_source_file () == NULL && type != (unsigned char) N_SO)
2546 {
2547 /* Ignore any symbols which appear before an N_SO symbol.
2548 Currently no one puts symbols there, but we should deal
2549 gracefully with the case. A complain()t might be in order,
2550 but this should not be an error (). */
2551 return;
2552 }
2553
2554 switch (type)
2555 {
2556 case N_FUN:
2557 case N_FNAME:
2558
2559 if (*name == '\000')
2560 {
2561 /* This N_FUN marks the end of a function. This closes off
2562 the current block. */
2563 struct block *block;
2564
2565 if (context_stack_depth <= 0)
2566 {
2567 lbrac_mismatch_complaint (symnum);
2568 break;
2569 }
2570
2571 /* The following check is added before recording line 0 at
2572 end of function so as to handle hand-generated stabs
2573 which may have an N_FUN stabs at the end of the function,
2574 but no N_SLINE stabs. */
2575 if (sline_found_in_function)
2576 {
2577 CORE_ADDR addr = last_function_start + valu;
2578
2579 record_line (current_subfile, 0,
2580 gdbarch_addr_bits_remove (gdbarch, addr));
2581 }
2582
2583 within_function = 0;
2584 newobj = pop_context ();
2585
2586 /* Make a block for the local symbols within. */
2587 block = finish_block (newobj->name, &local_symbols,
2588 newobj->old_blocks, NULL,
2589 newobj->start_addr, newobj->start_addr + valu);
2590
2591 /* For C++, set the block's scope. */
2592 if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
2593 cp_set_block_scope (newobj->name, block, &objfile->objfile_obstack);
2594
2595 /* May be switching to an assembler file which may not be using
2596 block relative stabs, so reset the offset. */
2597 function_start_offset = 0;
2598
2599 break;
2600 }
2601
2602 sline_found_in_function = 0;
2603
2604 /* Relocate for dynamic loading. */
2605 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2606 valu = gdbarch_addr_bits_remove (gdbarch, valu);
2607 last_function_start = valu;
2608
2609 goto define_a_symbol;
2610
2611 case N_LBRAC:
2612 /* This "symbol" just indicates the start of an inner lexical
2613 context within a function. */
2614
2615 /* Ignore extra outermost context from SunPRO cc and acc. */
2616 if (n_opt_found && desc == 1)
2617 break;
2618
2619 valu += function_start_offset;
2620
2621 push_context (desc, valu);
2622 break;
2623
2624 case N_RBRAC:
2625 /* This "symbol" just indicates the end of an inner lexical
2626 context that was started with N_LBRAC. */
2627
2628 /* Ignore extra outermost context from SunPRO cc and acc. */
2629 if (n_opt_found && desc == 1)
2630 break;
2631
2632 valu += function_start_offset;
2633
2634 if (context_stack_depth <= 0)
2635 {
2636 lbrac_mismatch_complaint (symnum);
2637 break;
2638 }
2639
2640 newobj = pop_context ();
2641 if (desc != newobj->depth)
2642 lbrac_mismatch_complaint (symnum);
2643
2644 if (local_symbols != NULL)
2645 {
2646 /* GCC development snapshots from March to December of
2647 2000 would output N_LSYM entries after N_LBRAC
2648 entries. As a consequence, these symbols are simply
2649 discarded. Complain if this is the case. */
2650 complaint (&symfile_complaints,
2651 _("misplaced N_LBRAC entry; discarding local "
2652 "symbols which have no enclosing block"));
2653 }
2654 local_symbols = newobj->locals;
2655
2656 if (context_stack_depth > 1)
2657 {
2658 /* This is not the outermost LBRAC...RBRAC pair in the
2659 function, its local symbols preceded it, and are the ones
2660 just recovered from the context stack. Define the block
2661 for them (but don't bother if the block contains no
2662 symbols. Should we complain on blocks without symbols?
2663 I can't think of any useful purpose for them). */
2664 if (local_symbols != NULL)
2665 {
2666 /* Muzzle a compiler bug that makes end < start.
2667
2668 ??? Which compilers? Is this ever harmful?. */
2669 if (newobj->start_addr > valu)
2670 {
2671 complaint (&symfile_complaints,
2672 _("block start larger than block end"));
2673 newobj->start_addr = valu;
2674 }
2675 /* Make a block for the local symbols within. */
2676 finish_block (0, &local_symbols, newobj->old_blocks, NULL,
2677 newobj->start_addr, valu);
2678 }
2679 }
2680 else
2681 {
2682 /* This is the outermost LBRAC...RBRAC pair. There is no
2683 need to do anything; leave the symbols that preceded it
2684 to be attached to the function's own block. We need to
2685 indicate that we just moved outside of the function. */
2686 within_function = 0;
2687 }
2688
2689 break;
2690
2691 case N_FN:
2692 case N_FN_SEQ:
2693 /* This kind of symbol indicates the start of an object file.
2694 Relocate for dynamic loading. */
2695 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2696 break;
2697
2698 case N_SO:
2699 /* This type of symbol indicates the start of data for one
2700 source file. Finish the symbol table of the previous source
2701 file (if any) and start accumulating a new symbol table.
2702 Relocate for dynamic loading. */
2703 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2704
2705 n_opt_found = 0;
2706
2707 if (get_last_source_file ())
2708 {
2709 /* Check if previous symbol was also an N_SO (with some
2710 sanity checks). If so, that one was actually the
2711 directory name, and the current one is the real file
2712 name. Patch things up. */
2713 if (previous_stab_code == (unsigned char) N_SO)
2714 {
2715 patch_subfile_names (current_subfile, name);
2716 break; /* Ignore repeated SOs. */
2717 }
2718 end_symtab (valu, SECT_OFF_TEXT (objfile));
2719 end_stabs ();
2720 }
2721
2722 /* Null name means this just marks the end of text for this .o
2723 file. Don't start a new symtab in this case. */
2724 if (*name == '\000')
2725 break;
2726
2727 function_start_offset = 0;
2728
2729 start_stabs ();
2730 start_symtab (objfile, name, NULL, valu);
2731 record_debugformat ("stabs");
2732 break;
2733
2734 case N_SOL:
2735 /* This type of symbol indicates the start of data for a
2736 sub-source-file, one whose contents were copied or included
2737 in the compilation of the main source file (whose name was
2738 given in the N_SO symbol). Relocate for dynamic loading. */
2739 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2740 start_subfile (name);
2741 break;
2742
2743 case N_BINCL:
2744 push_subfile ();
2745 add_new_header_file (name, valu);
2746 start_subfile (name);
2747 break;
2748
2749 case N_EINCL:
2750 start_subfile (pop_subfile ());
2751 break;
2752
2753 case N_EXCL:
2754 add_old_header_file (name, valu);
2755 break;
2756
2757 case N_SLINE:
2758 /* This type of "symbol" really just records one line-number --
2759 core-address correspondence. Enter it in the line list for
2760 this symbol table. */
2761
2762 /* Relocate for dynamic loading and for ELF acc
2763 function-relative symbols. */
2764 valu += function_start_offset;
2765
2766 /* GCC 2.95.3 emits the first N_SLINE stab somwehere in the
2767 middle of the prologue instead of right at the start of the
2768 function. To deal with this we record the address for the
2769 first N_SLINE stab to be the start of the function instead of
2770 the listed location. We really shouldn't to this. When
2771 compiling with optimization, this first N_SLINE stab might be
2772 optimized away. Other (non-GCC) compilers don't emit this
2773 stab at all. There is no real harm in having an extra
2774 numbered line, although it can be a bit annoying for the
2775 user. However, it totally screws up our testsuite.
2776
2777 So for now, keep adjusting the address of the first N_SLINE
2778 stab, but only for code compiled with GCC. */
2779
2780 if (within_function && sline_found_in_function == 0)
2781 {
2782 CORE_ADDR addr = processing_gcc_compilation == 2 ?
2783 last_function_start : valu;
2784
2785 record_line (current_subfile, desc,
2786 gdbarch_addr_bits_remove (gdbarch, addr));
2787 sline_found_in_function = 1;
2788 }
2789 else
2790 record_line (current_subfile, desc,
2791 gdbarch_addr_bits_remove (gdbarch, valu));
2792 break;
2793
2794 case N_BCOMM:
2795 common_block_start (name, objfile);
2796 break;
2797
2798 case N_ECOMM:
2799 common_block_end (objfile);
2800 break;
2801
2802 /* The following symbol types need to have the appropriate
2803 offset added to their value; then we process symbol
2804 definitions in the name. */
2805
2806 case N_STSYM: /* Static symbol in data segment. */
2807 case N_LCSYM: /* Static symbol in BSS segment. */
2808 case N_ROSYM: /* Static symbol in read-only data segment. */
2809 /* HORRID HACK DEPT. However, it's Sun's furgin' fault.
2810 Solaris 2's stabs-in-elf makes *most* symbols relative but
2811 leaves a few absolute (at least for Solaris 2.1 and version
2812 2.0.1 of the SunPRO compiler). N_STSYM and friends sit on
2813 the fence. .stab "foo:S...",N_STSYM is absolute (ld
2814 relocates it) .stab "foo:V...",N_STSYM is relative (section
2815 base subtracted). This leaves us no choice but to search for
2816 the 'S' or 'V'... (or pass the whole section_offsets stuff
2817 down ONE MORE function call level, which we really don't want
2818 to do). */
2819 {
2820 const char *p;
2821
2822 /* Normal object file and NLMs have non-zero text seg offsets,
2823 but don't need their static syms offset in this fashion.
2824 XXX - This is really a crock that should be fixed in the
2825 solib handling code so that I don't have to work around it
2826 here. */
2827
2828 if (!symfile_relocatable)
2829 {
2830 p = strchr (name, ':');
2831 if (p != 0 && p[1] == 'S')
2832 {
2833 /* The linker relocated it. We don't want to add a
2834 Sun-stabs Tfoo.foo-like offset, but we *do*
2835 want to add whatever solib.c passed to
2836 symbol_file_add as addr (this is known to affect
2837 SunOS 4, and I suspect ELF too). Since there is no
2838 Ttext.text symbol, we can get addr from the text offset. */
2839 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2840 goto define_a_symbol;
2841 }
2842 }
2843 /* Since it's not the kludge case, re-dispatch to the right
2844 handler. */
2845 switch (type)
2846 {
2847 case N_STSYM:
2848 goto case_N_STSYM;
2849 case N_LCSYM:
2850 goto case_N_LCSYM;
2851 case N_ROSYM:
2852 goto case_N_ROSYM;
2853 default:
2854 internal_error (__FILE__, __LINE__,
2855 _("failed internal consistency check"));
2856 }
2857 }
2858
2859 case_N_STSYM: /* Static symbol in data segment. */
2860 case N_DSLINE: /* Source line number, data segment. */
2861 valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
2862 goto define_a_symbol;
2863
2864 case_N_LCSYM: /* Static symbol in BSS segment. */
2865 case N_BSLINE: /* Source line number, BSS segment. */
2866 /* N_BROWS: overlaps with N_BSLINE. */
2867 valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
2868 goto define_a_symbol;
2869
2870 case_N_ROSYM: /* Static symbol in read-only data segment. */
2871 valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
2872 goto define_a_symbol;
2873
2874 case N_ENTRY: /* Alternate entry point. */
2875 /* Relocate for dynamic loading. */
2876 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2877 goto define_a_symbol;
2878
2879 /* The following symbol types we don't know how to process.
2880 Handle them in a "default" way, but complain to people who
2881 care. */
2882 default:
2883 case N_CATCH: /* Exception handler catcher. */
2884 case N_EHDECL: /* Exception handler name. */
2885 case N_PC: /* Global symbol in Pascal. */
2886 case N_M2C: /* Modula-2 compilation unit. */
2887 /* N_MOD2: overlaps with N_EHDECL. */
2888 case N_SCOPE: /* Modula-2 scope information. */
2889 case N_ECOML: /* End common (local name). */
2890 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
2891 case N_NBDATA:
2892 case N_NBBSS:
2893 case N_NBSTS:
2894 case N_NBLCS:
2895 unknown_symtype_complaint (hex_string (type));
2896 /* FALLTHROUGH */
2897
2898 /* The following symbol types don't need the address field
2899 relocated, since it is either unused, or is absolute. */
2900 define_a_symbol:
2901 case N_GSYM: /* Global variable. */
2902 case N_NSYMS: /* Number of symbols (Ultrix). */
2903 case N_NOMAP: /* No map? (Ultrix). */
2904 case N_RSYM: /* Register variable. */
2905 case N_DEFD: /* Modula-2 GNU module dependency. */
2906 case N_SSYM: /* Struct or union element. */
2907 case N_LSYM: /* Local symbol in stack. */
2908 case N_PSYM: /* Parameter variable. */
2909 case N_LENG: /* Length of preceding symbol type. */
2910 if (name)
2911 {
2912 int deftype;
2913 const char *colon_pos = strchr (name, ':');
2914
2915 if (colon_pos == NULL)
2916 deftype = '\0';
2917 else
2918 deftype = colon_pos[1];
2919
2920 switch (deftype)
2921 {
2922 case 'f':
2923 case 'F':
2924 /* Deal with the SunPRO 3.0 compiler which omits the
2925 address from N_FUN symbols. */
2926 if (type == N_FUN
2927 && valu == ANOFFSET (section_offsets,
2928 SECT_OFF_TEXT (objfile))
2929 && gdbarch_sofun_address_maybe_missing (gdbarch))
2930 {
2931 CORE_ADDR minsym_valu =
2932 find_stab_function_addr (name, get_last_source_file (),
2933 objfile);
2934
2935 /* The function find_stab_function_addr will return
2936 0 if the minimal symbol wasn't found.
2937 (Unfortunately, this might also be a valid
2938 address.) Anyway, if it *does* return 0, it is
2939 likely that the value was set correctly to begin
2940 with... */
2941 if (minsym_valu != 0)
2942 valu = minsym_valu;
2943 }
2944
2945 /* These addresses are absolute. */
2946 function_start_offset = valu;
2947
2948 within_function = 1;
2949
2950 if (context_stack_depth > 1)
2951 {
2952 complaint (&symfile_complaints,
2953 _("unmatched N_LBRAC before symtab pos %d"),
2954 symnum);
2955 break;
2956 }
2957
2958 if (context_stack_depth > 0)
2959 {
2960 struct block *block;
2961
2962 newobj = pop_context ();
2963 /* Make a block for the local symbols within. */
2964 block = finish_block (newobj->name, &local_symbols,
2965 newobj->old_blocks, NULL,
2966 newobj->start_addr, valu);
2967
2968 /* For C++, set the block's scope. */
2969 if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
2970 cp_set_block_scope (newobj->name, block,
2971 &objfile->objfile_obstack);
2972 }
2973
2974 newobj = push_context (0, valu);
2975 newobj->name = define_symbol (valu, name, desc, type, objfile);
2976 break;
2977
2978 default:
2979 define_symbol (valu, name, desc, type, objfile);
2980 break;
2981 }
2982 }
2983 break;
2984
2985 /* We use N_OPT to carry the gcc2_compiled flag. Sun uses it
2986 for a bunch of other flags, too. Someday we may parse their
2987 flags; for now we ignore theirs and hope they'll ignore ours. */
2988 case N_OPT: /* Solaris 2: Compiler options. */
2989 if (name)
2990 {
2991 if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2992 {
2993 processing_gcc_compilation = 2;
2994 }
2995 else
2996 n_opt_found = 1;
2997 }
2998 break;
2999
3000 case N_MAIN: /* Name of main routine. */
3001 /* FIXME: If one has a symbol file with N_MAIN and then replaces
3002 it with a symbol file with "main" and without N_MAIN. I'm
3003 not sure exactly what rule to follow but probably something
3004 like: N_MAIN takes precedence over "main" no matter what
3005 objfile it is in; If there is more than one N_MAIN, choose
3006 the one in the symfile_objfile; If there is more than one
3007 N_MAIN within a given objfile, complain() and choose
3008 arbitrarily. (kingdon) */
3009 if (name != NULL)
3010 set_objfile_main_name (objfile, name, language_unknown);
3011 break;
3012
3013 /* The following symbol types can be ignored. */
3014 case N_OBJ: /* Solaris 2: Object file dir and name. */
3015 case N_PATCH: /* Solaris 2: Patch Run Time Checker. */
3016 /* N_UNDF: Solaris 2: File separator mark. */
3017 /* N_UNDF: -- we will never encounter it, since we only process
3018 one file's symbols at once. */
3019 case N_ENDM: /* Solaris 2: End of module. */
3020 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
3021 break;
3022 }
3023
3024 /* '#' is a GNU C extension to allow one symbol to refer to another
3025 related symbol.
3026
3027 Generally this is used so that an alias can refer to its main
3028 symbol. */
3029 gdb_assert (name);
3030 if (name[0] == '#')
3031 {
3032 /* Initialize symbol reference names and determine if this is a
3033 definition. If a symbol reference is being defined, go ahead
3034 and add it. Otherwise, just return. */
3035
3036 const char *s = name;
3037 int refnum;
3038
3039 /* If this stab defines a new reference ID that is not on the
3040 reference list, then put it on the reference list.
3041
3042 We go ahead and advance NAME past the reference, even though
3043 it is not strictly necessary at this time. */
3044 refnum = symbol_reference_defined (&s);
3045 if (refnum >= 0)
3046 if (!ref_search (refnum))
3047 ref_add (refnum, 0, name, valu);
3048 name = s;
3049 }
3050
3051 previous_stab_code = type;
3052 }
3053
3054 /* FIXME: The only difference between this and elfstab_build_psymtabs
3056 is the call to install_minimal_symbols for elf, and the support for
3057 split sections. If the differences are really that small, the code
3058 should be shared. */
3059
3060 /* Scan and build partial symbols for an coff symbol file.
3061 The coff file has already been processed to get its minimal symbols.
3062
3063 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3064 rolled into one.
3065
3066 OBJFILE is the object file we are reading symbols from.
3067 ADDR is the address relative to which the symbols are (e.g.
3068 the base address of the text segment).
3069 TEXTADDR is the address of the text section.
3070 TEXTSIZE is the size of the text section.
3071 STABSECTS is the list of .stab sections in OBJFILE.
3072 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
3073 .stabstr section exists.
3074
3075 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
3076 adjusted for coff details. */
3077
3078 void
3079 coffstab_build_psymtabs (struct objfile *objfile,
3080 CORE_ADDR textaddr, unsigned int textsize,
3081 struct stab_section_list *stabsects,
3082 file_ptr stabstroffset, unsigned int stabstrsize)
3083 {
3084 int val;
3085 bfd *sym_bfd = objfile->obfd;
3086 char *name = bfd_get_filename (sym_bfd);
3087 unsigned int stabsize;
3088
3089 DBX_TEXT_ADDR (objfile) = textaddr;
3090 DBX_TEXT_SIZE (objfile) = textsize;
3091
3092 #define COFF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
3093 DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
3094 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
3095
3096 if (stabstrsize > bfd_get_size (sym_bfd))
3097 error (_("ridiculous string table size: %d bytes"), stabstrsize);
3098 DBX_STRINGTAB (objfile) = (char *)
3099 obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
3100 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
3101
3102 /* Now read in the string table in one big gulp. */
3103
3104 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3105 if (val < 0)
3106 perror_with_name (name);
3107 val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
3108 if (val != stabstrsize)
3109 perror_with_name (name);
3110
3111 stabsread_new_init ();
3112 buildsym_new_init ();
3113 free_header_files ();
3114 init_header_files ();
3115
3116 processing_acc_compilation = 1;
3117
3118 /* In a coff file, we've already installed the minimal symbols that came
3119 from the coff (non-stab) symbol table, so always act like an
3120 incremental load here. */
3121 if (stabsects->next == NULL)
3122 {
3123 stabsize = bfd_section_size (sym_bfd, stabsects->section);
3124 DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
3125 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3126 }
3127 else
3128 {
3129 struct stab_section_list *stabsect;
3130
3131 DBX_SYMCOUNT (objfile) = 0;
3132 for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
3133 {
3134 stabsize = bfd_section_size (sym_bfd, stabsect->section);
3135 DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
3136 }
3137
3138 DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3139
3140 symbuf_sections = stabsects->next;
3141 symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
3142 symbuf_read = 0;
3143 }
3144
3145 dbx_symfile_read (objfile, 0);
3146 }
3147
3148 /* Scan and build partial symbols for an ELF symbol file.
3150 This ELF file has already been processed to get its minimal symbols.
3151
3152 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3153 rolled into one.
3154
3155 OBJFILE is the object file we are reading symbols from.
3156 ADDR is the address relative to which the symbols are (e.g.
3157 the base address of the text segment).
3158 STABSECT is the BFD section information for the .stab section.
3159 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
3160 .stabstr section exists.
3161
3162 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
3163 adjusted for elf details. */
3164
3165 void
3166 elfstab_build_psymtabs (struct objfile *objfile, asection *stabsect,
3167 file_ptr stabstroffset, unsigned int stabstrsize)
3168 {
3169 int val;
3170 bfd *sym_bfd = objfile->obfd;
3171 char *name = bfd_get_filename (sym_bfd);
3172 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
3173
3174 /* Find the first and last text address. dbx_symfile_read seems to
3175 want this. */
3176 find_text_range (sym_bfd, objfile);
3177
3178 #define ELF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
3179 DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
3180 DBX_SYMCOUNT (objfile)
3181 = bfd_section_size (objfile->obfd, stabsect) / DBX_SYMBOL_SIZE (objfile);
3182 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
3183 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
3184 DBX_STAB_SECTION (objfile) = stabsect;
3185
3186 if (stabstrsize > bfd_get_size (sym_bfd))
3187 error (_("ridiculous string table size: %d bytes"), stabstrsize);
3188 DBX_STRINGTAB (objfile) = (char *)
3189 obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
3190 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
3191
3192 /* Now read in the string table in one big gulp. */
3193
3194 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3195 if (val < 0)
3196 perror_with_name (name);
3197 val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
3198 if (val != stabstrsize)
3199 perror_with_name (name);
3200
3201 stabsread_new_init ();
3202 buildsym_new_init ();
3203 free_header_files ();
3204 init_header_files ();
3205
3206 processing_acc_compilation = 1;
3207
3208 symbuf_read = 0;
3209 symbuf_left = bfd_section_size (objfile->obfd, stabsect);
3210 stabs_data = symfile_relocate_debug_section (objfile, stabsect, NULL);
3211 if (stabs_data)
3212 make_cleanup (free_current_contents, (void *) &stabs_data);
3213
3214 /* In an elf file, we've already installed the minimal symbols that came
3215 from the elf (non-stab) symbol table, so always act like an
3216 incremental load here. dbx_symfile_read should not generate any new
3217 minimal symbols, since we will have already read the ELF dynamic symbol
3218 table and normal symbol entries won't be in the ".stab" section; but in
3219 case it does, it will install them itself. */
3220 dbx_symfile_read (objfile, 0);
3221
3222 do_cleanups (back_to);
3223 }
3224
3225 /* Scan and build partial symbols for a file with special sections for stabs
3227 and stabstrings. The file has already been processed to get its minimal
3228 symbols, and any other symbols that might be necessary to resolve GSYMs.
3229
3230 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3231 rolled into one.
3232
3233 OBJFILE is the object file we are reading symbols from.
3234 ADDR is the address relative to which the symbols are (e.g. the base address
3235 of the text segment).
3236 STAB_NAME is the name of the section that contains the stabs.
3237 STABSTR_NAME is the name of the section that contains the stab strings.
3238
3239 This routine is mostly copied from dbx_symfile_init and
3240 dbx_symfile_read. */
3241
3242 void
3243 stabsect_build_psymtabs (struct objfile *objfile, char *stab_name,
3244 char *stabstr_name, char *text_name)
3245 {
3246 int val;
3247 bfd *sym_bfd = objfile->obfd;
3248 char *name = bfd_get_filename (sym_bfd);
3249 asection *stabsect;
3250 asection *stabstrsect;
3251 asection *text_sect;
3252 struct dbx_symfile_info *dbx;
3253
3254 stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
3255 stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
3256
3257 if (!stabsect)
3258 return;
3259
3260 if (!stabstrsect)
3261 error (_("stabsect_build_psymtabs: Found stabs (%s), "
3262 "but not string section (%s)"),
3263 stab_name, stabstr_name);
3264
3265 dbx = XCNEW (struct dbx_symfile_info);
3266 set_objfile_data (objfile, dbx_objfile_data_key, dbx);
3267
3268 text_sect = bfd_get_section_by_name (sym_bfd, text_name);
3269 if (!text_sect)
3270 error (_("Can't find %s section in symbol file"), text_name);
3271 DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
3272 DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
3273
3274 DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
3275 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
3276 / DBX_SYMBOL_SIZE (objfile);
3277 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
3278 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos; /* XXX - FIXME: POKING
3279 INSIDE BFD DATA
3280 STRUCTURES */
3281
3282 if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
3283 error (_("ridiculous string table size: %d bytes"),
3284 DBX_STRINGTAB_SIZE (objfile));
3285 DBX_STRINGTAB (objfile) = (char *)
3286 obstack_alloc (&objfile->objfile_obstack,
3287 DBX_STRINGTAB_SIZE (objfile) + 1);
3288 OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
3289
3290 /* Now read in the string table in one big gulp. */
3291
3292 val = bfd_get_section_contents (sym_bfd, /* bfd */
3293 stabstrsect, /* bfd section */
3294 DBX_STRINGTAB (objfile), /* input buffer */
3295 0, /* offset into section */
3296 DBX_STRINGTAB_SIZE (objfile)); /* amount to
3297 read */
3298
3299 if (!val)
3300 perror_with_name (name);
3301
3302 stabsread_new_init ();
3303 buildsym_new_init ();
3304 free_header_files ();
3305 init_header_files ();
3306
3307 /* Now, do an incremental load. */
3308
3309 processing_acc_compilation = 1;
3310 dbx_symfile_read (objfile, 0);
3311 }
3312
3313 static const struct sym_fns aout_sym_fns =
3315 {
3316 dbx_new_init, /* init anything gbl to entire symtab */
3317 dbx_symfile_init, /* read initial info, setup for sym_read() */
3318 dbx_symfile_read, /* read a symbol file into symtab */
3319 NULL, /* sym_read_psymbols */
3320 dbx_symfile_finish, /* finished with file, cleanup */
3321 default_symfile_offsets, /* parse user's offsets to internal form */
3322 default_symfile_segments, /* Get segment information from a file. */
3323 NULL,
3324 default_symfile_relocate, /* Relocate a debug section. */
3325 NULL, /* sym_probe_fns */
3326 &psym_functions
3327 };
3328
3329 void
3330 _initialize_dbxread (void)
3331 {
3332 add_symtab_fns (bfd_target_aout_flavour, &aout_sym_fns);
3333
3334 dbx_objfile_data_key
3335 = register_objfile_data_with_cleanup (NULL, dbx_free_symfile_info);
3336 }
3337