📄 symfile.c
字号:
/* Generic symbol file reading for the GNU debugger, GDB. Copyright 1990, 1991, 1992 Free Software Foundation, Inc. Contributed by Cygnus Support, using pieces from other GDB modules.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "defs.h"#include "symtab.h"#include "gdbtypes.h"#include "gdbcore.h"#include "frame.h"#include "target.h"#include "value.h"#include "symfile.h"#include "objfiles.h"#include "gdbcmd.h"#include "breakpoint.h"#include "language.h"#include <obstack.h>#include <assert.h>#include <sys/types.h>#include <fcntl.h>#include <string.h>#include <sys/stat.h>#include <ctype.h>/* Global variables owned by this file */int readnow_symbol_files; /* Read full symbols immediately *//* External variables and functions referenced. */extern int info_verbose;/* Functions this file defines */static voidset_initial_language PARAMS ((void));static voidload_command PARAMS ((char *, int));static voidadd_symbol_file_command PARAMS ((char *, int));static voidcashier_psymtab PARAMS ((struct partial_symtab *));static intcompare_psymbols PARAMS ((const void *, const void *));static intcompare_symbols PARAMS ((const void *, const void *));static bfd *symfile_bfd_open PARAMS ((char *));static voidfind_sym_fns PARAMS ((struct objfile *));voidclear_symtab_users_once PARAMS ((void));/* List of all available sym_fns. On gdb startup, each object file reader calls add_symtab_fns() to register information on each format it is prepared to read. */static struct sym_fns *symtab_fns = NULL;/* Structures with which to manage partial symbol allocation. */struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};/* Flag for whether user will be reloading symbols multiple times. Defaults to ON for VxWorks, otherwise OFF. */#ifdef SYMBOL_RELOADING_DEFAULTint symbol_reloading = SYMBOL_RELOADING_DEFAULT;#elseint symbol_reloading = 0;#endif/* Structure to manage complaints about symbol file contents. */struct complaint complaint_root[1] = { {(char *) 0, 0, complaint_root},};/* Some actual complaints. */struct complaint oldsyms_complaint = { "Replacing old symbols for `%s'", 0, 0 };struct complaint empty_symtab_complaint = { "Empty symbol table found for `%s'", 0, 0 };/* In the following sort, we always make sure that register debug symbol declarations always come before regular debug symbol declarations (as might happen when parameters are then put into registers by the compiler). Since this function is called from within qsort, in an ANSI environment it must conform to the prototype for qsort, which specifies that the comparison function takes two "void *" pointers. */static intcompare_symbols (s1p, s2p) const PTR s1p; const PTR s2p;{ register struct symbol **s1, **s2; register int namediff; s1 = (struct symbol **) s1p; s2 = (struct symbol **) s2p; /* Compare the initial characters. */ namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0]; if (namediff != 0) return namediff; /* If they match, compare the rest of the names. */ namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)); if (namediff != 0) return namediff; /* For symbols of the same name, registers should come first. */ return ((SYMBOL_CLASS (*s2) == LOC_REGISTER) - (SYMBOL_CLASS (*s1) == LOC_REGISTER));}/*LOCAL FUNCTION compare_psymbols -- compare two partial symbols by nameDESCRIPTION Given pointer to two partial symbol table entries, compare them by name and return -N, 0, or +N (ala strcmp). Typically used by sorting routines like qsort().NOTES Does direct compare of first two characters before punting and passing to strcmp for longer compares. Note that the original version had a bug whereby two null strings or two identically named one character strings would return the comparison of memory following the null byte. */static intcompare_psymbols (s1p, s2p) const PTR s1p; const PTR s2p;{ register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p); register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p); if ((st1[0] - st2[0]) || !st1[0]) { return (st1[0] - st2[0]); } else if ((st1[1] - st2[1]) || !st1[1]) { return (st1[1] - st2[1]); } else { return (strcmp (st1 + 2, st2 + 2)); }}voidsort_pst_symbols (pst) struct partial_symtab *pst;{ /* Sort the global list; don't sort the static list */ qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset, pst -> n_global_syms, sizeof (struct partial_symbol), compare_psymbols);}/* Call sort_block_syms to sort alphabetically the symbols of one block. */voidsort_block_syms (b) register struct block *b;{ qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b), sizeof (struct symbol *), compare_symbols);}/* Call sort_symtab_syms to sort alphabetically the symbols of each block of one symtab. */voidsort_symtab_syms (s) register struct symtab *s;{ register struct blockvector *bv; int nbl; int i; register struct block *b; if (s == 0) return; bv = BLOCKVECTOR (s); nbl = BLOCKVECTOR_NBLOCKS (bv); for (i = 0; i < nbl; i++) { b = BLOCKVECTOR_BLOCK (bv, i); if (BLOCK_SHOULD_SORT (b)) sort_block_syms (b); }}voidsort_all_symtab_syms (){ register struct symtab *s; register struct objfile *objfile; for (objfile = object_files; objfile != NULL; objfile = objfile -> next) { for (s = objfile -> symtabs; s != NULL; s = s -> next) { sort_symtab_syms (s); } }}/* Make a copy of the string at PTR with SIZE characters in the symbol obstack (and add a null character at the end in the copy). Returns the address of the copy. */char *obsavestring (ptr, size, obstackp) char *ptr; int size; struct obstack *obstackp;{ register char *p = (char *) obstack_alloc (obstackp, size + 1); /* Open-coded bcopy--saves function call time. These strings are usually short. */ { register char *p1 = ptr; register char *p2 = p; char *end = ptr + size; while (p1 != end) *p2++ = *p1++; } p[size] = 0; return p;}/* Concatenate strings S1, S2 and S3; return the new string. Space is found in the symbol_obstack. */char *obconcat (obstackp, s1, s2, s3) struct obstack *obstackp; const char *s1, *s2, *s3;{ register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; register char *val = (char *) obstack_alloc (obstackp, len); strcpy (val, s1); strcat (val, s2); strcat (val, s3); return val;}/* Get the symbol table that corresponds to a partial_symtab. This is fast after the first time you do it. In fact, there is an even faster macro PSYMTAB_TO_SYMTAB that does the fast case inline. */struct symtab *psymtab_to_symtab (pst) register struct partial_symtab *pst;{ /* If it's been looked up before, return it. */ if (pst->symtab) return pst->symtab; /* If it has not yet been read in, read it. */ if (!pst->readin) { (*pst->read_symtab) (pst); } return pst->symtab;}/* Initialize entry point information for this objfile. */voidinit_entry_point_info (objfile) struct objfile *objfile;{ /* Save startup file's range of PC addresses to help blockframe.c decide where the bottom of the stack is. */ if (bfd_get_file_flags (objfile -> obfd) & EXEC_P) { /* Executable file -- record its entry point so we'll recognize the startup file because it contains the entry point. */ objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd); } else { /* Examination of non-executable.o files. Short-circuit this stuff. */ /* ~0 will not be in any file, we hope. */ objfile -> ei.entry_point = ~0; /* set the startup file to be an empty range. */ objfile -> ei.entry_file_lowpc = 0; objfile -> ei.entry_file_highpc = 0; }}/* Remember the lowest-addressed loadable section we've seen. This function is called via bfd_map_over_sections. */#if 0 /* Not used yet */static voidfind_lowest_section (abfd, sect, obj) bfd *abfd; asection *sect; PTR obj;{ asection **lowest = (asection **)obj; if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD)) return; if (!*lowest) *lowest = sect; /* First loadable section */ else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect)) *lowest = sect; /* A lower loadable section */}#endif /* Process a symbol file, as either the main file or as a dynamically loaded file. NAME is the file name (which will be tilde-expanded and made absolute herein) (but we don't free or modify NAME itself). FROM_TTY says how verbose to be. MAINLINE specifies whether this is the main symbol file, or whether it's an extra symbol file such as dynamically loaded code. If !mainline, ADDR is the address where the text segment was loaded. If VERBO, the caller has printed a verbose message about the symbol reading (and complaints can be more terse about it). */voidsyms_from_objfile (objfile, addr, mainline, verbo) struct objfile *objfile; CORE_ADDR addr; int mainline; int verbo;{ struct section_offsets *section_offsets; asection *lowest_sect; /* There is a distinction between having no symbol table (we refuse to read the file, leaving the old set of symbols around) and having no debugging symbols in your symbol table (we read the file and end up with a mostly empty symbol table). FIXME: This strategy works correctly when the debugging symbols are intermixed with "normal" symbols. However, when the debugging symbols are separate, such as with ELF/DWARF, it is perfectly plausible for the symbol table to be missing but still have all the DWARF info intact. Thus in general it is wrong to assume that having no symbol table implies no debugging information. */ if (!(bfd_get_file_flags (objfile -> obfd) & HAS_SYMS)) return; init_entry_point_info (objfile); find_sym_fns (objfile); if (mainline) { /* Since no error yet, throw away the old symbol table. */ if (symfile_objfile != NULL) { free_objfile (symfile_objfile); symfile_objfile = NULL; } (*objfile -> sf -> sym_new_init) (objfile); } /* Convert addr into an offset rather than an absolute address. We find the lowest address of a loaded segment in the objfile, and assume that <addr> is where that got loaded. Due to historical precedent, we warn if that doesn't happen to be the ".text" segment. */ if (mainline) { addr = 0; /* No offset from objfile addresses. */ } else { lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");#if 0 lowest_sect = 0; bfd_map_over_sections (objfile->obfd, find_lowest_section, (PTR) &lowest_sect);#endif if (lowest_sect == 0) warning ("no loadable sections found in added symbol-file %s", objfile->name); else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect) || 0 != strcmp(".text", bfd_get_section_name (objfile->obfd, lowest_sect))) warning ("Lowest section in %s is %s at 0x%x", objfile->name, bfd_section_name (objfile->obfd, lowest_sect), bfd_section_vma (objfile->obfd, lowest_sect)); if (lowest_sect) addr -= bfd_section_vma (objfile->obfd, lowest_sect); } /* Initialize symbol reading routines for this objfile, allow complaints to appear for this new file, and record how verbose to be, then do the initial symbol reading for this file. */ (*objfile -> sf -> sym_init) (objfile); clear_complaints (1, verbo); section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr); (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline); /* Don't allow char * to have a typename (else would get caddr_t.) */ /* Ditto void *. FIXME should do this for all the builtin types. */ TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0; TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0; /* Mark the objfile has having had initial symbol read attempted. Note that this does not mean we found any symbols... */ objfile -> flags |= OBJF_SYMS;}/* Perform required actions immediately after either reading in the initial symbols for a new objfile, or mapping in the symbols from a reusable objfile. */ voidnew_symfile_objfile (objfile, mainline, verbo) struct objfile *objfile; int mainline; int verbo;{ if (mainline) { /* OK, make it the "real" symbol file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -