xref: /dflybsd-src/contrib/gdb-7/gdb/minsyms.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* GDB routines for manipulating the minimal symbol tables.
2*ef5ccd6cSJohn Marino    Copyright (C) 1992-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert /* This file contains support routines for creating, manipulating, and
225796c8dcSSimon Schubert    destroying minimal symbol tables.
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert    Minimal symbol tables are used to hold some very basic information about
255796c8dcSSimon Schubert    all defined global symbols (text, data, bss, abs, etc).  The only two
265796c8dcSSimon Schubert    required pieces of information are the symbol's name and the address
275796c8dcSSimon Schubert    associated with that symbol.
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert    In many cases, even if a file was compiled with no special options for
305796c8dcSSimon Schubert    debugging at all, as long as was not stripped it will contain sufficient
315796c8dcSSimon Schubert    information to build useful minimal symbol tables using this structure.
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert    Even when a file contains enough debugging information to build a full
345796c8dcSSimon Schubert    symbol table, these minimal symbols are still useful for quickly mapping
355796c8dcSSimon Schubert    between names and addresses, and vice versa.  They are also sometimes used
365796c8dcSSimon Schubert    to figure out what full symbol table entries need to be read in.  */
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert #include "defs.h"
405796c8dcSSimon Schubert #include <ctype.h>
415796c8dcSSimon Schubert #include "gdb_string.h"
425796c8dcSSimon Schubert #include "symtab.h"
435796c8dcSSimon Schubert #include "bfd.h"
44c50c785cSJohn Marino #include "filenames.h"
455796c8dcSSimon Schubert #include "symfile.h"
465796c8dcSSimon Schubert #include "objfiles.h"
475796c8dcSSimon Schubert #include "demangle.h"
485796c8dcSSimon Schubert #include "value.h"
495796c8dcSSimon Schubert #include "cp-abi.h"
505796c8dcSSimon Schubert #include "target.h"
515796c8dcSSimon Schubert #include "cp-support.h"
525796c8dcSSimon Schubert #include "language.h"
53*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
565796c8dcSSimon Schubert    At the end, copy them all into one newly allocated location on an objfile's
575796c8dcSSimon Schubert    symbol obstack.  */
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert #define BUNCH_SIZE 127
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert struct msym_bunch
625796c8dcSSimon Schubert   {
635796c8dcSSimon Schubert     struct msym_bunch *next;
645796c8dcSSimon Schubert     struct minimal_symbol contents[BUNCH_SIZE];
655796c8dcSSimon Schubert   };
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert /* Bunch currently being filled up.
685796c8dcSSimon Schubert    The next field points to chain of filled bunches.  */
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert static struct msym_bunch *msym_bunch;
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Number of slots filled in current bunch.  */
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert static int msym_bunch_index;
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert /* Total number of minimal symbols recorded so far for the objfile.  */
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert static int msym_count;
795796c8dcSSimon Schubert 
80*ef5ccd6cSJohn Marino /* See minsyms.h.  */
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert unsigned int
msymbol_hash_iw(const char * string)835796c8dcSSimon Schubert msymbol_hash_iw (const char *string)
845796c8dcSSimon Schubert {
855796c8dcSSimon Schubert   unsigned int hash = 0;
86cf7f2e2dSJohn Marino 
875796c8dcSSimon Schubert   while (*string && *string != '(')
885796c8dcSSimon Schubert     {
89*ef5ccd6cSJohn Marino       string = skip_spaces_const (string);
905796c8dcSSimon Schubert       if (*string && *string != '(')
915796c8dcSSimon Schubert 	{
92a45ae5f8SJohn Marino 	  hash = SYMBOL_HASH_NEXT (hash, *string);
935796c8dcSSimon Schubert 	  ++string;
945796c8dcSSimon Schubert 	}
955796c8dcSSimon Schubert     }
965796c8dcSSimon Schubert   return hash;
975796c8dcSSimon Schubert }
985796c8dcSSimon Schubert 
99*ef5ccd6cSJohn Marino /* See minsyms.h.  */
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert unsigned int
msymbol_hash(const char * string)1025796c8dcSSimon Schubert msymbol_hash (const char *string)
1035796c8dcSSimon Schubert {
1045796c8dcSSimon Schubert   unsigned int hash = 0;
105cf7f2e2dSJohn Marino 
1065796c8dcSSimon Schubert   for (; *string; ++string)
107a45ae5f8SJohn Marino     hash = SYMBOL_HASH_NEXT (hash, *string);
1085796c8dcSSimon Schubert   return hash;
1095796c8dcSSimon Schubert }
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE.  */
112*ef5ccd6cSJohn Marino static void
add_minsym_to_hash_table(struct minimal_symbol * sym,struct minimal_symbol ** table)1135796c8dcSSimon Schubert add_minsym_to_hash_table (struct minimal_symbol *sym,
1145796c8dcSSimon Schubert 			  struct minimal_symbol **table)
1155796c8dcSSimon Schubert {
1165796c8dcSSimon Schubert   if (sym->hash_next == NULL)
1175796c8dcSSimon Schubert     {
1185796c8dcSSimon Schubert       unsigned int hash
1195796c8dcSSimon Schubert 	= msymbol_hash (SYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;
120cf7f2e2dSJohn Marino 
1215796c8dcSSimon Schubert       sym->hash_next = table[hash];
1225796c8dcSSimon Schubert       table[hash] = sym;
1235796c8dcSSimon Schubert     }
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
1275796c8dcSSimon Schubert    TABLE.  */
1285796c8dcSSimon Schubert static void
add_minsym_to_demangled_hash_table(struct minimal_symbol * sym,struct minimal_symbol ** table)1295796c8dcSSimon Schubert add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
1305796c8dcSSimon Schubert                                   struct minimal_symbol **table)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert   if (sym->demangled_hash_next == NULL)
1335796c8dcSSimon Schubert     {
134c50c785cSJohn Marino       unsigned int hash = msymbol_hash_iw (SYMBOL_SEARCH_NAME (sym))
135c50c785cSJohn Marino 	% MINIMAL_SYMBOL_HASH_SIZE;
136cf7f2e2dSJohn Marino 
1375796c8dcSSimon Schubert       sym->demangled_hash_next = table[hash];
1385796c8dcSSimon Schubert       table[hash] = sym;
1395796c8dcSSimon Schubert     }
1405796c8dcSSimon Schubert }
1415796c8dcSSimon Schubert 
142*ef5ccd6cSJohn Marino /* See minsyms.h.  */
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert struct objfile *
msymbol_objfile(struct minimal_symbol * sym)1455796c8dcSSimon Schubert msymbol_objfile (struct minimal_symbol *sym)
1465796c8dcSSimon Schubert {
1475796c8dcSSimon Schubert   struct objfile *objf;
1485796c8dcSSimon Schubert   struct minimal_symbol *tsym;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   unsigned int hash
1515796c8dcSSimon Schubert     = msymbol_hash (SYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   for (objf = object_files; objf; objf = objf->next)
1545796c8dcSSimon Schubert     for (tsym = objf->msymbol_hash[hash]; tsym; tsym = tsym->hash_next)
1555796c8dcSSimon Schubert       if (tsym == sym)
1565796c8dcSSimon Schubert 	return objf;
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   /* We should always be able to find the objfile ...  */
1595796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
1605796c8dcSSimon Schubert }
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert /* Look through all the current minimal symbol tables and find the
1645796c8dcSSimon Schubert    first minimal symbol that matches NAME.  If OBJF is non-NULL, limit
1655796c8dcSSimon Schubert    the search to that objfile.  If SFILE is non-NULL, the only file-scope
1665796c8dcSSimon Schubert    symbols considered will be from that source file (global symbols are
1675796c8dcSSimon Schubert    still preferred).  Returns a pointer to the minimal symbol that
1685796c8dcSSimon Schubert    matches, or NULL if no match is found.
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert    Note:  One instance where there may be duplicate minimal symbols with
1715796c8dcSSimon Schubert    the same name is when the symbol tables for a shared library and the
1725796c8dcSSimon Schubert    symbol tables for an executable contain global symbols with the same
1735796c8dcSSimon Schubert    names (the dynamic linker deals with the duplication).
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert    It's also possible to have minimal symbols with different mangled
1765796c8dcSSimon Schubert    names, but identical demangled names.  For example, the GNU C++ v3
1775796c8dcSSimon Schubert    ABI requires the generation of two (or perhaps three) copies of
1785796c8dcSSimon Schubert    constructor functions --- "in-charge", "not-in-charge", and
1795796c8dcSSimon Schubert    "allocate" copies; destructors may be duplicated as well.
1805796c8dcSSimon Schubert    Obviously, there must be distinct mangled names for each of these,
1815796c8dcSSimon Schubert    but the demangled names are all the same: S::S or S::~S.  */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol(const char * name,const char * sfile,struct objfile * objf)1845796c8dcSSimon Schubert lookup_minimal_symbol (const char *name, const char *sfile,
1855796c8dcSSimon Schubert 		       struct objfile *objf)
1865796c8dcSSimon Schubert {
1875796c8dcSSimon Schubert   struct objfile *objfile;
1885796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
1895796c8dcSSimon Schubert   struct minimal_symbol *found_symbol = NULL;
1905796c8dcSSimon Schubert   struct minimal_symbol *found_file_symbol = NULL;
1915796c8dcSSimon Schubert   struct minimal_symbol *trampoline_symbol = NULL;
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
1945796c8dcSSimon Schubert   unsigned int dem_hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   int needtofreename = 0;
1975796c8dcSSimon Schubert   const char *modified_name;
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert   if (sfile != NULL)
200c50c785cSJohn Marino     sfile = lbasename (sfile);
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert   /* For C++, canonicalize the input name.  */
2035796c8dcSSimon Schubert   modified_name = name;
2045796c8dcSSimon Schubert   if (current_language->la_language == language_cplus)
2055796c8dcSSimon Schubert     {
2065796c8dcSSimon Schubert       char *cname = cp_canonicalize_string (name);
207cf7f2e2dSJohn Marino 
2085796c8dcSSimon Schubert       if (cname)
2095796c8dcSSimon Schubert 	{
2105796c8dcSSimon Schubert 	  modified_name = cname;
2115796c8dcSSimon Schubert 	  needtofreename = 1;
2125796c8dcSSimon Schubert 	}
2135796c8dcSSimon Schubert     }
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert   for (objfile = object_files;
2165796c8dcSSimon Schubert        objfile != NULL && found_symbol == NULL;
2175796c8dcSSimon Schubert        objfile = objfile->next)
2185796c8dcSSimon Schubert     {
2195796c8dcSSimon Schubert       if (objf == NULL || objf == objfile
220cf7f2e2dSJohn Marino 	  || objf == objfile->separate_debug_objfile_backlink)
2215796c8dcSSimon Schubert 	{
2225796c8dcSSimon Schubert 	  /* Do two passes: the first over the ordinary hash table,
2235796c8dcSSimon Schubert 	     and the second over the demangled hash table.  */
2245796c8dcSSimon Schubert         int pass;
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert         for (pass = 1; pass <= 2 && found_symbol == NULL; pass++)
2275796c8dcSSimon Schubert 	    {
2285796c8dcSSimon Schubert             /* Select hash list according to pass.  */
2295796c8dcSSimon Schubert             if (pass == 1)
2305796c8dcSSimon Schubert               msymbol = objfile->msymbol_hash[hash];
2315796c8dcSSimon Schubert             else
2325796c8dcSSimon Schubert               msymbol = objfile->msymbol_demangled_hash[dem_hash];
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert             while (msymbol != NULL && found_symbol == NULL)
2355796c8dcSSimon Schubert 		{
2365796c8dcSSimon Schubert 		  int match;
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert 		  if (pass == 1)
2395796c8dcSSimon Schubert 		    {
240a45ae5f8SJohn Marino 		      int (*cmp) (const char *, const char *);
241a45ae5f8SJohn Marino 
242a45ae5f8SJohn Marino 		      cmp = (case_sensitivity == case_sensitive_on
243a45ae5f8SJohn Marino 		             ? strcmp : strcasecmp);
244a45ae5f8SJohn Marino 		      match = cmp (SYMBOL_LINKAGE_NAME (msymbol),
2455796c8dcSSimon Schubert 				   modified_name) == 0;
2465796c8dcSSimon Schubert 		    }
2475796c8dcSSimon Schubert 		  else
2485796c8dcSSimon Schubert 		    {
249a45ae5f8SJohn Marino 		      /* The function respects CASE_SENSITIVITY.  */
2505796c8dcSSimon Schubert 		      match = SYMBOL_MATCHES_SEARCH_NAME (msymbol,
2515796c8dcSSimon Schubert 							  modified_name);
2525796c8dcSSimon Schubert 		    }
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert 		  if (match)
2555796c8dcSSimon Schubert 		    {
2565796c8dcSSimon Schubert                     switch (MSYMBOL_TYPE (msymbol))
2575796c8dcSSimon Schubert                       {
2585796c8dcSSimon Schubert                       case mst_file_text:
2595796c8dcSSimon Schubert                       case mst_file_data:
2605796c8dcSSimon Schubert                       case mst_file_bss:
2615796c8dcSSimon Schubert                         if (sfile == NULL
262c50c785cSJohn Marino 			    || filename_cmp (msymbol->filename, sfile) == 0)
2635796c8dcSSimon Schubert                           found_file_symbol = msymbol;
2645796c8dcSSimon Schubert                         break;
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert                       case mst_solib_trampoline:
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert                         /* If a trampoline symbol is found, we prefer to
2695796c8dcSSimon Schubert                            keep looking for the *real* symbol.  If the
2705796c8dcSSimon Schubert                            actual symbol is not found, then we'll use the
2715796c8dcSSimon Schubert                            trampoline entry.  */
2725796c8dcSSimon Schubert                         if (trampoline_symbol == NULL)
2735796c8dcSSimon Schubert                           trampoline_symbol = msymbol;
2745796c8dcSSimon Schubert                         break;
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert                       case mst_unknown:
2775796c8dcSSimon Schubert                       default:
2785796c8dcSSimon Schubert                         found_symbol = msymbol;
2795796c8dcSSimon Schubert                         break;
2805796c8dcSSimon Schubert                       }
2815796c8dcSSimon Schubert 		    }
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert                 /* Find the next symbol on the hash chain.  */
2845796c8dcSSimon Schubert                 if (pass == 1)
2855796c8dcSSimon Schubert                   msymbol = msymbol->hash_next;
2865796c8dcSSimon Schubert                 else
2875796c8dcSSimon Schubert                   msymbol = msymbol->demangled_hash_next;
2885796c8dcSSimon Schubert 		}
2895796c8dcSSimon Schubert 	    }
2905796c8dcSSimon Schubert 	}
2915796c8dcSSimon Schubert     }
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert   if (needtofreename)
2945796c8dcSSimon Schubert     xfree ((void *) modified_name);
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   /* External symbols are best.  */
2975796c8dcSSimon Schubert   if (found_symbol)
2985796c8dcSSimon Schubert     return found_symbol;
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert   /* File-local symbols are next best.  */
3015796c8dcSSimon Schubert   if (found_file_symbol)
3025796c8dcSSimon Schubert     return found_file_symbol;
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert   /* Symbols for shared library trampolines are next best.  */
3055796c8dcSSimon Schubert   if (trampoline_symbol)
3065796c8dcSSimon Schubert     return trampoline_symbol;
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert   return NULL;
3095796c8dcSSimon Schubert }
3105796c8dcSSimon Schubert 
311*ef5ccd6cSJohn Marino /* See minsyms.h.  */
312a45ae5f8SJohn Marino 
313a45ae5f8SJohn Marino void
iterate_over_minimal_symbols(struct objfile * objf,const char * name,void (* callback)(struct minimal_symbol *,void *),void * user_data)314a45ae5f8SJohn Marino iterate_over_minimal_symbols (struct objfile *objf, const char *name,
315a45ae5f8SJohn Marino 			      void (*callback) (struct minimal_symbol *,
316a45ae5f8SJohn Marino 						void *),
317a45ae5f8SJohn Marino 			      void *user_data)
318a45ae5f8SJohn Marino {
319a45ae5f8SJohn Marino   unsigned int hash;
320a45ae5f8SJohn Marino   struct minimal_symbol *iter;
321a45ae5f8SJohn Marino   int (*cmp) (const char *, const char *);
322a45ae5f8SJohn Marino 
323a45ae5f8SJohn Marino   /* The first pass is over the ordinary hash table.  */
324a45ae5f8SJohn Marino   hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
325a45ae5f8SJohn Marino   iter = objf->msymbol_hash[hash];
326a45ae5f8SJohn Marino   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
327a45ae5f8SJohn Marino   while (iter)
328a45ae5f8SJohn Marino     {
329a45ae5f8SJohn Marino       if (cmp (SYMBOL_LINKAGE_NAME (iter), name) == 0)
330a45ae5f8SJohn Marino 	(*callback) (iter, user_data);
331a45ae5f8SJohn Marino       iter = iter->hash_next;
332a45ae5f8SJohn Marino     }
333a45ae5f8SJohn Marino 
334a45ae5f8SJohn Marino   /* The second pass is over the demangled table.  */
335a45ae5f8SJohn Marino   hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
336a45ae5f8SJohn Marino   iter = objf->msymbol_demangled_hash[hash];
337a45ae5f8SJohn Marino   while (iter)
338a45ae5f8SJohn Marino     {
339a45ae5f8SJohn Marino       if (SYMBOL_MATCHES_SEARCH_NAME (iter, name))
340a45ae5f8SJohn Marino 	(*callback) (iter, user_data);
341a45ae5f8SJohn Marino       iter = iter->demangled_hash_next;
342a45ae5f8SJohn Marino     }
343a45ae5f8SJohn Marino }
344a45ae5f8SJohn Marino 
345*ef5ccd6cSJohn Marino /* See minsyms.h.  */
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol_text(const char * name,struct objfile * objf)3485796c8dcSSimon Schubert lookup_minimal_symbol_text (const char *name, struct objfile *objf)
3495796c8dcSSimon Schubert {
3505796c8dcSSimon Schubert   struct objfile *objfile;
3515796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
3525796c8dcSSimon Schubert   struct minimal_symbol *found_symbol = NULL;
3535796c8dcSSimon Schubert   struct minimal_symbol *found_file_symbol = NULL;
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   for (objfile = object_files;
3585796c8dcSSimon Schubert        objfile != NULL && found_symbol == NULL;
3595796c8dcSSimon Schubert        objfile = objfile->next)
3605796c8dcSSimon Schubert     {
3615796c8dcSSimon Schubert       if (objf == NULL || objf == objfile
362cf7f2e2dSJohn Marino 	  || objf == objfile->separate_debug_objfile_backlink)
3635796c8dcSSimon Schubert 	{
3645796c8dcSSimon Schubert 	  for (msymbol = objfile->msymbol_hash[hash];
3655796c8dcSSimon Schubert 	       msymbol != NULL && found_symbol == NULL;
3665796c8dcSSimon Schubert 	       msymbol = msymbol->hash_next)
3675796c8dcSSimon Schubert 	    {
3685796c8dcSSimon Schubert 	      if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
369c50c785cSJohn Marino 		  (MSYMBOL_TYPE (msymbol) == mst_text
370c50c785cSJohn Marino 		   || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
371c50c785cSJohn Marino 		   || MSYMBOL_TYPE (msymbol) == mst_file_text))
3725796c8dcSSimon Schubert 		{
3735796c8dcSSimon Schubert 		  switch (MSYMBOL_TYPE (msymbol))
3745796c8dcSSimon Schubert 		    {
3755796c8dcSSimon Schubert 		    case mst_file_text:
3765796c8dcSSimon Schubert 		      found_file_symbol = msymbol;
3775796c8dcSSimon Schubert 		      break;
3785796c8dcSSimon Schubert 		    default:
3795796c8dcSSimon Schubert 		      found_symbol = msymbol;
3805796c8dcSSimon Schubert 		      break;
3815796c8dcSSimon Schubert 		    }
3825796c8dcSSimon Schubert 		}
3835796c8dcSSimon Schubert 	    }
3845796c8dcSSimon Schubert 	}
3855796c8dcSSimon Schubert     }
3865796c8dcSSimon Schubert   /* External symbols are best.  */
3875796c8dcSSimon Schubert   if (found_symbol)
3885796c8dcSSimon Schubert     return found_symbol;
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert   /* File-local symbols are next best.  */
3915796c8dcSSimon Schubert   if (found_file_symbol)
3925796c8dcSSimon Schubert     return found_file_symbol;
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert   return NULL;
3955796c8dcSSimon Schubert }
3965796c8dcSSimon Schubert 
397*ef5ccd6cSJohn Marino /* See minsyms.h.  */
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol_by_pc_name(CORE_ADDR pc,const char * name,struct objfile * objf)4005796c8dcSSimon Schubert lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
4015796c8dcSSimon Schubert 				  struct objfile *objf)
4025796c8dcSSimon Schubert {
4035796c8dcSSimon Schubert   struct objfile *objfile;
4045796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   for (objfile = object_files;
4095796c8dcSSimon Schubert        objfile != NULL;
4105796c8dcSSimon Schubert        objfile = objfile->next)
4115796c8dcSSimon Schubert     {
4125796c8dcSSimon Schubert       if (objf == NULL || objf == objfile
413cf7f2e2dSJohn Marino 	  || objf == objfile->separate_debug_objfile_backlink)
4145796c8dcSSimon Schubert 	{
4155796c8dcSSimon Schubert 	  for (msymbol = objfile->msymbol_hash[hash];
4165796c8dcSSimon Schubert 	       msymbol != NULL;
4175796c8dcSSimon Schubert 	       msymbol = msymbol->hash_next)
4185796c8dcSSimon Schubert 	    {
4195796c8dcSSimon Schubert 	      if (SYMBOL_VALUE_ADDRESS (msymbol) == pc
4205796c8dcSSimon Schubert 		  && strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0)
4215796c8dcSSimon Schubert 		return msymbol;
4225796c8dcSSimon Schubert 	    }
4235796c8dcSSimon Schubert 	}
4245796c8dcSSimon Schubert     }
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert   return NULL;
4275796c8dcSSimon Schubert }
4285796c8dcSSimon Schubert 
429*ef5ccd6cSJohn Marino /* See minsyms.h.  */
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol_solib_trampoline(const char * name,struct objfile * objf)4325796c8dcSSimon Schubert lookup_minimal_symbol_solib_trampoline (const char *name,
4335796c8dcSSimon Schubert 					struct objfile *objf)
4345796c8dcSSimon Schubert {
4355796c8dcSSimon Schubert   struct objfile *objfile;
4365796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
4375796c8dcSSimon Schubert   struct minimal_symbol *found_symbol = NULL;
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert   for (objfile = object_files;
4425796c8dcSSimon Schubert        objfile != NULL && found_symbol == NULL;
4435796c8dcSSimon Schubert        objfile = objfile->next)
4445796c8dcSSimon Schubert     {
4455796c8dcSSimon Schubert       if (objf == NULL || objf == objfile
446cf7f2e2dSJohn Marino 	  || objf == objfile->separate_debug_objfile_backlink)
4475796c8dcSSimon Schubert 	{
4485796c8dcSSimon Schubert 	  for (msymbol = objfile->msymbol_hash[hash];
4495796c8dcSSimon Schubert 	       msymbol != NULL && found_symbol == NULL;
4505796c8dcSSimon Schubert 	       msymbol = msymbol->hash_next)
4515796c8dcSSimon Schubert 	    {
4525796c8dcSSimon Schubert 	      if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
4535796c8dcSSimon Schubert 		  MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
4545796c8dcSSimon Schubert 		return msymbol;
4555796c8dcSSimon Schubert 	    }
4565796c8dcSSimon Schubert 	}
4575796c8dcSSimon Schubert     }
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert   return NULL;
4605796c8dcSSimon Schubert }
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert /* Search through the minimal symbol table for each objfile and find
4635796c8dcSSimon Schubert    the symbol whose address is the largest address that is still less
4645796c8dcSSimon Schubert    than or equal to PC, and matches SECTION (which is not NULL).
4655796c8dcSSimon Schubert    Returns a pointer to the minimal symbol if such a symbol is found,
4665796c8dcSSimon Schubert    or NULL if PC is not in a suitable range.
4675796c8dcSSimon Schubert    Note that we need to look through ALL the minimal symbol tables
4685796c8dcSSimon Schubert    before deciding on the symbol that comes closest to the specified PC.
4695796c8dcSSimon Schubert    This is because objfiles can overlap, for example objfile A has .text
4705796c8dcSSimon Schubert    at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
4715796c8dcSSimon Schubert    .data at 0x40048.
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert    If WANT_TRAMPOLINE is set, prefer mst_solib_trampoline symbols when
4745796c8dcSSimon Schubert    there are text and trampoline symbols at the same address.
4755796c8dcSSimon Schubert    Otherwise prefer mst_text symbols.  */
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert static struct minimal_symbol *
lookup_minimal_symbol_by_pc_section_1(CORE_ADDR pc,struct obj_section * section,int want_trampoline)4785796c8dcSSimon Schubert lookup_minimal_symbol_by_pc_section_1 (CORE_ADDR pc,
4795796c8dcSSimon Schubert 				       struct obj_section *section,
4805796c8dcSSimon Schubert 				       int want_trampoline)
4815796c8dcSSimon Schubert {
4825796c8dcSSimon Schubert   int lo;
4835796c8dcSSimon Schubert   int hi;
4845796c8dcSSimon Schubert   int new;
4855796c8dcSSimon Schubert   struct objfile *objfile;
4865796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
4875796c8dcSSimon Schubert   struct minimal_symbol *best_symbol = NULL;
4885796c8dcSSimon Schubert   enum minimal_symbol_type want_type, other_type;
4895796c8dcSSimon Schubert 
4905796c8dcSSimon Schubert   want_type = want_trampoline ? mst_solib_trampoline : mst_text;
4915796c8dcSSimon Schubert   other_type = want_trampoline ? mst_text : mst_solib_trampoline;
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert   /* We can not require the symbol found to be in section, because
4945796c8dcSSimon Schubert      e.g. IRIX 6.5 mdebug relies on this code returning an absolute
4955796c8dcSSimon Schubert      symbol - but find_pc_section won't return an absolute section and
4965796c8dcSSimon Schubert      hence the code below would skip over absolute symbols.  We can
4975796c8dcSSimon Schubert      still take advantage of the call to find_pc_section, though - the
4985796c8dcSSimon Schubert      object file still must match.  In case we have separate debug
4995796c8dcSSimon Schubert      files, search both the file and its separate debug file.  There's
5005796c8dcSSimon Schubert      no telling which one will have the minimal symbols.  */
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert   gdb_assert (section != NULL);
5035796c8dcSSimon Schubert 
504cf7f2e2dSJohn Marino   for (objfile = section->objfile;
505cf7f2e2dSJohn Marino        objfile != NULL;
506cf7f2e2dSJohn Marino        objfile = objfile_separate_debug_iterate (section->objfile, objfile))
5075796c8dcSSimon Schubert     {
5085796c8dcSSimon Schubert       /* If this objfile has a minimal symbol table, go search it using
5095796c8dcSSimon Schubert          a binary search.  Note that a minimal symbol table always consists
5105796c8dcSSimon Schubert          of at least two symbols, a "real" symbol and the terminating
5115796c8dcSSimon Schubert          "null symbol".  If there are no real symbols, then there is no
5125796c8dcSSimon Schubert          minimal symbol table at all.  */
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert       if (objfile->minimal_symbol_count > 0)
5155796c8dcSSimon Schubert 	{
5165796c8dcSSimon Schubert 	  int best_zero_sized = -1;
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert           msymbol = objfile->msymbols;
5195796c8dcSSimon Schubert 	  lo = 0;
5205796c8dcSSimon Schubert 	  hi = objfile->minimal_symbol_count - 1;
5215796c8dcSSimon Schubert 
5225796c8dcSSimon Schubert 	  /* This code assumes that the minimal symbols are sorted by
5235796c8dcSSimon Schubert 	     ascending address values.  If the pc value is greater than or
5245796c8dcSSimon Schubert 	     equal to the first symbol's address, then some symbol in this
5255796c8dcSSimon Schubert 	     minimal symbol table is a suitable candidate for being the
5265796c8dcSSimon Schubert 	     "best" symbol.  This includes the last real symbol, for cases
5275796c8dcSSimon Schubert 	     where the pc value is larger than any address in this vector.
5285796c8dcSSimon Schubert 
5295796c8dcSSimon Schubert 	     By iterating until the address associated with the current
5305796c8dcSSimon Schubert 	     hi index (the endpoint of the test interval) is less than
5315796c8dcSSimon Schubert 	     or equal to the desired pc value, we accomplish two things:
5325796c8dcSSimon Schubert 	     (1) the case where the pc value is larger than any minimal
5335796c8dcSSimon Schubert 	     symbol address is trivially solved, (2) the address associated
5345796c8dcSSimon Schubert 	     with the hi index is always the one we want when the interation
5355796c8dcSSimon Schubert 	     terminates.  In essence, we are iterating the test interval
5365796c8dcSSimon Schubert 	     down until the pc value is pushed out of it from the high end.
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert 	     Warning: this code is trickier than it would appear at first.  */
5395796c8dcSSimon Schubert 
5405796c8dcSSimon Schubert 	  /* Should also require that pc is <= end of objfile.  FIXME!  */
5415796c8dcSSimon Schubert 	  if (pc >= SYMBOL_VALUE_ADDRESS (&msymbol[lo]))
5425796c8dcSSimon Schubert 	    {
5435796c8dcSSimon Schubert 	      while (SYMBOL_VALUE_ADDRESS (&msymbol[hi]) > pc)
5445796c8dcSSimon Schubert 		{
545c50c785cSJohn Marino 		  /* pc is still strictly less than highest address.  */
546c50c785cSJohn Marino 		  /* Note "new" will always be >= lo.  */
5475796c8dcSSimon Schubert 		  new = (lo + hi) / 2;
5485796c8dcSSimon Schubert 		  if ((SYMBOL_VALUE_ADDRESS (&msymbol[new]) >= pc) ||
5495796c8dcSSimon Schubert 		      (lo == new))
5505796c8dcSSimon Schubert 		    {
5515796c8dcSSimon Schubert 		      hi = new;
5525796c8dcSSimon Schubert 		    }
5535796c8dcSSimon Schubert 		  else
5545796c8dcSSimon Schubert 		    {
5555796c8dcSSimon Schubert 		      lo = new;
5565796c8dcSSimon Schubert 		    }
5575796c8dcSSimon Schubert 		}
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert 	      /* If we have multiple symbols at the same address, we want
5605796c8dcSSimon Schubert 	         hi to point to the last one.  That way we can find the
5615796c8dcSSimon Schubert 	         right symbol if it has an index greater than hi.  */
5625796c8dcSSimon Schubert 	      while (hi < objfile->minimal_symbol_count - 1
5635796c8dcSSimon Schubert 		     && (SYMBOL_VALUE_ADDRESS (&msymbol[hi])
5645796c8dcSSimon Schubert 			 == SYMBOL_VALUE_ADDRESS (&msymbol[hi + 1])))
5655796c8dcSSimon Schubert 		hi++;
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert 	      /* Skip various undesirable symbols.  */
5685796c8dcSSimon Schubert 	      while (hi >= 0)
5695796c8dcSSimon Schubert 		{
5705796c8dcSSimon Schubert 		  /* Skip any absolute symbols.  This is apparently
5715796c8dcSSimon Schubert 		     what adb and dbx do, and is needed for the CM-5.
5725796c8dcSSimon Schubert 		     There are two known possible problems: (1) on
5735796c8dcSSimon Schubert 		     ELF, apparently end, edata, etc. are absolute.
5745796c8dcSSimon Schubert 		     Not sure ignoring them here is a big deal, but if
5755796c8dcSSimon Schubert 		     we want to use them, the fix would go in
5765796c8dcSSimon Schubert 		     elfread.c.  (2) I think shared library entry
5775796c8dcSSimon Schubert 		     points on the NeXT are absolute.  If we want
5785796c8dcSSimon Schubert 		     special handling for this it probably should be
5795796c8dcSSimon Schubert 		     triggered by a special mst_abs_or_lib or some
5805796c8dcSSimon Schubert 		     such.  */
5815796c8dcSSimon Schubert 
5825796c8dcSSimon Schubert 		  if (MSYMBOL_TYPE (&msymbol[hi]) == mst_abs)
5835796c8dcSSimon Schubert 		    {
5845796c8dcSSimon Schubert 		      hi--;
5855796c8dcSSimon Schubert 		      continue;
5865796c8dcSSimon Schubert 		    }
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert 		  /* If SECTION was specified, skip any symbol from
5895796c8dcSSimon Schubert 		     wrong section.  */
5905796c8dcSSimon Schubert 		  if (section
5915796c8dcSSimon Schubert 		      /* Some types of debug info, such as COFF,
5925796c8dcSSimon Schubert 			 don't fill the bfd_section member, so don't
5935796c8dcSSimon Schubert 			 throw away symbols on those platforms.  */
5945796c8dcSSimon Schubert 		      && SYMBOL_OBJ_SECTION (&msymbol[hi]) != NULL
5955796c8dcSSimon Schubert 		      && (!matching_obj_sections
5965796c8dcSSimon Schubert 			  (SYMBOL_OBJ_SECTION (&msymbol[hi]), section)))
5975796c8dcSSimon Schubert 		    {
5985796c8dcSSimon Schubert 		      hi--;
5995796c8dcSSimon Schubert 		      continue;
6005796c8dcSSimon Schubert 		    }
6015796c8dcSSimon Schubert 
6025796c8dcSSimon Schubert 		  /* If we are looking for a trampoline and this is a
6035796c8dcSSimon Schubert 		     text symbol, or the other way around, check the
604a45ae5f8SJohn Marino 		     preceding symbol too.  If they are otherwise
6055796c8dcSSimon Schubert 		     identical prefer that one.  */
6065796c8dcSSimon Schubert 		  if (hi > 0
6075796c8dcSSimon Schubert 		      && MSYMBOL_TYPE (&msymbol[hi]) == other_type
6085796c8dcSSimon Schubert 		      && MSYMBOL_TYPE (&msymbol[hi - 1]) == want_type
6095796c8dcSSimon Schubert 		      && (MSYMBOL_SIZE (&msymbol[hi])
6105796c8dcSSimon Schubert 			  == MSYMBOL_SIZE (&msymbol[hi - 1]))
6115796c8dcSSimon Schubert 		      && (SYMBOL_VALUE_ADDRESS (&msymbol[hi])
6125796c8dcSSimon Schubert 			  == SYMBOL_VALUE_ADDRESS (&msymbol[hi - 1]))
6135796c8dcSSimon Schubert 		      && (SYMBOL_OBJ_SECTION (&msymbol[hi])
6145796c8dcSSimon Schubert 			  == SYMBOL_OBJ_SECTION (&msymbol[hi - 1])))
6155796c8dcSSimon Schubert 		    {
6165796c8dcSSimon Schubert 		      hi--;
6175796c8dcSSimon Schubert 		      continue;
6185796c8dcSSimon Schubert 		    }
6195796c8dcSSimon Schubert 
6205796c8dcSSimon Schubert 		  /* If the minimal symbol has a zero size, save it
6215796c8dcSSimon Schubert 		     but keep scanning backwards looking for one with
6225796c8dcSSimon Schubert 		     a non-zero size.  A zero size may mean that the
6235796c8dcSSimon Schubert 		     symbol isn't an object or function (e.g. a
6245796c8dcSSimon Schubert 		     label), or it may just mean that the size was not
6255796c8dcSSimon Schubert 		     specified.  */
6265796c8dcSSimon Schubert 		  if (MSYMBOL_SIZE (&msymbol[hi]) == 0
6275796c8dcSSimon Schubert 		      && best_zero_sized == -1)
6285796c8dcSSimon Schubert 		    {
6295796c8dcSSimon Schubert 		      best_zero_sized = hi;
6305796c8dcSSimon Schubert 		      hi--;
6315796c8dcSSimon Schubert 		      continue;
6325796c8dcSSimon Schubert 		    }
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert 		  /* If we are past the end of the current symbol, try
6355796c8dcSSimon Schubert 		     the previous symbol if it has a larger overlapping
6365796c8dcSSimon Schubert 		     size.  This happens on i686-pc-linux-gnu with glibc;
6375796c8dcSSimon Schubert 		     the nocancel variants of system calls are inside
6385796c8dcSSimon Schubert 		     the cancellable variants, but both have sizes.  */
6395796c8dcSSimon Schubert 		  if (hi > 0
6405796c8dcSSimon Schubert 		      && MSYMBOL_SIZE (&msymbol[hi]) != 0
6415796c8dcSSimon Schubert 		      && pc >= (SYMBOL_VALUE_ADDRESS (&msymbol[hi])
6425796c8dcSSimon Schubert 				+ MSYMBOL_SIZE (&msymbol[hi]))
6435796c8dcSSimon Schubert 		      && pc < (SYMBOL_VALUE_ADDRESS (&msymbol[hi - 1])
6445796c8dcSSimon Schubert 			       + MSYMBOL_SIZE (&msymbol[hi - 1])))
6455796c8dcSSimon Schubert 		    {
6465796c8dcSSimon Schubert 		      hi--;
6475796c8dcSSimon Schubert 		      continue;
6485796c8dcSSimon Schubert 		    }
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert 		  /* Otherwise, this symbol must be as good as we're going
6515796c8dcSSimon Schubert 		     to get.  */
6525796c8dcSSimon Schubert 		  break;
6535796c8dcSSimon Schubert 		}
6545796c8dcSSimon Schubert 
6555796c8dcSSimon Schubert 	      /* If HI has a zero size, and best_zero_sized is set,
6565796c8dcSSimon Schubert 		 then we had two or more zero-sized symbols; prefer
6575796c8dcSSimon Schubert 		 the first one we found (which may have a higher
6585796c8dcSSimon Schubert 		 address).  Also, if we ran off the end, be sure
6595796c8dcSSimon Schubert 		 to back up.  */
6605796c8dcSSimon Schubert 	      if (best_zero_sized != -1
6615796c8dcSSimon Schubert 		  && (hi < 0 || MSYMBOL_SIZE (&msymbol[hi]) == 0))
6625796c8dcSSimon Schubert 		hi = best_zero_sized;
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert 	      /* If the minimal symbol has a non-zero size, and this
6655796c8dcSSimon Schubert 		 PC appears to be outside the symbol's contents, then
6665796c8dcSSimon Schubert 		 refuse to use this symbol.  If we found a zero-sized
6675796c8dcSSimon Schubert 		 symbol with an address greater than this symbol's,
6685796c8dcSSimon Schubert 		 use that instead.  We assume that if symbols have
6695796c8dcSSimon Schubert 		 specified sizes, they do not overlap.  */
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert 	      if (hi >= 0
6725796c8dcSSimon Schubert 		  && MSYMBOL_SIZE (&msymbol[hi]) != 0
6735796c8dcSSimon Schubert 		  && pc >= (SYMBOL_VALUE_ADDRESS (&msymbol[hi])
6745796c8dcSSimon Schubert 			    + MSYMBOL_SIZE (&msymbol[hi])))
6755796c8dcSSimon Schubert 		{
6765796c8dcSSimon Schubert 		  if (best_zero_sized != -1)
6775796c8dcSSimon Schubert 		    hi = best_zero_sized;
6785796c8dcSSimon Schubert 		  else
6795796c8dcSSimon Schubert 		    /* Go on to the next object file.  */
6805796c8dcSSimon Schubert 		    continue;
6815796c8dcSSimon Schubert 		}
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert 	      /* The minimal symbol indexed by hi now is the best one in this
6845796c8dcSSimon Schubert 	         objfile's minimal symbol table.  See if it is the best one
6855796c8dcSSimon Schubert 	         overall.  */
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert 	      if (hi >= 0
6885796c8dcSSimon Schubert 		  && ((best_symbol == NULL) ||
6895796c8dcSSimon Schubert 		      (SYMBOL_VALUE_ADDRESS (best_symbol) <
6905796c8dcSSimon Schubert 		       SYMBOL_VALUE_ADDRESS (&msymbol[hi]))))
6915796c8dcSSimon Schubert 		{
6925796c8dcSSimon Schubert 		  best_symbol = &msymbol[hi];
6935796c8dcSSimon Schubert 		}
6945796c8dcSSimon Schubert 	    }
6955796c8dcSSimon Schubert 	}
6965796c8dcSSimon Schubert     }
6975796c8dcSSimon Schubert   return (best_symbol);
6985796c8dcSSimon Schubert }
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol_by_pc_section(CORE_ADDR pc,struct obj_section * section)7015796c8dcSSimon Schubert lookup_minimal_symbol_by_pc_section (CORE_ADDR pc, struct obj_section *section)
7025796c8dcSSimon Schubert {
7035796c8dcSSimon Schubert   if (section == NULL)
7045796c8dcSSimon Schubert     {
7055796c8dcSSimon Schubert       /* NOTE: cagney/2004-01-27: This was using find_pc_mapped_section to
7065796c8dcSSimon Schubert 	 force the section but that (well unless you're doing overlay
7075796c8dcSSimon Schubert 	 debugging) always returns NULL making the call somewhat useless.  */
7085796c8dcSSimon Schubert       section = find_pc_section (pc);
7095796c8dcSSimon Schubert       if (section == NULL)
7105796c8dcSSimon Schubert 	return NULL;
7115796c8dcSSimon Schubert     }
7125796c8dcSSimon Schubert   return lookup_minimal_symbol_by_pc_section_1 (pc, section, 0);
7135796c8dcSSimon Schubert }
7145796c8dcSSimon Schubert 
715*ef5ccd6cSJohn Marino /* See minsyms.h.  */
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert struct minimal_symbol *
lookup_minimal_symbol_by_pc(CORE_ADDR pc)7185796c8dcSSimon Schubert lookup_minimal_symbol_by_pc (CORE_ADDR pc)
7195796c8dcSSimon Schubert {
7205796c8dcSSimon Schubert   return lookup_minimal_symbol_by_pc_section (pc, NULL);
7215796c8dcSSimon Schubert }
722cf7f2e2dSJohn Marino 
723c50c785cSJohn Marino /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver.  */
724c50c785cSJohn Marino 
725c50c785cSJohn Marino int
in_gnu_ifunc_stub(CORE_ADDR pc)726c50c785cSJohn Marino in_gnu_ifunc_stub (CORE_ADDR pc)
727c50c785cSJohn Marino {
728c50c785cSJohn Marino   struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
729c50c785cSJohn Marino 
730c50c785cSJohn Marino   return msymbol && MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc;
731c50c785cSJohn Marino }
732c50c785cSJohn Marino 
733c50c785cSJohn Marino /* See elf_gnu_ifunc_resolve_addr for its real implementation.  */
734c50c785cSJohn Marino 
735c50c785cSJohn Marino static CORE_ADDR
stub_gnu_ifunc_resolve_addr(struct gdbarch * gdbarch,CORE_ADDR pc)736c50c785cSJohn Marino stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
737c50c785cSJohn Marino {
738c50c785cSJohn Marino   error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
739c50c785cSJohn Marino 	   "the ELF support compiled in."),
740c50c785cSJohn Marino 	 paddress (gdbarch, pc));
741c50c785cSJohn Marino }
742c50c785cSJohn Marino 
743c50c785cSJohn Marino /* See elf_gnu_ifunc_resolve_name for its real implementation.  */
744c50c785cSJohn Marino 
745c50c785cSJohn Marino static int
stub_gnu_ifunc_resolve_name(const char * function_name,CORE_ADDR * function_address_p)746c50c785cSJohn Marino stub_gnu_ifunc_resolve_name (const char *function_name,
747c50c785cSJohn Marino 			     CORE_ADDR *function_address_p)
748c50c785cSJohn Marino {
749c50c785cSJohn Marino   error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
750c50c785cSJohn Marino 	   "the ELF support compiled in."),
751c50c785cSJohn Marino 	 function_name);
752c50c785cSJohn Marino }
753c50c785cSJohn Marino 
754c50c785cSJohn Marino /* See elf_gnu_ifunc_resolver_stop for its real implementation.  */
755c50c785cSJohn Marino 
756c50c785cSJohn Marino static void
stub_gnu_ifunc_resolver_stop(struct breakpoint * b)757c50c785cSJohn Marino stub_gnu_ifunc_resolver_stop (struct breakpoint *b)
758c50c785cSJohn Marino {
759c50c785cSJohn Marino   internal_error (__FILE__, __LINE__,
760c50c785cSJohn Marino 		  _("elf_gnu_ifunc_resolver_stop cannot be reached."));
761c50c785cSJohn Marino }
762c50c785cSJohn Marino 
763c50c785cSJohn Marino /* See elf_gnu_ifunc_resolver_return_stop for its real implementation.  */
764c50c785cSJohn Marino 
765c50c785cSJohn Marino static void
stub_gnu_ifunc_resolver_return_stop(struct breakpoint * b)766c50c785cSJohn Marino stub_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
767c50c785cSJohn Marino {
768c50c785cSJohn Marino   internal_error (__FILE__, __LINE__,
769c50c785cSJohn Marino 		  _("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
770c50c785cSJohn Marino }
771c50c785cSJohn Marino 
772c50c785cSJohn Marino /* See elf_gnu_ifunc_fns for its real implementation.  */
773c50c785cSJohn Marino 
774c50c785cSJohn Marino static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
775c50c785cSJohn Marino {
776c50c785cSJohn Marino   stub_gnu_ifunc_resolve_addr,
777c50c785cSJohn Marino   stub_gnu_ifunc_resolve_name,
778c50c785cSJohn Marino   stub_gnu_ifunc_resolver_stop,
779c50c785cSJohn Marino   stub_gnu_ifunc_resolver_return_stop,
780c50c785cSJohn Marino };
781c50c785cSJohn Marino 
782c50c785cSJohn Marino /* A placeholder for &elf_gnu_ifunc_fns.  */
783c50c785cSJohn Marino 
784c50c785cSJohn Marino const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
785c50c785cSJohn Marino 
786*ef5ccd6cSJohn Marino /* See minsyms.h.  */
787cf7f2e2dSJohn Marino 
788cf7f2e2dSJohn Marino struct minimal_symbol *
lookup_minimal_symbol_and_objfile(const char * name,struct objfile ** objfile_p)789cf7f2e2dSJohn Marino lookup_minimal_symbol_and_objfile (const char *name,
790cf7f2e2dSJohn Marino 				   struct objfile **objfile_p)
791cf7f2e2dSJohn Marino {
792cf7f2e2dSJohn Marino   struct objfile *objfile;
793cf7f2e2dSJohn Marino   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
794cf7f2e2dSJohn Marino 
795cf7f2e2dSJohn Marino   ALL_OBJFILES (objfile)
796cf7f2e2dSJohn Marino     {
797cf7f2e2dSJohn Marino       struct minimal_symbol *msym;
798cf7f2e2dSJohn Marino 
799cf7f2e2dSJohn Marino       for (msym = objfile->msymbol_hash[hash];
800cf7f2e2dSJohn Marino 	   msym != NULL;
801cf7f2e2dSJohn Marino 	   msym = msym->hash_next)
802cf7f2e2dSJohn Marino 	{
803cf7f2e2dSJohn Marino 	  if (strcmp (SYMBOL_LINKAGE_NAME (msym), name) == 0)
804cf7f2e2dSJohn Marino 	    {
805cf7f2e2dSJohn Marino 	      *objfile_p = objfile;
806cf7f2e2dSJohn Marino 	      return msym;
807cf7f2e2dSJohn Marino 	    }
808cf7f2e2dSJohn Marino 	}
809cf7f2e2dSJohn Marino     }
810cf7f2e2dSJohn Marino 
811cf7f2e2dSJohn Marino   return 0;
812cf7f2e2dSJohn Marino }
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert /* Return leading symbol character for a BFD.  If BFD is NULL,
8165796c8dcSSimon Schubert    return the leading symbol character from the main objfile.  */
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert static int get_symbol_leading_char (bfd *);
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert static int
get_symbol_leading_char(bfd * abfd)8215796c8dcSSimon Schubert get_symbol_leading_char (bfd *abfd)
8225796c8dcSSimon Schubert {
8235796c8dcSSimon Schubert   if (abfd != NULL)
8245796c8dcSSimon Schubert     return bfd_get_symbol_leading_char (abfd);
8255796c8dcSSimon Schubert   if (symfile_objfile != NULL && symfile_objfile->obfd != NULL)
8265796c8dcSSimon Schubert     return bfd_get_symbol_leading_char (symfile_objfile->obfd);
8275796c8dcSSimon Schubert   return 0;
8285796c8dcSSimon Schubert }
8295796c8dcSSimon Schubert 
830*ef5ccd6cSJohn Marino /* See minsyms.h.  */
8315796c8dcSSimon Schubert 
8325796c8dcSSimon Schubert void
init_minimal_symbol_collection(void)8335796c8dcSSimon Schubert init_minimal_symbol_collection (void)
8345796c8dcSSimon Schubert {
8355796c8dcSSimon Schubert   msym_count = 0;
8365796c8dcSSimon Schubert   msym_bunch = NULL;
837*ef5ccd6cSJohn Marino   /* Note that presetting msym_bunch_index to BUNCH_SIZE causes the
838*ef5ccd6cSJohn Marino      first call to save a minimal symbol to allocate the memory for
839*ef5ccd6cSJohn Marino      the first bunch.  */
8405796c8dcSSimon Schubert   msym_bunch_index = BUNCH_SIZE;
8415796c8dcSSimon Schubert }
8425796c8dcSSimon Schubert 
843*ef5ccd6cSJohn Marino /* See minsyms.h.  */
844*ef5ccd6cSJohn Marino 
8455796c8dcSSimon Schubert void
prim_record_minimal_symbol(const char * name,CORE_ADDR address,enum minimal_symbol_type ms_type,struct objfile * objfile)8465796c8dcSSimon Schubert prim_record_minimal_symbol (const char *name, CORE_ADDR address,
8475796c8dcSSimon Schubert 			    enum minimal_symbol_type ms_type,
8485796c8dcSSimon Schubert 			    struct objfile *objfile)
8495796c8dcSSimon Schubert {
8505796c8dcSSimon Schubert   int section;
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert   switch (ms_type)
8535796c8dcSSimon Schubert     {
8545796c8dcSSimon Schubert     case mst_text:
855c50c785cSJohn Marino     case mst_text_gnu_ifunc:
8565796c8dcSSimon Schubert     case mst_file_text:
8575796c8dcSSimon Schubert     case mst_solib_trampoline:
8585796c8dcSSimon Schubert       section = SECT_OFF_TEXT (objfile);
8595796c8dcSSimon Schubert       break;
8605796c8dcSSimon Schubert     case mst_data:
8615796c8dcSSimon Schubert     case mst_file_data:
8625796c8dcSSimon Schubert       section = SECT_OFF_DATA (objfile);
8635796c8dcSSimon Schubert       break;
8645796c8dcSSimon Schubert     case mst_bss:
8655796c8dcSSimon Schubert     case mst_file_bss:
8665796c8dcSSimon Schubert       section = SECT_OFF_BSS (objfile);
8675796c8dcSSimon Schubert       break;
8685796c8dcSSimon Schubert     default:
8695796c8dcSSimon Schubert       section = -1;
8705796c8dcSSimon Schubert     }
8715796c8dcSSimon Schubert 
8725796c8dcSSimon Schubert   prim_record_minimal_symbol_and_info (name, address, ms_type,
8735796c8dcSSimon Schubert 				       section, NULL, objfile);
8745796c8dcSSimon Schubert }
8755796c8dcSSimon Schubert 
876*ef5ccd6cSJohn Marino /* See minsyms.h.  */
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert struct minimal_symbol *
prim_record_minimal_symbol_full(const char * name,int name_len,int copy_name,CORE_ADDR address,enum minimal_symbol_type ms_type,int section,asection * bfd_section,struct objfile * objfile)879cf7f2e2dSJohn Marino prim_record_minimal_symbol_full (const char *name, int name_len, int copy_name,
880cf7f2e2dSJohn Marino 				 CORE_ADDR address,
8815796c8dcSSimon Schubert 				 enum minimal_symbol_type ms_type,
8825796c8dcSSimon Schubert 				 int section,
8835796c8dcSSimon Schubert 				 asection *bfd_section,
8845796c8dcSSimon Schubert 				 struct objfile *objfile)
8855796c8dcSSimon Schubert {
8865796c8dcSSimon Schubert   struct obj_section *obj_section;
8875796c8dcSSimon Schubert   struct msym_bunch *new;
8885796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert   /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
8915796c8dcSSimon Schubert      the minimal symbols, because if there is also another symbol
8925796c8dcSSimon Schubert      at the same address (e.g. the first function of the file),
8935796c8dcSSimon Schubert      lookup_minimal_symbol_by_pc would have no way of getting the
8945796c8dcSSimon Schubert      right one.  */
8955796c8dcSSimon Schubert   if (ms_type == mst_file_text && name[0] == 'g'
8965796c8dcSSimon Schubert       && (strcmp (name, GCC_COMPILED_FLAG_SYMBOL) == 0
8975796c8dcSSimon Schubert 	  || strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0))
8985796c8dcSSimon Schubert     return (NULL);
8995796c8dcSSimon Schubert 
9005796c8dcSSimon Schubert   /* It's safe to strip the leading char here once, since the name
9015796c8dcSSimon Schubert      is also stored stripped in the minimal symbol table.  */
9025796c8dcSSimon Schubert   if (name[0] == get_symbol_leading_char (objfile->obfd))
903cf7f2e2dSJohn Marino     {
9045796c8dcSSimon Schubert       ++name;
905cf7f2e2dSJohn Marino       --name_len;
906cf7f2e2dSJohn Marino     }
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert   if (ms_type == mst_file_text && strncmp (name, "__gnu_compiled", 14) == 0)
9095796c8dcSSimon Schubert     return (NULL);
9105796c8dcSSimon Schubert 
9115796c8dcSSimon Schubert   if (msym_bunch_index == BUNCH_SIZE)
9125796c8dcSSimon Schubert     {
9135796c8dcSSimon Schubert       new = XCALLOC (1, struct msym_bunch);
9145796c8dcSSimon Schubert       msym_bunch_index = 0;
9155796c8dcSSimon Schubert       new->next = msym_bunch;
9165796c8dcSSimon Schubert       msym_bunch = new;
9175796c8dcSSimon Schubert     }
9185796c8dcSSimon Schubert   msymbol = &msym_bunch->contents[msym_bunch_index];
919c50c785cSJohn Marino   SYMBOL_SET_LANGUAGE (msymbol, language_auto);
920cf7f2e2dSJohn Marino   SYMBOL_SET_NAMES (msymbol, name, name_len, copy_name, objfile);
9215796c8dcSSimon Schubert 
9225796c8dcSSimon Schubert   SYMBOL_VALUE_ADDRESS (msymbol) = address;
9235796c8dcSSimon Schubert   SYMBOL_SECTION (msymbol) = section;
9245796c8dcSSimon Schubert   SYMBOL_OBJ_SECTION (msymbol) = NULL;
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert   /* Find obj_section corresponding to bfd_section.  */
9275796c8dcSSimon Schubert   if (bfd_section)
9285796c8dcSSimon Schubert     ALL_OBJFILE_OSECTIONS (objfile, obj_section)
9295796c8dcSSimon Schubert       {
9305796c8dcSSimon Schubert 	if (obj_section->the_bfd_section == bfd_section)
9315796c8dcSSimon Schubert 	  {
9325796c8dcSSimon Schubert 	    SYMBOL_OBJ_SECTION (msymbol) = obj_section;
9335796c8dcSSimon Schubert 	    break;
9345796c8dcSSimon Schubert 	  }
9355796c8dcSSimon Schubert       }
9365796c8dcSSimon Schubert 
9375796c8dcSSimon Schubert   MSYMBOL_TYPE (msymbol) = ms_type;
9385796c8dcSSimon Schubert   MSYMBOL_TARGET_FLAG_1 (msymbol) = 0;
9395796c8dcSSimon Schubert   MSYMBOL_TARGET_FLAG_2 (msymbol) = 0;
940*ef5ccd6cSJohn Marino   /* Do not use the SET_MSYMBOL_SIZE macro to initialize the size,
941*ef5ccd6cSJohn Marino      as it would also set the has_size flag.  */
942*ef5ccd6cSJohn Marino   msymbol->size = 0;
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert   /* The hash pointers must be cleared! If they're not,
9455796c8dcSSimon Schubert      add_minsym_to_hash_table will NOT add this msymbol to the hash table.  */
9465796c8dcSSimon Schubert   msymbol->hash_next = NULL;
9475796c8dcSSimon Schubert   msymbol->demangled_hash_next = NULL;
9485796c8dcSSimon Schubert 
9495796c8dcSSimon Schubert   msym_bunch_index++;
9505796c8dcSSimon Schubert   msym_count++;
9515796c8dcSSimon Schubert   OBJSTAT (objfile, n_minsyms++);
9525796c8dcSSimon Schubert   return msymbol;
9535796c8dcSSimon Schubert }
9545796c8dcSSimon Schubert 
955*ef5ccd6cSJohn Marino /* See minsyms.h.  */
956cf7f2e2dSJohn Marino 
957cf7f2e2dSJohn Marino struct minimal_symbol *
prim_record_minimal_symbol_and_info(const char * name,CORE_ADDR address,enum minimal_symbol_type ms_type,int section,asection * bfd_section,struct objfile * objfile)958cf7f2e2dSJohn Marino prim_record_minimal_symbol_and_info (const char *name, CORE_ADDR address,
959cf7f2e2dSJohn Marino 				     enum minimal_symbol_type ms_type,
960cf7f2e2dSJohn Marino 				     int section,
961cf7f2e2dSJohn Marino 				     asection *bfd_section,
962cf7f2e2dSJohn Marino 				     struct objfile *objfile)
963cf7f2e2dSJohn Marino {
964cf7f2e2dSJohn Marino   return prim_record_minimal_symbol_full (name, strlen (name), 1,
965cf7f2e2dSJohn Marino 					  address, ms_type, section,
966cf7f2e2dSJohn Marino 					  bfd_section, objfile);
967cf7f2e2dSJohn Marino }
968cf7f2e2dSJohn Marino 
9695796c8dcSSimon Schubert /* Compare two minimal symbols by address and return a signed result based
9705796c8dcSSimon Schubert    on unsigned comparisons, so that we sort into unsigned numeric order.
9715796c8dcSSimon Schubert    Within groups with the same address, sort by name.  */
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert static int
compare_minimal_symbols(const void * fn1p,const void * fn2p)9745796c8dcSSimon Schubert compare_minimal_symbols (const void *fn1p, const void *fn2p)
9755796c8dcSSimon Schubert {
9765796c8dcSSimon Schubert   const struct minimal_symbol *fn1;
9775796c8dcSSimon Schubert   const struct minimal_symbol *fn2;
9785796c8dcSSimon Schubert 
9795796c8dcSSimon Schubert   fn1 = (const struct minimal_symbol *) fn1p;
9805796c8dcSSimon Schubert   fn2 = (const struct minimal_symbol *) fn2p;
9815796c8dcSSimon Schubert 
9825796c8dcSSimon Schubert   if (SYMBOL_VALUE_ADDRESS (fn1) < SYMBOL_VALUE_ADDRESS (fn2))
9835796c8dcSSimon Schubert     {
984c50c785cSJohn Marino       return (-1);		/* addr 1 is less than addr 2.  */
9855796c8dcSSimon Schubert     }
9865796c8dcSSimon Schubert   else if (SYMBOL_VALUE_ADDRESS (fn1) > SYMBOL_VALUE_ADDRESS (fn2))
9875796c8dcSSimon Schubert     {
988c50c785cSJohn Marino       return (1);		/* addr 1 is greater than addr 2.  */
9895796c8dcSSimon Schubert     }
9905796c8dcSSimon Schubert   else
9915796c8dcSSimon Schubert     /* addrs are equal: sort by name */
9925796c8dcSSimon Schubert     {
993*ef5ccd6cSJohn Marino       const char *name1 = SYMBOL_LINKAGE_NAME (fn1);
994*ef5ccd6cSJohn Marino       const char *name2 = SYMBOL_LINKAGE_NAME (fn2);
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert       if (name1 && name2)	/* both have names */
9975796c8dcSSimon Schubert 	return strcmp (name1, name2);
9985796c8dcSSimon Schubert       else if (name2)
999c50c785cSJohn Marino 	return 1;		/* fn1 has no name, so it is "less".  */
1000c50c785cSJohn Marino       else if (name1)		/* fn2 has no name, so it is "less".  */
10015796c8dcSSimon Schubert 	return -1;
10025796c8dcSSimon Schubert       else
1003c50c785cSJohn Marino 	return (0);		/* Neither has a name, so they're equal.  */
10045796c8dcSSimon Schubert     }
10055796c8dcSSimon Schubert }
10065796c8dcSSimon Schubert 
10075796c8dcSSimon Schubert /* Discard the currently collected minimal symbols, if any.  If we wish
10085796c8dcSSimon Schubert    to save them for later use, we must have already copied them somewhere
10095796c8dcSSimon Schubert    else before calling this function.
10105796c8dcSSimon Schubert 
10115796c8dcSSimon Schubert    FIXME:  We could allocate the minimal symbol bunches on their own
10125796c8dcSSimon Schubert    obstack and then simply blow the obstack away when we are done with
10135796c8dcSSimon Schubert    it.  Is it worth the extra trouble though?  */
10145796c8dcSSimon Schubert 
10155796c8dcSSimon Schubert static void
do_discard_minimal_symbols_cleanup(void * arg)10165796c8dcSSimon Schubert do_discard_minimal_symbols_cleanup (void *arg)
10175796c8dcSSimon Schubert {
10185796c8dcSSimon Schubert   struct msym_bunch *next;
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert   while (msym_bunch != NULL)
10215796c8dcSSimon Schubert     {
10225796c8dcSSimon Schubert       next = msym_bunch->next;
10235796c8dcSSimon Schubert       xfree (msym_bunch);
10245796c8dcSSimon Schubert       msym_bunch = next;
10255796c8dcSSimon Schubert     }
10265796c8dcSSimon Schubert }
10275796c8dcSSimon Schubert 
1028*ef5ccd6cSJohn Marino /* See minsyms.h.  */
1029*ef5ccd6cSJohn Marino 
10305796c8dcSSimon Schubert struct cleanup *
make_cleanup_discard_minimal_symbols(void)10315796c8dcSSimon Schubert make_cleanup_discard_minimal_symbols (void)
10325796c8dcSSimon Schubert {
10335796c8dcSSimon Schubert   return make_cleanup (do_discard_minimal_symbols_cleanup, 0);
10345796c8dcSSimon Schubert }
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert 
10385796c8dcSSimon Schubert /* Compact duplicate entries out of a minimal symbol table by walking
10395796c8dcSSimon Schubert    through the table and compacting out entries with duplicate addresses
10405796c8dcSSimon Schubert    and matching names.  Return the number of entries remaining.
10415796c8dcSSimon Schubert 
10425796c8dcSSimon Schubert    On entry, the table resides between msymbol[0] and msymbol[mcount].
10435796c8dcSSimon Schubert    On exit, it resides between msymbol[0] and msymbol[result_count].
10445796c8dcSSimon Schubert 
10455796c8dcSSimon Schubert    When files contain multiple sources of symbol information, it is
10465796c8dcSSimon Schubert    possible for the minimal symbol table to contain many duplicate entries.
10475796c8dcSSimon Schubert    As an example, SVR4 systems use ELF formatted object files, which
10485796c8dcSSimon Schubert    usually contain at least two different types of symbol tables (a
10495796c8dcSSimon Schubert    standard ELF one and a smaller dynamic linking table), as well as
10505796c8dcSSimon Schubert    DWARF debugging information for files compiled with -g.
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert    Without compacting, the minimal symbol table for gdb itself contains
10535796c8dcSSimon Schubert    over a 1000 duplicates, about a third of the total table size.  Aside
10545796c8dcSSimon Schubert    from the potential trap of not noticing that two successive entries
10555796c8dcSSimon Schubert    identify the same location, this duplication impacts the time required
10565796c8dcSSimon Schubert    to linearly scan the table, which is done in a number of places.  So we
10575796c8dcSSimon Schubert    just do one linear scan here and toss out the duplicates.
10585796c8dcSSimon Schubert 
10595796c8dcSSimon Schubert    Note that we are not concerned here about recovering the space that
10605796c8dcSSimon Schubert    is potentially freed up, because the strings themselves are allocated
10615796c8dcSSimon Schubert    on the objfile_obstack, and will get automatically freed when the symbol
10625796c8dcSSimon Schubert    table is freed.  The caller can free up the unused minimal symbols at
10635796c8dcSSimon Schubert    the end of the compacted region if their allocation strategy allows it.
10645796c8dcSSimon Schubert 
10655796c8dcSSimon Schubert    Also note we only go up to the next to last entry within the loop
10665796c8dcSSimon Schubert    and then copy the last entry explicitly after the loop terminates.
10675796c8dcSSimon Schubert 
10685796c8dcSSimon Schubert    Since the different sources of information for each symbol may
10695796c8dcSSimon Schubert    have different levels of "completeness", we may have duplicates
10705796c8dcSSimon Schubert    that have one entry with type "mst_unknown" and the other with a
10715796c8dcSSimon Schubert    known type.  So if the one we are leaving alone has type mst_unknown,
10725796c8dcSSimon Schubert    overwrite its type with the type from the one we are compacting out.  */
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert static int
compact_minimal_symbols(struct minimal_symbol * msymbol,int mcount,struct objfile * objfile)10755796c8dcSSimon Schubert compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
10765796c8dcSSimon Schubert 			 struct objfile *objfile)
10775796c8dcSSimon Schubert {
10785796c8dcSSimon Schubert   struct minimal_symbol *copyfrom;
10795796c8dcSSimon Schubert   struct minimal_symbol *copyto;
10805796c8dcSSimon Schubert 
10815796c8dcSSimon Schubert   if (mcount > 0)
10825796c8dcSSimon Schubert     {
10835796c8dcSSimon Schubert       copyfrom = copyto = msymbol;
10845796c8dcSSimon Schubert       while (copyfrom < msymbol + mcount - 1)
10855796c8dcSSimon Schubert 	{
10865796c8dcSSimon Schubert 	  if (SYMBOL_VALUE_ADDRESS (copyfrom)
10875796c8dcSSimon Schubert 	      == SYMBOL_VALUE_ADDRESS ((copyfrom + 1))
10885796c8dcSSimon Schubert 	      && strcmp (SYMBOL_LINKAGE_NAME (copyfrom),
10895796c8dcSSimon Schubert 			 SYMBOL_LINKAGE_NAME ((copyfrom + 1))) == 0)
10905796c8dcSSimon Schubert 	    {
10915796c8dcSSimon Schubert 	      if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
10925796c8dcSSimon Schubert 		{
10935796c8dcSSimon Schubert 		  MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
10945796c8dcSSimon Schubert 		}
10955796c8dcSSimon Schubert 	      copyfrom++;
10965796c8dcSSimon Schubert 	    }
10975796c8dcSSimon Schubert 	  else
10985796c8dcSSimon Schubert 	    *copyto++ = *copyfrom++;
10995796c8dcSSimon Schubert 	}
11005796c8dcSSimon Schubert       *copyto++ = *copyfrom++;
11015796c8dcSSimon Schubert       mcount = copyto - msymbol;
11025796c8dcSSimon Schubert     }
11035796c8dcSSimon Schubert   return (mcount);
11045796c8dcSSimon Schubert }
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert /* Build (or rebuild) the minimal symbol hash tables.  This is necessary
11075796c8dcSSimon Schubert    after compacting or sorting the table since the entries move around
11085796c8dcSSimon Schubert    thus causing the internal minimal_symbol pointers to become jumbled.  */
11095796c8dcSSimon Schubert 
11105796c8dcSSimon Schubert static void
build_minimal_symbol_hash_tables(struct objfile * objfile)11115796c8dcSSimon Schubert build_minimal_symbol_hash_tables (struct objfile *objfile)
11125796c8dcSSimon Schubert {
11135796c8dcSSimon Schubert   int i;
11145796c8dcSSimon Schubert   struct minimal_symbol *msym;
11155796c8dcSSimon Schubert 
11165796c8dcSSimon Schubert   /* Clear the hash tables.  */
11175796c8dcSSimon Schubert   for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
11185796c8dcSSimon Schubert     {
11195796c8dcSSimon Schubert       objfile->msymbol_hash[i] = 0;
11205796c8dcSSimon Schubert       objfile->msymbol_demangled_hash[i] = 0;
11215796c8dcSSimon Schubert     }
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert   /* Now, (re)insert the actual entries.  */
11245796c8dcSSimon Schubert   for (i = objfile->minimal_symbol_count, msym = objfile->msymbols;
11255796c8dcSSimon Schubert        i > 0;
11265796c8dcSSimon Schubert        i--, msym++)
11275796c8dcSSimon Schubert     {
11285796c8dcSSimon Schubert       msym->hash_next = 0;
11295796c8dcSSimon Schubert       add_minsym_to_hash_table (msym, objfile->msymbol_hash);
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert       msym->demangled_hash_next = 0;
11325796c8dcSSimon Schubert       if (SYMBOL_SEARCH_NAME (msym) != SYMBOL_LINKAGE_NAME (msym))
11335796c8dcSSimon Schubert 	add_minsym_to_demangled_hash_table (msym,
11345796c8dcSSimon Schubert                                             objfile->msymbol_demangled_hash);
11355796c8dcSSimon Schubert     }
11365796c8dcSSimon Schubert }
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert /* Add the minimal symbols in the existing bunches to the objfile's official
11395796c8dcSSimon Schubert    minimal symbol table.  In most cases there is no minimal symbol table yet
11405796c8dcSSimon Schubert    for this objfile, and the existing bunches are used to create one.  Once
11415796c8dcSSimon Schubert    in a while (for shared libraries for example), we add symbols (e.g. common
11425796c8dcSSimon Schubert    symbols) to an existing objfile.
11435796c8dcSSimon Schubert 
11445796c8dcSSimon Schubert    Because of the way minimal symbols are collected, we generally have no way
11455796c8dcSSimon Schubert    of knowing what source language applies to any particular minimal symbol.
11465796c8dcSSimon Schubert    Specifically, we have no way of knowing if the minimal symbol comes from a
11475796c8dcSSimon Schubert    C++ compilation unit or not.  So for the sake of supporting cached
11485796c8dcSSimon Schubert    demangled C++ names, we have no choice but to try and demangle each new one
11495796c8dcSSimon Schubert    that comes in.  If the demangling succeeds, then we assume it is a C++
11505796c8dcSSimon Schubert    symbol and set the symbol's language and demangled name fields
11515796c8dcSSimon Schubert    appropriately.  Note that in order to avoid unnecessary demanglings, and
11525796c8dcSSimon Schubert    allocating obstack space that subsequently can't be freed for the demangled
11535796c8dcSSimon Schubert    names, we mark all newly added symbols with language_auto.  After
11545796c8dcSSimon Schubert    compaction of the minimal symbols, we go back and scan the entire minimal
11555796c8dcSSimon Schubert    symbol table looking for these new symbols.  For each new symbol we attempt
11565796c8dcSSimon Schubert    to demangle it, and if successful, record it as a language_cplus symbol
11575796c8dcSSimon Schubert    and cache the demangled form on the symbol obstack.  Symbols which don't
11585796c8dcSSimon Schubert    demangle are marked as language_unknown symbols, which inhibits future
11595796c8dcSSimon Schubert    attempts to demangle them if we later add more minimal symbols.  */
11605796c8dcSSimon Schubert 
11615796c8dcSSimon Schubert void
install_minimal_symbols(struct objfile * objfile)11625796c8dcSSimon Schubert install_minimal_symbols (struct objfile *objfile)
11635796c8dcSSimon Schubert {
11645796c8dcSSimon Schubert   int bindex;
11655796c8dcSSimon Schubert   int mcount;
11665796c8dcSSimon Schubert   struct msym_bunch *bunch;
11675796c8dcSSimon Schubert   struct minimal_symbol *msymbols;
11685796c8dcSSimon Schubert   int alloc_count;
11695796c8dcSSimon Schubert 
11705796c8dcSSimon Schubert   if (msym_count > 0)
11715796c8dcSSimon Schubert     {
1172*ef5ccd6cSJohn Marino       if (symtab_create_debug)
1173*ef5ccd6cSJohn Marino 	{
1174*ef5ccd6cSJohn Marino 	  fprintf_unfiltered (gdb_stdlog,
1175*ef5ccd6cSJohn Marino 			      "Installing %d minimal symbols of objfile %s.\n",
1176*ef5ccd6cSJohn Marino 			      msym_count, objfile->name);
1177*ef5ccd6cSJohn Marino 	}
1178*ef5ccd6cSJohn Marino 
11795796c8dcSSimon Schubert       /* Allocate enough space in the obstack, into which we will gather the
11805796c8dcSSimon Schubert          bunches of new and existing minimal symbols, sort them, and then
11815796c8dcSSimon Schubert          compact out the duplicate entries.  Once we have a final table,
11825796c8dcSSimon Schubert          we will give back the excess space.  */
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert       alloc_count = msym_count + objfile->minimal_symbol_count + 1;
11855796c8dcSSimon Schubert       obstack_blank (&objfile->objfile_obstack,
11865796c8dcSSimon Schubert 		     alloc_count * sizeof (struct minimal_symbol));
11875796c8dcSSimon Schubert       msymbols = (struct minimal_symbol *)
11885796c8dcSSimon Schubert 	obstack_base (&objfile->objfile_obstack);
11895796c8dcSSimon Schubert 
11905796c8dcSSimon Schubert       /* Copy in the existing minimal symbols, if there are any.  */
11915796c8dcSSimon Schubert 
11925796c8dcSSimon Schubert       if (objfile->minimal_symbol_count)
11935796c8dcSSimon Schubert 	memcpy ((char *) msymbols, (char *) objfile->msymbols,
11945796c8dcSSimon Schubert 	    objfile->minimal_symbol_count * sizeof (struct minimal_symbol));
11955796c8dcSSimon Schubert 
11965796c8dcSSimon Schubert       /* Walk through the list of minimal symbol bunches, adding each symbol
11975796c8dcSSimon Schubert          to the new contiguous array of symbols.  Note that we start with the
11985796c8dcSSimon Schubert          current, possibly partially filled bunch (thus we use the current
11995796c8dcSSimon Schubert          msym_bunch_index for the first bunch we copy over), and thereafter
12005796c8dcSSimon Schubert          each bunch is full.  */
12015796c8dcSSimon Schubert 
12025796c8dcSSimon Schubert       mcount = objfile->minimal_symbol_count;
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert       for (bunch = msym_bunch; bunch != NULL; bunch = bunch->next)
12055796c8dcSSimon Schubert 	{
12065796c8dcSSimon Schubert 	  for (bindex = 0; bindex < msym_bunch_index; bindex++, mcount++)
12075796c8dcSSimon Schubert 	    msymbols[mcount] = bunch->contents[bindex];
12085796c8dcSSimon Schubert 	  msym_bunch_index = BUNCH_SIZE;
12095796c8dcSSimon Schubert 	}
12105796c8dcSSimon Schubert 
12115796c8dcSSimon Schubert       /* Sort the minimal symbols by address.  */
12125796c8dcSSimon Schubert 
12135796c8dcSSimon Schubert       qsort (msymbols, mcount, sizeof (struct minimal_symbol),
12145796c8dcSSimon Schubert 	     compare_minimal_symbols);
12155796c8dcSSimon Schubert 
12165796c8dcSSimon Schubert       /* Compact out any duplicates, and free up whatever space we are
12175796c8dcSSimon Schubert          no longer using.  */
12185796c8dcSSimon Schubert 
12195796c8dcSSimon Schubert       mcount = compact_minimal_symbols (msymbols, mcount, objfile);
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert       obstack_blank (&objfile->objfile_obstack,
12225796c8dcSSimon Schubert 	       (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol));
12235796c8dcSSimon Schubert       msymbols = (struct minimal_symbol *)
12245796c8dcSSimon Schubert 	obstack_finish (&objfile->objfile_obstack);
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert       /* We also terminate the minimal symbol table with a "null symbol",
12275796c8dcSSimon Schubert          which is *not* included in the size of the table.  This makes it
12285796c8dcSSimon Schubert          easier to find the end of the table when we are handed a pointer
12295796c8dcSSimon Schubert          to some symbol in the middle of it.  Zero out the fields in the
12305796c8dcSSimon Schubert          "null symbol" allocated at the end of the array.  Note that the
12315796c8dcSSimon Schubert          symbol count does *not* include this null symbol, which is why it
12325796c8dcSSimon Schubert          is indexed by mcount and not mcount-1.  */
12335796c8dcSSimon Schubert 
1234*ef5ccd6cSJohn Marino       memset (&msymbols[mcount], 0, sizeof (struct minimal_symbol));
12355796c8dcSSimon Schubert 
12365796c8dcSSimon Schubert       /* Attach the minimal symbol table to the specified objfile.
12375796c8dcSSimon Schubert          The strings themselves are also located in the objfile_obstack
12385796c8dcSSimon Schubert          of this objfile.  */
12395796c8dcSSimon Schubert 
12405796c8dcSSimon Schubert       objfile->minimal_symbol_count = mcount;
12415796c8dcSSimon Schubert       objfile->msymbols = msymbols;
12425796c8dcSSimon Schubert 
12435796c8dcSSimon Schubert       /* Now build the hash tables; we can't do this incrementally
12445796c8dcSSimon Schubert          at an earlier point since we weren't finished with the obstack
12455796c8dcSSimon Schubert 	 yet.  (And if the msymbol obstack gets moved, all the internal
12465796c8dcSSimon Schubert 	 pointers to other msymbols need to be adjusted.)  */
12475796c8dcSSimon Schubert       build_minimal_symbol_hash_tables (objfile);
12485796c8dcSSimon Schubert     }
12495796c8dcSSimon Schubert }
12505796c8dcSSimon Schubert 
1251*ef5ccd6cSJohn Marino /* See minsyms.h.  */
1252*ef5ccd6cSJohn Marino 
1253*ef5ccd6cSJohn Marino void
terminate_minimal_symbol_table(struct objfile * objfile)1254*ef5ccd6cSJohn Marino terminate_minimal_symbol_table (struct objfile *objfile)
1255*ef5ccd6cSJohn Marino {
1256*ef5ccd6cSJohn Marino   if (! objfile->msymbols)
1257*ef5ccd6cSJohn Marino     objfile->msymbols = ((struct minimal_symbol *)
1258*ef5ccd6cSJohn Marino                          obstack_alloc (&objfile->objfile_obstack,
1259*ef5ccd6cSJohn Marino                                         sizeof (objfile->msymbols[0])));
1260*ef5ccd6cSJohn Marino 
1261*ef5ccd6cSJohn Marino   {
1262*ef5ccd6cSJohn Marino     struct minimal_symbol *m
1263*ef5ccd6cSJohn Marino       = &objfile->msymbols[objfile->minimal_symbol_count];
1264*ef5ccd6cSJohn Marino 
1265*ef5ccd6cSJohn Marino     memset (m, 0, sizeof (*m));
1266*ef5ccd6cSJohn Marino     /* Don't rely on these enumeration values being 0's.  */
1267*ef5ccd6cSJohn Marino     MSYMBOL_TYPE (m) = mst_unknown;
1268*ef5ccd6cSJohn Marino     SYMBOL_SET_LANGUAGE (m, language_unknown);
1269*ef5ccd6cSJohn Marino   }
1270*ef5ccd6cSJohn Marino }
1271*ef5ccd6cSJohn Marino 
12725796c8dcSSimon Schubert /* Sort all the minimal symbols in OBJFILE.  */
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert void
msymbols_sort(struct objfile * objfile)12755796c8dcSSimon Schubert msymbols_sort (struct objfile *objfile)
12765796c8dcSSimon Schubert {
12775796c8dcSSimon Schubert   qsort (objfile->msymbols, objfile->minimal_symbol_count,
12785796c8dcSSimon Schubert 	 sizeof (struct minimal_symbol), compare_minimal_symbols);
12795796c8dcSSimon Schubert   build_minimal_symbol_hash_tables (objfile);
12805796c8dcSSimon Schubert }
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert /* Check if PC is in a shared library trampoline code stub.
12835796c8dcSSimon Schubert    Return minimal symbol for the trampoline entry or NULL if PC is not
12845796c8dcSSimon Schubert    in a trampoline code stub.  */
12855796c8dcSSimon Schubert 
1286*ef5ccd6cSJohn Marino static struct minimal_symbol *
lookup_solib_trampoline_symbol_by_pc(CORE_ADDR pc)12875796c8dcSSimon Schubert lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
12885796c8dcSSimon Schubert {
12895796c8dcSSimon Schubert   struct obj_section *section = find_pc_section (pc);
12905796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
12915796c8dcSSimon Schubert 
12925796c8dcSSimon Schubert   if (section == NULL)
12935796c8dcSSimon Schubert     return NULL;
12945796c8dcSSimon Schubert   msymbol = lookup_minimal_symbol_by_pc_section_1 (pc, section, 1);
12955796c8dcSSimon Schubert 
12965796c8dcSSimon Schubert   if (msymbol != NULL && MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
12975796c8dcSSimon Schubert     return msymbol;
12985796c8dcSSimon Schubert   return NULL;
12995796c8dcSSimon Schubert }
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert /* If PC is in a shared library trampoline code stub, return the
13025796c8dcSSimon Schubert    address of the `real' function belonging to the stub.
13035796c8dcSSimon Schubert    Return 0 if PC is not in a trampoline code stub or if the real
13045796c8dcSSimon Schubert    function is not found in the minimal symbol table.
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert    We may fail to find the right function if a function with the
13075796c8dcSSimon Schubert    same name is defined in more than one shared library, but this
13085796c8dcSSimon Schubert    is considered bad programming style.  We could return 0 if we find
13095796c8dcSSimon Schubert    a duplicate function in case this matters someday.  */
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert CORE_ADDR
find_solib_trampoline_target(struct frame_info * frame,CORE_ADDR pc)13125796c8dcSSimon Schubert find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
13135796c8dcSSimon Schubert {
13145796c8dcSSimon Schubert   struct objfile *objfile;
13155796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
13165796c8dcSSimon Schubert   struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert   if (tsymbol != NULL)
13195796c8dcSSimon Schubert     {
13205796c8dcSSimon Schubert       ALL_MSYMBOLS (objfile, msymbol)
13215796c8dcSSimon Schubert       {
1322c50c785cSJohn Marino 	if ((MSYMBOL_TYPE (msymbol) == mst_text
1323c50c785cSJohn Marino 	    || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc)
13245796c8dcSSimon Schubert 	    && strcmp (SYMBOL_LINKAGE_NAME (msymbol),
13255796c8dcSSimon Schubert 		       SYMBOL_LINKAGE_NAME (tsymbol)) == 0)
13265796c8dcSSimon Schubert 	  return SYMBOL_VALUE_ADDRESS (msymbol);
13275796c8dcSSimon Schubert 
13285796c8dcSSimon Schubert 	/* Also handle minimal symbols pointing to function descriptors.  */
13295796c8dcSSimon Schubert 	if (MSYMBOL_TYPE (msymbol) == mst_data
13305796c8dcSSimon Schubert 	    && strcmp (SYMBOL_LINKAGE_NAME (msymbol),
13315796c8dcSSimon Schubert 		       SYMBOL_LINKAGE_NAME (tsymbol)) == 0)
13325796c8dcSSimon Schubert 	  {
13335796c8dcSSimon Schubert 	    CORE_ADDR func;
1334cf7f2e2dSJohn Marino 
13355796c8dcSSimon Schubert 	    func = gdbarch_convert_from_func_ptr_addr
13365796c8dcSSimon Schubert 		    (get_objfile_arch (objfile),
13375796c8dcSSimon Schubert 		     SYMBOL_VALUE_ADDRESS (msymbol),
13385796c8dcSSimon Schubert 		     &current_target);
13395796c8dcSSimon Schubert 
13405796c8dcSSimon Schubert 	    /* Ignore data symbols that are not function descriptors.  */
13415796c8dcSSimon Schubert 	    if (func != SYMBOL_VALUE_ADDRESS (msymbol))
13425796c8dcSSimon Schubert 	      return func;
13435796c8dcSSimon Schubert 	  }
13445796c8dcSSimon Schubert       }
13455796c8dcSSimon Schubert     }
13465796c8dcSSimon Schubert   return 0;
13475796c8dcSSimon Schubert }
1348