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();
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 int 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
},
97 /* In the following sort, we always make sure that
98 register debug symbol declarations always come before regular
99 debug symbol declarations (as might happen when parameters are
100 then put into registers by the compiler). */
103 compare_symbols (s1
, s2
)
104 struct symbol
**s1
, **s2
;
106 register int namediff
;
108 /* Compare the initial characters. */
109 namediff
= SYMBOL_NAME (*s1
)[0] - SYMBOL_NAME (*s2
)[0];
110 if (namediff
!= 0) return namediff
;
112 /* If they match, compare the rest of the names. */
113 namediff
= strcmp (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
));
114 if (namediff
!= 0) return namediff
;
116 /* For symbols of the same name, registers should come first. */
117 return ((SYMBOL_CLASS (*s2
) == LOC_REGISTER
)
118 - (SYMBOL_CLASS (*s1
) == LOC_REGISTER
));
121 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
125 register struct block
*b
;
127 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
128 sizeof (struct symbol
*), compare_symbols
);
131 /* Call sort_symtab_syms to sort alphabetically
132 the symbols of each block of one symtab. */
136 register struct symtab
*s
;
138 register struct blockvector
*bv
= BLOCKVECTOR (s
);
139 int nbl
= BLOCKVECTOR_NBLOCKS (bv
);
141 register struct block
*b
;
143 for (i
= 0; i
< nbl
; i
++)
145 b
= BLOCKVECTOR_BLOCK (bv
, i
);
146 if (BLOCK_SHOULD_SORT (b
))
152 sort_all_symtab_syms ()
154 register struct symtab
*s
;
156 for (s
= symtab_list
; s
; s
= s
->next
)
158 sort_symtab_syms (s
);
162 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
163 (and add a null character at the end in the copy).
164 Returns the address of the copy. */
167 obsavestring (ptr
, size
)
171 register char *p
= (char *) obstack_alloc (symbol_obstack
, size
+ 1);
172 /* Open-coded bcopy--saves function call time.
173 These strings are usually short. */
175 register char *p1
= ptr
;
176 register char *p2
= p
;
177 char *end
= ptr
+ size
;
185 /* Concatenate strings S1, S2 and S3; return the new string.
186 Space is found in the symbol_obstack. */
189 obconcat (s1
, s2
, s3
)
192 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
193 register char *val
= (char *) obstack_alloc (symbol_obstack
, len
);
200 /* Accumulate the misc functions in bunches of 127.
201 At the end, copy them all into one newly allocated structure. */
203 #define MISC_BUNCH_SIZE 127
207 struct misc_bunch
*next
;
208 struct misc_function contents
[MISC_BUNCH_SIZE
];
211 /* Bunch currently being filled up.
212 The next field points to chain of filled bunches. */
214 static struct misc_bunch
*misc_bunch
;
216 /* Number of slots filled in current bunch. */
218 static int misc_bunch_index
;
220 /* Total number of misc functions recorded so far. */
222 static int misc_count
;
229 misc_bunch_index
= MISC_BUNCH_SIZE
;
233 prim_record_misc_function (name
, address
, misc_type
)
236 enum misc_function_type misc_type
;
238 register struct misc_bunch
*new;
240 if (misc_bunch_index
== MISC_BUNCH_SIZE
)
242 new = (struct misc_bunch
*) xmalloc (sizeof (struct misc_bunch
));
243 misc_bunch_index
= 0;
244 new->next
= misc_bunch
;
247 misc_bunch
->contents
[misc_bunch_index
].name
= name
;
248 misc_bunch
->contents
[misc_bunch_index
].address
= address
;
249 misc_bunch
->contents
[misc_bunch_index
].type
= misc_type
;
250 misc_bunch
->contents
[misc_bunch_index
].misc_info
= 0;
256 compare_misc_functions (fn1
, fn2
)
257 struct misc_function
*fn1
, *fn2
;
259 /* Return a signed result based on unsigned comparisons
260 so that we sort into unsigned numeric order. */
261 if (fn1
->address
< fn2
->address
)
263 if (fn1
->address
> fn2
->address
)
270 discard_misc_bunches (foo
)
273 register struct misc_bunch
*next
;
277 next
= misc_bunch
->next
;
283 /* INCLINK nonzero means bunches are from an incrementally-linked file.
284 Add them to the existing bunches.
285 Otherwise INCLINK is zero, and we start from scratch. */
287 condense_misc_bunches (inclink
)
291 register struct misc_bunch
*bunch
;
296 = (struct misc_function
*)
297 xrealloc (misc_function_vector
, (misc_count
+ misc_function_count
)
298 * sizeof (struct misc_function
));
299 j
= misc_function_count
;
304 = (struct misc_function
*)
305 xmalloc (misc_count
* sizeof (struct misc_function
));
312 for (i
= 0; i
< misc_bunch_index
; i
++, j
++)
314 misc_function_vector
[j
] = bunch
->contents
[i
];
315 #ifdef NAMES_HAVE_UNDERSCORE
316 if (misc_function_vector
[j
].name
[0] == '_')
317 misc_function_vector
[j
].name
++;
321 misc_bunch_index
= MISC_BUNCH_SIZE
;
324 if (misc_function_count
+ misc_count
!= j
) /* DEBUG */
325 printf_filtered ("Function counts are off! %d + %d != %d\n",
326 misc_function_count
, misc_count
, j
);
328 misc_function_count
= j
;
330 /* Sort the misc functions by address. */
332 qsort (misc_function_vector
, misc_function_count
,
333 sizeof (struct misc_function
),
334 compare_misc_functions
);
338 /* Get the symbol table that corresponds to a partial_symtab.
339 This is fast after the first time you do it. In fact, there
340 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
344 psymtab_to_symtab (pst
)
345 register struct partial_symtab
*pst
;
347 register struct symtab
*result
;
349 /* If it's been looked up before, return it. */
353 /* If it has not yet been read in, read it. */
356 (*pst
->read_symtab
) (pst
);
359 /* Search through list for correct name. */
360 for (result
= symtab_list
; result
; result
= result
->next
)
361 if (!strcmp (result
->filename
, pst
->filename
))
363 pst
->symtab
= result
; /* Remember where it was. */
370 /* Process a symbol file, as either the main file or as a dynamically
373 NAME is the file name (which will be tilde-expanded and made absolute
374 herein). FROM_TTY says how verbose to be. MAINLINE specifies whether
375 this is the main symbol file, or whether it's an extra symbol file
376 such as dynamically loaded code. If !mainline, ADDR is the address
377 where the text segment was loaded. */
380 symbol_file_add (name
, from_tty
, addr
, mainline
)
391 sym_bfd
= symfile_open (name
);
393 entry_point
= bfd_get_start_address (sym_bfd
);
396 symfile_mtime
= bfd_get_mtime (sym_bfd
);
398 /* There is a distinction between having no symbol table
399 (we refuse to read the file, leaving the old set of symbols around)
400 and having no debugging symbols in your symbol table (we read
401 the file and end up with a mostly empty symbol table). */
403 if (!(bfd_get_file_flags (sym_bfd
) & HAS_SYMS
))
405 error ("%s has no symbol-table", name
);
408 if ((symtab_list
|| partial_symtab_list
)
411 && !query ("Load new symbol table from \"%s\"? ", name
))
412 error ("Not confirmed.");
416 printf_filtered ("Reading symbol data from %s...", name
);
421 sf
= symfile_init (sym_bfd
);
422 realname
= bfd_get_filename (sym_bfd
);
423 realname
= savestring (realname
, strlen (realname
));
424 /* FIXME, this probably creates a storage leak... */
428 /* Since no error yet, throw away the old symbol table. */
434 free_all_psymtabs ();
436 (*sf
->sym_new_init
) ();
438 /* For mainline, caller didn't know the specified address of the
439 text section. We fix that here. */
440 text_sect
= bfd_get_section_by_name (sym_bfd
, ".text");
441 addr
= bfd_section_vma (sym_bfd
, text_sect
);
444 clear_complaints(); /* Allow complaints to appear for this new file. */
446 (*sf
->sym_read
) (sf
, addr
, mainline
);
448 /* Don't allow char * to have a typename (else would get caddr_t.) */
449 /* Ditto void *. FIXME should do this for all the builtin types. */
451 TYPE_NAME (lookup_pointer_type (builtin_type_char
)) = 0;
452 TYPE_NAME (lookup_pointer_type (builtin_type_void
)) = 0;
456 /* OK, make it the "real" symbol file. */
461 /* If we have wiped out any old symbol tables, clean up. */
462 clear_symtab_users_once ();
466 printf_filtered ("done.\n");
471 /* This is the symbol-file command. Read the file, analyze its symbols,
472 and add a struct symtab to symtab_list. */
475 symbol_file_command (name
, from_tty
)
484 if ((symtab_list
|| partial_symtab_list
)
486 && !query ("Discard symbol table from `%s'? ", symfile
))
487 error ("Not confirmed.");
492 free_all_psymtabs ();
493 /* FIXME, this does not account for the main file and subsequent
494 files (shared libs, dynloads, etc) having different formats.
495 It only calls the cleanup routine for the main file's format. */
497 (*symfile_fns
->sym_new_init
) ();
504 symbol_file_add (name
, from_tty
, (CORE_ADDR
)0, 1);
507 /* Open NAME and hand it off to BFD for preliminary analysis. Result
508 is a BFD *, which includes a new copy of NAME dynamically allocated
509 (which will be freed by the cleanup chain). In case of trouble,
510 error() is called. */
520 name
= tilde_expand (name
);
521 make_cleanup (free
, name
);
523 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
, 0, &absolute_name
);
525 perror_with_name (name
);
528 make_cleanup (free
, absolute_name
);
529 name
= absolute_name
;
532 sym_bfd
= bfd_fdopenr (name
, NULL
, desc
);
536 error ("Could not open `%s' to read symbols: %s",
537 name
, bfd_errmsg (bfd_error
));
539 make_cleanup (bfd_close
, sym_bfd
);
541 if (!bfd_check_format (sym_bfd
, bfd_object
))
542 error ("\"%s\": can't read symbols: %s.",
543 name
, bfd_errmsg (bfd_error
));
548 /* Link a new symtab_fns into the global symtab_fns list.
549 Called by various _initialize routines. */
555 sf
->next
= symtab_fns
;
560 /* Initialize to read symbols from the symbol file sym_bfd. It either
561 returns or calls error(). The result is a malloc'd struct sym_fns
562 that contains cached information about the symbol file. */
564 static struct sym_fns
*
565 symfile_init (sym_bfd
)
568 struct sym_fns
*sf
, *sf2
;
570 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
->next
)
572 if (!strncmp (bfd_get_target (sym_bfd
), sf
->sym_name
, sf
->sym_namelen
))
574 sf2
= (struct sym_fns
*)xmalloc (sizeof (*sf2
));
575 /* FIXME, who frees this? */
577 sf2
->sym_bfd
= sym_bfd
;
578 sf2
->sym_private
= 0; /* Not alloc'd yet */
579 (*sf2
->sym_init
) (sf2
);
583 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
586 /* This function runs the load command of our current target. */
589 load_command (arg
, from_tty
)
593 target_load (arg
, from_tty
);
596 /* This function runs the add_syms command of our current target. */
599 add_syms_command (args
, from_tty
)
603 target_add_syms (args
, from_tty
);
606 /* This function allows the addition of incrementally linked object files. */
609 add_syms_addr_command (arg_string
, from_tty
)
617 error ("add-syms takes a file name and an address");
619 arg_string
= tilde_expand (arg_string
);
620 make_cleanup (free
, arg_string
);
622 for( ; *arg_string
== ' '; arg_string
++ );
624 for( ; *arg_string
&& *arg_string
!= ' ' ; arg_string
++ );
625 *arg_string
++ = (char) 0;
628 error ("add-syms takes a file name and an address");
630 text_addr
= parse_and_eval_address (arg_string
);
634 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
636 error ("Not confirmed.");
638 symbol_file_add (name
, 0, text_addr
, 0);
641 /* Re-read symbols if the symbol-file has changed. */
647 /* With the addition of shared libraries, this should be modified,
648 the load time should be saved in the partial symbol tables, since
649 different tables may come from different source files. FIXME.
650 This routine should then walk down each partial symbol table
651 and see if the symbol table that it originates from has been changed
654 if (stat (symfile
, &symstat
) < 0)
655 /* Can't read symbol-file. Assume it is up to date. */
658 if (symstat
.st_mtime
> symfile_mtime
)
660 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
661 symbol_file_command (symfile
, 0);
662 breakpoint_re_set ();
667 /* This function is really horrible, but to avoid it, there would need
668 to be more filling in of forward references. */
670 fill_in_vptr_fieldno (type
)
673 check_stub_type (type
);
674 if (TYPE_VPTR_FIELDNO (type
) < 0)
675 TYPE_VPTR_FIELDNO (type
) =
676 fill_in_vptr_fieldno (TYPE_BASECLASS (type
, 1));
677 return TYPE_VPTR_FIELDNO (type
);
680 /* Functions to handle complaints during symbol reading. */
682 /* How many complaints about a particular thing should be printed before
683 we stop whining about it? */
685 static unsigned stop_whining
= 1;
687 /* Print a complaint about the input symbols, and link the complaint block
688 into a chain for later handling. Result is 1 if the complaint was
689 printed, 0 if it was suppressed. */
692 complain (complaint
, val
)
693 struct complaint
*complaint
;
696 complaint
->counter
++;
697 if (complaint
->next
== 0) {
698 complaint
->next
= complaint_root
->next
;
699 complaint_root
->next
= complaint
;
701 if (complaint
->counter
> stop_whining
)
705 puts_filtered ("During symbol reading...");
707 printf_filtered (complaint
->message
, val
);
708 puts_filtered ("...");
711 puts_filtered ("\n");
715 /* Clear out all complaint counters that have ever been incremented. */
722 for (p
= complaint_root
->next
; p
!= complaint_root
; p
= p
->next
)
727 _initialize_symfile ()
730 add_com ("symbol-file", class_files
, symbol_file_command
,
731 "Load symbol table from executable file FILE.\n\
732 The `file' command can also load symbol tables, as well as setting the file\n\
735 add_com ("add-syms", class_files
, add_syms_command
,
736 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
737 The second argument provides the starting address of the file's text.");
739 add_com ("load", class_files
, load_command
,
740 "Dynamically load FILE into the running program, and record its symbols\n\
741 for access from GDB.");
744 (add_set_cmd ("complaints", class_support
, var_uinteger
,
745 (char *)&stop_whining
,
746 "Set max number of complaints about incorrect symbols.",
750 obstack_init (symbol_obstack
);
751 obstack_init (psymbol_obstack
);