119370Spst/* GDB routines for manipulating the minimal symbol tables. 2130803Smarcel Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 3130803Smarcel 2002, 2003, 2004 498944Sobrien Free Software Foundation, Inc. 519370Spst Contributed by Cygnus Support, using pieces from other GDB modules. 619370Spst 798944Sobrien This file is part of GDB. 819370Spst 998944Sobrien This program is free software; you can redistribute it and/or modify 1098944Sobrien it under the terms of the GNU General Public License as published by 1198944Sobrien the Free Software Foundation; either version 2 of the License, or 1298944Sobrien (at your option) any later version. 1319370Spst 1498944Sobrien This program is distributed in the hope that it will be useful, 1598944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1698944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1798944Sobrien GNU General Public License for more details. 1819370Spst 1998944Sobrien You should have received a copy of the GNU General Public License 2098944Sobrien along with this program; if not, write to the Free Software 2198944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2298944Sobrien Boston, MA 02111-1307, USA. */ 2319370Spst 2419370Spst 2519370Spst/* This file contains support routines for creating, manipulating, and 2619370Spst destroying minimal symbol tables. 2719370Spst 2819370Spst Minimal symbol tables are used to hold some very basic information about 2919370Spst all defined global symbols (text, data, bss, abs, etc). The only two 3019370Spst required pieces of information are the symbol's name and the address 3119370Spst associated with that symbol. 3219370Spst 3319370Spst In many cases, even if a file was compiled with no special options for 3419370Spst debugging at all, as long as was not stripped it will contain sufficient 3519370Spst information to build useful minimal symbol tables using this structure. 3698944Sobrien 3719370Spst Even when a file contains enough debugging information to build a full 3819370Spst symbol table, these minimal symbols are still useful for quickly mapping 3919370Spst between names and addresses, and vice versa. They are also sometimes used 4019370Spst to figure out what full symbol table entries need to be read in. */ 4119370Spst 4219370Spst 4319370Spst#include "defs.h" 4498944Sobrien#include <ctype.h> 4519370Spst#include "gdb_string.h" 4619370Spst#include "symtab.h" 4719370Spst#include "bfd.h" 4819370Spst#include "symfile.h" 4919370Spst#include "objfiles.h" 5019370Spst#include "demangle.h" 5198944Sobrien#include "value.h" 5298944Sobrien#include "cp-abi.h" 5319370Spst 5419370Spst/* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE. 5519370Spst At the end, copy them all into one newly allocated location on an objfile's 5619370Spst symbol obstack. */ 5719370Spst 5819370Spst#define BUNCH_SIZE 127 5919370Spst 6019370Spststruct msym_bunch 6198944Sobrien { 6298944Sobrien struct msym_bunch *next; 6398944Sobrien struct minimal_symbol contents[BUNCH_SIZE]; 6498944Sobrien }; 6519370Spst 6619370Spst/* Bunch currently being filled up. 6719370Spst The next field points to chain of filled bunches. */ 6819370Spst 6919370Spststatic struct msym_bunch *msym_bunch; 7019370Spst 7119370Spst/* Number of slots filled in current bunch. */ 7219370Spst 7319370Spststatic int msym_bunch_index; 7419370Spst 7519370Spst/* Total number of minimal symbols recorded so far for the objfile. */ 7619370Spst 7719370Spststatic int msym_count; 7819370Spst 7998944Sobrien/* Compute a hash code based using the same criteria as `strcmp_iw'. */ 8019370Spst 8198944Sobrienunsigned int 8298944Sobrienmsymbol_hash_iw (const char *string) 8398944Sobrien{ 8498944Sobrien unsigned int hash = 0; 8598944Sobrien while (*string && *string != '(') 8698944Sobrien { 8798944Sobrien while (isspace (*string)) 8898944Sobrien ++string; 8998944Sobrien if (*string && *string != '(') 9098944Sobrien { 9198944Sobrien hash = hash * 67 + *string - 113; 9298944Sobrien ++string; 9398944Sobrien } 9498944Sobrien } 95130803Smarcel return hash; 9698944Sobrien} 9719370Spst 9898944Sobrien/* Compute a hash code for a string. */ 9919370Spst 10098944Sobrienunsigned int 10198944Sobrienmsymbol_hash (const char *string) 10298944Sobrien{ 10398944Sobrien unsigned int hash = 0; 10498944Sobrien for (; *string; ++string) 10598944Sobrien hash = hash * 67 + *string - 113; 106130803Smarcel return hash; 10798944Sobrien} 10898944Sobrien 10998944Sobrien/* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE. */ 11098944Sobrienvoid 11198944Sobrienadd_minsym_to_hash_table (struct minimal_symbol *sym, 11298944Sobrien struct minimal_symbol **table) 11398944Sobrien{ 11498944Sobrien if (sym->hash_next == NULL) 11598944Sobrien { 116130803Smarcel unsigned int hash 117130803Smarcel = msymbol_hash (SYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE; 11898944Sobrien sym->hash_next = table[hash]; 11998944Sobrien table[hash] = sym; 12098944Sobrien } 12198944Sobrien} 12298944Sobrien 12398944Sobrien/* Add the minimal symbol SYM to an objfile's minsym demangled hash table, 12498944Sobrien TABLE. */ 12598944Sobrienstatic void 12698944Sobrienadd_minsym_to_demangled_hash_table (struct minimal_symbol *sym, 12798944Sobrien struct minimal_symbol **table) 12898944Sobrien{ 12998944Sobrien if (sym->demangled_hash_next == NULL) 13098944Sobrien { 131130803Smarcel unsigned int hash = msymbol_hash_iw (SYMBOL_DEMANGLED_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE; 13298944Sobrien sym->demangled_hash_next = table[hash]; 13398944Sobrien table[hash] = sym; 13498944Sobrien } 13598944Sobrien} 13698944Sobrien 13798944Sobrien 13819370Spst/* Look through all the current minimal symbol tables and find the 13919370Spst first minimal symbol that matches NAME. If OBJF is non-NULL, limit 140130803Smarcel the search to that objfile. If SFILE is non-NULL, the only file-scope 141130803Smarcel symbols considered will be from that source file (global symbols are 142130803Smarcel still preferred). Returns a pointer to the minimal symbol that 14319370Spst matches, or NULL if no match is found. 14419370Spst 14519370Spst Note: One instance where there may be duplicate minimal symbols with 14619370Spst the same name is when the symbol tables for a shared library and the 14719370Spst symbol tables for an executable contain global symbols with the same 148130803Smarcel names (the dynamic linker deals with the duplication). */ 14919370Spst 15019370Spststruct minimal_symbol * 151130803Smarcellookup_minimal_symbol (const char *name, const char *sfile, 15298944Sobrien struct objfile *objf) 15319370Spst{ 15419370Spst struct objfile *objfile; 15519370Spst struct minimal_symbol *msymbol; 15619370Spst struct minimal_symbol *found_symbol = NULL; 15719370Spst struct minimal_symbol *found_file_symbol = NULL; 15819370Spst struct minimal_symbol *trampoline_symbol = NULL; 15919370Spst 160130803Smarcel unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE; 161130803Smarcel unsigned int dem_hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE; 16298944Sobrien 16319370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING 16419370Spst if (sfile != NULL) 16519370Spst { 16619370Spst char *p = strrchr (sfile, '/'); 16719370Spst if (p != NULL) 16819370Spst sfile = p + 1; 16919370Spst } 17019370Spst#endif 17119370Spst 17219370Spst for (objfile = object_files; 17319370Spst objfile != NULL && found_symbol == NULL; 17498944Sobrien objfile = objfile->next) 17519370Spst { 17619370Spst if (objf == NULL || objf == objfile) 17719370Spst { 17898944Sobrien /* Do two passes: the first over the ordinary hash table, 17998944Sobrien and the second over the demangled hash table. */ 18098944Sobrien int pass; 18198944Sobrien 18298944Sobrien for (pass = 1; pass <= 2 && found_symbol == NULL; pass++) 18319370Spst { 18498944Sobrien /* Select hash list according to pass. */ 18598944Sobrien if (pass == 1) 18698944Sobrien msymbol = objfile->msymbol_hash[hash]; 18798944Sobrien else 18898944Sobrien msymbol = objfile->msymbol_demangled_hash[dem_hash]; 18998944Sobrien 19098944Sobrien while (msymbol != NULL && found_symbol == NULL) 19119370Spst { 192130803Smarcel /* FIXME: carlton/2003-02-27: This is an unholy 193130803Smarcel mixture of linkage names and natural names. If 194130803Smarcel you want to test the linkage names with strcmp, 195130803Smarcel do that. If you want to test the natural names 196130803Smarcel with strcmp_iw, use SYMBOL_MATCHES_NATURAL_NAME. */ 197130803Smarcel if (strcmp (DEPRECATED_SYMBOL_NAME (msymbol), (name)) == 0 198130803Smarcel || (SYMBOL_DEMANGLED_NAME (msymbol) != NULL 199130803Smarcel && strcmp_iw (SYMBOL_DEMANGLED_NAME (msymbol), 200130803Smarcel (name)) == 0)) 20119370Spst { 20298944Sobrien switch (MSYMBOL_TYPE (msymbol)) 20398944Sobrien { 20498944Sobrien case mst_file_text: 20598944Sobrien case mst_file_data: 20698944Sobrien case mst_file_bss: 20719370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING 208130803Smarcel if (sfile == NULL 209130803Smarcel || strcmp (msymbol->filename, sfile) == 0) 21098944Sobrien found_file_symbol = msymbol; 21119370Spst#else 21298944Sobrien /* We have neither the ability nor the need to 21398944Sobrien deal with the SFILE parameter. If we find 21498944Sobrien more than one symbol, just return the latest 21598944Sobrien one (the user can't expect useful behavior in 21698944Sobrien that case). */ 21798944Sobrien found_file_symbol = msymbol; 21819370Spst#endif 21998944Sobrien break; 22019370Spst 22198944Sobrien case mst_solib_trampoline: 22219370Spst 22398944Sobrien /* If a trampoline symbol is found, we prefer to 22498944Sobrien keep looking for the *real* symbol. If the 22598944Sobrien actual symbol is not found, then we'll use the 22698944Sobrien trampoline entry. */ 22798944Sobrien if (trampoline_symbol == NULL) 22898944Sobrien trampoline_symbol = msymbol; 22998944Sobrien break; 23019370Spst 23198944Sobrien case mst_unknown: 23298944Sobrien default: 23398944Sobrien found_symbol = msymbol; 23498944Sobrien break; 23598944Sobrien } 23619370Spst } 23798944Sobrien 23898944Sobrien /* Find the next symbol on the hash chain. */ 23998944Sobrien if (pass == 1) 24098944Sobrien msymbol = msymbol->hash_next; 24198944Sobrien else 24298944Sobrien msymbol = msymbol->demangled_hash_next; 24319370Spst } 24419370Spst } 24519370Spst } 24619370Spst } 24719370Spst /* External symbols are best. */ 24819370Spst if (found_symbol) 24919370Spst return found_symbol; 25019370Spst 25119370Spst /* File-local symbols are next best. */ 25219370Spst if (found_file_symbol) 25319370Spst return found_file_symbol; 25419370Spst 25519370Spst /* Symbols for shared library trampolines are next best. */ 25619370Spst if (trampoline_symbol) 25719370Spst return trampoline_symbol; 25819370Spst 25919370Spst return NULL; 26019370Spst} 26119370Spst 26219370Spst/* Look through all the current minimal symbol tables and find the 263130803Smarcel first minimal symbol that matches NAME and has text type. If OBJF 264130803Smarcel is non-NULL, limit the search to that objfile. Returns a pointer 265130803Smarcel to the minimal symbol that matches, or NULL if no match is found. 26698944Sobrien 267130803Smarcel This function only searches the mangled (linkage) names. */ 268130803Smarcel 26919370Spststruct minimal_symbol * 270130803Smarcellookup_minimal_symbol_text (const char *name, struct objfile *objf) 27119370Spst{ 27219370Spst struct objfile *objfile; 27319370Spst struct minimal_symbol *msymbol; 27419370Spst struct minimal_symbol *found_symbol = NULL; 27519370Spst struct minimal_symbol *found_file_symbol = NULL; 27619370Spst 277130803Smarcel unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE; 27819370Spst 27919370Spst for (objfile = object_files; 28019370Spst objfile != NULL && found_symbol == NULL; 28198944Sobrien objfile = objfile->next) 28219370Spst { 28319370Spst if (objf == NULL || objf == objfile) 28419370Spst { 285130803Smarcel for (msymbol = objfile->msymbol_hash[hash]; 286130803Smarcel msymbol != NULL && found_symbol == NULL; 287130803Smarcel msymbol = msymbol->hash_next) 28819370Spst { 289130803Smarcel if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 && 29019370Spst (MSYMBOL_TYPE (msymbol) == mst_text || 29119370Spst MSYMBOL_TYPE (msymbol) == mst_file_text)) 29219370Spst { 29319370Spst switch (MSYMBOL_TYPE (msymbol)) 29419370Spst { 29519370Spst case mst_file_text: 29619370Spst found_file_symbol = msymbol; 29719370Spst break; 29819370Spst default: 29919370Spst found_symbol = msymbol; 30019370Spst break; 30119370Spst } 30219370Spst } 30319370Spst } 30419370Spst } 30519370Spst } 30619370Spst /* External symbols are best. */ 30719370Spst if (found_symbol) 30819370Spst return found_symbol; 30919370Spst 31019370Spst /* File-local symbols are next best. */ 31119370Spst if (found_file_symbol) 31219370Spst return found_file_symbol; 31319370Spst 31419370Spst return NULL; 31519370Spst} 31619370Spst 31719370Spst/* Look through all the current minimal symbol tables and find the 318130803Smarcel first minimal symbol that matches NAME and is a solib trampoline. 319130803Smarcel If OBJF is non-NULL, limit the search to that objfile. Returns a 320130803Smarcel pointer to the minimal symbol that matches, or NULL if no match is 321130803Smarcel found. 32298944Sobrien 323130803Smarcel This function only searches the mangled (linkage) names. */ 324130803Smarcel 32519370Spststruct minimal_symbol * 326130803Smarcellookup_minimal_symbol_solib_trampoline (const char *name, 327130803Smarcel struct objfile *objf) 32819370Spst{ 32919370Spst struct objfile *objfile; 33019370Spst struct minimal_symbol *msymbol; 33119370Spst struct minimal_symbol *found_symbol = NULL; 33219370Spst 333130803Smarcel unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE; 33419370Spst 33519370Spst for (objfile = object_files; 33619370Spst objfile != NULL && found_symbol == NULL; 33798944Sobrien objfile = objfile->next) 33819370Spst { 33919370Spst if (objf == NULL || objf == objfile) 34019370Spst { 341130803Smarcel for (msymbol = objfile->msymbol_hash[hash]; 342130803Smarcel msymbol != NULL && found_symbol == NULL; 343130803Smarcel msymbol = msymbol->hash_next) 34419370Spst { 345130803Smarcel if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 && 34619370Spst MSYMBOL_TYPE (msymbol) == mst_solib_trampoline) 34719370Spst return msymbol; 34819370Spst } 34919370Spst } 35019370Spst } 35119370Spst 35219370Spst return NULL; 35319370Spst} 35419370Spst 35519370Spst 35646283Sdfr/* Search through the minimal symbol table for each objfile and find 35746283Sdfr the symbol whose address is the largest address that is still less 358130803Smarcel than or equal to PC, and matches SECTION (if non-NULL). Returns a 35946283Sdfr pointer to the minimal symbol if such a symbol is found, or NULL if 36046283Sdfr PC is not in a suitable range. Note that we need to look through 36146283Sdfr ALL the minimal symbol tables before deciding on the symbol that 36246283Sdfr comes closest to the specified PC. This is because objfiles can 36346283Sdfr overlap, for example objfile A has .text at 0x100 and .data at 36446283Sdfr 0x40000 and objfile B has .text at 0x234 and .data at 0x40048. */ 36519370Spst 36619370Spststruct minimal_symbol * 36798944Sobrienlookup_minimal_symbol_by_pc_section (CORE_ADDR pc, asection *section) 36819370Spst{ 36946283Sdfr int lo; 37046283Sdfr int hi; 37146283Sdfr int new; 37246283Sdfr struct objfile *objfile; 37346283Sdfr struct minimal_symbol *msymbol; 37446283Sdfr struct minimal_symbol *best_symbol = NULL; 375130803Smarcel struct obj_section *pc_section; 37619370Spst 377130803Smarcel /* PC has to be in a known section. This ensures that anything 378130803Smarcel beyond the end of the last segment doesn't appear to be part of 379130803Smarcel the last function in the last segment. */ 380130803Smarcel pc_section = find_pc_section (pc); 381130803Smarcel if (pc_section == NULL) 38246283Sdfr return NULL; 38346283Sdfr 384130803Smarcel /* NOTE: cagney/2004-01-27: Removed code (added 2003-07-19) that was 385130803Smarcel trying to force the PC into a valid section as returned by 386130803Smarcel find_pc_section. It broke IRIX 6.5 mdebug which relies on this 387130803Smarcel code returning an absolute symbol - the problem was that 388130803Smarcel find_pc_section wasn't returning an absolute section and hence 389130803Smarcel the code below would skip over absolute symbols. Since the 390130803Smarcel original problem was with finding a frame's function, and that 391130803Smarcel uses [indirectly] lookup_minimal_symbol_by_pc, the original 392130803Smarcel problem has been fixed by having that function use 393130803Smarcel find_pc_section. */ 394130803Smarcel 39519370Spst for (objfile = object_files; 39619370Spst objfile != NULL; 39798944Sobrien objfile = objfile->next) 39819370Spst { 39919370Spst /* If this objfile has a minimal symbol table, go search it using 40098944Sobrien a binary search. Note that a minimal symbol table always consists 40198944Sobrien of at least two symbols, a "real" symbol and the terminating 40298944Sobrien "null symbol". If there are no real symbols, then there is no 40398944Sobrien minimal symbol table at all. */ 40419370Spst 405130803Smarcel if (objfile->minimal_symbol_count > 0) 40619370Spst { 407130803Smarcel msymbol = objfile->msymbols; 40819370Spst lo = 0; 40998944Sobrien hi = objfile->minimal_symbol_count - 1; 41019370Spst 41119370Spst /* This code assumes that the minimal symbols are sorted by 41219370Spst ascending address values. If the pc value is greater than or 41319370Spst equal to the first symbol's address, then some symbol in this 41419370Spst minimal symbol table is a suitable candidate for being the 41519370Spst "best" symbol. This includes the last real symbol, for cases 41619370Spst where the pc value is larger than any address in this vector. 41719370Spst 41819370Spst By iterating until the address associated with the current 41919370Spst hi index (the endpoint of the test interval) is less than 42019370Spst or equal to the desired pc value, we accomplish two things: 42119370Spst (1) the case where the pc value is larger than any minimal 42219370Spst symbol address is trivially solved, (2) the address associated 42319370Spst with the hi index is always the one we want when the interation 42419370Spst terminates. In essence, we are iterating the test interval 42519370Spst down until the pc value is pushed out of it from the high end. 42619370Spst 42719370Spst Warning: this code is trickier than it would appear at first. */ 42819370Spst 42946283Sdfr /* Should also require that pc is <= end of objfile. FIXME! */ 43019370Spst if (pc >= SYMBOL_VALUE_ADDRESS (&msymbol[lo])) 43119370Spst { 43219370Spst while (SYMBOL_VALUE_ADDRESS (&msymbol[hi]) > pc) 43319370Spst { 43419370Spst /* pc is still strictly less than highest address */ 43519370Spst /* Note "new" will always be >= lo */ 43619370Spst new = (lo + hi) / 2; 43719370Spst if ((SYMBOL_VALUE_ADDRESS (&msymbol[new]) >= pc) || 43819370Spst (lo == new)) 43919370Spst { 44019370Spst hi = new; 44119370Spst } 44219370Spst else 44319370Spst { 44419370Spst lo = new; 44519370Spst } 44619370Spst } 44719370Spst 44819370Spst /* If we have multiple symbols at the same address, we want 44998944Sobrien hi to point to the last one. That way we can find the 45098944Sobrien right symbol if it has an index greater than hi. */ 45198944Sobrien while (hi < objfile->minimal_symbol_count - 1 45219370Spst && (SYMBOL_VALUE_ADDRESS (&msymbol[hi]) 45398944Sobrien == SYMBOL_VALUE_ADDRESS (&msymbol[hi + 1]))) 45419370Spst hi++; 45519370Spst 45619370Spst /* The minimal symbol indexed by hi now is the best one in this 45798944Sobrien objfile's minimal symbol table. See if it is the best one 45898944Sobrien overall. */ 45919370Spst 46019370Spst /* Skip any absolute symbols. This is apparently what adb 46198944Sobrien and dbx do, and is needed for the CM-5. There are two 46298944Sobrien known possible problems: (1) on ELF, apparently end, edata, 46398944Sobrien etc. are absolute. Not sure ignoring them here is a big 46498944Sobrien deal, but if we want to use them, the fix would go in 46598944Sobrien elfread.c. (2) I think shared library entry points on the 46698944Sobrien NeXT are absolute. If we want special handling for this 46798944Sobrien it probably should be triggered by a special 46898944Sobrien mst_abs_or_lib or some such. */ 46919370Spst while (hi >= 0 47019370Spst && msymbol[hi].type == mst_abs) 47119370Spst --hi; 47219370Spst 47346283Sdfr /* If "section" specified, skip any symbol from wrong section */ 47446283Sdfr /* This is the new code that distinguishes it from the old function */ 47546283Sdfr if (section) 47646283Sdfr while (hi >= 0 47798944Sobrien /* Some types of debug info, such as COFF, 47898944Sobrien don't fill the bfd_section member, so don't 47998944Sobrien throw away symbols on those platforms. */ 48098944Sobrien && SYMBOL_BFD_SECTION (&msymbol[hi]) != NULL 48146283Sdfr && SYMBOL_BFD_SECTION (&msymbol[hi]) != section) 48246283Sdfr --hi; 48346283Sdfr 48419370Spst if (hi >= 0 48519370Spst && ((best_symbol == NULL) || 48698944Sobrien (SYMBOL_VALUE_ADDRESS (best_symbol) < 48719370Spst SYMBOL_VALUE_ADDRESS (&msymbol[hi])))) 48819370Spst { 48919370Spst best_symbol = &msymbol[hi]; 49019370Spst } 49119370Spst } 49219370Spst } 49319370Spst } 49419370Spst return (best_symbol); 49519370Spst} 49619370Spst 49746283Sdfr/* Backward compatibility: search through the minimal symbol table 49846283Sdfr for a matching PC (no section given) */ 49946283Sdfr 50046283Sdfrstruct minimal_symbol * 50198944Sobrienlookup_minimal_symbol_by_pc (CORE_ADDR pc) 50246283Sdfr{ 503130803Smarcel /* NOTE: cagney/2004-01-27: This was using find_pc_mapped_section to 504130803Smarcel force the section but that (well unless you're doing overlay 505130803Smarcel debugging) always returns NULL making the call somewhat useless. */ 506130803Smarcel struct obj_section *section = find_pc_section (pc); 507130803Smarcel if (section == NULL) 508130803Smarcel return NULL; 509130803Smarcel return lookup_minimal_symbol_by_pc_section (pc, section->the_bfd_section); 51046283Sdfr} 51198944Sobrien 51219370Spst 51319370Spst/* Return leading symbol character for a BFD. If BFD is NULL, 51419370Spst return the leading symbol character from the main objfile. */ 51519370Spst 51698944Sobrienstatic int get_symbol_leading_char (bfd *); 51719370Spst 51819370Spststatic int 51998944Sobrienget_symbol_leading_char (bfd *abfd) 52019370Spst{ 52119370Spst if (abfd != NULL) 52219370Spst return bfd_get_symbol_leading_char (abfd); 52319370Spst if (symfile_objfile != NULL && symfile_objfile->obfd != NULL) 52419370Spst return bfd_get_symbol_leading_char (symfile_objfile->obfd); 52519370Spst return 0; 52619370Spst} 52719370Spst 52819370Spst/* Prepare to start collecting minimal symbols. Note that presetting 52919370Spst msym_bunch_index to BUNCH_SIZE causes the first call to save a minimal 53019370Spst symbol to allocate the memory for the first bunch. */ 53119370Spst 53219370Spstvoid 53398944Sobrieninit_minimal_symbol_collection (void) 53419370Spst{ 53519370Spst msym_count = 0; 53619370Spst msym_bunch = NULL; 53719370Spst msym_bunch_index = BUNCH_SIZE; 53819370Spst} 53919370Spst 54019370Spstvoid 54198944Sobrienprim_record_minimal_symbol (const char *name, CORE_ADDR address, 54298944Sobrien enum minimal_symbol_type ms_type, 54398944Sobrien struct objfile *objfile) 54419370Spst{ 54519370Spst int section; 54619370Spst 54719370Spst switch (ms_type) 54819370Spst { 54919370Spst case mst_text: 55019370Spst case mst_file_text: 55119370Spst case mst_solib_trampoline: 55298944Sobrien section = SECT_OFF_TEXT (objfile); 55319370Spst break; 55419370Spst case mst_data: 55519370Spst case mst_file_data: 55698944Sobrien section = SECT_OFF_DATA (objfile); 55719370Spst break; 55819370Spst case mst_bss: 55919370Spst case mst_file_bss: 56098944Sobrien section = SECT_OFF_BSS (objfile); 56119370Spst break; 56219370Spst default: 56319370Spst section = -1; 56419370Spst } 56519370Spst 56619370Spst prim_record_minimal_symbol_and_info (name, address, ms_type, 56746283Sdfr NULL, section, NULL, objfile); 56819370Spst} 56919370Spst 57019370Spst/* Record a minimal symbol in the msym bunches. Returns the symbol 57119370Spst newly created. */ 57246283Sdfr 57319370Spststruct minimal_symbol * 57498944Sobrienprim_record_minimal_symbol_and_info (const char *name, CORE_ADDR address, 57598944Sobrien enum minimal_symbol_type ms_type, 57698944Sobrien char *info, int section, 57798944Sobrien asection *bfd_section, 57898944Sobrien struct objfile *objfile) 57919370Spst{ 580130803Smarcel struct msym_bunch *new; 581130803Smarcel struct minimal_symbol *msymbol; 58219370Spst 58319370Spst if (ms_type == mst_file_text) 58419370Spst { 58519370Spst /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into 58698944Sobrien the minimal symbols, because if there is also another symbol 58798944Sobrien at the same address (e.g. the first function of the file), 58898944Sobrien lookup_minimal_symbol_by_pc would have no way of getting the 58998944Sobrien right one. */ 59019370Spst if (name[0] == 'g' 59119370Spst && (strcmp (name, GCC_COMPILED_FLAG_SYMBOL) == 0 59219370Spst || strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)) 59319370Spst return (NULL); 59419370Spst 59519370Spst { 59619370Spst const char *tempstring = name; 59719370Spst if (tempstring[0] == get_symbol_leading_char (objfile->obfd)) 59819370Spst ++tempstring; 599130803Smarcel if (strncmp (tempstring, "__gnu_compiled", 14) == 0) 60019370Spst return (NULL); 60119370Spst } 60219370Spst } 60319370Spst 60419370Spst if (msym_bunch_index == BUNCH_SIZE) 60519370Spst { 60619370Spst new = (struct msym_bunch *) xmalloc (sizeof (struct msym_bunch)); 60719370Spst msym_bunch_index = 0; 60898944Sobrien new->next = msym_bunch; 60919370Spst msym_bunch = new; 61019370Spst } 61198944Sobrien msymbol = &msym_bunch->contents[msym_bunch_index]; 61219370Spst SYMBOL_INIT_LANGUAGE_SPECIFIC (msymbol, language_unknown); 613130803Smarcel SYMBOL_LANGUAGE (msymbol) = language_auto; 614130803Smarcel SYMBOL_SET_NAMES (msymbol, (char *)name, strlen (name), objfile); 615130803Smarcel 61619370Spst SYMBOL_VALUE_ADDRESS (msymbol) = address; 61719370Spst SYMBOL_SECTION (msymbol) = section; 61846283Sdfr SYMBOL_BFD_SECTION (msymbol) = bfd_section; 61919370Spst 62019370Spst MSYMBOL_TYPE (msymbol) = ms_type; 62119370Spst /* FIXME: This info, if it remains, needs its own field. */ 62298944Sobrien MSYMBOL_INFO (msymbol) = info; /* FIXME! */ 623130803Smarcel MSYMBOL_SIZE (msymbol) = 0; 62498944Sobrien 62598944Sobrien /* The hash pointers must be cleared! If they're not, 62698944Sobrien add_minsym_to_hash_table will NOT add this msymbol to the hash table. */ 62798944Sobrien msymbol->hash_next = NULL; 62898944Sobrien msymbol->demangled_hash_next = NULL; 62998944Sobrien 63019370Spst msym_bunch_index++; 63119370Spst msym_count++; 63219370Spst OBJSTAT (objfile, n_minsyms++); 63319370Spst return msymbol; 63419370Spst} 63519370Spst 63619370Spst/* Compare two minimal symbols by address and return a signed result based 63746283Sdfr on unsigned comparisons, so that we sort into unsigned numeric order. 63846283Sdfr Within groups with the same address, sort by name. */ 63919370Spst 64019370Spststatic int 64198944Sobriencompare_minimal_symbols (const void *fn1p, const void *fn2p) 64219370Spst{ 643130803Smarcel const struct minimal_symbol *fn1; 644130803Smarcel const struct minimal_symbol *fn2; 64519370Spst 64619370Spst fn1 = (const struct minimal_symbol *) fn1p; 64719370Spst fn2 = (const struct minimal_symbol *) fn2p; 64819370Spst 64919370Spst if (SYMBOL_VALUE_ADDRESS (fn1) < SYMBOL_VALUE_ADDRESS (fn2)) 65019370Spst { 65198944Sobrien return (-1); /* addr 1 is less than addr 2 */ 65219370Spst } 65319370Spst else if (SYMBOL_VALUE_ADDRESS (fn1) > SYMBOL_VALUE_ADDRESS (fn2)) 65419370Spst { 65598944Sobrien return (1); /* addr 1 is greater than addr 2 */ 65619370Spst } 65798944Sobrien else 65898944Sobrien /* addrs are equal: sort by name */ 65919370Spst { 660130803Smarcel char *name1 = SYMBOL_LINKAGE_NAME (fn1); 661130803Smarcel char *name2 = SYMBOL_LINKAGE_NAME (fn2); 66246283Sdfr 66346283Sdfr if (name1 && name2) /* both have names */ 66446283Sdfr return strcmp (name1, name2); 66546283Sdfr else if (name2) 66698944Sobrien return 1; /* fn1 has no name, so it is "less" */ 66798944Sobrien else if (name1) /* fn2 has no name, so it is "less" */ 66846283Sdfr return -1; 66946283Sdfr else 67098944Sobrien return (0); /* neither has a name, so they're equal. */ 67119370Spst } 67219370Spst} 67319370Spst 67419370Spst/* Discard the currently collected minimal symbols, if any. If we wish 67519370Spst to save them for later use, we must have already copied them somewhere 67619370Spst else before calling this function. 67719370Spst 67819370Spst FIXME: We could allocate the minimal symbol bunches on their own 67919370Spst obstack and then simply blow the obstack away when we are done with 68019370Spst it. Is it worth the extra trouble though? */ 68119370Spst 68298944Sobrienstatic void 68398944Sobriendo_discard_minimal_symbols_cleanup (void *arg) 68419370Spst{ 685130803Smarcel struct msym_bunch *next; 68619370Spst 68719370Spst while (msym_bunch != NULL) 68819370Spst { 68998944Sobrien next = msym_bunch->next; 69098944Sobrien xfree (msym_bunch); 69119370Spst msym_bunch = next; 69219370Spst } 69319370Spst} 69419370Spst 69598944Sobrienstruct cleanup * 69698944Sobrienmake_cleanup_discard_minimal_symbols (void) 69798944Sobrien{ 69898944Sobrien return make_cleanup (do_discard_minimal_symbols_cleanup, 0); 69998944Sobrien} 70098944Sobrien 70198944Sobrien 70298944Sobrien 70319370Spst/* Compact duplicate entries out of a minimal symbol table by walking 70419370Spst through the table and compacting out entries with duplicate addresses 70519370Spst and matching names. Return the number of entries remaining. 70619370Spst 70719370Spst On entry, the table resides between msymbol[0] and msymbol[mcount]. 70819370Spst On exit, it resides between msymbol[0] and msymbol[result_count]. 70919370Spst 71019370Spst When files contain multiple sources of symbol information, it is 71119370Spst possible for the minimal symbol table to contain many duplicate entries. 71219370Spst As an example, SVR4 systems use ELF formatted object files, which 71319370Spst usually contain at least two different types of symbol tables (a 71419370Spst standard ELF one and a smaller dynamic linking table), as well as 71519370Spst DWARF debugging information for files compiled with -g. 71619370Spst 71719370Spst Without compacting, the minimal symbol table for gdb itself contains 71819370Spst over a 1000 duplicates, about a third of the total table size. Aside 71919370Spst from the potential trap of not noticing that two successive entries 72019370Spst identify the same location, this duplication impacts the time required 72119370Spst to linearly scan the table, which is done in a number of places. So we 72219370Spst just do one linear scan here and toss out the duplicates. 72319370Spst 72419370Spst Note that we are not concerned here about recovering the space that 72519370Spst is potentially freed up, because the strings themselves are allocated 726130803Smarcel on the objfile_obstack, and will get automatically freed when the symbol 72719370Spst table is freed. The caller can free up the unused minimal symbols at 72819370Spst the end of the compacted region if their allocation strategy allows it. 72919370Spst 73019370Spst Also note we only go up to the next to last entry within the loop 73119370Spst and then copy the last entry explicitly after the loop terminates. 73219370Spst 73319370Spst Since the different sources of information for each symbol may 73419370Spst have different levels of "completeness", we may have duplicates 73519370Spst that have one entry with type "mst_unknown" and the other with a 73619370Spst known type. So if the one we are leaving alone has type mst_unknown, 73719370Spst overwrite its type with the type from the one we are compacting out. */ 73819370Spst 73919370Spststatic int 74098944Sobriencompact_minimal_symbols (struct minimal_symbol *msymbol, int mcount, 74198944Sobrien struct objfile *objfile) 74219370Spst{ 74319370Spst struct minimal_symbol *copyfrom; 74419370Spst struct minimal_symbol *copyto; 74519370Spst 74619370Spst if (mcount > 0) 74719370Spst { 74819370Spst copyfrom = copyto = msymbol; 74919370Spst while (copyfrom < msymbol + mcount - 1) 75019370Spst { 751130803Smarcel if (SYMBOL_VALUE_ADDRESS (copyfrom) 752130803Smarcel == SYMBOL_VALUE_ADDRESS ((copyfrom + 1)) 753130803Smarcel && strcmp (SYMBOL_LINKAGE_NAME (copyfrom), 754130803Smarcel SYMBOL_LINKAGE_NAME ((copyfrom + 1))) == 0) 75519370Spst { 75698944Sobrien if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown) 75719370Spst { 75819370Spst MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom); 75919370Spst } 76019370Spst copyfrom++; 76119370Spst } 76219370Spst else 76398944Sobrien *copyto++ = *copyfrom++; 76419370Spst } 76519370Spst *copyto++ = *copyfrom++; 76619370Spst mcount = copyto - msymbol; 76719370Spst } 76819370Spst return (mcount); 76919370Spst} 77019370Spst 77198944Sobrien/* Build (or rebuild) the minimal symbol hash tables. This is necessary 77298944Sobrien after compacting or sorting the table since the entries move around 77398944Sobrien thus causing the internal minimal_symbol pointers to become jumbled. */ 77498944Sobrien 77598944Sobrienstatic void 77698944Sobrienbuild_minimal_symbol_hash_tables (struct objfile *objfile) 77798944Sobrien{ 77898944Sobrien int i; 77998944Sobrien struct minimal_symbol *msym; 78098944Sobrien 78198944Sobrien /* Clear the hash tables. */ 78298944Sobrien for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++) 78398944Sobrien { 78498944Sobrien objfile->msymbol_hash[i] = 0; 78598944Sobrien objfile->msymbol_demangled_hash[i] = 0; 78698944Sobrien } 78798944Sobrien 78898944Sobrien /* Now, (re)insert the actual entries. */ 78998944Sobrien for (i = objfile->minimal_symbol_count, msym = objfile->msymbols; 79098944Sobrien i > 0; 79198944Sobrien i--, msym++) 79298944Sobrien { 79398944Sobrien msym->hash_next = 0; 79498944Sobrien add_minsym_to_hash_table (msym, objfile->msymbol_hash); 79598944Sobrien 79698944Sobrien msym->demangled_hash_next = 0; 79798944Sobrien if (SYMBOL_DEMANGLED_NAME (msym) != NULL) 79898944Sobrien add_minsym_to_demangled_hash_table (msym, 79998944Sobrien objfile->msymbol_demangled_hash); 80098944Sobrien } 80198944Sobrien} 80298944Sobrien 80319370Spst/* Add the minimal symbols in the existing bunches to the objfile's official 80419370Spst minimal symbol table. In most cases there is no minimal symbol table yet 80519370Spst for this objfile, and the existing bunches are used to create one. Once 80619370Spst in a while (for shared libraries for example), we add symbols (e.g. common 80719370Spst symbols) to an existing objfile. 80819370Spst 80919370Spst Because of the way minimal symbols are collected, we generally have no way 81019370Spst of knowing what source language applies to any particular minimal symbol. 81119370Spst Specifically, we have no way of knowing if the minimal symbol comes from a 81219370Spst C++ compilation unit or not. So for the sake of supporting cached 81319370Spst demangled C++ names, we have no choice but to try and demangle each new one 81419370Spst that comes in. If the demangling succeeds, then we assume it is a C++ 81519370Spst symbol and set the symbol's language and demangled name fields 81619370Spst appropriately. Note that in order to avoid unnecessary demanglings, and 81719370Spst allocating obstack space that subsequently can't be freed for the demangled 81819370Spst names, we mark all newly added symbols with language_auto. After 81919370Spst compaction of the minimal symbols, we go back and scan the entire minimal 82019370Spst symbol table looking for these new symbols. For each new symbol we attempt 82119370Spst to demangle it, and if successful, record it as a language_cplus symbol 82219370Spst and cache the demangled form on the symbol obstack. Symbols which don't 82319370Spst demangle are marked as language_unknown symbols, which inhibits future 82419370Spst attempts to demangle them if we later add more minimal symbols. */ 82519370Spst 82619370Spstvoid 82798944Sobrieninstall_minimal_symbols (struct objfile *objfile) 82819370Spst{ 829130803Smarcel int bindex; 830130803Smarcel int mcount; 831130803Smarcel struct msym_bunch *bunch; 832130803Smarcel struct minimal_symbol *msymbols; 83319370Spst int alloc_count; 834130803Smarcel char leading_char; 83519370Spst 83619370Spst if (msym_count > 0) 83719370Spst { 83819370Spst /* Allocate enough space in the obstack, into which we will gather the 83998944Sobrien bunches of new and existing minimal symbols, sort them, and then 84098944Sobrien compact out the duplicate entries. Once we have a final table, 84198944Sobrien we will give back the excess space. */ 84219370Spst 84319370Spst alloc_count = msym_count + objfile->minimal_symbol_count + 1; 844130803Smarcel obstack_blank (&objfile->objfile_obstack, 84519370Spst alloc_count * sizeof (struct minimal_symbol)); 84619370Spst msymbols = (struct minimal_symbol *) 847130803Smarcel obstack_base (&objfile->objfile_obstack); 84819370Spst 84919370Spst /* Copy in the existing minimal symbols, if there are any. */ 85019370Spst 85119370Spst if (objfile->minimal_symbol_count) 85298944Sobrien memcpy ((char *) msymbols, (char *) objfile->msymbols, 85398944Sobrien objfile->minimal_symbol_count * sizeof (struct minimal_symbol)); 85419370Spst 85519370Spst /* Walk through the list of minimal symbol bunches, adding each symbol 85698944Sobrien to the new contiguous array of symbols. Note that we start with the 85798944Sobrien current, possibly partially filled bunch (thus we use the current 85898944Sobrien msym_bunch_index for the first bunch we copy over), and thereafter 85998944Sobrien each bunch is full. */ 86098944Sobrien 86119370Spst mcount = objfile->minimal_symbol_count; 86219370Spst leading_char = get_symbol_leading_char (objfile->obfd); 86398944Sobrien 86498944Sobrien for (bunch = msym_bunch; bunch != NULL; bunch = bunch->next) 86519370Spst { 86619370Spst for (bindex = 0; bindex < msym_bunch_index; bindex++, mcount++) 86719370Spst { 86898944Sobrien msymbols[mcount] = bunch->contents[bindex]; 869130803Smarcel if (SYMBOL_LINKAGE_NAME (&msymbols[mcount])[0] == leading_char) 87019370Spst { 871130803Smarcel SYMBOL_LINKAGE_NAME (&msymbols[mcount])++; 87219370Spst } 87319370Spst } 87419370Spst msym_bunch_index = BUNCH_SIZE; 87519370Spst } 87619370Spst 87719370Spst /* Sort the minimal symbols by address. */ 87898944Sobrien 87919370Spst qsort (msymbols, mcount, sizeof (struct minimal_symbol), 88019370Spst compare_minimal_symbols); 88198944Sobrien 88219370Spst /* Compact out any duplicates, and free up whatever space we are 88398944Sobrien no longer using. */ 88419370Spst 88598944Sobrien mcount = compact_minimal_symbols (msymbols, mcount, objfile); 88698944Sobrien 887130803Smarcel obstack_blank (&objfile->objfile_obstack, 88898944Sobrien (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol)); 88919370Spst msymbols = (struct minimal_symbol *) 890130803Smarcel obstack_finish (&objfile->objfile_obstack); 89119370Spst 89219370Spst /* We also terminate the minimal symbol table with a "null symbol", 89398944Sobrien which is *not* included in the size of the table. This makes it 89498944Sobrien easier to find the end of the table when we are handed a pointer 89598944Sobrien to some symbol in the middle of it. Zero out the fields in the 89698944Sobrien "null symbol" allocated at the end of the array. Note that the 89798944Sobrien symbol count does *not* include this null symbol, which is why it 89898944Sobrien is indexed by mcount and not mcount-1. */ 89919370Spst 900130803Smarcel SYMBOL_LINKAGE_NAME (&msymbols[mcount]) = NULL; 90119370Spst SYMBOL_VALUE_ADDRESS (&msymbols[mcount]) = 0; 90219370Spst MSYMBOL_INFO (&msymbols[mcount]) = NULL; 903130803Smarcel MSYMBOL_SIZE (&msymbols[mcount]) = 0; 90419370Spst MSYMBOL_TYPE (&msymbols[mcount]) = mst_unknown; 90519370Spst SYMBOL_INIT_LANGUAGE_SPECIFIC (&msymbols[mcount], language_unknown); 90619370Spst 90719370Spst /* Attach the minimal symbol table to the specified objfile. 908130803Smarcel The strings themselves are also located in the objfile_obstack 90998944Sobrien of this objfile. */ 91019370Spst 91198944Sobrien objfile->minimal_symbol_count = mcount; 91298944Sobrien objfile->msymbols = msymbols; 91319370Spst 91498944Sobrien /* Try to guess the appropriate C++ ABI by looking at the names 91598944Sobrien of the minimal symbols in the table. */ 91698944Sobrien { 91798944Sobrien int i; 91898944Sobrien 91998944Sobrien for (i = 0; i < mcount; i++) 92098944Sobrien { 921130803Smarcel /* If a symbol's name starts with _Z and was successfully 922130803Smarcel demangled, then we can assume we've found a GNU v3 symbol. 923130803Smarcel For now we set the C++ ABI globally; if the user is 924130803Smarcel mixing ABIs then the user will need to "set cp-abi" 925130803Smarcel manually. */ 926130803Smarcel const char *name = SYMBOL_LINKAGE_NAME (&objfile->msymbols[i]); 927130803Smarcel if (name[0] == '_' && name[1] == 'Z' 928130803Smarcel && SYMBOL_DEMANGLED_NAME (&objfile->msymbols[i]) != NULL) 92998944Sobrien { 930130803Smarcel set_cp_abi_as_auto_default ("gnu-v3"); 93198944Sobrien break; 93298944Sobrien } 93398944Sobrien } 93498944Sobrien } 93519370Spst 93698944Sobrien /* Now build the hash tables; we can't do this incrementally 93798944Sobrien at an earlier point since we weren't finished with the obstack 93898944Sobrien yet. (And if the msymbol obstack gets moved, all the internal 93998944Sobrien pointers to other msymbols need to be adjusted.) */ 94098944Sobrien build_minimal_symbol_hash_tables (objfile); 94119370Spst } 94219370Spst} 94319370Spst 94419370Spst/* Sort all the minimal symbols in OBJFILE. */ 94519370Spst 94619370Spstvoid 94798944Sobrienmsymbols_sort (struct objfile *objfile) 94819370Spst{ 94919370Spst qsort (objfile->msymbols, objfile->minimal_symbol_count, 95019370Spst sizeof (struct minimal_symbol), compare_minimal_symbols); 95198944Sobrien build_minimal_symbol_hash_tables (objfile); 95219370Spst} 95319370Spst 95419370Spst/* Check if PC is in a shared library trampoline code stub. 95519370Spst Return minimal symbol for the trampoline entry or NULL if PC is not 95619370Spst in a trampoline code stub. */ 95719370Spst 95819370Spststruct minimal_symbol * 95998944Sobrienlookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc) 96019370Spst{ 96119370Spst struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc); 96219370Spst 96319370Spst if (msymbol != NULL && MSYMBOL_TYPE (msymbol) == mst_solib_trampoline) 96419370Spst return msymbol; 96519370Spst return NULL; 96619370Spst} 96719370Spst 96819370Spst/* If PC is in a shared library trampoline code stub, return the 96919370Spst address of the `real' function belonging to the stub. 97019370Spst Return 0 if PC is not in a trampoline code stub or if the real 97119370Spst function is not found in the minimal symbol table. 97219370Spst 97319370Spst We may fail to find the right function if a function with the 97419370Spst same name is defined in more than one shared library, but this 97519370Spst is considered bad programming style. We could return 0 if we find 97619370Spst a duplicate function in case this matters someday. */ 97719370Spst 97819370SpstCORE_ADDR 97998944Sobrienfind_solib_trampoline_target (CORE_ADDR pc) 98019370Spst{ 98119370Spst struct objfile *objfile; 98219370Spst struct minimal_symbol *msymbol; 98319370Spst struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc); 98419370Spst 98519370Spst if (tsymbol != NULL) 98619370Spst { 98719370Spst ALL_MSYMBOLS (objfile, msymbol) 98898944Sobrien { 98998944Sobrien if (MSYMBOL_TYPE (msymbol) == mst_text 990130803Smarcel && strcmp (SYMBOL_LINKAGE_NAME (msymbol), 991130803Smarcel SYMBOL_LINKAGE_NAME (tsymbol)) == 0) 99298944Sobrien return SYMBOL_VALUE_ADDRESS (msymbol); 99398944Sobrien } 99419370Spst } 99519370Spst return 0; 99619370Spst} 997