1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "gdbcore.h"
25 #include "frame.h"
26 #include "target.h"
27 #include "value.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "gdbcmd.h"
31 #include "breakpoint.h"
32 #include "language.h"
33
34 #include <obstack.h>
35 #include <assert.h>
36
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <sys/stat.h>
41 #include <ctype.h>
42
43 /* Global variables owned by this file */
44
45 int readnow_symbol_files; /* Read full symbols immediately */
46
47 /* External variables and functions referenced. */
48
49 extern int info_verbose;
50
51 /* Functions this file defines */
52
53 static void
54 set_initial_language PARAMS ((void));
55
56 static void
57 load_command PARAMS ((char *, int));
58
59 static void
60 add_symbol_file_command PARAMS ((char *, int));
61
62 static void
63 cashier_psymtab PARAMS ((struct partial_symtab *));
64
65 static int
66 compare_psymbols PARAMS ((const void *, const void *));
67
68 static int
69 compare_symbols PARAMS ((const void *, const void *));
70
71 static bfd *
72 symfile_bfd_open PARAMS ((char *));
73
74 static void
75 find_sym_fns PARAMS ((struct objfile *));
76
77 void
78 clear_symtab_users_once PARAMS ((void));
79
80 /* List of all available sym_fns. On gdb startup, each object file reader
81 calls add_symtab_fns() to register information on each format it is
82 prepared to read. */
83
84 static struct sym_fns *symtab_fns = NULL;
85
86 /* Structures with which to manage partial symbol allocation. */
87
88 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
89
90 /* Flag for whether user will be reloading symbols multiple times.
91 Defaults to ON for VxWorks, otherwise OFF. */
92
93 #ifdef SYMBOL_RELOADING_DEFAULT
94 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
95 #else
96 int symbol_reloading = 0;
97 #endif
98
99 /* Structure to manage complaints about symbol file contents. */
100
101 struct complaint complaint_root[1] = {
102 {(char *) 0, 0, complaint_root},
103 };
104
105 /* Some actual complaints. */
106
107 struct complaint oldsyms_complaint = {
108 "Replacing old symbols for `%s'", 0, 0 };
109
110 struct complaint empty_symtab_complaint = {
111 "Empty symbol table found for `%s'", 0, 0 };
112
113
114 /* In the following sort, we always make sure that
115 register debug symbol declarations always come before regular
116 debug symbol declarations (as might happen when parameters are
117 then put into registers by the compiler).
118
119 Since this function is called from within qsort, in an ANSI environment
120 it must conform to the prototype for qsort, which specifies that the
121 comparison function takes two "void *" pointers. */
122
123 static int
compare_symbols(s1p,s2p)124 compare_symbols (s1p, s2p)
125 const PTR s1p;
126 const PTR s2p;
127 {
128 register struct symbol **s1, **s2;
129 register int namediff;
130
131 s1 = (struct symbol **) s1p;
132 s2 = (struct symbol **) s2p;
133
134 /* Compare the initial characters. */
135 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
136 if (namediff != 0) return namediff;
137
138 /* If they match, compare the rest of the names. */
139 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
140 if (namediff != 0) return namediff;
141
142 /* For symbols of the same name, registers should come first. */
143 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
144 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
145 }
146
147 /*
148
149 LOCAL FUNCTION
150
151 compare_psymbols -- compare two partial symbols by name
152
153 DESCRIPTION
154
155 Given pointer to two partial symbol table entries, compare
156 them by name and return -N, 0, or +N (ala strcmp). Typically
157 used by sorting routines like qsort().
158
159 NOTES
160
161 Does direct compare of first two characters before punting
162 and passing to strcmp for longer compares. Note that the
163 original version had a bug whereby two null strings or two
164 identically named one character strings would return the
165 comparison of memory following the null byte.
166
167 */
168
169 static int
compare_psymbols(s1p,s2p)170 compare_psymbols (s1p, s2p)
171 const PTR s1p;
172 const PTR s2p;
173 {
174 register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
175 register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
176
177 if ((st1[0] - st2[0]) || !st1[0])
178 {
179 return (st1[0] - st2[0]);
180 }
181 else if ((st1[1] - st2[1]) || !st1[1])
182 {
183 return (st1[1] - st2[1]);
184 }
185 else
186 {
187 return (strcmp (st1 + 2, st2 + 2));
188 }
189 }
190
191 void
sort_pst_symbols(pst)192 sort_pst_symbols (pst)
193 struct partial_symtab *pst;
194 {
195 /* Sort the global list; don't sort the static list */
196
197 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
198 pst -> n_global_syms, sizeof (struct partial_symbol),
199 compare_psymbols);
200 }
201
202 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
203
204 void
sort_block_syms(b)205 sort_block_syms (b)
206 register struct block *b;
207 {
208 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
209 sizeof (struct symbol *), compare_symbols);
210 }
211
212 /* Call sort_symtab_syms to sort alphabetically
213 the symbols of each block of one symtab. */
214
215 void
sort_symtab_syms(s)216 sort_symtab_syms (s)
217 register struct symtab *s;
218 {
219 register struct blockvector *bv;
220 int nbl;
221 int i;
222 register struct block *b;
223
224 if (s == 0)
225 return;
226 bv = BLOCKVECTOR (s);
227 nbl = BLOCKVECTOR_NBLOCKS (bv);
228 for (i = 0; i < nbl; i++)
229 {
230 b = BLOCKVECTOR_BLOCK (bv, i);
231 if (BLOCK_SHOULD_SORT (b))
232 sort_block_syms (b);
233 }
234 }
235
236 void
sort_all_symtab_syms()237 sort_all_symtab_syms ()
238 {
239 register struct symtab *s;
240 register struct objfile *objfile;
241
242 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
243 {
244 for (s = objfile -> symtabs; s != NULL; s = s -> next)
245 {
246 sort_symtab_syms (s);
247 }
248 }
249 }
250
251 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
252 (and add a null character at the end in the copy).
253 Returns the address of the copy. */
254
255 char *
obsavestring(ptr,size,obstackp)256 obsavestring (ptr, size, obstackp)
257 char *ptr;
258 int size;
259 struct obstack *obstackp;
260 {
261 register char *p = (char *) obstack_alloc (obstackp, size + 1);
262 /* Open-coded bcopy--saves function call time.
263 These strings are usually short. */
264 {
265 register char *p1 = ptr;
266 register char *p2 = p;
267 char *end = ptr + size;
268 while (p1 != end)
269 *p2++ = *p1++;
270 }
271 p[size] = 0;
272 return p;
273 }
274
275 /* Concatenate strings S1, S2 and S3; return the new string.
276 Space is found in the symbol_obstack. */
277
278 char *
obconcat(obstackp,s1,s2,s3)279 obconcat (obstackp, s1, s2, s3)
280 struct obstack *obstackp;
281 const char *s1, *s2, *s3;
282 {
283 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
284 register char *val = (char *) obstack_alloc (obstackp, len);
285 strcpy (val, s1);
286 strcat (val, s2);
287 strcat (val, s3);
288 return val;
289 }
290
291 /* Get the symbol table that corresponds to a partial_symtab.
292 This is fast after the first time you do it. In fact, there
293 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
294 case inline. */
295
296 struct symtab *
psymtab_to_symtab(pst)297 psymtab_to_symtab (pst)
298 register struct partial_symtab *pst;
299 {
300 /* If it's been looked up before, return it. */
301 if (pst->symtab)
302 return pst->symtab;
303
304 /* If it has not yet been read in, read it. */
305 if (!pst->readin)
306 {
307 (*pst->read_symtab) (pst);
308 }
309
310 return pst->symtab;
311 }
312
313 /* Initialize entry point information for this objfile. */
314
315 void
init_entry_point_info(objfile)316 init_entry_point_info (objfile)
317 struct objfile *objfile;
318 {
319 /* Save startup file's range of PC addresses to help blockframe.c
320 decide where the bottom of the stack is. */
321
322 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
323 {
324 /* Executable file -- record its entry point so we'll recognize
325 the startup file because it contains the entry point. */
326 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
327 }
328 else
329 {
330 /* Examination of non-executable.o files. Short-circuit this stuff. */
331 /* ~0 will not be in any file, we hope. */
332 objfile -> ei.entry_point = ~0;
333 /* set the startup file to be an empty range. */
334 objfile -> ei.entry_file_lowpc = 0;
335 objfile -> ei.entry_file_highpc = 0;
336 }
337 }
338
339 /* Remember the lowest-addressed loadable section we've seen.
340 This function is called via bfd_map_over_sections. */
341
342 #if 0 /* Not used yet */
343 static void
344 find_lowest_section (abfd, sect, obj)
345 bfd *abfd;
346 asection *sect;
347 PTR obj;
348 {
349 asection **lowest = (asection **)obj;
350
351 if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
352 return;
353 if (!*lowest)
354 *lowest = sect; /* First loadable section */
355 else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect))
356 *lowest = sect; /* A lower loadable section */
357 }
358 #endif
359
360 /* Process a symbol file, as either the main file or as a dynamically
361 loaded file.
362
363 NAME is the file name (which will be tilde-expanded and made
364 absolute herein) (but we don't free or modify NAME itself).
365 FROM_TTY says how verbose to be. MAINLINE specifies whether this
366 is the main symbol file, or whether it's an extra symbol file such
367 as dynamically loaded code. If !mainline, ADDR is the address
368 where the text segment was loaded. If VERBO, the caller has printed
369 a verbose message about the symbol reading (and complaints can be
370 more terse about it). */
371
372 void
syms_from_objfile(objfile,addr,mainline,verbo)373 syms_from_objfile (objfile, addr, mainline, verbo)
374 struct objfile *objfile;
375 CORE_ADDR addr;
376 int mainline;
377 int verbo;
378 {
379 struct section_offsets *section_offsets;
380 asection *lowest_sect;
381
382 /* There is a distinction between having no symbol table
383 (we refuse to read the file, leaving the old set of symbols around)
384 and having no debugging symbols in your symbol table (we read
385 the file and end up with a mostly empty symbol table).
386
387 FIXME: This strategy works correctly when the debugging symbols are
388 intermixed with "normal" symbols. However, when the debugging symbols
389 are separate, such as with ELF/DWARF, it is perfectly plausible for
390 the symbol table to be missing but still have all the DWARF info
391 intact. Thus in general it is wrong to assume that having no symbol
392 table implies no debugging information. */
393
394 if (!(bfd_get_file_flags (objfile -> obfd) & HAS_SYMS))
395 return;
396
397 init_entry_point_info (objfile);
398 find_sym_fns (objfile);
399
400 if (mainline)
401 {
402 /* Since no error yet, throw away the old symbol table. */
403
404 if (symfile_objfile != NULL)
405 {
406 free_objfile (symfile_objfile);
407 symfile_objfile = NULL;
408 }
409
410 (*objfile -> sf -> sym_new_init) (objfile);
411 }
412
413 /* Convert addr into an offset rather than an absolute address.
414 We find the lowest address of a loaded segment in the objfile,
415 and assume that <addr> is where that got loaded. Due to historical
416 precedent, we warn if that doesn't happen to be the ".text"
417 segment. */
418
419 if (mainline)
420 {
421 addr = 0; /* No offset from objfile addresses. */
422 }
423 else
424 {
425 lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
426 #if 0
427 lowest_sect = 0;
428 bfd_map_over_sections (objfile->obfd, find_lowest_section,
429 (PTR) &lowest_sect);
430 #endif
431
432 if (lowest_sect == 0)
433 warning ("no loadable sections found in added symbol-file %s",
434 objfile->name);
435 else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect)
436 || 0 != strcmp(".text",
437 bfd_get_section_name (objfile->obfd, lowest_sect)))
438 warning ("Lowest section in %s is %s at 0x%x",
439 objfile->name,
440 bfd_section_name (objfile->obfd, lowest_sect),
441 bfd_section_vma (objfile->obfd, lowest_sect));
442
443 if (lowest_sect)
444 addr -= bfd_section_vma (objfile->obfd, lowest_sect);
445 }
446
447 /* Initialize symbol reading routines for this objfile, allow complaints to
448 appear for this new file, and record how verbose to be, then do the
449 initial symbol reading for this file. */
450
451 (*objfile -> sf -> sym_init) (objfile);
452 clear_complaints (1, verbo);
453 section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
454 (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
455
456 /* Don't allow char * to have a typename (else would get caddr_t.) */
457 /* Ditto void *. FIXME should do this for all the builtin types. */
458
459 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
460 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
461
462 /* Mark the objfile has having had initial symbol read attempted. Note
463 that this does not mean we found any symbols... */
464
465 objfile -> flags |= OBJF_SYMS;
466 }
467
468 /* Perform required actions immediately after either reading in the initial
469 symbols for a new objfile, or mapping in the symbols from a reusable
470 objfile. */
471
472 void
new_symfile_objfile(objfile,mainline,verbo)473 new_symfile_objfile (objfile, mainline, verbo)
474 struct objfile *objfile;
475 int mainline;
476 int verbo;
477 {
478 if (mainline)
479 {
480 /* OK, make it the "real" symbol file. */
481 symfile_objfile = objfile;
482 }
483
484 /* If we have wiped out any old symbol tables, clean up. */
485 clear_symtab_users_once ();
486
487 /* We're done reading the symbol file; finish off complaints. */
488 clear_complaints (0, verbo);
489
490 /* Fixup all the breakpoints that may have been redefined by this
491 symbol file. */
492
493 breakpoint_re_set ();
494 }
495
496 /* Process a symbol file, as either the main file or as a dynamically
497 loaded file.
498
499 NAME is the file name (which will be tilde-expanded and made
500 absolute herein) (but we don't free or modify NAME itself).
501 FROM_TTY says how verbose to be. MAINLINE specifies whether this
502 is the main symbol file, or whether it's an extra symbol file such
503 as dynamically loaded code. If !mainline, ADDR is the address
504 where the text segment was loaded.
505
506 Upon success, returns a pointer to the objfile that was added.
507 Upon failure, jumps back to command level (never returns). */
508
509 struct objfile *
symbol_file_add(name,from_tty,addr,mainline,mapped,readnow)510 symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
511 char *name;
512 int from_tty;
513 CORE_ADDR addr;
514 int mainline;
515 int mapped;
516 int readnow;
517 {
518 struct objfile *objfile;
519 struct partial_symtab *psymtab;
520 bfd *abfd;
521
522 /* Open a bfd for the file and then check to see if the file has a
523 symbol table. There is a distinction between having no symbol table
524 (we refuse to read the file, leaving the old set of symbols around)
525 and having no debugging symbols in the symbol table (we read the file
526 and end up with a mostly empty symbol table, but with lots of stuff in
527 the minimal symbol table). We need to make the decision about whether
528 to continue with the file before allocating and building a objfile.
529
530 FIXME: This strategy works correctly when the debugging symbols are
531 intermixed with "normal" symbols. However, when the debugging symbols
532 are separate, such as with ELF/DWARF, it is perfectly plausible for
533 the symbol table to be missing but still have all the DWARF info
534 intact. Thus in general it is wrong to assume that having no symbol
535 table implies no debugging information. */
536
537 abfd = symfile_bfd_open (name);
538 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
539 {
540 error ("%s has no symbol-table", name);
541 }
542
543 if ((have_full_symbols () || have_partial_symbols ())
544 && mainline
545 && from_tty
546 && !query ("Load new symbol table from \"%s\"? ", name))
547 error ("Not confirmed.");
548
549 /* Getting new symbols may change our opinion about what is
550 frameless. */
551
552 reinit_frame_cache ();
553
554 objfile = allocate_objfile (abfd, mapped);
555
556 /* If the objfile uses a mapped symbol file, and we have a psymtab for
557 it, then skip reading any symbols at this time. */
558
559 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
560 {
561 /* We mapped in an existing symbol table file that already has had
562 initial symbol reading performed, so we can skip that part. Notify
563 the user that instead of reading the symbols, they have been mapped.
564 */
565 if (from_tty || info_verbose)
566 {
567 printf_filtered ("Mapped symbols for %s...", name);
568 wrap_here ("");
569 fflush (stdout);
570 }
571 init_entry_point_info (objfile);
572 find_sym_fns (objfile);
573 }
574 else
575 {
576 /* We either created a new mapped symbol table, mapped an existing
577 symbol table file which has not had initial symbol reading
578 performed, or need to read an unmapped symbol table. */
579 if (from_tty || info_verbose)
580 {
581 printf_filtered ("Reading symbols from %s...", name);
582 wrap_here ("");
583 fflush (stdout);
584 }
585 syms_from_objfile (objfile, addr, mainline, from_tty);
586 }
587
588 new_symfile_objfile (objfile, mainline, from_tty);
589
590 /* We now have at least a partial symbol table. Check to see if the
591 user requested that all symbols be read on initial access via either
592 the gdb startup command line or on a per symbol file basis. Expand
593 all partial symbol tables for this objfile if so. */
594
595 if (readnow || readnow_symbol_files)
596 {
597 if (from_tty || info_verbose)
598 {
599 printf_filtered ("expanding to full symbols...");
600 wrap_here ("");
601 fflush (stdout);
602 }
603
604 for (psymtab = objfile -> psymtabs;
605 psymtab != NULL;
606 psymtab = psymtab -> next)
607 {
608 psymtab_to_symtab (psymtab);
609 }
610 }
611
612 if (from_tty || info_verbose)
613 {
614 printf_filtered ("done.\n");
615 fflush (stdout);
616 }
617
618 return (objfile);
619 }
620
621 /* This is the symbol-file command. Read the file, analyze its symbols,
622 and add a struct symtab to a symtab list. */
623
624 void
symbol_file_command(args,from_tty)625 symbol_file_command (args, from_tty)
626 char *args;
627 int from_tty;
628 {
629 char **argv;
630 char *name = NULL;
631 struct cleanup *cleanups;
632 int mapped = 0;
633 int readnow = 0;
634
635 dont_repeat ();
636
637 if (args == NULL)
638 {
639 if ((have_full_symbols () || have_partial_symbols ())
640 && from_tty
641 && !query ("Discard symbol table from `%s'? ",
642 symfile_objfile -> name))
643 error ("Not confirmed.");
644 free_all_objfiles ();
645 symfile_objfile = NULL;
646 current_source_symtab = NULL;
647 current_source_line = 0;
648 if (from_tty)
649 {
650 printf ("No symbol file now.\n");
651 }
652 }
653 else
654 {
655 if ((argv = buildargv (args)) == NULL)
656 {
657 nomem (0);
658 }
659 cleanups = make_cleanup (freeargv, (char *) argv);
660 while (*argv != NULL)
661 {
662 if (strcmp (*argv, "-mapped") == 0)
663 {
664 mapped = 1;
665 }
666 else if (strcmp (*argv, "-readnow") == 0)
667 {
668 readnow = 1;
669 }
670 else if (**argv == '-')
671 {
672 error ("unknown option `%s'", *argv);
673 }
674 else
675 {
676 name = *argv;
677 }
678 argv++;
679 }
680
681 if (name == NULL)
682 {
683 error ("no symbol file name was specified");
684 }
685 else
686 {
687 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped, readnow);
688 set_initial_language ();
689 }
690 do_cleanups (cleanups);
691 }
692 }
693
694 /* Set the initial language.
695
696 A better solution would be to record the language in the psymtab when reading
697 partial symbols, and then use it (if known) to set the language. This would
698 be a win for formats that encode the language in an easily discoverable place,
699 such as DWARF. For stabs, we can jump through hoops looking for specially
700 named symbols or try to intuit the language from the specific type of stabs
701 we find, but we can't do that until later when we read in full symbols.
702 FIXME. */
703
704 static void
set_initial_language()705 set_initial_language ()
706 {
707 struct partial_symtab *pst;
708 enum language lang = language_unknown;
709
710 pst = find_main_psymtab ();
711 if (pst != NULL)
712 {
713 if (pst -> filename != NULL)
714 {
715 lang = deduce_language_from_filename (pst -> filename);
716 }
717 if (lang == language_unknown)
718 {
719 /* Make C the default language */
720 lang = language_c;
721 }
722 set_language (lang);
723 expected_language = current_language; /* Don't warn the user */
724 }
725 }
726
727 /* Open file specified by NAME and hand it off to BFD for preliminary
728 analysis. Result is a newly initialized bfd *, which includes a newly
729 malloc'd` copy of NAME (tilde-expanded and made absolute).
730 In case of trouble, error() is called. */
731
732 static bfd *
symfile_bfd_open(name)733 symfile_bfd_open (name)
734 char *name;
735 {
736 bfd *sym_bfd;
737 int desc;
738 char *absolute_name;
739
740 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
741
742 /* Look down path for it, allocate 2nd new malloc'd copy. */
743 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
744 if (desc < 0)
745 {
746 make_cleanup (free, name);
747 perror_with_name (name);
748 }
749 free (name); /* Free 1st new malloc'd copy */
750 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
751 /* It'll be freed in free_objfile(). */
752
753 sym_bfd = bfd_fdopenr (name, NULL, desc);
754 if (!sym_bfd)
755 {
756 close (desc);
757 make_cleanup (free, name);
758 error ("\"%s\": can't open to read symbols: %s.", name,
759 bfd_errmsg (bfd_error));
760 }
761
762 if (!bfd_check_format (sym_bfd, bfd_object))
763 {
764 bfd_close (sym_bfd); /* This also closes desc */
765 make_cleanup (free, name);
766 error ("\"%s\": can't read symbols: %s.", name,
767 bfd_errmsg (bfd_error));
768 }
769
770 return (sym_bfd);
771 }
772
773 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
774 startup by the _initialize routine in each object file format reader,
775 to register information about each format the the reader is prepared
776 to handle. */
777
778 void
add_symtab_fns(sf)779 add_symtab_fns (sf)
780 struct sym_fns *sf;
781 {
782 sf->next = symtab_fns;
783 symtab_fns = sf;
784 }
785
786
787 /* Initialize to read symbols from the symbol file sym_bfd. It either
788 returns or calls error(). The result is an initialized struct sym_fns
789 in the objfile structure, that contains cached information about the
790 symbol file. */
791
792 static void
find_sym_fns(objfile)793 find_sym_fns (objfile)
794 struct objfile *objfile;
795 {
796 struct sym_fns *sf;
797
798 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
799 {
800 if (strncmp (bfd_get_target (objfile -> obfd),
801 sf -> sym_name, sf -> sym_namelen) == 0)
802 {
803 objfile -> sf = sf;
804 return;
805 }
806 }
807 /* XXX What moron thought this error message was cute? */
808 #ifdef notdef
809 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
810 bfd_get_target (objfile -> obfd));
811 #else
812 error ("Symbol format `%s' unknown.", bfd_get_target (objfile->obfd));
813 #endif
814 }
815
816 /* This function runs the load command of our current target. */
817
818 static void
load_command(arg,from_tty)819 load_command (arg, from_tty)
820 char *arg;
821 int from_tty;
822 {
823 target_load (arg, from_tty);
824 }
825
826 /* This function allows the addition of incrementally linked object files.
827 It does not modify any state in the target, only in the debugger. */
828
829 /* ARGSUSED */
830 static void
add_symbol_file_command(args,from_tty)831 add_symbol_file_command (args, from_tty)
832 char *args;
833 int from_tty;
834 {
835 char *name = NULL;
836 CORE_ADDR text_addr;
837 char *arg;
838 int readnow = 0;
839 int mapped = 0;
840
841 dont_repeat ();
842
843 if (args == NULL)
844 {
845 error ("add-symbol-file takes a file name and an address");
846 }
847
848 /* Make a copy of the string that we can safely write into. */
849
850 args = strdup (args);
851 make_cleanup (free, args);
852
853 /* Pick off any -option args and the file name. */
854
855 while ((*args != '\000') && (name == NULL))
856 {
857 while (isspace (*args)) {args++;}
858 arg = args;
859 while ((*args != '\000') && !isspace (*args)) {args++;}
860 if (*args != '\000')
861 {
862 *args++ = '\000';
863 }
864 if (*arg != '-')
865 {
866 name = arg;
867 }
868 else if (strcmp (arg, "-mapped") == 0)
869 {
870 mapped = 1;
871 }
872 else if (strcmp (arg, "-readnow") == 0)
873 {
874 readnow = 1;
875 }
876 else
877 {
878 error ("unknown option `%s'", arg);
879 }
880 }
881
882 /* After picking off any options and the file name, args should be
883 left pointing at the remainder of the command line, which should
884 be the address expression to evaluate. */
885
886 if ((name == NULL) || (*args == '\000') )
887 {
888 error ("add-symbol-file takes a file name and an address");
889 }
890 name = tilde_expand (name);
891 make_cleanup (free, name);
892
893 text_addr = parse_and_eval_address (args);
894
895 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
896 name, local_hex_string (text_addr)))
897 error ("Not confirmed.");
898
899 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
900 }
901
902 /* Re-read symbols if a symbol-file has changed. */
903 void
reread_symbols()904 reread_symbols ()
905 {
906 struct objfile *objfile;
907 long new_modtime;
908 int reread_one = 0;
909 struct stat new_statbuf;
910 int res;
911
912 /* With the addition of shared libraries, this should be modified,
913 the load time should be saved in the partial symbol tables, since
914 different tables may come from different source files. FIXME.
915 This routine should then walk down each partial symbol table
916 and see if the symbol table that it originates from has been changed */
917
918 the_big_top:
919 for (objfile = object_files; objfile; objfile = objfile->next) {
920 if (objfile->obfd) {
921 #ifdef IBM6000_TARGET
922 /* If this object is from a shared library, then you should
923 stat on the library name, not member name. */
924
925 if (objfile->obfd->my_archive)
926 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
927 else
928 #endif
929 res = stat (objfile->name, &new_statbuf);
930 if (res != 0) {
931 /* FIXME, should use print_sys_errmsg but it's not filtered. */
932 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
933 objfile->name);
934 continue;
935 }
936 new_modtime = new_statbuf.st_mtime;
937 if (new_modtime != objfile->mtime) {
938 printf_filtered ("`%s' has changed; re-reading symbols.\n",
939 objfile->name);
940 /* FIXME, this should use a different command...that would only
941 affect this objfile's symbols, and would reset objfile->mtime.
942 (objfile->mtime = new_modtime;)
943 HOWEVER, that command isn't written yet -- so call symbol_file_
944 command, and restart the scan from the top, because it munges
945 the object_files list. */
946 symbol_file_command (objfile->name, 0);
947 reread_one = 1;
948 goto the_big_top; /* Start over. */
949 }
950 }
951 }
952
953 if (reread_one)
954 breakpoint_re_set ();
955 }
956
957 /* Functions to handle complaints during symbol reading. */
958
959 /* How many complaints about a particular thing should be printed before
960 we stop whining about it? Default is no whining at all, since so many
961 systems have ill-constructed symbol files. */
962
963 static unsigned stop_whining = 0;
964
965 /* Should each complaint be self explanatory, or should we assume that
966 a series of complaints is being produced?
967 case 0: self explanatory message.
968 case 1: First message of a series that must start off with explanation.
969 case 2: Subsequent message, when user already knows we are reading
970 symbols and we can just state our piece. */
971
972 static int complaint_series = 0;
973
974 /* Print a complaint about the input symbols, and link the complaint block
975 into a chain for later handling. */
976
977 void
complain(complaint,val)978 complain (complaint, val)
979 struct complaint *complaint;
980 char *val;
981 {
982 complaint->counter++;
983 if (complaint->next == 0) {
984 complaint->next = complaint_root->next;
985 complaint_root->next = complaint;
986 }
987 if (complaint->counter > stop_whining)
988 return;
989 wrap_here ("");
990
991 switch (complaint_series + (info_verbose << 1)) {
992
993 /* Isolated messages, must be self-explanatory. */
994 case 0:
995 puts_filtered ("During symbol reading, ");
996 wrap_here("");
997 printf_filtered (complaint->message, val);
998 puts_filtered (".\n");
999 break;
1000
1001 /* First of a series, without `set verbose'. */
1002 case 1:
1003 puts_filtered ("During symbol reading...");
1004 printf_filtered (complaint->message, val);
1005 puts_filtered ("...");
1006 wrap_here("");
1007 complaint_series++;
1008 break;
1009
1010 /* Subsequent messages of a series, or messages under `set verbose'.
1011 (We'll already have produced a "Reading in symbols for XXX..." message
1012 and will clean up at the end with a newline.) */
1013 default:
1014 printf_filtered (complaint->message, val);
1015 puts_filtered ("...");
1016 wrap_here("");
1017 }
1018 }
1019
1020 /* Clear out all complaint counters that have ever been incremented.
1021 If sym_reading is 1, be less verbose about successive complaints,
1022 since the messages are appearing all together during a command that
1023 reads symbols (rather than scattered around as psymtabs get fleshed
1024 out into symtabs at random times). If noisy is 1, we are in a
1025 noisy symbol reading command, and our caller will print enough
1026 context for the user to figure it out. */
1027
1028 void
clear_complaints(sym_reading,noisy)1029 clear_complaints (sym_reading, noisy)
1030 int sym_reading;
1031 int noisy;
1032 {
1033 struct complaint *p;
1034
1035 for (p = complaint_root->next; p != complaint_root; p = p->next)
1036 p->counter = 0;
1037
1038 if (!sym_reading && !noisy && complaint_series > 1) {
1039 /* Terminate previous series, since caller won't. */
1040 puts_filtered ("\n");
1041 }
1042
1043 complaint_series = sym_reading? 1 + noisy: 0;
1044 }
1045
1046 enum language
deduce_language_from_filename(filename)1047 deduce_language_from_filename (filename)
1048 char *filename;
1049 {
1050 char *c = strrchr (filename, '.');
1051
1052 if (!c) ; /* Get default. */
1053 else if(!strcmp(c,".mod"))
1054 return language_m2;
1055 else if(!strcmp(c,".c"))
1056 return language_c;
1057 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
1058 return language_cplus;
1059
1060 return language_unknown; /* default */
1061 }
1062
1063 /* allocate_symtab:
1064
1065 Allocate and partly initialize a new symbol table. Return a pointer
1066 to it. error() if no space.
1067
1068 Caller must set these fields:
1069 LINETABLE(symtab)
1070 symtab->blockvector
1071 symtab->dirname
1072 symtab->free_code
1073 symtab->free_ptr
1074 initialize any EXTRA_SYMTAB_INFO
1075 possibly free_named_symtabs (symtab->filename);
1076 */
1077
1078 struct symtab *
allocate_symtab(filename,objfile)1079 allocate_symtab (filename, objfile)
1080 char *filename;
1081 struct objfile *objfile;
1082 {
1083 register struct symtab *symtab;
1084
1085 symtab = (struct symtab *)
1086 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
1087 memset (symtab, 0, sizeof (*symtab));
1088 symtab -> filename = obsavestring (filename, strlen (filename),
1089 &objfile -> symbol_obstack);
1090 symtab -> fullname = NULL;
1091 symtab -> language = deduce_language_from_filename (filename);
1092
1093 /* Hook it to the objfile it comes from */
1094
1095 symtab -> objfile = objfile;
1096 symtab -> next = objfile -> symtabs;
1097 objfile -> symtabs = symtab;
1098
1099 #ifdef INIT_EXTRA_SYMTAB_INFO
1100 INIT_EXTRA_SYMTAB_INFO (symtab);
1101 #endif
1102
1103 return (symtab);
1104 }
1105
1106 struct partial_symtab *
allocate_psymtab(filename,objfile)1107 allocate_psymtab (filename, objfile)
1108 char *filename;
1109 struct objfile *objfile;
1110 {
1111 struct partial_symtab *psymtab;
1112
1113 if (objfile -> free_psymtabs)
1114 {
1115 psymtab = objfile -> free_psymtabs;
1116 objfile -> free_psymtabs = psymtab -> next;
1117 }
1118 else
1119 psymtab = (struct partial_symtab *)
1120 obstack_alloc (&objfile -> psymbol_obstack,
1121 sizeof (struct partial_symtab));
1122
1123 memset (psymtab, 0, sizeof (struct partial_symtab));
1124 psymtab -> filename = obsavestring (filename, strlen (filename),
1125 &objfile -> psymbol_obstack);
1126 psymtab -> symtab = NULL;
1127
1128 /* Hook it to the objfile it comes from */
1129
1130 psymtab -> objfile = objfile;
1131 psymtab -> next = objfile -> psymtabs;
1132 objfile -> psymtabs = psymtab;
1133
1134 return (psymtab);
1135 }
1136
1137
1138 /* clear_symtab_users_once:
1139
1140 This function is run after symbol reading, or from a cleanup.
1141 If an old symbol table was obsoleted, the old symbol table
1142 has been blown away, but the other GDB data structures that may
1143 reference it have not yet been cleared or re-directed. (The old
1144 symtab was zapped, and the cleanup queued, in free_named_symtab()
1145 below.)
1146
1147 This function can be queued N times as a cleanup, or called
1148 directly; it will do all the work the first time, and then will be a
1149 no-op until the next time it is queued. This works by bumping a
1150 counter at queueing time. Much later when the cleanup is run, or at
1151 the end of symbol processing (in case the cleanup is discarded), if
1152 the queued count is greater than the "done-count", we do the work
1153 and set the done-count to the queued count. If the queued count is
1154 less than or equal to the done-count, we just ignore the call. This
1155 is needed because reading a single .o file will often replace many
1156 symtabs (one per .h file, for example), and we don't want to reset
1157 the breakpoints N times in the user's face.
1158
1159 The reason we both queue a cleanup, and call it directly after symbol
1160 reading, is because the cleanup protects us in case of errors, but is
1161 discarded if symbol reading is successful. */
1162
1163 static int clear_symtab_users_queued;
1164 static int clear_symtab_users_done;
1165
1166 void
clear_symtab_users_once()1167 clear_symtab_users_once ()
1168 {
1169 /* Enforce once-per-`do_cleanups'-semantics */
1170 if (clear_symtab_users_queued <= clear_symtab_users_done)
1171 return;
1172 clear_symtab_users_done = clear_symtab_users_queued;
1173
1174 printf ("Resetting debugger state after updating old symbol tables\n");
1175
1176 /* Someday, we should do better than this, by only blowing away
1177 the things that really need to be blown. */
1178 clear_value_history ();
1179 clear_displays ();
1180 clear_internalvars ();
1181 breakpoint_re_set ();
1182 set_default_breakpoint (0, 0, 0, 0);
1183 current_source_symtab = 0;
1184 }
1185
1186 /* Delete the specified psymtab, and any others that reference it. */
1187
1188 static void
cashier_psymtab(pst)1189 cashier_psymtab (pst)
1190 struct partial_symtab *pst;
1191 {
1192 struct partial_symtab *ps, *pprev;
1193 int i;
1194
1195 /* Find its previous psymtab in the chain */
1196 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1197 if (ps == pst)
1198 break;
1199 pprev = ps;
1200 }
1201
1202 if (ps) {
1203 /* Unhook it from the chain. */
1204 if (ps == pst->objfile->psymtabs)
1205 pst->objfile->psymtabs = ps->next;
1206 else
1207 pprev->next = ps->next;
1208
1209 /* FIXME, we can't conveniently deallocate the entries in the
1210 partial_symbol lists (global_psymbols/static_psymbols) that
1211 this psymtab points to. These just take up space until all
1212 the psymtabs are reclaimed. Ditto the dependencies list and
1213 filename, which are all in the psymbol_obstack. */
1214
1215 /* We need to cashier any psymtab that has this one as a dependency... */
1216 again:
1217 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1218 for (i = 0; i < ps->number_of_dependencies; i++) {
1219 if (ps->dependencies[i] == pst) {
1220 cashier_psymtab (ps);
1221 goto again; /* Must restart, chain has been munged. */
1222 }
1223 }
1224 }
1225 }
1226 }
1227
1228 /* If a symtab or psymtab for filename NAME is found, free it along
1229 with any dependent breakpoints, displays, etc.
1230 Used when loading new versions of object modules with the "add-file"
1231 command. This is only called on the top-level symtab or psymtab's name;
1232 it is not called for subsidiary files such as .h files.
1233
1234 Return value is 1 if we blew away the environment, 0 if not.
1235 FIXME. The return valu appears to never be used.
1236
1237 FIXME. I think this is not the best way to do this. We should
1238 work on being gentler to the environment while still cleaning up
1239 all stray pointers into the freed symtab. */
1240
1241 int
free_named_symtabs(name)1242 free_named_symtabs (name)
1243 char *name;
1244 {
1245 #if 0
1246 /* FIXME: With the new method of each objfile having it's own
1247 psymtab list, this function needs serious rethinking. In particular,
1248 why was it ever necessary to toss psymtabs with specific compilation
1249 unit filenames, as opposed to all psymtabs from a particular symbol
1250 file? -- fnf
1251 Well, the answer is that some systems permit reloading of particular
1252 compilation units. We want to blow away any old info about these
1253 compilation units, regardless of which objfiles they arrived in. --gnu. */
1254
1255 register struct symtab *s;
1256 register struct symtab *prev;
1257 register struct partial_symtab *ps;
1258 struct blockvector *bv;
1259 int blewit = 0;
1260
1261 /* We only wack things if the symbol-reload switch is set. */
1262 if (!symbol_reloading)
1263 return 0;
1264
1265 /* Some symbol formats have trouble providing file names... */
1266 if (name == 0 || *name == '\0')
1267 return 0;
1268
1269 /* Look for a psymtab with the specified name. */
1270
1271 again2:
1272 for (ps = partial_symtab_list; ps; ps = ps->next) {
1273 if (!strcmp (name, ps->filename)) {
1274 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1275 goto again2; /* Must restart, chain has been munged */
1276 }
1277 }
1278
1279 /* Look for a symtab with the specified name. */
1280
1281 for (s = symtab_list; s; s = s->next)
1282 {
1283 if (!strcmp (name, s->filename))
1284 break;
1285 prev = s;
1286 }
1287
1288 if (s)
1289 {
1290 if (s == symtab_list)
1291 symtab_list = s->next;
1292 else
1293 prev->next = s->next;
1294
1295 /* For now, queue a delete for all breakpoints, displays, etc., whether
1296 or not they depend on the symtab being freed. This should be
1297 changed so that only those data structures affected are deleted. */
1298
1299 /* But don't delete anything if the symtab is empty.
1300 This test is necessary due to a bug in "dbxread.c" that
1301 causes empty symtabs to be created for N_SO symbols that
1302 contain the pathname of the object file. (This problem
1303 has been fixed in GDB 3.9x). */
1304
1305 bv = BLOCKVECTOR (s);
1306 if (BLOCKVECTOR_NBLOCKS (bv) > 2
1307 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1308 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1309 {
1310 complain (&oldsyms_complaint, name);
1311
1312 clear_symtab_users_queued++;
1313 make_cleanup (clear_symtab_users_once, 0);
1314 blewit = 1;
1315 } else {
1316 complain (&empty_symtab_complaint, name);
1317 }
1318
1319 free_symtab (s);
1320 }
1321 else
1322 {
1323 /* It is still possible that some breakpoints will be affected
1324 even though no symtab was found, since the file might have
1325 been compiled without debugging, and hence not be associated
1326 with a symtab. In order to handle this correctly, we would need
1327 to keep a list of text address ranges for undebuggable files.
1328 For now, we do nothing, since this is a fairly obscure case. */
1329 ;
1330 }
1331
1332 /* FIXME, what about the minimal symbol table? */
1333 return blewit;
1334 #else
1335 return (0);
1336 #endif
1337 }
1338
1339 /* Allocate and partially fill a partial symtab. It will be
1340 completely filled at the end of the symbol list.
1341
1342 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1343 is the address relative to which its symbols are (incremental) or 0
1344 (normal). */
1345
1346
1347 struct partial_symtab *
start_psymtab_common(objfile,section_offsets,filename,textlow,global_syms,static_syms)1348 start_psymtab_common (objfile, section_offsets,
1349 filename, textlow, global_syms, static_syms)
1350 struct objfile *objfile;
1351 struct section_offsets *section_offsets;
1352 char *filename;
1353 CORE_ADDR textlow;
1354 struct partial_symbol *global_syms;
1355 struct partial_symbol *static_syms;
1356 {
1357 struct partial_symtab *psymtab;
1358
1359 psymtab = allocate_psymtab (filename, objfile);
1360 psymtab -> section_offsets = section_offsets;
1361 psymtab -> textlow = textlow;
1362 psymtab -> texthigh = psymtab -> textlow; /* default */
1363 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1364 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1365 return (psymtab);
1366 }
1367
1368 /* Debugging versions of functions that are usually inline macros
1369 (see symfile.h). */
1370
1371 #if 0 /* Don't quite work nowadays... */
1372
1373 /* Add a symbol with a long value to a psymtab.
1374 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1375
1376 void
1377 add_psymbol_to_list (name, namelength, namespace, class, list, val)
1378 char *name;
1379 int namelength;
1380 enum namespace namespace;
1381 enum address_class class;
1382 struct psymbol_allocation_list *list;
1383 long val;
1384 {
1385 ADD_PSYMBOL_VT_TO_LIST (name, namelength, namespace, class, (*list), val,
1386 SYMBOL_VALUE);
1387 }
1388
1389 /* Add a symbol with a CORE_ADDR value to a psymtab. */
1390
1391 void
1392 add_psymbol_addr_to_list (name, namelength, namespace, class, list, val)
1393 char *name;
1394 int namelength;
1395 enum namespace namespace;
1396 enum address_class class;
1397 struct psymbol_allocation_list *list;
1398 CORE_ADDR val;
1399 {
1400 ADD_PSYMBOL_VT_TO_LIST (name, namelength, namespace, class, (*list), val,
1401 SYMBOL_VALUE_ADDRESS);
1402 }
1403
1404 #endif /* 0 */
1405
1406 void
_initialize_symfile()1407 _initialize_symfile ()
1408 {
1409
1410 add_com ("symbol-file", class_files, symbol_file_command,
1411 "Load symbol table from executable file FILE.\n\
1412 The `file' command can also load symbol tables, as well as setting the file\n\
1413 to execute.");
1414
1415 add_com ("add-symbol-file", class_files, add_symbol_file_command,
1416 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1417 The second argument provides the starting address of the file's text.");
1418
1419 add_com ("load", class_files, load_command,
1420 "Dynamically load FILE into the running program, and record its symbols\n\
1421 for access from GDB.");
1422
1423 add_show_from_set
1424 (add_set_cmd ("complaints", class_support, var_zinteger,
1425 (char *)&stop_whining,
1426 "Set max number of complaints about incorrect symbols.",
1427 &setlist),
1428 &showlist);
1429
1430 add_show_from_set
1431 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1432 (char *)&symbol_reloading,
1433 "Set dynamic symbol table reloading multiple times in one run.",
1434 &setlist),
1435 &showlist);
1436
1437 }
1438