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
;
45 extern char *getenv ();
47 /* Functions this file defines */
48 static bfd
*symfile_open();
49 static struct sym_fns
*symfile_init();
50 static void clear_symtab_users_once();
52 /* List of all available sym_fns. */
54 struct sym_fns
*symtab_fns
= NULL
;
56 /* Saves the sym_fns of the current symbol table, so we can call
57 the right sym_discard function when we free it. */
59 static struct sym_fns
*symfile_fns
;
61 /* Allocate an obstack to hold objects that should be freed
62 when we load a new symbol table.
63 This includes the symbols made by dbxread
64 and the types that are not permanent. */
66 struct obstack obstack1
;
68 struct obstack
*symbol_obstack
= &obstack1
;
70 /* This obstack will be used for partial_symbol objects. It can
71 probably actually be the same as the symbol_obstack above, but I'd
72 like to keep them seperate for now. If I want to later, I'll
73 replace one with the other. */
75 struct obstack obstack2
;
77 struct obstack
*psymbol_obstack
= &obstack2
;
79 /* File name symbols were loaded from. */
83 /* The modification date of the file when they were loaded. */
85 int symfile_mtime
= 0;
87 /* Structures with which to manage partial symbol allocation. */
89 struct psymbol_allocation_list global_psymbols
= {0}, static_psymbols
= {0};
91 /* Structure to manage complaints about symbol file contents. */
93 struct complaint complaint_root
[1] = {
94 {(char *)0, 0, complaint_root
},
97 /* Some actual complaints. */
99 struct complaint oldsyms_complaint
= {
100 "Replacing old symbols for `%s'", 0, 0 };
102 struct complaint empty_symtab_complaint
= {
103 "Empty symbol table found for `%s'", 0, 0 };
106 /* In the following sort, we always make sure that
107 register debug symbol declarations always come before regular
108 debug symbol declarations (as might happen when parameters are
109 then put into registers by the compiler). */
112 compare_symbols (s1
, s2
)
113 struct symbol
**s1
, **s2
;
115 register int namediff
;
117 /* Compare the initial characters. */
118 namediff
= SYMBOL_NAME (*s1
)[0] - SYMBOL_NAME (*s2
)[0];
119 if (namediff
!= 0) return namediff
;
121 /* If they match, compare the rest of the names. */
122 namediff
= strcmp (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
));
123 if (namediff
!= 0) return namediff
;
125 /* For symbols of the same name, registers should come first. */
126 return ((SYMBOL_CLASS (*s2
) == LOC_REGISTER
)
127 - (SYMBOL_CLASS (*s1
) == LOC_REGISTER
));
130 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
134 register struct block
*b
;
136 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
137 sizeof (struct symbol
*), compare_symbols
);
140 /* Call sort_symtab_syms to sort alphabetically
141 the symbols of each block of one symtab. */
145 register struct symtab
*s
;
147 register struct blockvector
*bv
= BLOCKVECTOR (s
);
148 int nbl
= BLOCKVECTOR_NBLOCKS (bv
);
150 register struct block
*b
;
152 for (i
= 0; i
< nbl
; i
++)
154 b
= BLOCKVECTOR_BLOCK (bv
, i
);
155 if (BLOCK_SHOULD_SORT (b
))
161 sort_all_symtab_syms ()
163 register struct symtab
*s
;
165 for (s
= symtab_list
; s
; s
= s
->next
)
167 sort_symtab_syms (s
);
171 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
172 (and add a null character at the end in the copy).
173 Returns the address of the copy. */
176 obsavestring (ptr
, size
)
180 register char *p
= (char *) obstack_alloc (symbol_obstack
, size
+ 1);
181 /* Open-coded bcopy--saves function call time.
182 These strings are usually short. */
184 register char *p1
= ptr
;
185 register char *p2
= p
;
186 char *end
= ptr
+ size
;
194 /* Concatenate strings S1, S2 and S3; return the new string.
195 Space is found in the symbol_obstack. */
198 obconcat (s1
, s2
, s3
)
201 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
202 register char *val
= (char *) obstack_alloc (symbol_obstack
, len
);
209 /* Accumulate the misc functions in bunches of 127.
210 At the end, copy them all into one newly allocated structure. */
212 #define MISC_BUNCH_SIZE 127
216 struct misc_bunch
*next
;
217 struct misc_function contents
[MISC_BUNCH_SIZE
];
220 /* Bunch currently being filled up.
221 The next field points to chain of filled bunches. */
223 static struct misc_bunch
*misc_bunch
;
225 /* Number of slots filled in current bunch. */
227 static int misc_bunch_index
;
229 /* Total number of misc functions recorded so far. */
231 static int misc_count
;
238 misc_bunch_index
= MISC_BUNCH_SIZE
;
242 prim_record_misc_function (name
, address
, misc_type
)
245 enum misc_function_type misc_type
;
247 register struct misc_bunch
*new;
249 if (misc_bunch_index
== MISC_BUNCH_SIZE
)
251 new = (struct misc_bunch
*) xmalloc (sizeof (struct misc_bunch
));
252 misc_bunch_index
= 0;
253 new->next
= misc_bunch
;
256 misc_bunch
->contents
[misc_bunch_index
].name
= name
;
257 misc_bunch
->contents
[misc_bunch_index
].address
= address
;
258 misc_bunch
->contents
[misc_bunch_index
].type
= misc_type
;
259 misc_bunch
->contents
[misc_bunch_index
].misc_info
= 0;
265 compare_misc_functions (fn1
, fn2
)
266 struct misc_function
*fn1
, *fn2
;
268 /* Return a signed result based on unsigned comparisons
269 so that we sort into unsigned numeric order. */
270 if (fn1
->address
< fn2
->address
)
272 if (fn1
->address
> fn2
->address
)
279 discard_misc_bunches (foo
)
282 register struct misc_bunch
*next
;
286 next
= misc_bunch
->next
;
292 /* INCLINK nonzero means bunches are from an incrementally-linked file.
293 Add them to the existing bunches.
294 Otherwise INCLINK is zero, and we start from scratch. */
296 condense_misc_bunches (inclink
)
300 register struct misc_bunch
*bunch
;
305 = (struct misc_function
*)
306 xrealloc (misc_function_vector
, (misc_count
+ misc_function_count
)
307 * sizeof (struct misc_function
));
308 j
= misc_function_count
;
313 = (struct misc_function
*)
314 xmalloc (misc_count
* sizeof (struct misc_function
));
321 for (i
= 0; i
< misc_bunch_index
; i
++, j
++)
323 misc_function_vector
[j
] = bunch
->contents
[i
];
324 #ifdef NAMES_HAVE_UNDERSCORE
325 if (misc_function_vector
[j
].name
[0] == '_')
326 misc_function_vector
[j
].name
++;
330 misc_bunch_index
= MISC_BUNCH_SIZE
;
333 if (misc_function_count
+ misc_count
!= j
) /* DEBUG */
334 printf_filtered ("Function counts are off! %d + %d != %d\n",
335 misc_function_count
, misc_count
, j
);
337 misc_function_count
= j
;
339 /* Sort the misc functions by address. */
341 qsort (misc_function_vector
, misc_function_count
,
342 sizeof (struct misc_function
),
343 compare_misc_functions
);
347 /* Get the symbol table that corresponds to a partial_symtab.
348 This is fast after the first time you do it. In fact, there
349 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
353 psymtab_to_symtab (pst
)
354 register struct partial_symtab
*pst
;
356 register struct symtab
*result
;
358 /* If it's been looked up before, return it. */
362 /* If it has not yet been read in, read it. */
365 (*pst
->read_symtab
) (pst
);
368 /* Search through list for correct name. */
369 for (result
= symtab_list
; result
; result
= result
->next
)
370 if (!strcmp (result
->filename
, pst
->filename
))
372 pst
->symtab
= result
; /* Remember where it was. */
379 /* Process a symbol file, as either the main file or as a dynamically
382 NAME is the file name (which will be tilde-expanded and made absolute
383 herein). FROM_TTY says how verbose to be. MAINLINE specifies whether
384 this is the main symbol file, or whether it's an extra symbol file
385 such 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 symbol_file_add (name
, from_tty
, (CORE_ADDR
)0, 1);
516 /* Open NAME and hand it off to BFD for preliminary analysis. Result
517 is a BFD *, which includes a new copy of NAME dynamically allocated
518 (which will be freed by the cleanup chain). In case of trouble,
519 error() is called. */
529 name
= tilde_expand (name
);
530 make_cleanup (free
, name
);
532 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
, 0, &absolute_name
);
534 perror_with_name (name
);
537 make_cleanup (free
, absolute_name
);
538 name
= absolute_name
;
541 sym_bfd
= bfd_fdopenr (name
, NULL
, desc
);
545 error ("Could not open `%s' to read symbols: %s",
546 name
, bfd_errmsg (bfd_error
));
548 make_cleanup (bfd_close
, sym_bfd
);
550 if (!bfd_check_format (sym_bfd
, bfd_object
))
551 error ("\"%s\": can't read symbols: %s.",
552 name
, bfd_errmsg (bfd_error
));
557 /* Link a new symtab_fns into the global symtab_fns list.
558 Called by various _initialize routines. */
564 sf
->next
= symtab_fns
;
569 /* Initialize to read symbols from the symbol file sym_bfd. It either
570 returns or calls error(). The result is a malloc'd struct sym_fns
571 that contains cached information about the symbol file. */
573 static struct sym_fns
*
574 symfile_init (sym_bfd
)
577 struct sym_fns
*sf
, *sf2
;
579 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
->next
)
581 if (!strncmp (bfd_get_target (sym_bfd
), sf
->sym_name
, sf
->sym_namelen
))
583 sf2
= (struct sym_fns
*)xmalloc (sizeof (*sf2
));
584 /* FIXME, who frees this? */
586 sf2
->sym_bfd
= sym_bfd
;
587 sf2
->sym_private
= 0; /* Not alloc'd yet */
588 (*sf2
->sym_init
) (sf2
);
592 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
595 /* This function runs the load command of our current target. */
598 load_command (arg
, from_tty
)
602 target_load (arg
, from_tty
);
605 /* This function runs the add_syms command of our current target. */
608 add_syms_command (args
, from_tty
)
612 target_add_syms (args
, from_tty
);
615 /* This function allows the addition of incrementally linked object files. */
618 add_syms_addr_command (arg_string
, from_tty
)
626 error ("add-syms takes a file name and an address");
628 arg_string
= tilde_expand (arg_string
);
629 make_cleanup (free
, arg_string
);
631 for( ; *arg_string
== ' '; arg_string
++ );
633 for( ; *arg_string
&& *arg_string
!= ' ' ; arg_string
++ );
634 *arg_string
++ = (char) 0;
637 error ("add-syms takes a file name and an address");
639 text_addr
= parse_and_eval_address (arg_string
);
643 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
645 error ("Not confirmed.");
647 symbol_file_add (name
, 0, text_addr
, 0);
650 /* Re-read symbols if the symbol-file has changed. */
656 /* With the addition of shared libraries, this should be modified,
657 the load time should be saved in the partial symbol tables, since
658 different tables may come from different source files. FIXME.
659 This routine should then walk down each partial symbol table
660 and see if the symbol table that it originates from has been changed
663 if (stat (symfile
, &symstat
) < 0)
664 /* Can't read symbol-file. Assume it is up to date. */
667 if (symstat
.st_mtime
> symfile_mtime
)
669 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
670 symbol_file_command (symfile
, 0);
671 breakpoint_re_set ();
676 /* This function is really horrible, but to avoid it, there would need
677 to be more filling in of forward references. */
679 fill_in_vptr_fieldno (type
)
682 check_stub_type (type
);
683 if (TYPE_VPTR_FIELDNO (type
) < 0)
684 TYPE_VPTR_FIELDNO (type
) =
685 fill_in_vptr_fieldno (TYPE_BASECLASS (type
, 1));
686 return TYPE_VPTR_FIELDNO (type
);
689 /* Functions to handle complaints during symbol reading. */
691 /* How many complaints about a particular thing should be printed before
692 we stop whining about it? */
694 static unsigned stop_whining
= 1;
696 /* Print a complaint about the input symbols, and link the complaint block
697 into a chain for later handling. Result is 1 if the complaint was
698 printed, 0 if it was suppressed. */
701 complain (complaint
, val
)
702 struct complaint
*complaint
;
705 complaint
->counter
++;
706 if (complaint
->next
== 0) {
707 complaint
->next
= complaint_root
->next
;
708 complaint_root
->next
= complaint
;
710 if (complaint
->counter
> stop_whining
)
714 puts_filtered ("During symbol reading...");
716 printf_filtered (complaint
->message
, val
);
717 puts_filtered ("...");
720 puts_filtered ("\n");
724 /* Clear out all complaint counters that have ever been incremented. */
731 for (p
= complaint_root
->next
; p
!= complaint_root
; p
= p
->next
)
735 /* clear_symtab_users_once:
737 This function is run after symbol reading, or from a cleanup.
738 If an old symbol table was obsoleted, the old symbol table
739 has been blown away, but the other GDB data structures that may
740 reference it have not yet been cleared or re-directed. (The old
741 symtab was zapped, and the cleanup queued, in free_named_symtab()
744 This function can be queued N times as a cleanup, or called
745 directly; it will do all the work the first time, and then will be a
746 no-op until the next time it is queued. This works by bumping a
747 counter at queueing time. Much later when the cleanup is run, or at
748 the end of symbol processing (in case the cleanup is discarded), if
749 the queued count is greater than the "done-count", we do the work
750 and set the done-count to the queued count. If the queued count is
751 less than or equal to the done-count, we just ignore the call. This
752 is needed because reading a single .o file will often replace many
753 symtabs (one per .h file, for example), and we don't want to reset
754 the breakpoints N times in the user's face.
756 The reason we both queue a cleanup, and call it directly after symbol
757 reading, is because the cleanup protects us in case of errors, but is
758 discarded if symbol reading is successful. */
760 static int clear_symtab_users_queued
;
761 static int clear_symtab_users_done
;
764 clear_symtab_users_once ()
766 /* Enforce once-per-`do_cleanups'-semantics */
767 if (clear_symtab_users_queued
<= clear_symtab_users_done
)
769 clear_symtab_users_done
= clear_symtab_users_queued
;
771 printf ("Resetting debugger state after updating old symbol tables\n");
773 /* Someday, we should do better than this, by only blowing away
774 the things that really need to be blown. */
775 clear_value_history ();
777 clear_internalvars ();
778 breakpoint_re_set ();
779 set_default_breakpoint (0, 0, 0, 0);
780 current_source_symtab
= 0;
783 /* Delete the specified psymtab, and any others that reference it. */
785 cashier_psymtab (pst
)
786 struct partial_symtab
*pst
;
788 struct partial_symtab
*ps
, *pprev
;
791 /* Find its previous psymtab in the chain */
792 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
799 /* Unhook it from the chain. */
800 if (ps
== partial_symtab_list
)
801 partial_symtab_list
= ps
->next
;
803 pprev
->next
= ps
->next
;
805 /* FIXME, we can't conveniently deallocate the entries in the
806 partial_symbol lists (global_psymbols/static_psymbols) that
807 this psymtab points to. These just take up space until all
808 the psymtabs are reclaimed. Ditto the dependencies list and
809 filename, which are all in the psymbol_obstack. */
811 /* We need to cashier any psymtab that has this one as a dependency... */
813 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
814 for (i
= 0; i
< ps
->number_of_dependencies
; i
++) {
815 if (ps
->dependencies
[i
] == pst
) {
816 cashier_psymtab (ps
);
817 goto again
; /* Must restart, chain has been munged. */
824 /* If a symtab or psymtab for filename NAME is found, free it along
825 with any dependent breakpoints, displays, etc.
826 Used when loading new versions of object modules with the "add-file"
827 command. This is only called on the top-level symtab or psymtab's name;
828 it is not called for subsidiary files such as .h files.
830 Return value is 1 if we blew away the environment, 0 if not.
832 FIXME. I think this is not the best way to do this. We should
833 work on being gentler to the environment while still cleaning up
834 all stray pointers into the freed symtab. */
837 free_named_symtabs (name
)
840 register struct symtab
*s
;
841 register struct symtab
*prev
;
842 register struct partial_symtab
*ps
;
843 register struct partial_symtab
*pprev
;
844 struct blockvector
*bv
;
847 /* Look for a psymtab with the specified name. */
850 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
851 if (!strcmp (name
, ps
->filename
)) {
852 cashier_psymtab (ps
); /* Blow it away...and its little dog, too. */
853 goto again2
; /* Must restart, chain has been munged */
857 /* Look for a symtab with the specified name. */
859 for (s
= symtab_list
; s
; s
= s
->next
)
861 if (!strcmp (name
, s
->filename
))
868 if (s
== symtab_list
)
869 symtab_list
= s
->next
;
871 prev
->next
= s
->next
;
873 /* For now, queue a delete for all breakpoints, displays, etc., whether
874 or not they depend on the symtab being freed. This should be
875 changed so that only those data structures affected are deleted. */
877 /* But don't delete anything if the symtab is empty.
878 This test is necessary due to a bug in "dbxread.c" that
879 causes empty symtabs to be created for N_SO symbols that
880 contain the pathname of the object file. (This problem
881 has been fixed in GDB 3.9x). */
884 if (BLOCKLIST_NBLOCKS (bv
) > 2
885 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
))
886 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
)))
888 complain (&oldsyms_complaint
, name
);
890 clear_symtab_users_queued
++;
891 make_cleanup (clear_symtab_users_once
, 0);
894 complain (&empty_symtab_complaint
, name
);
900 /* It is still possible that some breakpoints will be affected
901 even though no symtab was found, since the file might have
902 been compiled without debugging, and hence not be associated
903 with a symtab. In order to handle this correctly, we would need
904 to keep a list of text address ranges for undebuggable files.
905 For now, we do nothing, since this is a fairly obscure case. */
908 /* FIXME, what about the misc function vector? */
913 _initialize_symfile ()
916 add_com ("symbol-file", class_files
, symbol_file_command
,
917 "Load symbol table from executable file FILE.\n\
918 The `file' command can also load symbol tables, as well as setting the file\n\
921 add_com ("add-syms", class_files
, add_syms_command
,
922 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
923 The second argument provides the starting address of the file's text.");
925 add_com ("load", class_files
, load_command
,
926 "Dynamically load FILE into the running program, and record its symbols\n\
927 for access from GDB.");
930 (add_set_cmd ("complaints", class_support
, var_uinteger
,
931 (char *)&stop_whining
,
932 "Set max number of complaints about incorrect symbols.",
936 obstack_init (symbol_obstack
);
937 obstack_init (psymbol_obstack
);