1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
5 This file is part of GDB.
7 GDB 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 1, or (at your option)
12 GDB 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.
17 You should have received a copy of the GNU General Public License
18 along with GDB; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 #include "breakpoint.h"
36 #include <sys/types.h>
41 extern int info_verbose;
44 extern char *getenv ();
46 /* Functions this file defines */
47 static bfd *symfile_open();
48 static struct sym_fns *symfile_init();
49 static void clear_symtab_users_once();
51 /* List of all available sym_fns. */
53 struct sym_fns *symtab_fns = NULL;
55 /* Saves the sym_fns of the current symbol table, so we can call
56 the right sym_discard function when we free it. */
58 static struct sym_fns *symfile_fns;
60 /* Allocate an obstack to hold objects that should be freed
61 when we load a new symbol table.
62 This includes the symbols made by dbxread
63 and the types that are not permanent. */
65 struct obstack obstack1;
67 struct obstack *symbol_obstack = &obstack1;
69 /* This obstack will be used for partial_symbol objects. It can
70 probably actually be the same as the symbol_obstack above, but I'd
71 like to keep them seperate for now. If I want to later, I'll
72 replace one with the other. */
74 struct obstack obstack2;
76 struct obstack *psymbol_obstack = &obstack2;
78 /* File name symbols were loaded from. */
82 /* The modification date of the file when they were loaded. */
84 long /* really time_t */ symfile_mtime = 0;
86 /* Structures with which to manage partial symbol allocation. */
88 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
90 /* Structure to manage complaints about symbol file contents. */
92 struct complaint complaint_root[1] = {
93 {(char *)0, 0, complaint_root},
96 /* Some actual complaints. */
98 struct complaint oldsyms_complaint = {
99 "Replacing old symbols for `%s'", 0, 0 };
101 struct complaint empty_symtab_complaint = {
102 "Empty symbol table found for `%s'", 0, 0 };
105 /* In the following sort, we always make sure that
106 register debug symbol declarations always come before regular
107 debug symbol declarations (as might happen when parameters are
108 then put into registers by the compiler). */
111 compare_symbols (s1, s2)
112 struct symbol **s1, **s2;
114 register int namediff;
116 /* Compare the initial characters. */
117 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
118 if (namediff != 0) return namediff;
120 /* If they match, compare the rest of the names. */
121 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
122 if (namediff != 0) return namediff;
124 /* For symbols of the same name, registers should come first. */
125 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
126 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
129 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
133 register struct block *b;
135 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
136 sizeof (struct symbol *), compare_symbols);
139 /* Call sort_symtab_syms to sort alphabetically
140 the symbols of each block of one symtab. */
144 register struct symtab *s;
146 register struct blockvector *bv = BLOCKVECTOR (s);
147 int nbl = BLOCKVECTOR_NBLOCKS (bv);
149 register struct block *b;
151 for (i = 0; i < nbl; i++)
153 b = BLOCKVECTOR_BLOCK (bv, i);
154 if (BLOCK_SHOULD_SORT (b))
160 sort_all_symtab_syms ()
162 register struct symtab *s;
164 for (s = symtab_list; s; s = s->next)
166 sort_symtab_syms (s);
170 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
171 (and add a null character at the end in the copy).
172 Returns the address of the copy. */
175 obsavestring (ptr, size)
179 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
180 /* Open-coded bcopy--saves function call time.
181 These strings are usually short. */
183 register char *p1 = ptr;
184 register char *p2 = p;
185 char *end = ptr + size;
193 /* Concatenate strings S1, S2 and S3; return the new string.
194 Space is found in the symbol_obstack. */
197 obconcat (s1, s2, s3)
200 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
201 register char *val = (char *) obstack_alloc (symbol_obstack, len);
208 /* Accumulate the misc functions in bunches of 127.
209 At the end, copy them all into one newly allocated structure. */
211 #define MISC_BUNCH_SIZE 127
215 struct misc_bunch *next;
216 struct misc_function contents[MISC_BUNCH_SIZE];
219 /* Bunch currently being filled up.
220 The next field points to chain of filled bunches. */
222 static struct misc_bunch *misc_bunch;
224 /* Number of slots filled in current bunch. */
226 static int misc_bunch_index;
228 /* Total number of misc functions recorded so far. */
230 static int misc_count;
237 misc_bunch_index = MISC_BUNCH_SIZE;
241 prim_record_misc_function (name, address, misc_type)
244 enum misc_function_type misc_type;
246 register struct misc_bunch *new;
248 if (misc_bunch_index == MISC_BUNCH_SIZE)
250 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
251 misc_bunch_index = 0;
252 new->next = misc_bunch;
255 misc_bunch->contents[misc_bunch_index].name = name;
256 misc_bunch->contents[misc_bunch_index].address = address;
257 misc_bunch->contents[misc_bunch_index].type = misc_type;
258 misc_bunch->contents[misc_bunch_index].misc_info = 0;
264 compare_misc_functions (fn1, fn2)
265 struct misc_function *fn1, *fn2;
267 /* Return a signed result based on unsigned comparisons
268 so that we sort into unsigned numeric order. */
269 if (fn1->address < fn2->address)
271 if (fn1->address > fn2->address)
278 discard_misc_bunches (foo)
281 register struct misc_bunch *next;
285 next = misc_bunch->next;
291 /* INCLINK nonzero means bunches are from an incrementally-linked file.
292 Add them to the existing bunches.
293 Otherwise INCLINK is zero, and we start from scratch. */
295 condense_misc_bunches (inclink)
299 register struct misc_bunch *bunch;
304 = (struct misc_function *)
305 xrealloc (misc_function_vector, (misc_count + misc_function_count)
306 * sizeof (struct misc_function));
307 j = misc_function_count;
312 = (struct misc_function *)
313 xmalloc (misc_count * sizeof (struct misc_function));
320 for (i = 0; i < misc_bunch_index; i++, j++)
322 misc_function_vector[j] = bunch->contents[i];
323 #ifdef NAMES_HAVE_UNDERSCORE
324 if (misc_function_vector[j].name[0] == '_')
325 misc_function_vector[j].name++;
329 misc_bunch_index = MISC_BUNCH_SIZE;
332 if (misc_function_count + misc_count != j) /* DEBUG */
333 printf_filtered ("Function counts are off! %d + %d != %d\n",
334 misc_function_count, misc_count, j);
336 misc_function_count = j;
338 /* Sort the misc functions by address. */
340 qsort (misc_function_vector, misc_function_count,
341 sizeof (struct misc_function),
342 compare_misc_functions);
346 /* Get the symbol table that corresponds to a partial_symtab.
347 This is fast after the first time you do it. In fact, there
348 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
352 psymtab_to_symtab (pst)
353 register struct partial_symtab *pst;
355 register struct symtab *result;
357 /* If it's been looked up before, return it. */
361 /* If it has not yet been read in, read it. */
364 (*pst->read_symtab) (pst);
367 /* Search through list for correct name. */
368 for (result = symtab_list; result; result = result->next)
369 if (!strcmp (result->filename, pst->filename))
371 pst->symtab = result; /* Remember where it was. */
378 /* Process a symbol file, as either the main file or as a dynamically
381 NAME is the file name (which will be tilde-expanded and made
382 absolute herein) (but we don't free or modify NAME itself).
383 FROM_TTY says how verbose to be. MAINLINE specifies whether this
384 is the main symbol file, or whether it's an extra symbol file such
385 as dynamically loaded code. If !mainline, ADDR is the address
386 where the text segment was loaded. */
389 symbol_file_add (name, from_tty, addr, mainline)
400 sym_bfd = symfile_open (name);
402 entry_point = bfd_get_start_address (sym_bfd);
405 symfile_mtime = bfd_get_mtime (sym_bfd);
407 /* There is a distinction between having no symbol table
408 (we refuse to read the file, leaving the old set of symbols around)
409 and having no debugging symbols in your symbol table (we read
410 the file and end up with a mostly empty symbol table). */
412 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
414 error ("%s has no symbol-table", name);
417 if ((symtab_list || partial_symtab_list)
420 && !query ("Load new symbol table from \"%s\"? ", name))
421 error ("Not confirmed.");
425 printf_filtered ("Reading symbol data from %s...", name);
430 sf = symfile_init (sym_bfd);
431 realname = bfd_get_filename (sym_bfd);
432 realname = savestring (realname, strlen (realname));
433 /* FIXME, this probably creates a storage leak... */
437 /* Since no error yet, throw away the old symbol table. */
443 free_all_psymtabs ();
445 (*sf->sym_new_init) ();
447 /* For mainline, caller didn't know the specified address of the
448 text section. We fix that here. */
449 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
450 addr = bfd_section_vma (sym_bfd, text_sect);
453 clear_complaints(); /* Allow complaints to appear for this new file. */
455 (*sf->sym_read) (sf, addr, mainline);
457 /* Don't allow char * to have a typename (else would get caddr_t.) */
458 /* Ditto void *. FIXME should do this for all the builtin types. */
460 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
461 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
465 /* OK, make it the "real" symbol file. */
470 /* If we have wiped out any old symbol tables, clean up. */
471 clear_symtab_users_once ();
475 printf_filtered ("done.\n");
480 /* This is the symbol-file command. Read the file, analyze its symbols,
481 and add a struct symtab to symtab_list. */
484 symbol_file_command (name, from_tty)
493 if ((symtab_list || partial_symtab_list)
495 && !query ("Discard symbol table from `%s'? ", symfile))
496 error ("Not confirmed.");
501 free_all_psymtabs ();
502 /* FIXME, this does not account for the main file and subsequent
503 files (shared libs, dynloads, etc) having different formats.
504 It only calls the cleanup routine for the main file's format. */
506 (*symfile_fns->sym_new_init) ();
513 /* Getting new symbols may change our opinion about what is
515 reinit_frame_cache ();
517 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
520 /* Open NAME and hand it off to BFD for preliminary analysis. Result
521 is a BFD *, which includes a new copy of NAME dynamically allocated
522 (which will be freed by the cleanup chain). In case of trouble,
523 error() is called. */
533 name = tilde_expand (name);
534 make_cleanup (free, name);
536 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
538 perror_with_name (name);
541 make_cleanup (free, absolute_name);
542 name = absolute_name;
545 sym_bfd = bfd_fdopenr (name, NULL, desc);
549 error ("Could not open `%s' to read symbols: %s",
550 name, bfd_errmsg (bfd_error));
552 make_cleanup (bfd_close, sym_bfd);
554 if (!bfd_check_format (sym_bfd, bfd_object))
555 error ("\"%s\": can't read symbols: %s.",
556 name, bfd_errmsg (bfd_error));
561 /* Link a new symtab_fns into the global symtab_fns list.
562 Called by various _initialize routines. */
568 sf->next = symtab_fns;
573 /* Initialize to read symbols from the symbol file sym_bfd. It either
574 returns or calls error(). The result is a malloc'd struct sym_fns
575 that contains cached information about the symbol file. */
577 static struct sym_fns *
578 symfile_init (sym_bfd)
581 struct sym_fns *sf, *sf2;
583 for (sf = symtab_fns; sf != NULL; sf = sf->next)
585 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
587 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
588 /* FIXME, who frees this? */
590 sf2->sym_bfd = sym_bfd;
591 sf2->sym_private = 0; /* Not alloc'd yet */
592 (*sf2->sym_init) (sf2);
596 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
597 return 0; /* Appease lint. */
600 /* This function runs the load command of our current target. */
603 load_command (arg, from_tty)
607 target_load (arg, from_tty);
610 /* This function runs the add_syms command of our current target. */
613 add_symbol_file_command (args, from_tty)
617 /* Getting new symbols may change our opinion about what is
619 reinit_frame_cache ();
621 target_add_syms (args, from_tty);
624 /* This function allows the addition of incrementally linked object files. */
628 add_syms_addr_command (arg_string, from_tty)
636 error ("add-symbol-file takes a file name and an address");
638 arg_string = tilde_expand (arg_string);
639 make_cleanup (free, arg_string);
641 for( ; *arg_string == ' '; arg_string++ );
643 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
644 *arg_string++ = (char) 0;
647 error ("add-symbol-file takes a file name and an address");
649 text_addr = parse_and_eval_address (arg_string);
653 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
655 error ("Not confirmed.");
657 symbol_file_add (name, 0, text_addr, 0);
660 /* Re-read symbols if the symbol-file has changed. */
666 /* With the addition of shared libraries, this should be modified,
667 the load time should be saved in the partial symbol tables, since
668 different tables may come from different source files. FIXME.
669 This routine should then walk down each partial symbol table
670 and see if the symbol table that it originates from has been changed
673 if (stat (symfile, &symstat) < 0)
674 /* Can't read symbol-file. Assume it is up to date. */
677 if (symstat.st_mtime > symfile_mtime)
679 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
680 symbol_file_command (symfile, 0);
681 breakpoint_re_set ();
685 /* This function is really horrible, but to avoid it, there would need
686 to be more filling in of forward references. */
688 fill_in_vptr_fieldno (type)
691 if (TYPE_VPTR_FIELDNO (type) < 0)
694 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
696 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
697 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
699 TYPE_VPTR_FIELDNO (type)
700 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
701 TYPE_VPTR_BASETYPE (type)
702 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
709 /* Functions to handle complaints during symbol reading. */
711 /* How many complaints about a particular thing should be printed before
712 we stop whining about it? */
714 static unsigned stop_whining = 1;
716 /* Print a complaint about the input symbols, and link the complaint block
717 into a chain for later handling. Result is 1 if the complaint was
718 printed, 0 if it was suppressed. */
721 complain (complaint, val)
722 struct complaint *complaint;
725 complaint->counter++;
726 if (complaint->next == 0) {
727 complaint->next = complaint_root->next;
728 complaint_root->next = complaint;
730 if (complaint->counter > stop_whining)
734 puts_filtered ("During symbol reading...");
736 printf_filtered (complaint->message, val);
737 puts_filtered ("...");
740 puts_filtered ("\n");
744 /* Clear out all complaint counters that have ever been incremented. */
751 for (p = complaint_root->next; p != complaint_root; p = p->next)
755 /* clear_symtab_users_once:
757 This function is run after symbol reading, or from a cleanup.
758 If an old symbol table was obsoleted, the old symbol table
759 has been blown away, but the other GDB data structures that may
760 reference it have not yet been cleared or re-directed. (The old
761 symtab was zapped, and the cleanup queued, in free_named_symtab()
764 This function can be queued N times as a cleanup, or called
765 directly; it will do all the work the first time, and then will be a
766 no-op until the next time it is queued. This works by bumping a
767 counter at queueing time. Much later when the cleanup is run, or at
768 the end of symbol processing (in case the cleanup is discarded), if
769 the queued count is greater than the "done-count", we do the work
770 and set the done-count to the queued count. If the queued count is
771 less than or equal to the done-count, we just ignore the call. This
772 is needed because reading a single .o file will often replace many
773 symtabs (one per .h file, for example), and we don't want to reset
774 the breakpoints N times in the user's face.
776 The reason we both queue a cleanup, and call it directly after symbol
777 reading, is because the cleanup protects us in case of errors, but is
778 discarded if symbol reading is successful. */
780 static int clear_symtab_users_queued;
781 static int clear_symtab_users_done;
784 clear_symtab_users_once ()
786 /* Enforce once-per-`do_cleanups'-semantics */
787 if (clear_symtab_users_queued <= clear_symtab_users_done)
789 clear_symtab_users_done = clear_symtab_users_queued;
791 printf ("Resetting debugger state after updating old symbol tables\n");
793 /* Someday, we should do better than this, by only blowing away
794 the things that really need to be blown. */
795 clear_value_history ();
797 clear_internalvars ();
798 breakpoint_re_set ();
799 set_default_breakpoint (0, 0, 0, 0);
800 current_source_symtab = 0;
803 /* Delete the specified psymtab, and any others that reference it. */
806 cashier_psymtab (pst)
807 struct partial_symtab *pst;
809 struct partial_symtab *ps, *pprev;
812 /* Find its previous psymtab in the chain */
813 for (ps = partial_symtab_list; ps; ps = ps->next) {
820 /* Unhook it from the chain. */
821 if (ps == partial_symtab_list)
822 partial_symtab_list = ps->next;
824 pprev->next = ps->next;
826 /* FIXME, we can't conveniently deallocate the entries in the
827 partial_symbol lists (global_psymbols/static_psymbols) that
828 this psymtab points to. These just take up space until all
829 the psymtabs are reclaimed. Ditto the dependencies list and
830 filename, which are all in the psymbol_obstack. */
832 /* We need to cashier any psymtab that has this one as a dependency... */
834 for (ps = partial_symtab_list; ps; ps = ps->next) {
835 for (i = 0; i < ps->number_of_dependencies; i++) {
836 if (ps->dependencies[i] == pst) {
837 cashier_psymtab (ps);
838 goto again; /* Must restart, chain has been munged. */
845 /* If a symtab or psymtab for filename NAME is found, free it along
846 with any dependent breakpoints, displays, etc.
847 Used when loading new versions of object modules with the "add-file"
848 command. This is only called on the top-level symtab or psymtab's name;
849 it is not called for subsidiary files such as .h files.
851 Return value is 1 if we blew away the environment, 0 if not.
853 FIXME. I think this is not the best way to do this. We should
854 work on being gentler to the environment while still cleaning up
855 all stray pointers into the freed symtab. */
858 free_named_symtabs (name)
861 register struct symtab *s;
862 register struct symtab *prev;
863 register struct partial_symtab *ps;
864 struct blockvector *bv;
867 /* Some symbol formats have trouble providing file names... */
868 if (name == 0 || *name == '\0')
871 /* Look for a psymtab with the specified name. */
874 for (ps = partial_symtab_list; ps; ps = ps->next) {
875 if (!strcmp (name, ps->filename)) {
876 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
877 goto again2; /* Must restart, chain has been munged */
881 /* Look for a symtab with the specified name. */
883 for (s = symtab_list; s; s = s->next)
885 if (!strcmp (name, s->filename))
892 if (s == symtab_list)
893 symtab_list = s->next;
895 prev->next = s->next;
897 /* For now, queue a delete for all breakpoints, displays, etc., whether
898 or not they depend on the symtab being freed. This should be
899 changed so that only those data structures affected are deleted. */
901 /* But don't delete anything if the symtab is empty.
902 This test is necessary due to a bug in "dbxread.c" that
903 causes empty symtabs to be created for N_SO symbols that
904 contain the pathname of the object file. (This problem
905 has been fixed in GDB 3.9x). */
908 if (BLOCKLIST_NBLOCKS (bv) > 2
909 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
910 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
912 complain (&oldsyms_complaint, name);
914 clear_symtab_users_queued++;
915 make_cleanup (clear_symtab_users_once, 0);
918 complain (&empty_symtab_complaint, name);
924 /* It is still possible that some breakpoints will be affected
925 even though no symtab was found, since the file might have
926 been compiled without debugging, and hence not be associated
927 with a symtab. In order to handle this correctly, we would need
928 to keep a list of text address ranges for undebuggable files.
929 For now, we do nothing, since this is a fairly obscure case. */
932 /* FIXME, what about the misc function vector? */
937 _initialize_symfile ()
940 add_com ("symbol-file", class_files, symbol_file_command,
941 "Load symbol table from executable file FILE.\n\
942 The `file' command can also load symbol tables, as well as setting the file\n\
945 add_com ("add-symbol-file", class_files, add_symbol_file_command,
946 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
947 The second argument provides the starting address of the file's text.");
949 add_com ("load", class_files, load_command,
950 "Dynamically load FILE into the running program, and record its symbols\n\
951 for access from GDB.");
954 (add_set_cmd ("complaints", class_support, var_uinteger,
955 (char *)&stop_whining,
956 "Set max number of complaints about incorrect symbols.",
960 obstack_init (symbol_obstack);
961 obstack_init (psymbol_obstack);