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 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.
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.
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. */
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 XXX_new_init function when we free it. FIXME. This
57 should be extended to calling the new_init function for each
58 existing symtab or psymtab, since the main symbol file and
59 subsequent added symbol files can have different types. */
61 static struct sym_fns
*symfile_fns
;
63 /* Allocate an obstack to hold objects that should be freed
64 when we load a new symbol table.
65 This includes the symbols made by dbxread
66 and the types that are not permanent. */
68 struct obstack obstack1
;
70 struct obstack
*symbol_obstack
= &obstack1
;
72 /* This obstack will be used for partial_symbol objects. It can
73 probably actually be the same as the symbol_obstack above, but I'd
74 like to keep them seperate for now. If I want to later, I'll
75 replace one with the other. */
77 struct obstack obstack2
;
79 struct obstack
*psymbol_obstack
= &obstack2
;
81 /* File name symbols were loaded from. */
85 /* The modification date of the file when they were loaded. */
87 long /* really time_t */ symfile_mtime
= 0;
89 /* Structures with which to manage partial symbol allocation. */
91 struct psymbol_allocation_list global_psymbols
= {0}, static_psymbols
= {0};
93 /* Flag for whether user will be reloading symbols multiple times.
94 Defaults to ON for VxWorks, otherwise OFF. */
96 #ifdef SYMBOL_RELOADING_DEFAULT
97 int symbol_reloading
= SYMBOL_RELOADING_DEFAULT
;
99 int symbol_reloading
= 0;
102 /* Structure to manage complaints about symbol file contents. */
104 struct complaint complaint_root
[1] = {
105 {(char *)0, 0, complaint_root
},
108 /* Some actual complaints. */
110 struct complaint oldsyms_complaint
= {
111 "Replacing old symbols for `%s'", 0, 0 };
113 struct complaint empty_symtab_complaint
= {
114 "Empty symbol table found for `%s'", 0, 0 };
117 /* In the following sort, we always make sure that
118 register debug symbol declarations always come before regular
119 debug symbol declarations (as might happen when parameters are
120 then put into registers by the compiler). */
123 compare_symbols (s1
, s2
)
124 struct symbol
**s1
, **s2
;
126 register int namediff
;
128 /* Compare the initial characters. */
129 namediff
= SYMBOL_NAME (*s1
)[0] - SYMBOL_NAME (*s2
)[0];
130 if (namediff
!= 0) return namediff
;
132 /* If they match, compare the rest of the names. */
133 namediff
= strcmp (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
));
134 if (namediff
!= 0) return namediff
;
136 /* For symbols of the same name, registers should come first. */
137 return ((SYMBOL_CLASS (*s2
) == LOC_REGISTER
)
138 - (SYMBOL_CLASS (*s1
) == LOC_REGISTER
));
141 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
145 register struct block
*b
;
147 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
148 sizeof (struct symbol
*), compare_symbols
);
151 /* Call sort_symtab_syms to sort alphabetically
152 the symbols of each block of one symtab. */
156 register struct symtab
*s
;
158 register struct blockvector
*bv
= BLOCKVECTOR (s
);
159 int nbl
= BLOCKVECTOR_NBLOCKS (bv
);
161 register struct block
*b
;
163 for (i
= 0; i
< nbl
; i
++)
165 b
= BLOCKVECTOR_BLOCK (bv
, i
);
166 if (BLOCK_SHOULD_SORT (b
))
172 sort_all_symtab_syms ()
174 register struct symtab
*s
;
176 for (s
= symtab_list
; s
; s
= s
->next
)
178 sort_symtab_syms (s
);
182 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
183 (and add a null character at the end in the copy).
184 Returns the address of the copy. */
187 obsavestring (ptr
, size
)
191 register char *p
= (char *) obstack_alloc (symbol_obstack
, size
+ 1);
192 /* Open-coded bcopy--saves function call time.
193 These strings are usually short. */
195 register char *p1
= ptr
;
196 register char *p2
= p
;
197 char *end
= ptr
+ size
;
205 /* Concatenate strings S1, S2 and S3; return the new string.
206 Space is found in the symbol_obstack. */
209 obconcat (s1
, s2
, s3
)
212 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
213 register char *val
= (char *) obstack_alloc (symbol_obstack
, len
);
220 /* Accumulate the misc functions in bunches of 127.
221 At the end, copy them all into one newly allocated structure. */
223 #define MISC_BUNCH_SIZE 127
227 struct misc_bunch
*next
;
228 struct misc_function contents
[MISC_BUNCH_SIZE
];
231 /* Bunch currently being filled up.
232 The next field points to chain of filled bunches. */
234 static struct misc_bunch
*misc_bunch
;
236 /* Number of slots filled in current bunch. */
238 static int misc_bunch_index
;
240 /* Total number of misc functions recorded so far. */
242 static int misc_count
;
249 misc_bunch_index
= MISC_BUNCH_SIZE
;
253 prim_record_misc_function (name
, address
, misc_type
)
256 enum misc_function_type misc_type
;
258 register struct misc_bunch
*new;
260 if (misc_bunch_index
== MISC_BUNCH_SIZE
)
262 new = (struct misc_bunch
*) xmalloc (sizeof (struct misc_bunch
));
263 misc_bunch_index
= 0;
264 new->next
= misc_bunch
;
267 misc_bunch
->contents
[misc_bunch_index
].name
= name
;
268 misc_bunch
->contents
[misc_bunch_index
].address
= address
;
269 misc_bunch
->contents
[misc_bunch_index
].type
= misc_type
;
270 misc_bunch
->contents
[misc_bunch_index
].misc_info
= 0;
276 compare_misc_functions (fn1
, fn2
)
277 struct misc_function
*fn1
, *fn2
;
279 /* Return a signed result based on unsigned comparisons
280 so that we sort into unsigned numeric order. */
281 if (fn1
->address
< fn2
->address
)
283 if (fn1
->address
> fn2
->address
)
290 discard_misc_bunches (foo
)
293 register struct misc_bunch
*next
;
297 next
= misc_bunch
->next
;
303 /* INCLINK nonzero means bunches are from an incrementally-linked file.
304 Add them to the existing bunches.
305 Otherwise INCLINK is zero, and we start from scratch. */
307 condense_misc_bunches (inclink
)
311 register struct misc_bunch
*bunch
;
316 = (struct misc_function
*)
317 xrealloc (misc_function_vector
, (misc_count
+ misc_function_count
)
318 * sizeof (struct misc_function
));
319 j
= misc_function_count
;
324 = (struct misc_function
*)
325 xmalloc (misc_count
* sizeof (struct misc_function
));
332 for (i
= 0; i
< misc_bunch_index
; i
++, j
++)
334 misc_function_vector
[j
] = bunch
->contents
[i
];
335 #ifdef NAMES_HAVE_UNDERSCORE
336 if (misc_function_vector
[j
].name
[0] == '_')
337 misc_function_vector
[j
].name
++;
341 misc_bunch_index
= MISC_BUNCH_SIZE
;
344 if (misc_function_count
+ misc_count
!= j
) /* DEBUG */
345 printf_filtered ("Function counts are off! %d + %d != %d\n",
346 misc_function_count
, misc_count
, j
);
348 misc_function_count
= j
;
350 /* Sort the misc functions by address. */
352 qsort (misc_function_vector
, misc_function_count
,
353 sizeof (struct misc_function
),
354 compare_misc_functions
);
358 /* Get the symbol table that corresponds to a partial_symtab.
359 This is fast after the first time you do it. In fact, there
360 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
364 psymtab_to_symtab (pst
)
365 register struct partial_symtab
*pst
;
367 register struct symtab
*result
;
369 /* If it's been looked up before, return it. */
373 /* If it has not yet been read in, read it. */
376 (*pst
->read_symtab
) (pst
);
382 /* Process a symbol file, as either the main file or as a dynamically
385 NAME is the file name (which will be tilde-expanded and made
386 absolute herein) (but we don't free or modify NAME itself).
387 FROM_TTY says how verbose to be. MAINLINE specifies whether this
388 is the main symbol file, or whether it's an extra symbol file such
389 as dynamically loaded code. If !mainline, ADDR is the address
390 where the text segment was loaded. */
393 symbol_file_add (name
, from_tty
, addr
, mainline
)
404 sym_bfd
= symfile_open (name
);
406 entry_point
= bfd_get_start_address (sym_bfd
);
409 symfile_mtime
= bfd_get_mtime (sym_bfd
);
411 /* There is a distinction between having no symbol table
412 (we refuse to read the file, leaving the old set of symbols around)
413 and having no debugging symbols in your symbol table (we read
414 the file and end up with a mostly empty symbol table). */
416 if (!(bfd_get_file_flags (sym_bfd
) & HAS_SYMS
))
418 error ("%s has no symbol-table", name
);
421 if ((symtab_list
|| partial_symtab_list
)
424 && !query ("Load new symbol table from \"%s\"? ", name
))
425 error ("Not confirmed.");
429 printf_filtered ("Reading symbols from %s...", name
);
434 sf
= symfile_init (sym_bfd
);
435 realname
= bfd_get_filename (sym_bfd
);
436 realname
= savestring (realname
, strlen (realname
));
437 /* FIXME, this probably creates a storage leak... */
441 /* Since no error yet, throw away the old symbol table. */
447 free_all_psymtabs ();
449 (*sf
->sym_new_init
) ();
451 /* For mainline, caller didn't know the specified address of the
452 text section. We fix that here. */
453 text_sect
= bfd_get_section_by_name (sym_bfd
, ".text");
454 addr
= bfd_section_vma (sym_bfd
, text_sect
);
457 clear_complaints(); /* Allow complaints to appear for this new file. */
459 (*sf
->sym_read
) (sf
, addr
, mainline
);
461 /* Don't allow char * to have a typename (else would get caddr_t.) */
462 /* Ditto void *. FIXME should do this for all the builtin types. */
464 TYPE_NAME (lookup_pointer_type (builtin_type_char
)) = 0;
465 TYPE_NAME (lookup_pointer_type (builtin_type_void
)) = 0;
469 /* OK, make it the "real" symbol file. */
474 /* If we have wiped out any old symbol tables, clean up. */
475 clear_symtab_users_once ();
479 printf_filtered ("done.\n");
484 /* This is the symbol-file command. Read the file, analyze its symbols,
485 and add a struct symtab to symtab_list. */
488 symbol_file_command (name
, from_tty
)
497 if ((symtab_list
|| partial_symtab_list
)
499 && !query ("Discard symbol table from `%s'? ", symfile
))
500 error ("Not confirmed.");
505 free_all_psymtabs ();
506 /* FIXME, this does not account for the main file and subsequent
507 files (shared libs, dynloads, etc) having different formats.
508 It only calls the cleanup routine for the main file's format. */
510 (*symfile_fns
->sym_new_init
) ();
517 /* Getting new symbols may change our opinion about what is
519 reinit_frame_cache ();
521 symbol_file_add (name
, from_tty
, (CORE_ADDR
)0, 1);
524 /* Open NAME and hand it off to BFD for preliminary analysis. Result
525 is a BFD *, which includes a new copy of NAME dynamically allocated
526 (which will be freed by the cleanup chain). In case of trouble,
527 error() is called. */
537 name
= tilde_expand (name
);
538 make_cleanup (free
, name
);
540 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
, 0, &absolute_name
);
542 perror_with_name (name
);
545 make_cleanup (free
, absolute_name
);
546 name
= absolute_name
;
549 sym_bfd
= bfd_fdopenr (name
, NULL
, desc
);
553 error ("Could not open `%s' to read symbols: %s",
554 name
, bfd_errmsg (bfd_error
));
556 make_cleanup (bfd_close
, sym_bfd
);
558 if (!bfd_check_format (sym_bfd
, bfd_object
))
559 error ("\"%s\": can't read symbols: %s.",
560 name
, bfd_errmsg (bfd_error
));
565 /* Link a new symtab_fns into the global symtab_fns list.
566 Called by various _initialize routines. */
572 sf
->next
= symtab_fns
;
577 /* Initialize to read symbols from the symbol file sym_bfd. It either
578 returns or calls error(). The result is a malloc'd struct sym_fns
579 that contains cached information about the symbol file. */
581 static struct sym_fns
*
582 symfile_init (sym_bfd
)
585 struct sym_fns
*sf
, *sf2
;
587 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
->next
)
589 if (!strncmp (bfd_get_target (sym_bfd
), sf
->sym_name
, sf
->sym_namelen
))
591 sf2
= (struct sym_fns
*)xmalloc (sizeof (*sf2
));
592 /* FIXME, who frees this? */
594 sf2
->sym_bfd
= sym_bfd
;
595 sf2
->sym_private
= 0; /* Not alloc'd yet */
596 (*sf2
->sym_init
) (sf2
);
600 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
601 return 0; /* Appease lint. */
604 /* This function runs the load command of our current target. */
607 load_command (arg
, from_tty
)
611 target_load (arg
, from_tty
);
614 /* This function allows the addition of incrementally linked object files.
615 It does not modify any state in the target, only in the debugger. */
619 add_symbol_file_command (arg_string
, from_tty
)
626 /* Getting new symbols may change our opinion about what is
628 reinit_frame_cache ();
631 error ("add-symbol-file takes a file name and an address");
633 arg_string
= tilde_expand (arg_string
);
634 make_cleanup (free
, arg_string
);
636 for( ; *arg_string
== ' '; arg_string
++ );
638 for( ; *arg_string
&& *arg_string
!= ' ' ; arg_string
++ );
639 *arg_string
++ = (char) 0;
642 error ("add-symbol-file takes a file name and an address");
644 text_addr
= parse_and_eval_address (arg_string
);
648 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
650 error ("Not confirmed.");
652 symbol_file_add (name
, 0, text_addr
, 0);
655 /* Re-read symbols if the symbol-file has changed. */
661 /* With the addition of shared libraries, this should be modified,
662 the load time should be saved in the partial symbol tables, since
663 different tables may come from different source files. FIXME.
664 This routine should then walk down each partial symbol table
665 and see if the symbol table that it originates from has been changed
668 if (stat (symfile
, &symstat
) < 0)
669 /* Can't read symbol-file. Assume it is up to date. */
672 if (symstat
.st_mtime
> symfile_mtime
)
674 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
675 symbol_file_command (symfile
, 0);
676 breakpoint_re_set ();
680 /* This function is really horrible, but to avoid it, there would need
681 to be more filling in of forward references. */
683 fill_in_vptr_fieldno (type
)
686 if (TYPE_VPTR_FIELDNO (type
) < 0)
689 for (i
= 1; i
< TYPE_N_BASECLASSES (type
); i
++)
691 fill_in_vptr_fieldno (TYPE_BASECLASS (type
, i
));
692 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type
, i
)) >= 0)
694 TYPE_VPTR_FIELDNO (type
)
695 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type
, i
));
696 TYPE_VPTR_BASETYPE (type
)
697 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type
, i
));
704 /* Functions to handle complaints during symbol reading. */
706 /* How many complaints about a particular thing should be printed before
707 we stop whining about it? Default is no whining at all, since so many
708 systems have ill-constructed symbol files. */
710 static unsigned stop_whining
= 0;
712 /* Print a complaint about the input symbols, and link the complaint block
713 into a chain for later handling. Result is 1 if the complaint was
714 printed, 0 if it was suppressed. */
717 complain (complaint
, val
)
718 struct complaint
*complaint
;
721 complaint
->counter
++;
722 if (complaint
->next
== 0) {
723 complaint
->next
= complaint_root
->next
;
724 complaint_root
->next
= complaint
;
726 if (complaint
->counter
> stop_whining
)
730 puts_filtered ("During symbol reading...");
732 printf_filtered (complaint
->message
, val
);
733 puts_filtered ("...");
736 puts_filtered ("\n");
740 /* Clear out all complaint counters that have ever been incremented. */
747 for (p
= complaint_root
->next
; p
!= complaint_root
; p
= p
->next
)
751 /* clear_symtab_users_once:
753 This function is run after symbol reading, or from a cleanup.
754 If an old symbol table was obsoleted, the old symbol table
755 has been blown away, but the other GDB data structures that may
756 reference it have not yet been cleared or re-directed. (The old
757 symtab was zapped, and the cleanup queued, in free_named_symtab()
760 This function can be queued N times as a cleanup, or called
761 directly; it will do all the work the first time, and then will be a
762 no-op until the next time it is queued. This works by bumping a
763 counter at queueing time. Much later when the cleanup is run, or at
764 the end of symbol processing (in case the cleanup is discarded), if
765 the queued count is greater than the "done-count", we do the work
766 and set the done-count to the queued count. If the queued count is
767 less than or equal to the done-count, we just ignore the call. This
768 is needed because reading a single .o file will often replace many
769 symtabs (one per .h file, for example), and we don't want to reset
770 the breakpoints N times in the user's face.
772 The reason we both queue a cleanup, and call it directly after symbol
773 reading, is because the cleanup protects us in case of errors, but is
774 discarded if symbol reading is successful. */
776 static int clear_symtab_users_queued
;
777 static int clear_symtab_users_done
;
780 clear_symtab_users_once ()
782 /* Enforce once-per-`do_cleanups'-semantics */
783 if (clear_symtab_users_queued
<= clear_symtab_users_done
)
785 clear_symtab_users_done
= clear_symtab_users_queued
;
787 printf ("Resetting debugger state after updating old symbol tables\n");
789 /* Someday, we should do better than this, by only blowing away
790 the things that really need to be blown. */
791 clear_value_history ();
793 clear_internalvars ();
794 breakpoint_re_set ();
795 set_default_breakpoint (0, 0, 0, 0);
796 current_source_symtab
= 0;
799 /* Delete the specified psymtab, and any others that reference it. */
802 cashier_psymtab (pst
)
803 struct partial_symtab
*pst
;
805 struct partial_symtab
*ps
, *pprev
;
808 /* Find its previous psymtab in the chain */
809 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
816 /* Unhook it from the chain. */
817 if (ps
== partial_symtab_list
)
818 partial_symtab_list
= ps
->next
;
820 pprev
->next
= ps
->next
;
822 /* FIXME, we can't conveniently deallocate the entries in the
823 partial_symbol lists (global_psymbols/static_psymbols) that
824 this psymtab points to. These just take up space until all
825 the psymtabs are reclaimed. Ditto the dependencies list and
826 filename, which are all in the psymbol_obstack. */
828 /* We need to cashier any psymtab that has this one as a dependency... */
830 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
831 for (i
= 0; i
< ps
->number_of_dependencies
; i
++) {
832 if (ps
->dependencies
[i
] == pst
) {
833 cashier_psymtab (ps
);
834 goto again
; /* Must restart, chain has been munged. */
841 /* If a symtab or psymtab for filename NAME is found, free it along
842 with any dependent breakpoints, displays, etc.
843 Used when loading new versions of object modules with the "add-file"
844 command. This is only called on the top-level symtab or psymtab's name;
845 it is not called for subsidiary files such as .h files.
847 Return value is 1 if we blew away the environment, 0 if not.
849 FIXME. I think this is not the best way to do this. We should
850 work on being gentler to the environment while still cleaning up
851 all stray pointers into the freed symtab. */
854 free_named_symtabs (name
)
857 register struct symtab
*s
;
858 register struct symtab
*prev
;
859 register struct partial_symtab
*ps
;
860 struct blockvector
*bv
;
863 /* We only wack things if the symbol-reload switch is set. */
864 if (!symbol_reloading
)
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.",
961 (add_set_cmd ("symbol-reloading", class_support
, var_boolean
,
962 (char *)&symbol_reloading
,
963 "Set dynamic symbol table reloading multiple times in one run.",
967 obstack_init (symbol_obstack
);
968 obstack_init (psymbol_obstack
);