Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / minsyms.c
blob38176c4bdcb48b08369efabfa0026d72c556c1c7
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.
5 This file is part of GDB.
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.
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.
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/>. */
21 /* This file contains support routines for creating, manipulating, and
22 destroying minimal symbol tables.
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.
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.
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. */
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"
58 #if CXX_STD_THREAD
59 #include <mutex>
60 #endif
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]. */
70 static bool
71 msymbol_is_cold_clone (minimal_symbol *minsym)
73 const char *name = minsym->natural_name ();
74 size_t name_len = strlen (name);
75 if (name_len < 1)
76 return false;
78 const char *last = &name[name_len - 1];
79 if (*last != ']')
80 return false;
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;
88 const char *start = &found[suffix_len];
89 if (*start == ']')
90 return true;
92 if (*start != '.')
93 return false;
95 const char *p;
96 for (p = start + 1; p <= last; ++p)
98 if (*p >= '0' && *p <= '9')
99 continue;
100 break;
103 if (p == last)
104 return true;
106 return false;
109 /* See minsyms.h. */
111 bool
112 msymbol_is_function (struct objfile *objfile, minimal_symbol *minsym,
113 CORE_ADDR *func_address_p)
115 CORE_ADDR msym_addr = minsym->value_address (objfile);
117 switch (minsym->type ())
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:
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)
132 if (func_address_p != NULL)
133 *func_address_p = pc;
134 return true;
136 return false;
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;
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. */
153 #define BUNCH_SIZE 127
155 struct msym_bunch
157 struct msym_bunch *next;
158 struct minimal_symbol contents[BUNCH_SIZE];
161 /* See minsyms.h. */
163 unsigned int
164 msymbol_hash_iw (const char *string)
166 unsigned int hash = 0;
168 while (*string && *string != '(')
170 string = skip_spaces (string);
171 if (*string && *string != '(')
173 hash = SYMBOL_HASH_NEXT (hash, *string);
174 ++string;
177 return hash;
180 /* See minsyms.h. */
182 unsigned int
183 msymbol_hash (const char *string)
185 unsigned int hash = 0;
187 for (; *string; ++string)
188 hash = SYMBOL_HASH_NEXT (hash, *string);
189 return hash;
192 /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE. */
193 static void
194 add_minsym_to_hash_table (struct minimal_symbol *sym,
195 struct minimal_symbol **table,
196 unsigned int hash_value)
198 if (sym->hash_next == NULL)
200 unsigned int hash = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
202 sym->hash_next = table[hash];
203 table[hash] = sym;
207 /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
208 TABLE. */
209 static void
210 add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
211 struct objfile *objfile,
212 unsigned int hash_value)
214 if (sym->demangled_hash_next == NULL)
216 objfile->per_bfd->demangled_hash_languages.set (sym->language ());
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;
226 /* Worker object for lookup_minimal_symbol. Stores temporary results
227 while walking the symbol tables. */
229 struct found_minimal_symbols
231 /* External symbols are best. */
232 bound_minimal_symbol external_symbol;
234 /* File-local symbols are next best. */
235 bound_minimal_symbol file_symbol;
237 /* Symbols for shared library trampolines are next best. */
238 bound_minimal_symbol trampoline_symbol;
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);
248 /* See declaration above. */
250 bool
251 found_minimal_symbols::maybe_collect (const char *sfile,
252 struct objfile *objfile,
253 minimal_symbol *msymbol)
255 switch (msymbol->type ())
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)
263 file_symbol.minsym = msymbol;
264 file_symbol.objfile = objfile;
266 break;
268 case mst_solib_trampoline:
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)
276 trampoline_symbol.minsym = msymbol;
277 trampoline_symbol.objfile = objfile;
279 break;
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;
289 /* Keep looking. */
290 return false;
293 /* Walk the mangled name hash table, and pass each symbol whose name
294 matches LOOKUP_NAME according to NAMECMP to FOUND. */
296 static void
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)
305 for (minimal_symbol *msymbol = table[hash];
306 msymbol != NULL;
307 msymbol = msymbol->hash_next)
309 const char *symbol_name = msymbol->linkage_name ();
311 if (namecmp (symbol_name, lookup_name) == 0
312 && found.maybe_collect (sfile, objfile, msymbol))
313 return;
317 /* Walk the demangled name hash table, and pass each symbol whose name
318 matches LOOKUP_NAME according to MATCHER to FOUND. */
320 static void
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)
329 for (minimal_symbol *msymbol = table[hash];
330 msymbol != NULL;
331 msymbol = msymbol->demangled_hash_next)
333 const char *symbol_name = msymbol->search_name ();
335 if (matcher (symbol_name, lookup_name, NULL)
336 && found.maybe_collect (sfile, objfile, msymbol))
337 return;
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.
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).
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. */
361 struct bound_minimal_symbol
362 lookup_minimal_symbol (const char *name, const char *sfile,
363 struct objfile *objf)
365 found_minimal_symbols found;
367 unsigned int mangled_hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
369 auto *mangled_cmp
370 = (case_sensitivity == case_sensitive_on
371 ? strcmp
372 : strcasecmp);
374 if (sfile != NULL)
375 sfile = lbasename (sfile);
377 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
379 for (objfile *objfile : current_program_space->objfiles ())
381 if (found.external_symbol.minsym != NULL)
382 break;
384 if (objf == NULL || objf == objfile
385 || objf == objfile->separate_debug_objfile_backlink)
387 symbol_lookup_debug_printf ("lookup_minimal_symbol (%s, %s, %s)",
388 name, sfile != NULL ? sfile : "NULL",
389 objfile_debug_name (objfile));
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);
397 /* If not found, try the demangled hash table. */
398 if (found.external_symbol.minsym == NULL)
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)
404 if (!objfile->per_bfd->demangled_hash_languages.test (iter))
405 continue;
406 enum language lang = (enum language) iter;
408 unsigned int hash
409 = (lookup_name.search_name_hash (lang)
410 % MINIMAL_SYMBOL_HASH_SIZE);
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;
418 lookup_minimal_symbol_demangled (lookup_name, sfile, objfile,
419 msymbol_demangled_hash,
420 hash, match, found);
422 if (found.external_symbol.minsym != NULL)
423 break;
429 /* External symbols are best. */
430 if (found.external_symbol.minsym != NULL)
432 if (symbol_lookup_debug)
434 minimal_symbol *minsym = found.external_symbol.minsym;
436 symbol_lookup_debug_printf
437 ("lookup_minimal_symbol (...) = %s (external)",
438 host_address_to_string (minsym));
440 return found.external_symbol;
443 /* File-local symbols are next best. */
444 if (found.file_symbol.minsym != NULL)
446 if (symbol_lookup_debug)
448 minimal_symbol *minsym = found.file_symbol.minsym;
450 symbol_lookup_debug_printf
451 ("lookup_minimal_symbol (...) = %s (file-local)",
452 host_address_to_string (minsym));
454 return found.file_symbol;
457 /* Symbols for shared library trampolines are next best. */
458 if (found.trampoline_symbol.minsym != NULL)
460 if (symbol_lookup_debug)
462 minimal_symbol *minsym = found.trampoline_symbol.minsym;
464 symbol_lookup_debug_printf
465 ("lookup_minimal_symbol (...) = %s (trampoline)",
466 host_address_to_string (minsym));
469 return found.trampoline_symbol;
472 /* Not found. */
473 symbol_lookup_debug_printf ("lookup_minimal_symbol (...) = NULL");
474 return {};
477 /* See minsyms.h. */
479 struct bound_minimal_symbol
480 lookup_bound_minimal_symbol (const char *name)
482 return lookup_minimal_symbol (name, NULL, NULL);
485 /* See gdbsupport/symbol.h. */
488 find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
489 struct objfile *objfile)
491 struct bound_minimal_symbol sym
492 = lookup_minimal_symbol (name, NULL, objfile);
494 if (sym.minsym != NULL)
495 *addr = sym.value_address ();
497 return sym.minsym == NULL;
500 /* Get the lookup name form best suitable for linkage name
501 matching. */
503 static const char *
504 linkage_name_str (const lookup_name_info &lookup_name)
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 ();
516 return lookup_name.c_str ();
519 /* See minsyms.h. */
521 void
522 iterate_over_minimal_symbols
523 (struct objfile *objf, const lookup_name_info &lookup_name,
524 gdb::function_view<bool (struct minimal_symbol *)> callback)
526 /* The first pass is over the ordinary hash table. */
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);
535 for (minimal_symbol *iter = objf->per_bfd->msymbol_hash[hash];
536 iter != NULL;
537 iter = iter->hash_next)
539 if (mangled_cmp (iter->linkage_name (), name) == 0)
540 if (callback (iter))
541 return;
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)
550 if (!objf->per_bfd->demangled_hash_languages.test (liter))
551 continue;
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);
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;
569 /* See minsyms.h. */
571 bound_minimal_symbol
572 lookup_minimal_symbol_linkage (const char *name, struct objfile *objf)
574 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
576 for (objfile *objfile : objf->separate_debug_objfiles ())
578 for (minimal_symbol *msymbol = objfile->per_bfd->msymbol_hash[hash];
579 msymbol != NULL;
580 msymbol = msymbol->hash_next)
582 if (strcmp (msymbol->linkage_name (), name) == 0
583 && (msymbol->type () == mst_data
584 || msymbol->type () == mst_bss))
585 return {msymbol, objfile};
589 return {};
592 /* See minsyms.h. */
594 struct bound_minimal_symbol
595 lookup_minimal_symbol_linkage (const char *name, bool only_main)
597 for (objfile *objfile : current_program_space->objfiles ())
599 if (objfile->separate_debug_objfile_backlink != nullptr)
600 continue;
602 if (only_main && (objfile->flags & OBJF_MAINLINE) == 0)
603 continue;
605 bound_minimal_symbol minsym = lookup_minimal_symbol_linkage (name,
606 objfile);
607 if (minsym.minsym != nullptr)
608 return minsym;
611 return {};
614 /* See minsyms.h. */
616 struct bound_minimal_symbol
617 lookup_minimal_symbol_text (const char *name, struct objfile *objf)
619 struct minimal_symbol *msymbol;
620 struct bound_minimal_symbol found_symbol;
621 struct bound_minimal_symbol found_file_symbol;
623 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
625 auto search = [&] (struct objfile *objfile)
627 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
628 msymbol != NULL && found_symbol.minsym == NULL;
629 msymbol = msymbol->hash_next)
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))
636 switch (msymbol->type ())
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;
651 if (objf == nullptr)
653 for (objfile *objfile : current_program_space->objfiles ())
655 if (found_symbol.minsym != NULL)
656 break;
657 search (objfile);
660 else
662 for (objfile *objfile : objf->separate_debug_objfiles ())
664 if (found_symbol.minsym != NULL)
665 break;
666 search (objfile);
670 /* External symbols are best. */
671 if (found_symbol.minsym)
672 return found_symbol;
674 /* File-local symbols are next best. */
675 return found_file_symbol;
678 /* See minsyms.h. */
680 struct minimal_symbol *
681 lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
682 struct objfile *objf)
684 struct minimal_symbol *msymbol;
686 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
688 for (objfile *objfile : current_program_space->objfiles ())
690 if (objf == NULL || objf == objfile
691 || objf == objfile->separate_debug_objfile_backlink)
693 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
694 msymbol != NULL;
695 msymbol = msymbol->hash_next)
697 if (msymbol->value_address (objfile) == pc
698 && strcmp (msymbol->linkage_name (), name) == 0)
699 return msymbol;
704 return NULL;
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. */
712 static int
713 frob_address (struct objfile *objfile, CORE_ADDR pc,
714 unrelocated_addr *unrel_addr)
716 for (obj_section *iter : objfile->sections ())
718 if (iter->contains (pc))
720 *unrel_addr = unrelocated_addr (pc - iter->offset ());
721 return 1;
725 return 0;
728 /* Helper for lookup_minimal_symbol_by_pc_section. Convert a
729 lookup_msym_prefer to a minimal_symbol_type. */
731 static minimal_symbol_type
732 msym_prefer_to_msym_type (lookup_msym_prefer prefer)
734 switch (prefer)
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;
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");
749 /* See minsyms.h.
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. */
757 bound_minimal_symbol
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)
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;
770 if (previous != nullptr)
772 previous->minsym = nullptr;
773 previous->objfile = nullptr;
776 if (section == NULL)
778 section = find_pc_section (pc_in);
779 if (section == NULL)
780 return {};
783 minimal_symbol_type want_type = msym_prefer_to_msym_type (prefer);
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. */
794 gdb_assert (section != NULL);
796 for (objfile *objfile : section->objfile->separate_debug_objfiles ())
798 CORE_ADDR pc = pc_in;
800 /* If this objfile has a minimal symbol table, go search it
801 using a binary search. */
803 if (objfile->per_bfd->minimal_symbol_count > 0)
805 int best_zero_sized = -1;
807 msymbol = objfile->per_bfd->msymbols.get ();
808 lo = 0;
809 hi = objfile->per_bfd->minimal_symbol_count - 1;
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.
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.
827 Warning: this code is trickier than it would appear at first. */
829 unrelocated_addr unrel_pc;
830 if (frob_address (objfile, pc, &unrel_pc)
831 && unrel_pc >= msymbol[lo].unrelocated_address ())
833 while (msymbol[hi].unrelocated_address () > unrel_pc)
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))
841 hi = newobj;
843 else
845 lo = newobj;
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++;
857 /* Skip various undesirable symbols. */
858 while (hi >= 0)
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. */
872 if (msymbol[hi].type () == mst_abs)
874 hi--;
875 continue;
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)))
889 hi--;
890 continue;
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)))
906 hi--;
907 continue;
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)
918 if (best_zero_sized == -1)
919 best_zero_sized = hi;
920 hi--;
921 continue;
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 ())
934 hi--;
935 continue;
938 /* Otherwise, this symbol must be as good as we're going
939 to get. */
940 break;
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;
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. */
959 if (hi >= 0
960 && msymbol[hi].size () != 0
961 && unrel_pc >= msymbol[hi].unrelocated_end_address ())
963 if (best_zero_sized != -1)
964 hi = best_zero_sized;
965 else
967 /* If needed record this symbol as the closest
968 previous symbol. */
969 if (previous != nullptr)
971 if (previous->minsym == nullptr
972 || (msymbol[hi].unrelocated_address ()
973 > previous->minsym->unrelocated_address ()))
975 previous->minsym = &msymbol[hi];
976 previous->objfile = objfile;
979 /* Go on to the next object file. */
980 continue;
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. */
988 if (hi >= 0
989 && ((best_symbol == NULL) ||
990 (best_symbol->unrelocated_address () <
991 msymbol[hi].unrelocated_address ())))
993 best_symbol = &msymbol[hi];
994 best_objfile = objfile;
1000 result.minsym = best_symbol;
1001 result.objfile = best_objfile;
1002 return result;
1005 /* See minsyms.h. */
1007 struct bound_minimal_symbol
1008 lookup_minimal_symbol_by_pc (CORE_ADDR pc)
1010 return lookup_minimal_symbol_by_pc_section (pc, NULL);
1013 /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver. */
1015 bool
1016 in_gnu_ifunc_stub (CORE_ADDR pc)
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;
1024 /* See elf_gnu_ifunc_resolve_addr for its real implementation. */
1026 static CORE_ADDR
1027 stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
1029 error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
1030 "the ELF support compiled in."),
1031 paddress (gdbarch, pc));
1034 /* See elf_gnu_ifunc_resolve_name for its real implementation. */
1036 static bool
1037 stub_gnu_ifunc_resolve_name (const char *function_name,
1038 CORE_ADDR *function_address_p)
1040 error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
1041 "the ELF support compiled in."),
1042 function_name);
1045 /* See elf_gnu_ifunc_resolver_stop for its real implementation. */
1047 static void
1048 stub_gnu_ifunc_resolver_stop (code_breakpoint *b)
1050 internal_error (_("elf_gnu_ifunc_resolver_stop cannot be reached."));
1053 /* See elf_gnu_ifunc_resolver_return_stop for its real implementation. */
1055 static void
1056 stub_gnu_ifunc_resolver_return_stop (code_breakpoint *b)
1058 internal_error (_("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
1061 /* See elf_gnu_ifunc_fns for its real implementation. */
1063 static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
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,
1071 /* A placeholder for &elf_gnu_ifunc_fns. */
1073 const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
1077 /* Return leading symbol character for a BFD. If BFD is NULL,
1078 return the leading symbol character from the main objfile. */
1080 static int
1081 get_symbol_leading_char (bfd *abfd)
1083 if (abfd != NULL)
1084 return bfd_get_symbol_leading_char (abfd);
1085 if (current_program_space->symfile_object_file != NULL)
1087 objfile *objf = current_program_space->symfile_object_file;
1088 if (objf->obfd != NULL)
1089 return bfd_get_symbol_leading_char (objf->obfd.get ());
1091 return 0;
1094 /* See minsyms.h. */
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)
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. */
1111 minimal_symbol_reader::~minimal_symbol_reader ()
1113 struct msym_bunch *next;
1115 while (m_msym_bunch != NULL)
1117 next = m_msym_bunch->next;
1118 xfree (m_msym_bunch);
1119 m_msym_bunch = next;
1123 /* See minsyms.h. */
1125 void
1126 minimal_symbol_reader::record (const char *name, unrelocated_addr address,
1127 enum minimal_symbol_type ms_type)
1129 int section;
1131 switch (ms_type)
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;
1152 record_with_info (name, address, ms_type, section);
1155 /* Convert an enumerator of type minimal_symbol_type to its string
1156 representation. */
1158 static const char *
1159 mst_str (minimal_symbol_type t)
1161 #define MST_TO_STR(x) case x: return #x;
1162 switch (t)
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);
1176 default:
1177 return "mst_???";
1179 #undef MST_TO_STR
1182 /* See minsyms.h. */
1184 struct minimal_symbol *
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)
1190 struct msym_bunch *newobj;
1191 struct minimal_symbol *msymbol;
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);
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);
1208 if (ms_type == mst_file_text && startswith (name, "__gnu_compiled"))
1209 return (NULL);
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 ());
1216 if (m_msym_bunch_index == BUNCH_SIZE)
1218 newobj = XCNEW (struct msym_bunch);
1219 m_msym_bunch_index = 0;
1220 newobj->next = m_msym_bunch;
1221 m_msym_bunch = newobj;
1223 msymbol = &m_msym_bunch->contents[m_msym_bunch_index];
1224 msymbol->set_language (language_unknown,
1225 &m_objfile->per_bfd->storage_obstack);
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 ();
1233 msymbol->set_unrelocated_address (address);
1234 msymbol->set_section_index (section);
1236 msymbol->set_type (ms_type);
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)
1242 m_msym_bunch_index++;
1243 m_objfile->per_bfd->n_minsyms++;
1245 m_msym_count++;
1246 return msymbol;
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. */
1253 static inline bool
1254 minimal_symbol_is_less_than (const minimal_symbol &fn1,
1255 const minimal_symbol &fn2)
1257 if ((&fn1)->unrelocated_address () < (&fn2)->unrelocated_address ())
1259 return true; /* addr 1 is less than addr 2. */
1261 else if ((&fn1)->unrelocated_address () > (&fn2)->unrelocated_address ())
1263 return false; /* addr 1 is greater than addr 2. */
1265 else
1266 /* addrs are equal: sort by name */
1268 const char *name1 = fn1.linkage_name ();
1269 const char *name2 = fn2.linkage_name ();
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. */
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.
1286 On entry, the table resides between msymbol[0] and msymbol[mcount].
1287 On exit, it resides between msymbol[0] and msymbol[result_count].
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.
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.
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. */
1309 static int
1310 compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
1311 struct objfile *objfile)
1313 struct minimal_symbol *copyfrom;
1314 struct minimal_symbol *copyto;
1316 if (mcount > 0)
1318 copyfrom = copyto = msymbol;
1319 while (copyfrom < msymbol + mcount - 1)
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)
1328 if ((copyfrom + 1)->type () == mst_unknown)
1329 (copyfrom + 1)->set_type (copyfrom->type ());
1331 copyfrom++;
1333 else
1334 *copyto++ = *copyfrom++;
1336 *copyto++ = *copyfrom++;
1337 mcount = copyto - msymbol;
1339 return (mcount);
1342 static void
1343 clear_minimal_symbol_hash_tables (struct objfile *objfile)
1345 for (size_t i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
1347 objfile->per_bfd->msymbol_hash[i] = 0;
1348 objfile->per_bfd->msymbol_demangled_hash[i] = 0;
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
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;
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. */
1370 static void
1371 build_minimal_symbol_hash_tables
1372 (struct objfile *objfile,
1373 const std::vector<computed_hash_values>& hash_values)
1375 int i;
1376 struct minimal_symbol *msym;
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++)
1385 msym->hash_next = 0;
1386 add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash,
1387 hash_values[i].minsym_hash);
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);
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. */
1402 void
1403 minimal_symbol_reader::install ()
1405 int mcount;
1406 struct msym_bunch *bunch;
1407 struct minimal_symbol *msymbols;
1408 int alloc_count;
1410 if (m_objfile->per_bfd->minsyms_read)
1411 return;
1413 if (m_msym_count > 0)
1415 symtab_create_debug_printf ("installing %d minimal symbols of objfile %s",
1416 m_msym_count, objfile_name (m_objfile));
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. */
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 ();
1428 /* Copy in the existing minimal symbols, if there are any. */
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));
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. */
1441 mcount = m_objfile->per_bfd->minimal_symbol_count;
1443 for (bunch = m_msym_bunch; bunch != NULL; bunch = bunch->next)
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;
1451 /* Sort the minimal symbols by address. */
1453 std::sort (msymbols, msymbols + mcount, minimal_symbol_is_less_than);
1455 /* Compact out any duplicates, and free up whatever space we are
1456 no longer using. */
1458 mcount = compact_minimal_symbols (msymbols, mcount, m_objfile);
1459 msym_holder.reset (XRESIZEVEC (struct minimal_symbol,
1460 msym_holder.release (),
1461 mcount));
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. */
1467 if (m_objfile->per_bfd->minimal_symbol_count != 0)
1468 clear_minimal_symbol_hash_tables (m_objfile);
1470 m_objfile->per_bfd->minimal_symbol_count = mcount;
1471 m_objfile->per_bfd->msymbols = std::move (msym_holder);
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
1479 std::vector<computed_hash_values> hash_values (mcount);
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)
1486 for (minimal_symbol *msym = start; msym < end; ++msym)
1488 size_t idx = msym - msymbols;
1489 hash_values[idx].name_length = strlen (msym->linkage_name ());
1490 if (!msym->name_set)
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;
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 ());
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)
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);
1534 build_minimal_symbol_hash_tables (m_objfile, hash_values);
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. */
1542 static struct minimal_symbol *
1543 lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
1545 bound_minimal_symbol msymbol
1546 = lookup_minimal_symbol_by_pc_section (pc, NULL,
1547 lookup_msym_prefer::TRAMPOLINE);
1549 if (msymbol.minsym != NULL
1550 && msymbol.minsym->type () == mst_solib_trampoline)
1551 return msymbol.minsym;
1552 return NULL;
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.
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. */
1565 CORE_ADDR
1566 find_solib_trampoline_target (const frame_info_ptr &frame, CORE_ADDR pc)
1568 struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
1570 if (tsymbol != NULL)
1572 for (objfile *objfile : current_program_space->objfiles ())
1574 for (minimal_symbol *msymbol : objfile->msymbols ())
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)
1585 CORE_ADDR func;
1587 /* Ignore data symbols that are not function
1588 descriptors. */
1589 if (msymbol_is_function (objfile, msymbol, &func))
1590 return func;
1595 return 0;
1598 /* See minsyms.h. */
1600 CORE_ADDR
1601 minimal_symbol_upper_bound (struct bound_minimal_symbol minsym)
1603 short section;
1604 struct obj_section *obj_section;
1605 CORE_ADDR result;
1606 struct minimal_symbol *iter, *msymbol;
1608 gdb_assert (minsym.minsym != NULL);
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. */
1614 if (minsym.minsym->size () != 0)
1615 return minsym.value_address () + minsym.minsym->size ();
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. */
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)
1628 if ((iter->unrelocated_address ()
1629 != msymbol->unrelocated_address ())
1630 && iter->section_index () == section)
1631 break;
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 ();
1644 return result;
1647 /* See minsyms.h. */
1649 type *
1650 find_minsym_type_and_address (minimal_symbol *msymbol,
1651 struct objfile *objfile,
1652 CORE_ADDR *address_p)
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 ();
1658 bool is_tls = (section != NULL
1659 && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
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)
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 ());
1670 else if (msymbol_is_function (objfile, msymbol, &addr))
1672 if (addr != bound_msym.value_address ())
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;
1684 else
1685 addr = bound_msym.value_address ();
1687 if (overlay_debugging)
1688 addr = symbol_overlayed_address (addr, section);
1690 if (is_tls)
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;
1698 if (address_p != NULL)
1699 *address_p = addr;
1701 switch (type)
1703 case mst_text:
1704 case mst_file_text:
1705 case mst_solib_trampoline:
1706 return builtin_type (objfile)->nodebug_text_symbol;
1708 case mst_text_gnu_ifunc:
1709 return builtin_type (objfile)->nodebug_text_gnu_ifunc_symbol;
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;
1717 case mst_slot_got_plt:
1718 return builtin_type (objfile)->nodebug_got_plt_symbol;
1720 default:
1721 return builtin_type (objfile)->nodebug_unknown_symbol;