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