1 /* GDB routines for manipulating the minimal symbol tables.
2    Copyright (C) 1992-2021 Free Software Foundation, Inc.
3    Contributed by Cygnus Support, using pieces from other GDB modules.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 
21 /* This file contains support routines for creating, manipulating, and
22    destroying minimal symbol tables.
23 
24    Minimal symbol tables are used to hold some very basic information about
25    all defined global symbols (text, data, bss, abs, etc).  The only two
26    required pieces of information are the symbol's name and the address
27    associated with that symbol.
28 
29    In many cases, even if a file was compiled with no special options for
30    debugging at all, as long as was not stripped it will contain sufficient
31    information to build useful minimal symbol tables using this structure.
32 
33    Even when a file contains enough debugging information to build a full
34    symbol table, these minimal symbols are still useful for quickly mapping
35    between names and addresses, and vice versa.  They are also sometimes used
36    to figure out what full symbol table entries need to be read in.  */
37 
38 
39 #include "defs.h"
40 #include <ctype.h>
41 #include "symtab.h"
42 #include "bfd.h"
43 #include "filenames.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "demangle.h"
47 #include "value.h"
48 #include "cp-abi.h"
49 #include "target.h"
50 #include "cp-support.h"
51 #include "language.h"
52 #include "cli/cli-utils.h"
53 #include "gdbsupport/symbol.h"
54 #include <algorithm>
55 #include "safe-ctype.h"
56 #include "gdbsupport/parallel-for.h"
57 #include "inferior.h"
58 
59 #if CXX_STD_THREAD
60 #include <mutex>
61 #endif
62 
63 /* Return true if MINSYM is a cold clone symbol.
64    Recognize f.i. these symbols (mangled/demangled):
65    - _ZL3foov.cold
66      foo() [clone .cold]
67    - _ZL9do_rpo_vnP8functionP8edge_defP11bitmap_headbb.cold.138
68      do_rpo_vn(function*, edge_def*, bitmap_head*, bool, bool)	\
69        [clone .cold.138].  */
70 
71 static bool
msymbol_is_cold_clone(minimal_symbol * minsym)72 msymbol_is_cold_clone (minimal_symbol *minsym)
73 {
74   const char *name = minsym->natural_name ();
75   size_t name_len = strlen (name);
76   if (name_len < 1)
77     return false;
78 
79   const char *last = &name[name_len - 1];
80   if (*last != ']')
81     return false;
82 
83   const char *suffix = " [clone .cold";
84   size_t suffix_len = strlen (suffix);
85   const char *found = strstr (name, suffix);
86   if (found == nullptr)
87     return false;
88 
89   const char *start = &found[suffix_len];
90   if (*start == ']')
91     return true;
92 
93   if (*start != '.')
94     return false;
95 
96   const char *p;
97   for (p = start + 1; p <= last; ++p)
98     {
99       if (*p >= '0' && *p <= '9')
100 	continue;
101       break;
102     }
103 
104   if (p == last)
105     return true;
106 
107   return false;
108 }
109 
110 /* See minsyms.h.  */
111 
112 bool
msymbol_is_function(struct objfile * objfile,minimal_symbol * minsym,CORE_ADDR * func_address_p)113 msymbol_is_function (struct objfile *objfile, minimal_symbol *minsym,
114 		     CORE_ADDR *func_address_p)
115 {
116   CORE_ADDR msym_addr = MSYMBOL_VALUE_ADDRESS (objfile, minsym);
117 
118   switch (minsym->type)
119     {
120     case mst_slot_got_plt:
121     case mst_data:
122     case mst_bss:
123     case mst_abs:
124     case mst_file_data:
125     case mst_file_bss:
126     case mst_data_gnu_ifunc:
127       {
128 	struct gdbarch *gdbarch = objfile->arch ();
129 	CORE_ADDR pc = gdbarch_convert_from_func_ptr_addr
130 	  (gdbarch, msym_addr, current_inferior ()->top_target ());
131 	if (pc != msym_addr)
132 	  {
133 	    if (func_address_p != NULL)
134 	      *func_address_p = pc;
135 	    return true;
136 	  }
137 	return false;
138       }
139     case mst_file_text:
140       /* Ignore function symbol that is not a function entry.  */
141       if (msymbol_is_cold_clone (minsym))
142 	return false;
143       /* fallthru */
144     default:
145       if (func_address_p != NULL)
146 	*func_address_p = msym_addr;
147       return true;
148     }
149 }
150 
151 /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
152    At the end, copy them all into one newly allocated array.  */
153 
154 #define BUNCH_SIZE 127
155 
156 struct msym_bunch
157   {
158     struct msym_bunch *next;
159     struct minimal_symbol contents[BUNCH_SIZE];
160   };
161 
162 /* See minsyms.h.  */
163 
164 unsigned int
msymbol_hash_iw(const char * string)165 msymbol_hash_iw (const char *string)
166 {
167   unsigned int hash = 0;
168 
169   while (*string && *string != '(')
170     {
171       string = skip_spaces (string);
172       if (*string && *string != '(')
173 	{
174 	  hash = SYMBOL_HASH_NEXT (hash, *string);
175 	  ++string;
176 	}
177     }
178   return hash;
179 }
180 
181 /* See minsyms.h.  */
182 
183 unsigned int
msymbol_hash(const char * string)184 msymbol_hash (const char *string)
185 {
186   unsigned int hash = 0;
187 
188   for (; *string; ++string)
189     hash = SYMBOL_HASH_NEXT (hash, *string);
190   return hash;
191 }
192 
193 /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE.  */
194 static void
add_minsym_to_hash_table(struct minimal_symbol * sym,struct minimal_symbol ** table,unsigned int hash_value)195 add_minsym_to_hash_table (struct minimal_symbol *sym,
196 			  struct minimal_symbol **table,
197 			  unsigned int hash_value)
198 {
199   if (sym->hash_next == NULL)
200     {
201       unsigned int hash = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
202 
203       sym->hash_next = table[hash];
204       table[hash] = sym;
205     }
206 }
207 
208 /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
209    TABLE.  */
210 static void
add_minsym_to_demangled_hash_table(struct minimal_symbol * sym,struct objfile * objfile,unsigned int hash_value)211 add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
212 				    struct objfile *objfile,
213 				    unsigned int hash_value)
214 {
215   if (sym->demangled_hash_next == NULL)
216     {
217       objfile->per_bfd->demangled_hash_languages.set (sym->language ());
218 
219       struct minimal_symbol **table
220 	= objfile->per_bfd->msymbol_demangled_hash;
221       unsigned int hash_index = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
222       sym->demangled_hash_next = table[hash_index];
223       table[hash_index] = sym;
224     }
225 }
226 
227 /* Worker object for lookup_minimal_symbol.  Stores temporary results
228    while walking the symbol tables.  */
229 
230 struct found_minimal_symbols
231 {
232   /* External symbols are best.  */
233   bound_minimal_symbol external_symbol {};
234 
235   /* File-local symbols are next best.  */
236   bound_minimal_symbol file_symbol {};
237 
238   /* Symbols for shared library trampolines are next best.  */
239   bound_minimal_symbol trampoline_symbol {};
240 
241   /* Called when a symbol name matches.  Check if the minsym is a
242      better type than what we had already found, and record it in one
243      of the members fields if so.  Returns true if we collected the
244      real symbol, in which case we can stop searching.  */
245   bool maybe_collect (const char *sfile, objfile *objf,
246 		      minimal_symbol *msymbol);
247 };
248 
249 /* See declaration above.  */
250 
251 bool
maybe_collect(const char * sfile,struct objfile * objfile,minimal_symbol * msymbol)252 found_minimal_symbols::maybe_collect (const char *sfile,
253 				      struct objfile *objfile,
254 				      minimal_symbol *msymbol)
255 {
256   switch (MSYMBOL_TYPE (msymbol))
257     {
258     case mst_file_text:
259     case mst_file_data:
260     case mst_file_bss:
261       if (sfile == NULL
262 	  || filename_cmp (msymbol->filename, sfile) == 0)
263 	{
264 	  file_symbol.minsym = msymbol;
265 	  file_symbol.objfile = objfile;
266 	}
267       break;
268 
269     case mst_solib_trampoline:
270 
271       /* If a trampoline symbol is found, we prefer to keep
272 	 looking for the *real* symbol.  If the actual symbol
273 	 is not found, then we'll use the trampoline
274 	 entry.  */
275       if (trampoline_symbol.minsym == NULL)
276 	{
277 	  trampoline_symbol.minsym = msymbol;
278 	  trampoline_symbol.objfile = objfile;
279 	}
280       break;
281 
282     case mst_unknown:
283     default:
284       external_symbol.minsym = msymbol;
285       external_symbol.objfile = objfile;
286       /* We have the real symbol.  No use looking further.  */
287       return true;
288     }
289 
290   /* Keep looking.  */
291   return false;
292 }
293 
294 /* Walk the mangled name hash table, and pass each symbol whose name
295    matches LOOKUP_NAME according to NAMECMP to FOUND.  */
296 
297 static void
lookup_minimal_symbol_mangled(const char * lookup_name,const char * sfile,struct objfile * objfile,struct minimal_symbol ** table,unsigned int hash,int (* namecmp)(const char *,const char *),found_minimal_symbols & found)298 lookup_minimal_symbol_mangled (const char *lookup_name,
299 			       const char *sfile,
300 			       struct objfile *objfile,
301 			       struct minimal_symbol **table,
302 			       unsigned int hash,
303 			       int (*namecmp) (const char *, const char *),
304 			       found_minimal_symbols &found)
305 {
306   for (minimal_symbol *msymbol = table[hash];
307        msymbol != NULL;
308        msymbol = msymbol->hash_next)
309     {
310       const char *symbol_name = msymbol->linkage_name ();
311 
312       if (namecmp (symbol_name, lookup_name) == 0
313 	  && found.maybe_collect (sfile, objfile, msymbol))
314 	return;
315     }
316 }
317 
318 /* Walk the demangled name hash table, and pass each symbol whose name
319    matches LOOKUP_NAME according to MATCHER to FOUND.  */
320 
321 static void
lookup_minimal_symbol_demangled(const lookup_name_info & lookup_name,const char * sfile,struct objfile * objfile,struct minimal_symbol ** table,unsigned int hash,symbol_name_matcher_ftype * matcher,found_minimal_symbols & found)322 lookup_minimal_symbol_demangled (const lookup_name_info &lookup_name,
323 				 const char *sfile,
324 				 struct objfile *objfile,
325 				 struct minimal_symbol **table,
326 				 unsigned int hash,
327 				 symbol_name_matcher_ftype *matcher,
328 				 found_minimal_symbols &found)
329 {
330   for (minimal_symbol *msymbol = table[hash];
331        msymbol != NULL;
332        msymbol = msymbol->demangled_hash_next)
333     {
334       const char *symbol_name = msymbol->search_name ();
335 
336       if (matcher (symbol_name, lookup_name, NULL)
337 	  && found.maybe_collect (sfile, objfile, msymbol))
338 	return;
339     }
340 }
341 
342 /* Look through all the current minimal symbol tables and find the
343    first minimal symbol that matches NAME.  If OBJF is non-NULL, limit
344    the search to that objfile.  If SFILE is non-NULL, the only file-scope
345    symbols considered will be from that source file (global symbols are
346    still preferred).  Returns a pointer to the minimal symbol that
347    matches, or NULL if no match is found.
348 
349    Note:  One instance where there may be duplicate minimal symbols with
350    the same name is when the symbol tables for a shared library and the
351    symbol tables for an executable contain global symbols with the same
352    names (the dynamic linker deals with the duplication).
353 
354    It's also possible to have minimal symbols with different mangled
355    names, but identical demangled names.  For example, the GNU C++ v3
356    ABI requires the generation of two (or perhaps three) copies of
357    constructor functions --- "in-charge", "not-in-charge", and
358    "allocate" copies; destructors may be duplicated as well.
359    Obviously, there must be distinct mangled names for each of these,
360    but the demangled names are all the same: S::S or S::~S.  */
361 
362 struct bound_minimal_symbol
lookup_minimal_symbol(const char * name,const char * sfile,struct objfile * objf)363 lookup_minimal_symbol (const char *name, const char *sfile,
364 		       struct objfile *objf)
365 {
366   found_minimal_symbols found;
367 
368   unsigned int mangled_hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
369 
370   auto *mangled_cmp
371     = (case_sensitivity == case_sensitive_on
372        ? strcmp
373        : strcasecmp);
374 
375   if (sfile != NULL)
376     sfile = lbasename (sfile);
377 
378   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
379 
380   for (objfile *objfile : current_program_space->objfiles ())
381     {
382       if (found.external_symbol.minsym != NULL)
383 	break;
384 
385       if (objf == NULL || objf == objfile
386 	  || objf == objfile->separate_debug_objfile_backlink)
387 	{
388 	  if (symbol_lookup_debug)
389 	    {
390 	      fprintf_unfiltered (gdb_stdlog,
391 				  "lookup_minimal_symbol (%s, %s, %s)\n",
392 				  name, sfile != NULL ? sfile : "NULL",
393 				  objfile_debug_name (objfile));
394 	    }
395 
396 	  /* Do two passes: the first over the ordinary hash table,
397 	     and the second over the demangled hash table.  */
398 	  lookup_minimal_symbol_mangled (name, sfile, objfile,
399 					 objfile->per_bfd->msymbol_hash,
400 					 mangled_hash, mangled_cmp, found);
401 
402 	  /* If not found, try the demangled hash table.  */
403 	  if (found.external_symbol.minsym == NULL)
404 	    {
405 	      /* Once for each language in the demangled hash names
406 		 table (usually just zero or one languages).  */
407 	      for (unsigned iter = 0; iter < nr_languages; ++iter)
408 		{
409 		  if (!objfile->per_bfd->demangled_hash_languages.test (iter))
410 		    continue;
411 		  enum language lang = (enum language) iter;
412 
413 		  unsigned int hash
414 		    = (lookup_name.search_name_hash (lang)
415 		       % MINIMAL_SYMBOL_HASH_SIZE);
416 
417 		  symbol_name_matcher_ftype *match
418 		    = language_def (lang)->get_symbol_name_matcher
419 							(lookup_name);
420 		  struct minimal_symbol **msymbol_demangled_hash
421 		    = objfile->per_bfd->msymbol_demangled_hash;
422 
423 		  lookup_minimal_symbol_demangled (lookup_name, sfile, objfile,
424 						   msymbol_demangled_hash,
425 						   hash, match, found);
426 
427 		  if (found.external_symbol.minsym != NULL)
428 		    break;
429 		}
430 	    }
431 	}
432     }
433 
434   /* External symbols are best.  */
435   if (found.external_symbol.minsym != NULL)
436     {
437       if (symbol_lookup_debug)
438 	{
439 	  minimal_symbol *minsym = found.external_symbol.minsym;
440 
441 	  fprintf_unfiltered (gdb_stdlog,
442 			      "lookup_minimal_symbol (...) = %s (external)\n",
443 			      host_address_to_string (minsym));
444 	}
445       return found.external_symbol;
446     }
447 
448   /* File-local symbols are next best.  */
449   if (found.file_symbol.minsym != NULL)
450     {
451       if (symbol_lookup_debug)
452 	{
453 	  minimal_symbol *minsym = found.file_symbol.minsym;
454 
455 	  fprintf_unfiltered (gdb_stdlog,
456 			      "lookup_minimal_symbol (...) = %s (file-local)\n",
457 			      host_address_to_string (minsym));
458 	}
459       return found.file_symbol;
460     }
461 
462   /* Symbols for shared library trampolines are next best.  */
463   if (found.trampoline_symbol.minsym != NULL)
464     {
465       if (symbol_lookup_debug)
466 	{
467 	  minimal_symbol *minsym = found.trampoline_symbol.minsym;
468 
469 	  fprintf_unfiltered (gdb_stdlog,
470 			      "lookup_minimal_symbol (...) = %s (trampoline)\n",
471 			      host_address_to_string (minsym));
472 	}
473 
474       return found.trampoline_symbol;
475     }
476 
477   /* Not found.  */
478   if (symbol_lookup_debug)
479     fprintf_unfiltered (gdb_stdlog, "lookup_minimal_symbol (...) = NULL\n");
480   return {};
481 }
482 
483 /* See minsyms.h.  */
484 
485 struct bound_minimal_symbol
lookup_bound_minimal_symbol(const char * name)486 lookup_bound_minimal_symbol (const char *name)
487 {
488   return lookup_minimal_symbol (name, NULL, NULL);
489 }
490 
491 /* See gdbsupport/symbol.h.  */
492 
493 int
find_minimal_symbol_address(const char * name,CORE_ADDR * addr,struct objfile * objfile)494 find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
495 			     struct objfile *objfile)
496 {
497   struct bound_minimal_symbol sym
498     = lookup_minimal_symbol (name, NULL, objfile);
499 
500   if (sym.minsym != NULL)
501     *addr = BMSYMBOL_VALUE_ADDRESS (sym);
502 
503   return sym.minsym == NULL;
504 }
505 
506 /* Get the lookup name form best suitable for linkage name
507    matching.  */
508 
509 static const char *
linkage_name_str(const lookup_name_info & lookup_name)510 linkage_name_str (const lookup_name_info &lookup_name)
511 {
512   /* Unlike most languages (including C++), Ada uses the
513      encoded/linkage name as the search name recorded in symbols.  So
514      if debugging in Ada mode, prefer the Ada-encoded name.  This also
515      makes Ada's verbatim match syntax ("<...>") work, because
516      "lookup_name.name()" includes the "<>"s, while
517      "lookup_name.ada().lookup_name()" is the encoded name with "<>"s
518      stripped.  */
519   if (current_language->la_language == language_ada)
520     return lookup_name.ada ().lookup_name ().c_str ();
521 
522   return lookup_name.c_str ();
523 }
524 
525 /* See minsyms.h.  */
526 
527 void
iterate_over_minimal_symbols(struct objfile * objf,const lookup_name_info & lookup_name,gdb::function_view<bool (struct minimal_symbol *)> callback)528 iterate_over_minimal_symbols
529     (struct objfile *objf, const lookup_name_info &lookup_name,
530      gdb::function_view<bool (struct minimal_symbol *)> callback)
531 {
532   /* The first pass is over the ordinary hash table.  */
533     {
534       const char *name = linkage_name_str (lookup_name);
535       unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
536       auto *mangled_cmp
537 	= (case_sensitivity == case_sensitive_on
538 	   ? strcmp
539 	   : strcasecmp);
540 
541       for (minimal_symbol *iter = objf->per_bfd->msymbol_hash[hash];
542 	   iter != NULL;
543 	   iter = iter->hash_next)
544 	{
545 	  if (mangled_cmp (iter->linkage_name (), name) == 0)
546 	    if (callback (iter))
547 	      return;
548 	}
549     }
550 
551   /* The second pass is over the demangled table.  Once for each
552      language in the demangled hash names table (usually just zero or
553      one).  */
554   for (unsigned liter = 0; liter < nr_languages; ++liter)
555     {
556       if (!objf->per_bfd->demangled_hash_languages.test (liter))
557 	continue;
558 
559       enum language lang = (enum language) liter;
560       const language_defn *lang_def = language_def (lang);
561       symbol_name_matcher_ftype *name_match
562 	= lang_def->get_symbol_name_matcher (lookup_name);
563 
564       unsigned int hash
565 	= lookup_name.search_name_hash (lang) % MINIMAL_SYMBOL_HASH_SIZE;
566       for (minimal_symbol *iter = objf->per_bfd->msymbol_demangled_hash[hash];
567 	   iter != NULL;
568 	   iter = iter->demangled_hash_next)
569 	if (name_match (iter->search_name (), lookup_name, NULL))
570 	  if (callback (iter))
571 	    return;
572     }
573 }
574 
575 /* See minsyms.h.  */
576 
577 bound_minimal_symbol
lookup_minimal_symbol_linkage(const char * name,struct objfile * objf)578 lookup_minimal_symbol_linkage (const char *name, struct objfile *objf)
579 {
580   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
581 
582   for (objfile *objfile : objf->separate_debug_objfiles ())
583     {
584       for (minimal_symbol *msymbol = objfile->per_bfd->msymbol_hash[hash];
585 	   msymbol != NULL;
586 	   msymbol = msymbol->hash_next)
587 	{
588 	  if (strcmp (msymbol->linkage_name (), name) == 0
589 	      && (MSYMBOL_TYPE (msymbol) == mst_data
590 		  || MSYMBOL_TYPE (msymbol) == mst_bss))
591 	    return {msymbol, objfile};
592 	}
593     }
594 
595   return {};
596 }
597 
598 /* See minsyms.h.  */
599 
600 struct bound_minimal_symbol
lookup_minimal_symbol_text(const char * name,struct objfile * objf)601 lookup_minimal_symbol_text (const char *name, struct objfile *objf)
602 {
603   struct minimal_symbol *msymbol;
604   struct bound_minimal_symbol found_symbol = { NULL, NULL };
605   struct bound_minimal_symbol found_file_symbol = { NULL, NULL };
606 
607   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
608 
609   for (objfile *objfile : current_program_space->objfiles ())
610     {
611       if (found_symbol.minsym != NULL)
612 	break;
613 
614       if (objf == NULL || objf == objfile
615 	  || objf == objfile->separate_debug_objfile_backlink)
616 	{
617 	  for (msymbol = objfile->per_bfd->msymbol_hash[hash];
618 	       msymbol != NULL && found_symbol.minsym == NULL;
619 	       msymbol = msymbol->hash_next)
620 	    {
621 	      if (strcmp (msymbol->linkage_name (), name) == 0 &&
622 		  (MSYMBOL_TYPE (msymbol) == mst_text
623 		   || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
624 		   || MSYMBOL_TYPE (msymbol) == mst_file_text))
625 		{
626 		  switch (MSYMBOL_TYPE (msymbol))
627 		    {
628 		    case mst_file_text:
629 		      found_file_symbol.minsym = msymbol;
630 		      found_file_symbol.objfile = objfile;
631 		      break;
632 		    default:
633 		      found_symbol.minsym = msymbol;
634 		      found_symbol.objfile = objfile;
635 		      break;
636 		    }
637 		}
638 	    }
639 	}
640     }
641   /* External symbols are best.  */
642   if (found_symbol.minsym)
643     return found_symbol;
644 
645   /* File-local symbols are next best.  */
646   return found_file_symbol;
647 }
648 
649 /* See minsyms.h.  */
650 
651 struct minimal_symbol *
lookup_minimal_symbol_by_pc_name(CORE_ADDR pc,const char * name,struct objfile * objf)652 lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
653 				  struct objfile *objf)
654 {
655   struct minimal_symbol *msymbol;
656 
657   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
658 
659   for (objfile *objfile : current_program_space->objfiles ())
660     {
661       if (objf == NULL || objf == objfile
662 	  || objf == objfile->separate_debug_objfile_backlink)
663 	{
664 	  for (msymbol = objfile->per_bfd->msymbol_hash[hash];
665 	       msymbol != NULL;
666 	       msymbol = msymbol->hash_next)
667 	    {
668 	      if (MSYMBOL_VALUE_ADDRESS (objfile, msymbol) == pc
669 		  && strcmp (msymbol->linkage_name (), name) == 0)
670 		return msymbol;
671 	    }
672 	}
673     }
674 
675   return NULL;
676 }
677 
678 /* A helper function that makes *PC section-relative.  This searches
679    the sections of OBJFILE and if *PC is in a section, it subtracts
680    the section offset and returns true.  Otherwise it returns
681    false.  */
682 
683 static int
frob_address(struct objfile * objfile,CORE_ADDR * pc)684 frob_address (struct objfile *objfile, CORE_ADDR *pc)
685 {
686   struct obj_section *iter;
687 
688   ALL_OBJFILE_OSECTIONS (objfile, iter)
689     {
690       if (*pc >= iter->addr () && *pc < iter->endaddr ())
691 	{
692 	  *pc -= iter->offset ();
693 	  return 1;
694 	}
695     }
696 
697   return 0;
698 }
699 
700 /* Helper for lookup_minimal_symbol_by_pc_section.  Convert a
701    lookup_msym_prefer to a minimal_symbol_type.  */
702 
703 static minimal_symbol_type
msym_prefer_to_msym_type(lookup_msym_prefer prefer)704 msym_prefer_to_msym_type (lookup_msym_prefer prefer)
705 {
706   switch (prefer)
707     {
708     case lookup_msym_prefer::TEXT:
709       return mst_text;
710     case lookup_msym_prefer::TRAMPOLINE:
711       return mst_solib_trampoline;
712     case lookup_msym_prefer::GNU_IFUNC:
713       return mst_text_gnu_ifunc;
714     }
715 
716   /* Assert here instead of in a default switch case above so that
717      -Wswitch warns if a new enumerator is added.  */
718   gdb_assert_not_reached ("unhandled lookup_msym_prefer");
719 }
720 
721 /* See minsyms.h.
722 
723    Note that we need to look through ALL the minimal symbol tables
724    before deciding on the symbol that comes closest to the specified PC.
725    This is because objfiles can overlap, for example objfile A has .text
726    at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
727    .data at 0x40048.  */
728 
729 bound_minimal_symbol
lookup_minimal_symbol_by_pc_section(CORE_ADDR pc_in,struct obj_section * section,lookup_msym_prefer prefer,bound_minimal_symbol * previous)730 lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *section,
731 				     lookup_msym_prefer prefer,
732 				     bound_minimal_symbol *previous)
733 {
734   int lo;
735   int hi;
736   int newobj;
737   struct minimal_symbol *msymbol;
738   struct minimal_symbol *best_symbol = NULL;
739   struct objfile *best_objfile = NULL;
740   struct bound_minimal_symbol result;
741 
742   if (previous != nullptr)
743     {
744       previous->minsym = nullptr;
745       previous->objfile = nullptr;
746     }
747 
748   if (section == NULL)
749     {
750       section = find_pc_section (pc_in);
751       if (section == NULL)
752 	return {};
753     }
754 
755   minimal_symbol_type want_type = msym_prefer_to_msym_type (prefer);
756 
757   /* We can not require the symbol found to be in section, because
758      e.g. IRIX 6.5 mdebug relies on this code returning an absolute
759      symbol - but find_pc_section won't return an absolute section and
760      hence the code below would skip over absolute symbols.  We can
761      still take advantage of the call to find_pc_section, though - the
762      object file still must match.  In case we have separate debug
763      files, search both the file and its separate debug file.  There's
764      no telling which one will have the minimal symbols.  */
765 
766   gdb_assert (section != NULL);
767 
768   for (objfile *objfile : section->objfile->separate_debug_objfiles ())
769     {
770       CORE_ADDR pc = pc_in;
771 
772       /* If this objfile has a minimal symbol table, go search it
773 	 using a binary search.  */
774 
775       if (objfile->per_bfd->minimal_symbol_count > 0)
776 	{
777 	  int best_zero_sized = -1;
778 
779 	  msymbol = objfile->per_bfd->msymbols.get ();
780 	  lo = 0;
781 	  hi = objfile->per_bfd->minimal_symbol_count - 1;
782 
783 	  /* This code assumes that the minimal symbols are sorted by
784 	     ascending address values.  If the pc value is greater than or
785 	     equal to the first symbol's address, then some symbol in this
786 	     minimal symbol table is a suitable candidate for being the
787 	     "best" symbol.  This includes the last real symbol, for cases
788 	     where the pc value is larger than any address in this vector.
789 
790 	     By iterating until the address associated with the current
791 	     hi index (the endpoint of the test interval) is less than
792 	     or equal to the desired pc value, we accomplish two things:
793 	     (1) the case where the pc value is larger than any minimal
794 	     symbol address is trivially solved, (2) the address associated
795 	     with the hi index is always the one we want when the iteration
796 	     terminates.  In essence, we are iterating the test interval
797 	     down until the pc value is pushed out of it from the high end.
798 
799 	     Warning: this code is trickier than it would appear at first.  */
800 
801 	  if (frob_address (objfile, &pc)
802 	      && pc >= MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[lo]))
803 	    {
804 	      while (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]) > pc)
805 		{
806 		  /* pc is still strictly less than highest address.  */
807 		  /* Note "new" will always be >= lo.  */
808 		  newobj = (lo + hi) / 2;
809 		  if ((MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[newobj]) >= pc)
810 		      || (lo == newobj))
811 		    {
812 		      hi = newobj;
813 		    }
814 		  else
815 		    {
816 		      lo = newobj;
817 		    }
818 		}
819 
820 	      /* If we have multiple symbols at the same address, we want
821 		 hi to point to the last one.  That way we can find the
822 		 right symbol if it has an index greater than hi.  */
823 	      while (hi < objfile->per_bfd->minimal_symbol_count - 1
824 		     && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
825 			 == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi + 1])))
826 		hi++;
827 
828 	      /* Skip various undesirable symbols.  */
829 	      while (hi >= 0)
830 		{
831 		  /* Skip any absolute symbols.  This is apparently
832 		     what adb and dbx do, and is needed for the CM-5.
833 		     There are two known possible problems: (1) on
834 		     ELF, apparently end, edata, etc. are absolute.
835 		     Not sure ignoring them here is a big deal, but if
836 		     we want to use them, the fix would go in
837 		     elfread.c.  (2) I think shared library entry
838 		     points on the NeXT are absolute.  If we want
839 		     special handling for this it probably should be
840 		     triggered by a special mst_abs_or_lib or some
841 		     such.  */
842 
843 		  if (MSYMBOL_TYPE (&msymbol[hi]) == mst_abs)
844 		    {
845 		      hi--;
846 		      continue;
847 		    }
848 
849 		  /* If SECTION was specified, skip any symbol from
850 		     wrong section.  */
851 		  if (section
852 		      /* Some types of debug info, such as COFF,
853 			 don't fill the bfd_section member, so don't
854 			 throw away symbols on those platforms.  */
855 		      && msymbol[hi].obj_section (objfile) != nullptr
856 		      && (!matching_obj_sections
857 			  (msymbol[hi].obj_section (objfile),
858 			   section)))
859 		    {
860 		      hi--;
861 		      continue;
862 		    }
863 
864 		  /* If we are looking for a trampoline and this is a
865 		     text symbol, or the other way around, check the
866 		     preceding symbol too.  If they are otherwise
867 		     identical prefer that one.  */
868 		  if (hi > 0
869 		      && MSYMBOL_TYPE (&msymbol[hi]) != want_type
870 		      && MSYMBOL_TYPE (&msymbol[hi - 1]) == want_type
871 		      && (MSYMBOL_SIZE (&msymbol[hi])
872 			  == MSYMBOL_SIZE (&msymbol[hi - 1]))
873 		      && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
874 			  == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1]))
875 		      && (msymbol[hi].obj_section (objfile)
876 			  == msymbol[hi - 1].obj_section (objfile)))
877 		    {
878 		      hi--;
879 		      continue;
880 		    }
881 
882 		  /* If the minimal symbol has a zero size, save it
883 		     but keep scanning backwards looking for one with
884 		     a non-zero size.  A zero size may mean that the
885 		     symbol isn't an object or function (e.g. a
886 		     label), or it may just mean that the size was not
887 		     specified.  */
888 		  if (MSYMBOL_SIZE (&msymbol[hi]) == 0)
889 		    {
890 		      if (best_zero_sized == -1)
891 			best_zero_sized = hi;
892 		      hi--;
893 		      continue;
894 		    }
895 
896 		  /* If we are past the end of the current symbol, try
897 		     the previous symbol if it has a larger overlapping
898 		     size.  This happens on i686-pc-linux-gnu with glibc;
899 		     the nocancel variants of system calls are inside
900 		     the cancellable variants, but both have sizes.  */
901 		  if (hi > 0
902 		      && MSYMBOL_SIZE (&msymbol[hi]) != 0
903 		      && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
904 				+ MSYMBOL_SIZE (&msymbol[hi]))
905 		      && pc < (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1])
906 			       + MSYMBOL_SIZE (&msymbol[hi - 1])))
907 		    {
908 		      hi--;
909 		      continue;
910 		    }
911 
912 		  /* Otherwise, this symbol must be as good as we're going
913 		     to get.  */
914 		  break;
915 		}
916 
917 	      /* If HI has a zero size, and best_zero_sized is set,
918 		 then we had two or more zero-sized symbols; prefer
919 		 the first one we found (which may have a higher
920 		 address).  Also, if we ran off the end, be sure
921 		 to back up.  */
922 	      if (best_zero_sized != -1
923 		  && (hi < 0 || MSYMBOL_SIZE (&msymbol[hi]) == 0))
924 		hi = best_zero_sized;
925 
926 	      /* If the minimal symbol has a non-zero size, and this
927 		 PC appears to be outside the symbol's contents, then
928 		 refuse to use this symbol.  If we found a zero-sized
929 		 symbol with an address greater than this symbol's,
930 		 use that instead.  We assume that if symbols have
931 		 specified sizes, they do not overlap.  */
932 
933 	      if (hi >= 0
934 		  && MSYMBOL_SIZE (&msymbol[hi]) != 0
935 		  && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
936 			    + MSYMBOL_SIZE (&msymbol[hi])))
937 		{
938 		  if (best_zero_sized != -1)
939 		    hi = best_zero_sized;
940 		  else
941 		    {
942 		      /* If needed record this symbol as the closest
943 			 previous symbol.  */
944 		      if (previous != nullptr)
945 			{
946 			  if (previous->minsym == nullptr
947 			      || (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
948 				  > MSYMBOL_VALUE_RAW_ADDRESS
949 					(previous->minsym)))
950 			    {
951 			      previous->minsym = &msymbol[hi];
952 			      previous->objfile = objfile;
953 			    }
954 			}
955 		      /* Go on to the next object file.  */
956 		      continue;
957 		    }
958 		}
959 
960 	      /* The minimal symbol indexed by hi now is the best one in this
961 		 objfile's minimal symbol table.  See if it is the best one
962 		 overall.  */
963 
964 	      if (hi >= 0
965 		  && ((best_symbol == NULL) ||
966 		      (MSYMBOL_VALUE_RAW_ADDRESS (best_symbol) <
967 		       MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]))))
968 		{
969 		  best_symbol = &msymbol[hi];
970 		  best_objfile = objfile;
971 		}
972 	    }
973 	}
974     }
975 
976   result.minsym = best_symbol;
977   result.objfile = best_objfile;
978   return result;
979 }
980 
981 /* See minsyms.h.  */
982 
983 struct bound_minimal_symbol
lookup_minimal_symbol_by_pc(CORE_ADDR pc)984 lookup_minimal_symbol_by_pc (CORE_ADDR pc)
985 {
986   return lookup_minimal_symbol_by_pc_section (pc, NULL);
987 }
988 
989 /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver.  */
990 
991 bool
in_gnu_ifunc_stub(CORE_ADDR pc)992 in_gnu_ifunc_stub (CORE_ADDR pc)
993 {
994   bound_minimal_symbol msymbol
995     = lookup_minimal_symbol_by_pc_section (pc, NULL,
996 					   lookup_msym_prefer::GNU_IFUNC);
997   return msymbol.minsym && MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc;
998 }
999 
1000 /* See elf_gnu_ifunc_resolve_addr for its real implementation.  */
1001 
1002 static CORE_ADDR
stub_gnu_ifunc_resolve_addr(struct gdbarch * gdbarch,CORE_ADDR pc)1003 stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
1004 {
1005   error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
1006 	   "the ELF support compiled in."),
1007 	 paddress (gdbarch, pc));
1008 }
1009 
1010 /* See elf_gnu_ifunc_resolve_name for its real implementation.  */
1011 
1012 static bool
stub_gnu_ifunc_resolve_name(const char * function_name,CORE_ADDR * function_address_p)1013 stub_gnu_ifunc_resolve_name (const char *function_name,
1014 			     CORE_ADDR *function_address_p)
1015 {
1016   error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
1017 	   "the ELF support compiled in."),
1018 	 function_name);
1019 }
1020 
1021 /* See elf_gnu_ifunc_resolver_stop for its real implementation.  */
1022 
1023 static void
stub_gnu_ifunc_resolver_stop(struct breakpoint * b)1024 stub_gnu_ifunc_resolver_stop (struct breakpoint *b)
1025 {
1026   internal_error (__FILE__, __LINE__,
1027 		  _("elf_gnu_ifunc_resolver_stop cannot be reached."));
1028 }
1029 
1030 /* See elf_gnu_ifunc_resolver_return_stop for its real implementation.  */
1031 
1032 static void
stub_gnu_ifunc_resolver_return_stop(struct breakpoint * b)1033 stub_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
1034 {
1035   internal_error (__FILE__, __LINE__,
1036 		  _("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
1037 }
1038 
1039 /* See elf_gnu_ifunc_fns for its real implementation.  */
1040 
1041 static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
1042 {
1043   stub_gnu_ifunc_resolve_addr,
1044   stub_gnu_ifunc_resolve_name,
1045   stub_gnu_ifunc_resolver_stop,
1046   stub_gnu_ifunc_resolver_return_stop,
1047 };
1048 
1049 /* A placeholder for &elf_gnu_ifunc_fns.  */
1050 
1051 const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
1052 
1053 
1054 
1055 /* Return leading symbol character for a BFD.  If BFD is NULL,
1056    return the leading symbol character from the main objfile.  */
1057 
1058 static int
get_symbol_leading_char(bfd * abfd)1059 get_symbol_leading_char (bfd *abfd)
1060 {
1061   if (abfd != NULL)
1062     return bfd_get_symbol_leading_char (abfd);
1063   if (current_program_space->symfile_object_file != NULL)
1064     {
1065       objfile *objf = current_program_space->symfile_object_file;
1066       if (objf->obfd != NULL)
1067 	return bfd_get_symbol_leading_char (objf->obfd);
1068     }
1069   return 0;
1070 }
1071 
1072 /* See minsyms.h.  */
1073 
minimal_symbol_reader(struct objfile * obj)1074 minimal_symbol_reader::minimal_symbol_reader (struct objfile *obj)
1075 : m_objfile (obj),
1076   m_msym_bunch (NULL),
1077   /* Note that presetting m_msym_bunch_index to BUNCH_SIZE causes the
1078      first call to save a minimal symbol to allocate the memory for
1079      the first bunch.  */
1080   m_msym_bunch_index (BUNCH_SIZE),
1081   m_msym_count (0)
1082 {
1083 }
1084 
1085 /* Discard the currently collected minimal symbols, if any.  If we wish
1086    to save them for later use, we must have already copied them somewhere
1087    else before calling this function.  */
1088 
~minimal_symbol_reader()1089 minimal_symbol_reader::~minimal_symbol_reader ()
1090 {
1091   struct msym_bunch *next;
1092 
1093   while (m_msym_bunch != NULL)
1094     {
1095       next = m_msym_bunch->next;
1096       xfree (m_msym_bunch);
1097       m_msym_bunch = next;
1098     }
1099 }
1100 
1101 /* See minsyms.h.  */
1102 
1103 void
record(const char * name,CORE_ADDR address,enum minimal_symbol_type ms_type)1104 minimal_symbol_reader::record (const char *name, CORE_ADDR address,
1105 			       enum minimal_symbol_type ms_type)
1106 {
1107   int section;
1108 
1109   switch (ms_type)
1110     {
1111     case mst_text:
1112     case mst_text_gnu_ifunc:
1113     case mst_file_text:
1114     case mst_solib_trampoline:
1115       section = SECT_OFF_TEXT (m_objfile);
1116       break;
1117     case mst_data:
1118     case mst_data_gnu_ifunc:
1119     case mst_file_data:
1120       section = SECT_OFF_DATA (m_objfile);
1121       break;
1122     case mst_bss:
1123     case mst_file_bss:
1124       section = SECT_OFF_BSS (m_objfile);
1125       break;
1126     default:
1127       section = -1;
1128     }
1129 
1130   record_with_info (name, address, ms_type, section);
1131 }
1132 
1133 /* Convert an enumerator of type minimal_symbol_type to its string
1134    representation.  */
1135 
1136 static const char *
mst_str(minimal_symbol_type t)1137 mst_str (minimal_symbol_type t)
1138 {
1139 #define MST_TO_STR(x) case x: return #x;
1140   switch (t)
1141   {
1142     MST_TO_STR (mst_unknown);
1143     MST_TO_STR (mst_text);
1144     MST_TO_STR (mst_text_gnu_ifunc);
1145     MST_TO_STR (mst_slot_got_plt);
1146     MST_TO_STR (mst_data);
1147     MST_TO_STR (mst_bss);
1148     MST_TO_STR (mst_abs);
1149     MST_TO_STR (mst_solib_trampoline);
1150     MST_TO_STR (mst_file_text);
1151     MST_TO_STR (mst_file_data);
1152     MST_TO_STR (mst_file_bss);
1153 
1154     default:
1155       return "mst_???";
1156   }
1157 #undef MST_TO_STR
1158 }
1159 
1160 /* See minsyms.h.  */
1161 
1162 struct minimal_symbol *
record_full(gdb::string_view name,bool copy_name,CORE_ADDR address,enum minimal_symbol_type ms_type,int section)1163 minimal_symbol_reader::record_full (gdb::string_view name,
1164 				    bool copy_name, CORE_ADDR address,
1165 				    enum minimal_symbol_type ms_type,
1166 				    int section)
1167 {
1168   struct msym_bunch *newobj;
1169   struct minimal_symbol *msymbol;
1170 
1171   /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
1172      the minimal symbols, because if there is also another symbol
1173      at the same address (e.g. the first function of the file),
1174      lookup_minimal_symbol_by_pc would have no way of getting the
1175      right one.  */
1176   if (ms_type == mst_file_text && name[0] == 'g'
1177       && (name == GCC_COMPILED_FLAG_SYMBOL
1178 	  || name == GCC2_COMPILED_FLAG_SYMBOL))
1179     return (NULL);
1180 
1181   /* It's safe to strip the leading char here once, since the name
1182      is also stored stripped in the minimal symbol table.  */
1183   if (name[0] == get_symbol_leading_char (m_objfile->obfd))
1184     name = name.substr (1);
1185 
1186   if (ms_type == mst_file_text && startswith (name, "__gnu_compiled"))
1187     return (NULL);
1188 
1189   if (symtab_create_debug >= 2)
1190     printf_unfiltered ("Recording minsym:  %-21s  %18s  %4d  %.*s\n",
1191 	       mst_str (ms_type), hex_string (address), section,
1192 	       (int) name.size (), name.data ());
1193 
1194   if (m_msym_bunch_index == BUNCH_SIZE)
1195     {
1196       newobj = XCNEW (struct msym_bunch);
1197       m_msym_bunch_index = 0;
1198       newobj->next = m_msym_bunch;
1199       m_msym_bunch = newobj;
1200     }
1201   msymbol = &m_msym_bunch->contents[m_msym_bunch_index];
1202   msymbol->set_language (language_auto,
1203 			 &m_objfile->per_bfd->storage_obstack);
1204 
1205   if (copy_name)
1206     msymbol->m_name = obstack_strndup (&m_objfile->per_bfd->storage_obstack,
1207 				       name.data (), name.size ());
1208   else
1209     msymbol->m_name = name.data ();
1210 
1211   SET_MSYMBOL_VALUE_ADDRESS (msymbol, address);
1212   msymbol->set_section_index (section);
1213 
1214   MSYMBOL_TYPE (msymbol) = ms_type;
1215 
1216   /* If we already read minimal symbols for this objfile, then don't
1217      ever allocate a new one.  */
1218   if (!m_objfile->per_bfd->minsyms_read)
1219     {
1220       m_msym_bunch_index++;
1221       m_objfile->per_bfd->n_minsyms++;
1222     }
1223   m_msym_count++;
1224   return msymbol;
1225 }
1226 
1227 /* Compare two minimal symbols by address and return true if FN1's address
1228    is less than FN2's, so that we sort into unsigned numeric order.
1229    Within groups with the same address, sort by name.  */
1230 
1231 static inline bool
minimal_symbol_is_less_than(const minimal_symbol & fn1,const minimal_symbol & fn2)1232 minimal_symbol_is_less_than (const minimal_symbol &fn1,
1233 			     const minimal_symbol &fn2)
1234 {
1235   if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) < MSYMBOL_VALUE_RAW_ADDRESS (&fn2))
1236     {
1237       return true;		/* addr 1 is less than addr 2.  */
1238     }
1239   else if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) > MSYMBOL_VALUE_RAW_ADDRESS (&fn2))
1240     {
1241       return false;		/* addr 1 is greater than addr 2.  */
1242     }
1243   else
1244     /* addrs are equal: sort by name */
1245     {
1246       const char *name1 = fn1.linkage_name ();
1247       const char *name2 = fn2.linkage_name ();
1248 
1249       if (name1 && name2)	/* both have names */
1250 	return strcmp (name1, name2) < 0;
1251       else if (name2)
1252 	return true;		/* fn1 has no name, so it is "less".  */
1253       else if (name1)		/* fn2 has no name, so it is "less".  */
1254 	return false;
1255       else
1256 	return false;		/* Neither has a name, so they're equal.  */
1257     }
1258 }
1259 
1260 /* Compact duplicate entries out of a minimal symbol table by walking
1261    through the table and compacting out entries with duplicate addresses
1262    and matching names.  Return the number of entries remaining.
1263 
1264    On entry, the table resides between msymbol[0] and msymbol[mcount].
1265    On exit, it resides between msymbol[0] and msymbol[result_count].
1266 
1267    When files contain multiple sources of symbol information, it is
1268    possible for the minimal symbol table to contain many duplicate entries.
1269    As an example, SVR4 systems use ELF formatted object files, which
1270    usually contain at least two different types of symbol tables (a
1271    standard ELF one and a smaller dynamic linking table), as well as
1272    DWARF debugging information for files compiled with -g.
1273 
1274    Without compacting, the minimal symbol table for gdb itself contains
1275    over a 1000 duplicates, about a third of the total table size.  Aside
1276    from the potential trap of not noticing that two successive entries
1277    identify the same location, this duplication impacts the time required
1278    to linearly scan the table, which is done in a number of places.  So we
1279    just do one linear scan here and toss out the duplicates.
1280 
1281    Since the different sources of information for each symbol may
1282    have different levels of "completeness", we may have duplicates
1283    that have one entry with type "mst_unknown" and the other with a
1284    known type.  So if the one we are leaving alone has type mst_unknown,
1285    overwrite its type with the type from the one we are compacting out.  */
1286 
1287 static int
compact_minimal_symbols(struct minimal_symbol * msymbol,int mcount,struct objfile * objfile)1288 compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
1289 			 struct objfile *objfile)
1290 {
1291   struct minimal_symbol *copyfrom;
1292   struct minimal_symbol *copyto;
1293 
1294   if (mcount > 0)
1295     {
1296       copyfrom = copyto = msymbol;
1297       while (copyfrom < msymbol + mcount - 1)
1298 	{
1299 	  if (MSYMBOL_VALUE_RAW_ADDRESS (copyfrom)
1300 	      == MSYMBOL_VALUE_RAW_ADDRESS ((copyfrom + 1))
1301 	      && (copyfrom->section_index ()
1302 		  == (copyfrom + 1)->section_index ())
1303 	      && strcmp (copyfrom->linkage_name (),
1304 			 (copyfrom + 1)->linkage_name ()) == 0)
1305 	    {
1306 	      if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
1307 		{
1308 		  MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
1309 		}
1310 	      copyfrom++;
1311 	    }
1312 	  else
1313 	    *copyto++ = *copyfrom++;
1314 	}
1315       *copyto++ = *copyfrom++;
1316       mcount = copyto - msymbol;
1317     }
1318   return (mcount);
1319 }
1320 
1321 static void
clear_minimal_symbol_hash_tables(struct objfile * objfile)1322 clear_minimal_symbol_hash_tables (struct objfile *objfile)
1323 {
1324   for (size_t i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
1325     {
1326       objfile->per_bfd->msymbol_hash[i] = 0;
1327       objfile->per_bfd->msymbol_demangled_hash[i] = 0;
1328     }
1329 }
1330 
1331 /* This struct is used to store values we compute for msymbols on the
1332    background threads but don't need to keep around long term.  */
1333 struct computed_hash_values
1334 {
1335   /* Length of the linkage_name of the symbol.  */
1336   size_t name_length;
1337   /* Hash code (using fast_hash) of the linkage_name.  */
1338   hashval_t mangled_name_hash;
1339   /* The msymbol_hash of the linkage_name.  */
1340   unsigned int minsym_hash;
1341   /* The msymbol_hash of the search_name.  */
1342   unsigned int minsym_demangled_hash;
1343 };
1344 
1345 /* Build (or rebuild) the minimal symbol hash tables.  This is necessary
1346    after compacting or sorting the table since the entries move around
1347    thus causing the internal minimal_symbol pointers to become jumbled.  */
1348 
1349 static void
build_minimal_symbol_hash_tables(struct objfile * objfile,const std::vector<computed_hash_values> & hash_values)1350 build_minimal_symbol_hash_tables
1351   (struct objfile *objfile,
1352    const std::vector<computed_hash_values>& hash_values)
1353 {
1354   int i;
1355   struct minimal_symbol *msym;
1356 
1357   /* (Re)insert the actual entries.  */
1358   int mcount = objfile->per_bfd->minimal_symbol_count;
1359   for ((i = 0,
1360 	msym = objfile->per_bfd->msymbols.get ());
1361        i < mcount;
1362        i++, msym++)
1363     {
1364       msym->hash_next = 0;
1365       add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash,
1366 				hash_values[i].minsym_hash);
1367 
1368       msym->demangled_hash_next = 0;
1369       if (msym->search_name () != msym->linkage_name ())
1370 	add_minsym_to_demangled_hash_table
1371 	  (msym, objfile, hash_values[i].minsym_demangled_hash);
1372     }
1373 }
1374 
1375 /* Add the minimal symbols in the existing bunches to the objfile's official
1376    minimal symbol table.  In most cases there is no minimal symbol table yet
1377    for this objfile, and the existing bunches are used to create one.  Once
1378    in a while (for shared libraries for example), we add symbols (e.g. common
1379    symbols) to an existing objfile.  */
1380 
1381 void
install()1382 minimal_symbol_reader::install ()
1383 {
1384   int mcount;
1385   struct msym_bunch *bunch;
1386   struct minimal_symbol *msymbols;
1387   int alloc_count;
1388 
1389   if (m_objfile->per_bfd->minsyms_read)
1390     return;
1391 
1392   if (m_msym_count > 0)
1393     {
1394       if (symtab_create_debug)
1395 	{
1396 	  fprintf_unfiltered (gdb_stdlog,
1397 			      "Installing %d minimal symbols of objfile %s.\n",
1398 			      m_msym_count, objfile_name (m_objfile));
1399 	}
1400 
1401       /* Allocate enough space, into which we will gather the bunches
1402 	 of new and existing minimal symbols, sort them, and then
1403 	 compact out the duplicate entries.  Once we have a final
1404 	 table, we will give back the excess space.  */
1405 
1406       alloc_count = m_msym_count + m_objfile->per_bfd->minimal_symbol_count;
1407       gdb::unique_xmalloc_ptr<minimal_symbol>
1408 	msym_holder (XNEWVEC (minimal_symbol, alloc_count));
1409       msymbols = msym_holder.get ();
1410 
1411       /* Copy in the existing minimal symbols, if there are any.  */
1412 
1413       if (m_objfile->per_bfd->minimal_symbol_count)
1414 	memcpy (msymbols, m_objfile->per_bfd->msymbols.get (),
1415 		m_objfile->per_bfd->minimal_symbol_count
1416 		* sizeof (struct minimal_symbol));
1417 
1418       /* Walk through the list of minimal symbol bunches, adding each symbol
1419 	 to the new contiguous array of symbols.  Note that we start with the
1420 	 current, possibly partially filled bunch (thus we use the current
1421 	 msym_bunch_index for the first bunch we copy over), and thereafter
1422 	 each bunch is full.  */
1423 
1424       mcount = m_objfile->per_bfd->minimal_symbol_count;
1425 
1426       for (bunch = m_msym_bunch; bunch != NULL; bunch = bunch->next)
1427 	{
1428 	  memcpy (&msymbols[mcount], &bunch->contents[0],
1429 		  m_msym_bunch_index * sizeof (struct minimal_symbol));
1430 	  mcount += m_msym_bunch_index;
1431 	  m_msym_bunch_index = BUNCH_SIZE;
1432 	}
1433 
1434       /* Sort the minimal symbols by address.  */
1435 
1436       std::sort (msymbols, msymbols + mcount, minimal_symbol_is_less_than);
1437 
1438       /* Compact out any duplicates, and free up whatever space we are
1439 	 no longer using.  */
1440 
1441       mcount = compact_minimal_symbols (msymbols, mcount, m_objfile);
1442       msym_holder.reset (XRESIZEVEC (struct minimal_symbol,
1443 				     msym_holder.release (),
1444 				     mcount));
1445 
1446       /* Attach the minimal symbol table to the specified objfile.
1447 	 The strings themselves are also located in the storage_obstack
1448 	 of this objfile.  */
1449 
1450       if (m_objfile->per_bfd->minimal_symbol_count != 0)
1451 	clear_minimal_symbol_hash_tables (m_objfile);
1452 
1453       m_objfile->per_bfd->minimal_symbol_count = mcount;
1454       m_objfile->per_bfd->msymbols = std::move (msym_holder);
1455 
1456 #if CXX_STD_THREAD
1457       /* Mutex that is used when modifying or accessing the demangled
1458 	 hash table.  */
1459       std::mutex demangled_mutex;
1460 #endif
1461 
1462       std::vector<computed_hash_values> hash_values (mcount);
1463 
1464       msymbols = m_objfile->per_bfd->msymbols.get ();
1465       gdb::parallel_for_each
1466 	(&msymbols[0], &msymbols[mcount],
1467 	 [&] (minimal_symbol *start, minimal_symbol *end)
1468 	 {
1469 	   for (minimal_symbol *msym = start; msym < end; ++msym)
1470 	     {
1471 	       size_t idx = msym - msymbols;
1472 	       hash_values[idx].name_length = strlen (msym->linkage_name ());
1473 	       if (!msym->name_set)
1474 		 {
1475 		   /* This will be freed later, by compute_and_set_names.  */
1476 		   char *demangled_name
1477 		     = symbol_find_demangled_name (msym, msym->linkage_name ());
1478 		   msym->set_demangled_name
1479 		     (demangled_name, &m_objfile->per_bfd->storage_obstack);
1480 		   msym->name_set = 1;
1481 		 }
1482 	       /* This mangled_name_hash computation has to be outside of
1483 		  the name_set check, or compute_and_set_names below will
1484 		  be called with an invalid hash value.  */
1485 	       hash_values[idx].mangled_name_hash
1486 		 = fast_hash (msym->linkage_name (),
1487 			      hash_values[idx].name_length);
1488 	       hash_values[idx].minsym_hash
1489 		 = msymbol_hash (msym->linkage_name ());
1490 	       /* We only use this hash code if the search name differs
1491 		  from the linkage name.  See the code in
1492 		  build_minimal_symbol_hash_tables.  */
1493 	       if (msym->search_name () != msym->linkage_name ())
1494 		 hash_values[idx].minsym_demangled_hash
1495 		   = search_name_hash (msym->language (), msym->search_name ());
1496 	     }
1497 	   {
1498 	     /* To limit how long we hold the lock, we only acquire it here
1499 		and not while we demangle the names above.  */
1500 #if CXX_STD_THREAD
1501 	     std::lock_guard<std::mutex> guard (demangled_mutex);
1502 #endif
1503 	     for (minimal_symbol *msym = start; msym < end; ++msym)
1504 	       {
1505 		 size_t idx = msym - msymbols;
1506 		 msym->compute_and_set_names
1507 		   (gdb::string_view (msym->linkage_name (),
1508 				      hash_values[idx].name_length),
1509 		    false,
1510 		    m_objfile->per_bfd,
1511 		    hash_values[idx].mangled_name_hash);
1512 	       }
1513 	   }
1514 	 });
1515 
1516       build_minimal_symbol_hash_tables (m_objfile, hash_values);
1517     }
1518 }
1519 
1520 /* Check if PC is in a shared library trampoline code stub.
1521    Return minimal symbol for the trampoline entry or NULL if PC is not
1522    in a trampoline code stub.  */
1523 
1524 static struct minimal_symbol *
lookup_solib_trampoline_symbol_by_pc(CORE_ADDR pc)1525 lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
1526 {
1527   bound_minimal_symbol msymbol
1528     = lookup_minimal_symbol_by_pc_section (pc, NULL,
1529 					   lookup_msym_prefer::TRAMPOLINE);
1530 
1531   if (msymbol.minsym != NULL
1532       && MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
1533     return msymbol.minsym;
1534   return NULL;
1535 }
1536 
1537 /* If PC is in a shared library trampoline code stub, return the
1538    address of the `real' function belonging to the stub.
1539    Return 0 if PC is not in a trampoline code stub or if the real
1540    function is not found in the minimal symbol table.
1541 
1542    We may fail to find the right function if a function with the
1543    same name is defined in more than one shared library, but this
1544    is considered bad programming style.  We could return 0 if we find
1545    a duplicate function in case this matters someday.  */
1546 
1547 CORE_ADDR
find_solib_trampoline_target(struct frame_info * frame,CORE_ADDR pc)1548 find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
1549 {
1550   struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
1551 
1552   if (tsymbol != NULL)
1553     {
1554       for (objfile *objfile : current_program_space->objfiles ())
1555 	{
1556 	  for (minimal_symbol *msymbol : objfile->msymbols ())
1557 	    {
1558 	      /* Also handle minimal symbols pointing to function
1559 		 descriptors.  */
1560 	      if ((MSYMBOL_TYPE (msymbol) == mst_text
1561 		   || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
1562 		   || MSYMBOL_TYPE (msymbol) == mst_data
1563 		   || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
1564 		  && strcmp (msymbol->linkage_name (),
1565 			     tsymbol->linkage_name ()) == 0)
1566 		{
1567 		  CORE_ADDR func;
1568 
1569 		  /* Ignore data symbols that are not function
1570 		     descriptors.  */
1571 		  if (msymbol_is_function (objfile, msymbol, &func))
1572 		    return func;
1573 		}
1574 	    }
1575 	}
1576     }
1577   return 0;
1578 }
1579 
1580 /* See minsyms.h.  */
1581 
1582 CORE_ADDR
minimal_symbol_upper_bound(struct bound_minimal_symbol minsym)1583 minimal_symbol_upper_bound (struct bound_minimal_symbol minsym)
1584 {
1585   short section;
1586   struct obj_section *obj_section;
1587   CORE_ADDR result;
1588   struct minimal_symbol *iter, *msymbol;
1589 
1590   gdb_assert (minsym.minsym != NULL);
1591 
1592   /* If the minimal symbol has a size, use it.  Otherwise use the
1593      lesser of the next minimal symbol in the same section, or the end
1594      of the section, as the end of the function.  */
1595 
1596   if (MSYMBOL_SIZE (minsym.minsym) != 0)
1597     return BMSYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym.minsym);
1598 
1599   /* Step over other symbols at this same address, and symbols in
1600      other sections, to find the next symbol in this section with a
1601      different address.  */
1602 
1603   struct minimal_symbol *past_the_end
1604     = (minsym.objfile->per_bfd->msymbols.get ()
1605        + minsym.objfile->per_bfd->minimal_symbol_count);
1606   msymbol = minsym.minsym;
1607   section = msymbol->section_index ();
1608   for (iter = msymbol + 1; iter != past_the_end; ++iter)
1609     {
1610       if ((MSYMBOL_VALUE_RAW_ADDRESS (iter)
1611 	   != MSYMBOL_VALUE_RAW_ADDRESS (msymbol))
1612 	  && iter->section_index () == section)
1613 	break;
1614     }
1615 
1616   obj_section = minsym.obj_section ();
1617   if (iter != past_the_end
1618       && (MSYMBOL_VALUE_ADDRESS (minsym.objfile, iter)
1619 	  < obj_section->endaddr ()))
1620     result = MSYMBOL_VALUE_ADDRESS (minsym.objfile, iter);
1621   else
1622     /* We got the start address from the last msymbol in the objfile.
1623        So the end address is the end of the section.  */
1624     result = obj_section->endaddr ();
1625 
1626   return result;
1627 }
1628