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