More updated translations
[binutils-gdb.git] / gdb / minsyms.c
blob4e1868c9f87ffed15df9eb5a7b632ae01f9fdbaa
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 bound_minimal_symbol
362 lookup_minimal_symbol (program_space *pspace, const char *name, objfile *objf,
363 const char *sfile)
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 : pspace->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, %s)",
388 host_address_to_string (pspace),
389 name, sfile != NULL ? sfile : "NULL",
390 objfile_debug_name (objfile));
392 /* Do two passes: the first over the ordinary hash table,
393 and the second over the demangled hash table. */
394 lookup_minimal_symbol_mangled (name, sfile, objfile,
395 objfile->per_bfd->msymbol_hash,
396 mangled_hash, mangled_cmp, found);
398 /* If not found, try the demangled hash table. */
399 if (found.external_symbol.minsym == NULL)
401 /* Once for each language in the demangled hash names
402 table (usually just zero or one languages). */
403 for (unsigned iter = 0; iter < nr_languages; ++iter)
405 if (!objfile->per_bfd->demangled_hash_languages.test (iter))
406 continue;
407 enum language lang = (enum language) iter;
409 unsigned int hash
410 = (lookup_name.search_name_hash (lang)
411 % MINIMAL_SYMBOL_HASH_SIZE);
413 symbol_name_matcher_ftype *match
414 = language_def (lang)->get_symbol_name_matcher
415 (lookup_name);
416 struct minimal_symbol **msymbol_demangled_hash
417 = objfile->per_bfd->msymbol_demangled_hash;
419 lookup_minimal_symbol_demangled (lookup_name, sfile, objfile,
420 msymbol_demangled_hash,
421 hash, match, found);
423 if (found.external_symbol.minsym != NULL)
424 break;
430 /* External symbols are best. */
431 if (found.external_symbol.minsym != NULL)
433 if (symbol_lookup_debug)
435 minimal_symbol *minsym = found.external_symbol.minsym;
437 symbol_lookup_debug_printf
438 ("lookup_minimal_symbol (...) = %s (external)",
439 host_address_to_string (minsym));
441 return found.external_symbol;
444 /* File-local symbols are next best. */
445 if (found.file_symbol.minsym != NULL)
447 if (symbol_lookup_debug)
449 minimal_symbol *minsym = found.file_symbol.minsym;
451 symbol_lookup_debug_printf
452 ("lookup_minimal_symbol (...) = %s (file-local)",
453 host_address_to_string (minsym));
455 return found.file_symbol;
458 /* Symbols for shared library trampolines are next best. */
459 if (found.trampoline_symbol.minsym != NULL)
461 if (symbol_lookup_debug)
463 minimal_symbol *minsym = found.trampoline_symbol.minsym;
465 symbol_lookup_debug_printf
466 ("lookup_minimal_symbol (...) = %s (trampoline)",
467 host_address_to_string (minsym));
470 return found.trampoline_symbol;
473 /* Not found. */
474 symbol_lookup_debug_printf ("lookup_minimal_symbol (...) = NULL");
475 return {};
478 /* See gdbsupport/symbol.h. */
481 find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
482 struct objfile *objfile)
484 bound_minimal_symbol sym
485 = lookup_minimal_symbol (current_program_space, name, objfile);
486 if (sym.minsym != NULL)
487 *addr = sym.value_address ();
489 return sym.minsym == NULL;
492 /* Get the lookup name form best suitable for linkage name
493 matching. */
495 static const char *
496 linkage_name_str (const lookup_name_info &lookup_name)
498 /* Unlike most languages (including C++), Ada uses the
499 encoded/linkage name as the search name recorded in symbols. So
500 if debugging in Ada mode, prefer the Ada-encoded name. This also
501 makes Ada's verbatim match syntax ("<...>") work, because
502 "lookup_name.name()" includes the "<>"s, while
503 "lookup_name.ada().lookup_name()" is the encoded name with "<>"s
504 stripped. */
505 if (current_language->la_language == language_ada)
506 return lookup_name.ada ().lookup_name ().c_str ();
508 return lookup_name.c_str ();
511 /* See minsyms.h. */
513 void
514 iterate_over_minimal_symbols
515 (struct objfile *objf, const lookup_name_info &lookup_name,
516 gdb::function_view<bool (struct minimal_symbol *)> callback)
518 /* The first pass is over the ordinary hash table. */
520 const char *name = linkage_name_str (lookup_name);
521 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
522 auto *mangled_cmp
523 = (case_sensitivity == case_sensitive_on
524 ? strcmp
525 : strcasecmp);
527 for (minimal_symbol *iter = objf->per_bfd->msymbol_hash[hash];
528 iter != NULL;
529 iter = iter->hash_next)
531 if (mangled_cmp (iter->linkage_name (), name) == 0)
532 if (callback (iter))
533 return;
537 /* The second pass is over the demangled table. Once for each
538 language in the demangled hash names table (usually just zero or
539 one). */
540 for (unsigned liter = 0; liter < nr_languages; ++liter)
542 if (!objf->per_bfd->demangled_hash_languages.test (liter))
543 continue;
545 enum language lang = (enum language) liter;
546 const language_defn *lang_def = language_def (lang);
547 symbol_name_matcher_ftype *name_match
548 = lang_def->get_symbol_name_matcher (lookup_name);
550 unsigned int hash
551 = lookup_name.search_name_hash (lang) % MINIMAL_SYMBOL_HASH_SIZE;
552 for (minimal_symbol *iter = objf->per_bfd->msymbol_demangled_hash[hash];
553 iter != NULL;
554 iter = iter->demangled_hash_next)
555 if (name_match (iter->search_name (), lookup_name, NULL))
556 if (callback (iter))
557 return;
561 /* See minsyms.h. */
563 bound_minimal_symbol
564 lookup_minimal_symbol_linkage (const char *name, struct objfile *objf,
565 bool match_static_type)
567 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
569 for (objfile *objfile : objf->separate_debug_objfiles ())
571 for (minimal_symbol *msymbol = objfile->per_bfd->msymbol_hash[hash];
572 msymbol != NULL;
573 msymbol = msymbol->hash_next)
575 if (strcmp (msymbol->linkage_name (), name) == 0
576 && (msymbol->type () == mst_data
577 || msymbol->type () == mst_bss
578 || (match_static_type
579 && (msymbol->type () == mst_file_data
580 || msymbol->type () == mst_file_bss))))
581 return {msymbol, objfile};
585 return {};
588 /* See minsyms.h. */
590 bound_minimal_symbol
591 lookup_minimal_symbol_linkage (program_space *pspace, const char *name,
592 bool only_main)
594 for (objfile *objfile : pspace->objfiles ())
596 if (objfile->separate_debug_objfile_backlink != nullptr)
597 continue;
599 if (only_main && (objfile->flags & OBJF_MAINLINE) == 0)
600 continue;
602 bound_minimal_symbol minsym = lookup_minimal_symbol_linkage (name,
603 objfile,
604 false);
605 if (minsym.minsym != nullptr)
606 return minsym;
609 return {};
612 /* See minsyms.h. */
614 bound_minimal_symbol
615 lookup_minimal_symbol_text (program_space *pspace, const char *name,
616 objfile *objf)
618 struct minimal_symbol *msymbol;
619 bound_minimal_symbol found_symbol;
620 bound_minimal_symbol found_file_symbol;
622 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
624 auto search = [&] (struct objfile *objfile)
626 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
627 msymbol != NULL && found_symbol.minsym == NULL;
628 msymbol = msymbol->hash_next)
630 if (strcmp (msymbol->linkage_name (), name) == 0 &&
631 (msymbol->type () == mst_text
632 || msymbol->type () == mst_text_gnu_ifunc
633 || msymbol->type () == mst_file_text))
635 switch (msymbol->type ())
637 case mst_file_text:
638 found_file_symbol.minsym = msymbol;
639 found_file_symbol.objfile = objfile;
640 break;
641 default:
642 found_symbol.minsym = msymbol;
643 found_symbol.objfile = objfile;
644 break;
650 if (objf == nullptr)
652 for (objfile *objfile : pspace->objfiles ())
654 if (found_symbol.minsym != NULL)
655 break;
656 search (objfile);
659 else
661 for (objfile *objfile : objf->separate_debug_objfiles ())
663 if (found_symbol.minsym != NULL)
664 break;
665 search (objfile);
669 /* External symbols are best. */
670 if (found_symbol.minsym)
671 return found_symbol;
673 /* File-local symbols are next best. */
674 return found_file_symbol;
677 /* See minsyms.h. */
679 struct minimal_symbol *
680 lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
681 struct objfile *objf)
683 struct minimal_symbol *msymbol;
685 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
687 for (objfile *objfile : current_program_space->objfiles ())
689 if (objf == NULL || objf == objfile
690 || objf == objfile->separate_debug_objfile_backlink)
692 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
693 msymbol != NULL;
694 msymbol = msymbol->hash_next)
696 if (msymbol->value_address (objfile) == pc
697 && strcmp (msymbol->linkage_name (), name) == 0)
698 return msymbol;
703 return NULL;
706 /* A helper function that makes *PC section-relative. This searches
707 the sections of OBJFILE and if *PC is in a section, it subtracts
708 the section offset, stores the result into UNREL_ADDR, and returns
709 true. Otherwise it returns false. */
711 static int
712 frob_address (struct objfile *objfile, CORE_ADDR pc,
713 unrelocated_addr *unrel_addr)
715 for (obj_section *iter : objfile->sections ())
717 if (iter->contains (pc))
719 *unrel_addr = unrelocated_addr (pc - iter->offset ());
720 return 1;
724 return 0;
727 /* Helper for lookup_minimal_symbol_by_pc_section. Convert a
728 lookup_msym_prefer to a minimal_symbol_type. */
730 static minimal_symbol_type
731 msym_prefer_to_msym_type (lookup_msym_prefer prefer)
733 switch (prefer)
735 case lookup_msym_prefer::TEXT:
736 return mst_text;
737 case lookup_msym_prefer::TRAMPOLINE:
738 return mst_solib_trampoline;
739 case lookup_msym_prefer::GNU_IFUNC:
740 return mst_text_gnu_ifunc;
743 /* Assert here instead of in a default switch case above so that
744 -Wswitch warns if a new enumerator is added. */
745 gdb_assert_not_reached ("unhandled lookup_msym_prefer");
748 /* See minsyms.h.
750 Note that we need to look through ALL the minimal symbol tables
751 before deciding on the symbol that comes closest to the specified PC.
752 This is because objfiles can overlap, for example objfile A has .text
753 at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
754 .data at 0x40048. */
756 bound_minimal_symbol
757 lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *section,
758 lookup_msym_prefer prefer,
759 bound_minimal_symbol *previous)
761 int lo;
762 int hi;
763 int newobj;
764 struct minimal_symbol *msymbol;
765 struct minimal_symbol *best_symbol = NULL;
766 struct objfile *best_objfile = NULL;
768 if (previous != nullptr)
770 previous->minsym = nullptr;
771 previous->objfile = nullptr;
774 if (section == NULL)
776 section = find_pc_section (pc_in);
777 if (section == NULL)
778 return {};
781 minimal_symbol_type want_type = msym_prefer_to_msym_type (prefer);
783 /* We can not require the symbol found to be in section, because
784 e.g. IRIX 6.5 mdebug relies on this code returning an absolute
785 symbol - but find_pc_section won't return an absolute section and
786 hence the code below would skip over absolute symbols. We can
787 still take advantage of the call to find_pc_section, though - the
788 object file still must match. In case we have separate debug
789 files, search both the file and its separate debug file. There's
790 no telling which one will have the minimal symbols. */
792 gdb_assert (section != NULL);
794 for (objfile *objfile : section->objfile->separate_debug_objfiles ())
796 CORE_ADDR pc = pc_in;
798 /* If this objfile has a minimal symbol table, go search it
799 using a binary search. */
801 if (objfile->per_bfd->minimal_symbol_count > 0)
803 int best_zero_sized = -1;
805 msymbol = objfile->per_bfd->msymbols.get ();
806 lo = 0;
807 hi = objfile->per_bfd->minimal_symbol_count - 1;
809 /* This code assumes that the minimal symbols are sorted by
810 ascending address values. If the pc value is greater than or
811 equal to the first symbol's address, then some symbol in this
812 minimal symbol table is a suitable candidate for being the
813 "best" symbol. This includes the last real symbol, for cases
814 where the pc value is larger than any address in this vector.
816 By iterating until the address associated with the current
817 hi index (the endpoint of the test interval) is less than
818 or equal to the desired pc value, we accomplish two things:
819 (1) the case where the pc value is larger than any minimal
820 symbol address is trivially solved, (2) the address associated
821 with the hi index is always the one we want when the iteration
822 terminates. In essence, we are iterating the test interval
823 down until the pc value is pushed out of it from the high end.
825 Warning: this code is trickier than it would appear at first. */
827 unrelocated_addr unrel_pc;
828 if (frob_address (objfile, pc, &unrel_pc)
829 && unrel_pc >= msymbol[lo].unrelocated_address ())
831 while (msymbol[hi].unrelocated_address () > unrel_pc)
833 /* pc is still strictly less than highest address. */
834 /* Note "new" will always be >= lo. */
835 newobj = (lo + hi) / 2;
836 if ((msymbol[newobj].unrelocated_address () >= unrel_pc)
837 || (lo == newobj))
839 hi = newobj;
841 else
843 lo = newobj;
847 /* If we have multiple symbols at the same address, we want
848 hi to point to the last one. That way we can find the
849 right symbol if it has an index greater than hi. */
850 while (hi < objfile->per_bfd->minimal_symbol_count - 1
851 && (msymbol[hi].unrelocated_address ()
852 == msymbol[hi + 1].unrelocated_address ()))
853 hi++;
855 /* Skip various undesirable symbols. */
856 while (hi >= 0)
858 /* Skip any absolute symbols. This is apparently
859 what adb and dbx do, and is needed for the CM-5.
860 There are two known possible problems: (1) on
861 ELF, apparently end, edata, etc. are absolute.
862 Not sure ignoring them here is a big deal, but if
863 we want to use them, the fix would go in
864 elfread.c. (2) I think shared library entry
865 points on the NeXT are absolute. If we want
866 special handling for this it probably should be
867 triggered by a special mst_abs_or_lib or some
868 such. */
870 if (msymbol[hi].type () == mst_abs)
872 hi--;
873 continue;
876 /* If SECTION was specified, skip any symbol from
877 wrong section. */
878 if (section
879 /* Some types of debug info, such as COFF,
880 don't fill the bfd_section member, so don't
881 throw away symbols on those platforms. */
882 && msymbol[hi].obj_section (objfile) != nullptr
883 && (!matching_obj_sections
884 (msymbol[hi].obj_section (objfile),
885 section)))
887 hi--;
888 continue;
891 /* If we are looking for a trampoline and this is a
892 text symbol, or the other way around, check the
893 preceding symbol too. If they are otherwise
894 identical prefer that one. */
895 if (hi > 0
896 && msymbol[hi].type () != want_type
897 && msymbol[hi - 1].type () == want_type
898 && (msymbol[hi].size () == msymbol[hi - 1].size ())
899 && (msymbol[hi].unrelocated_address ()
900 == msymbol[hi - 1].unrelocated_address ())
901 && (msymbol[hi].obj_section (objfile)
902 == msymbol[hi - 1].obj_section (objfile)))
904 hi--;
905 continue;
908 /* If the minimal symbol has a zero size, save it
909 but keep scanning backwards looking for one with
910 a non-zero size. A zero size may mean that the
911 symbol isn't an object or function (e.g. a
912 label), or it may just mean that the size was not
913 specified. */
914 if (msymbol[hi].size () == 0)
916 if (best_zero_sized == -1)
917 best_zero_sized = hi;
918 hi--;
919 continue;
922 /* If we are past the end of the current symbol, try
923 the previous symbol if it has a larger overlapping
924 size. This happens on i686-pc-linux-gnu with glibc;
925 the nocancel variants of system calls are inside
926 the cancellable variants, but both have sizes. */
927 if (hi > 0
928 && msymbol[hi].size () != 0
929 && unrel_pc >= msymbol[hi].unrelocated_end_address ()
930 && unrel_pc < msymbol[hi - 1].unrelocated_end_address ())
932 hi--;
933 continue;
936 /* Otherwise, this symbol must be as good as we're going
937 to get. */
938 break;
941 /* If HI has a zero size, and best_zero_sized is set,
942 then we had two or more zero-sized symbols; prefer
943 the first one we found (which may have a higher
944 address). Also, if we ran off the end, be sure
945 to back up. */
946 if (best_zero_sized != -1
947 && (hi < 0 || msymbol[hi].size () == 0))
948 hi = best_zero_sized;
950 /* If the minimal symbol has a non-zero size, and this
951 PC appears to be outside the symbol's contents, then
952 refuse to use this symbol. If we found a zero-sized
953 symbol with an address greater than this symbol's,
954 use that instead. We assume that if symbols have
955 specified sizes, they do not overlap. */
957 if (hi >= 0
958 && msymbol[hi].size () != 0
959 && unrel_pc >= msymbol[hi].unrelocated_end_address ())
961 if (best_zero_sized != -1)
962 hi = best_zero_sized;
963 else
965 /* If needed record this symbol as the closest
966 previous symbol. */
967 if (previous != nullptr)
969 if (previous->minsym == nullptr
970 || (msymbol[hi].unrelocated_address ()
971 > previous->minsym->unrelocated_address ()))
973 previous->minsym = &msymbol[hi];
974 previous->objfile = objfile;
977 /* Go on to the next object file. */
978 continue;
982 /* The minimal symbol indexed by hi now is the best one in this
983 objfile's minimal symbol table. See if it is the best one
984 overall. */
986 if (hi >= 0
987 && ((best_symbol == NULL) ||
988 (best_symbol->unrelocated_address () <
989 msymbol[hi].unrelocated_address ())))
991 best_symbol = &msymbol[hi];
992 best_objfile = objfile;
998 bound_minimal_symbol result;
999 result.minsym = best_symbol;
1000 result.objfile = best_objfile;
1001 return result;
1004 /* See minsyms.h. */
1006 bound_minimal_symbol
1007 lookup_minimal_symbol_by_pc (CORE_ADDR pc)
1009 return lookup_minimal_symbol_by_pc_section (pc, NULL);
1012 /* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver. */
1014 bool
1015 in_gnu_ifunc_stub (CORE_ADDR pc)
1017 bound_minimal_symbol msymbol
1018 = lookup_minimal_symbol_by_pc_section (pc, NULL,
1019 lookup_msym_prefer::GNU_IFUNC);
1020 return msymbol.minsym && msymbol.minsym->type () == mst_text_gnu_ifunc;
1023 /* See elf_gnu_ifunc_resolve_addr for its real implementation. */
1025 static CORE_ADDR
1026 stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
1028 error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
1029 "the ELF support compiled in."),
1030 paddress (gdbarch, pc));
1033 /* See elf_gnu_ifunc_resolve_name for its real implementation. */
1035 static bool
1036 stub_gnu_ifunc_resolve_name (const char *function_name,
1037 CORE_ADDR *function_address_p)
1039 error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
1040 "the ELF support compiled in."),
1041 function_name);
1044 /* See elf_gnu_ifunc_resolver_stop for its real implementation. */
1046 static void
1047 stub_gnu_ifunc_resolver_stop (code_breakpoint *b)
1049 internal_error (_("elf_gnu_ifunc_resolver_stop cannot be reached."));
1052 /* See elf_gnu_ifunc_resolver_return_stop for its real implementation. */
1054 static void
1055 stub_gnu_ifunc_resolver_return_stop (code_breakpoint *b)
1057 internal_error (_("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
1060 /* See elf_gnu_ifunc_fns for its real implementation. */
1062 static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
1064 stub_gnu_ifunc_resolve_addr,
1065 stub_gnu_ifunc_resolve_name,
1066 stub_gnu_ifunc_resolver_stop,
1067 stub_gnu_ifunc_resolver_return_stop,
1070 /* A placeholder for &elf_gnu_ifunc_fns. */
1072 const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
1076 /* Return the leading symbol character for BFD ABFD. If ABFD is nullptr,
1077 return the leading symbol character from the the main objfile of PSPACE.. */
1079 static int
1080 get_symbol_leading_char (program_space *pspace, bfd *abfd)
1082 if (abfd != NULL)
1083 return bfd_get_symbol_leading_char (abfd);
1085 if (objfile *objf = pspace->symfile_object_file;
1086 objf != nullptr && objf->obfd != nullptr)
1087 return bfd_get_symbol_leading_char (objf->obfd.get ());
1089 return 0;
1092 /* See minsyms.h. */
1094 minimal_symbol_reader::minimal_symbol_reader (struct objfile *obj)
1095 : m_objfile (obj),
1096 m_msym_bunch (NULL),
1097 /* Note that presetting m_msym_bunch_index to BUNCH_SIZE causes the
1098 first call to save a minimal symbol to allocate the memory for
1099 the first bunch. */
1100 m_msym_bunch_index (BUNCH_SIZE),
1101 m_msym_count (0)
1105 /* Discard the currently collected minimal symbols, if any. If we wish
1106 to save them for later use, we must have already copied them somewhere
1107 else before calling this function. */
1109 minimal_symbol_reader::~minimal_symbol_reader ()
1111 struct msym_bunch *next;
1113 while (m_msym_bunch != NULL)
1115 next = m_msym_bunch->next;
1116 xfree (m_msym_bunch);
1117 m_msym_bunch = next;
1121 /* See minsyms.h. */
1123 void
1124 minimal_symbol_reader::record (const char *name, unrelocated_addr address,
1125 enum minimal_symbol_type ms_type)
1127 int section;
1129 switch (ms_type)
1131 case mst_text:
1132 case mst_text_gnu_ifunc:
1133 case mst_file_text:
1134 case mst_solib_trampoline:
1135 section = SECT_OFF_TEXT (m_objfile);
1136 break;
1137 case mst_data:
1138 case mst_data_gnu_ifunc:
1139 case mst_file_data:
1140 section = SECT_OFF_DATA (m_objfile);
1141 break;
1142 case mst_bss:
1143 case mst_file_bss:
1144 section = SECT_OFF_BSS (m_objfile);
1145 break;
1146 default:
1147 section = -1;
1150 record_with_info (name, address, ms_type, section);
1153 /* Convert an enumerator of type minimal_symbol_type to its string
1154 representation. */
1156 static const char *
1157 mst_str (minimal_symbol_type t)
1159 #define MST_TO_STR(x) case x: return #x;
1160 switch (t)
1162 MST_TO_STR (mst_unknown);
1163 MST_TO_STR (mst_text);
1164 MST_TO_STR (mst_text_gnu_ifunc);
1165 MST_TO_STR (mst_slot_got_plt);
1166 MST_TO_STR (mst_data);
1167 MST_TO_STR (mst_bss);
1168 MST_TO_STR (mst_abs);
1169 MST_TO_STR (mst_solib_trampoline);
1170 MST_TO_STR (mst_file_text);
1171 MST_TO_STR (mst_file_data);
1172 MST_TO_STR (mst_file_bss);
1174 default:
1175 return "mst_???";
1177 #undef MST_TO_STR
1180 /* See minsyms.h. */
1182 struct minimal_symbol *
1183 minimal_symbol_reader::record_full (std::string_view name,
1184 bool copy_name, unrelocated_addr address,
1185 enum minimal_symbol_type ms_type,
1186 int section)
1188 struct msym_bunch *newobj;
1189 struct minimal_symbol *msymbol;
1191 /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
1192 the minimal symbols, because if there is also another symbol
1193 at the same address (e.g. the first function of the file),
1194 lookup_minimal_symbol_by_pc would have no way of getting the
1195 right one. */
1196 if (ms_type == mst_file_text && name[0] == 'g'
1197 && (name == GCC_COMPILED_FLAG_SYMBOL
1198 || name == GCC2_COMPILED_FLAG_SYMBOL))
1199 return (NULL);
1201 /* It's safe to strip the leading char here once, since the name
1202 is also stored stripped in the minimal symbol table. */
1203 if (name[0] == get_symbol_leading_char (m_objfile->pspace (),
1204 m_objfile->obfd.get ()))
1205 name = name.substr (1);
1207 if (ms_type == mst_file_text && startswith (name, "__gnu_compiled"))
1208 return (NULL);
1210 symtab_create_debug_printf_v ("recording minsym: %-21s %18s %4d %.*s",
1211 mst_str (ms_type),
1212 hex_string (LONGEST (address)),
1213 section, (int) name.size (), name.data ());
1215 if (m_msym_bunch_index == BUNCH_SIZE)
1217 newobj = XCNEW (struct msym_bunch);
1218 m_msym_bunch_index = 0;
1219 newobj->next = m_msym_bunch;
1220 m_msym_bunch = newobj;
1222 msymbol = &m_msym_bunch->contents[m_msym_bunch_index];
1223 msymbol->set_language (language_unknown,
1224 &m_objfile->per_bfd->storage_obstack);
1226 if (copy_name)
1227 msymbol->m_name = obstack_strndup (&m_objfile->per_bfd->storage_obstack,
1228 name.data (), name.size ());
1229 else
1230 msymbol->m_name = name.data ();
1232 msymbol->set_unrelocated_address (address);
1233 msymbol->set_section_index (section);
1235 msymbol->set_type (ms_type);
1237 /* If we already read minimal symbols for this objfile, then don't
1238 ever allocate a new one. */
1239 if (!m_objfile->per_bfd->minsyms_read)
1241 m_msym_bunch_index++;
1242 m_objfile->per_bfd->n_minsyms++;
1244 m_msym_count++;
1245 return msymbol;
1248 /* Compare two minimal symbols by address and return true if FN1's address
1249 is less than FN2's, so that we sort into unsigned numeric order.
1250 Within groups with the same address, sort by name. */
1252 static inline bool
1253 minimal_symbol_is_less_than (const minimal_symbol &fn1,
1254 const minimal_symbol &fn2)
1256 if ((&fn1)->unrelocated_address () < (&fn2)->unrelocated_address ())
1258 return true; /* addr 1 is less than addr 2. */
1260 else if ((&fn1)->unrelocated_address () > (&fn2)->unrelocated_address ())
1262 return false; /* addr 1 is greater than addr 2. */
1264 else
1265 /* addrs are equal: sort by name */
1267 const char *name1 = fn1.linkage_name ();
1268 const char *name2 = fn2.linkage_name ();
1270 if (name1 && name2) /* both have names */
1271 return strcmp (name1, name2) < 0;
1272 else if (name2)
1273 return true; /* fn1 has no name, so it is "less". */
1274 else if (name1) /* fn2 has no name, so it is "less". */
1275 return false;
1276 else
1277 return false; /* Neither has a name, so they're equal. */
1281 /* Compact duplicate entries out of a minimal symbol table by walking
1282 through the table and compacting out entries with duplicate addresses
1283 and matching names. Return the number of entries remaining.
1285 On entry, the table resides between msymbol[0] and msymbol[mcount].
1286 On exit, it resides between msymbol[0] and msymbol[result_count].
1288 When files contain multiple sources of symbol information, it is
1289 possible for the minimal symbol table to contain many duplicate entries.
1290 As an example, SVR4 systems use ELF formatted object files, which
1291 usually contain at least two different types of symbol tables (a
1292 standard ELF one and a smaller dynamic linking table), as well as
1293 DWARF debugging information for files compiled with -g.
1295 Without compacting, the minimal symbol table for gdb itself contains
1296 over a 1000 duplicates, about a third of the total table size. Aside
1297 from the potential trap of not noticing that two successive entries
1298 identify the same location, this duplication impacts the time required
1299 to linearly scan the table, which is done in a number of places. So we
1300 just do one linear scan here and toss out the duplicates.
1302 Since the different sources of information for each symbol may
1303 have different levels of "completeness", we may have duplicates
1304 that have one entry with type "mst_unknown" and the other with a
1305 known type. So if the one we are leaving alone has type mst_unknown,
1306 overwrite its type with the type from the one we are compacting out. */
1308 static int
1309 compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
1310 struct objfile *objfile)
1312 struct minimal_symbol *copyfrom;
1313 struct minimal_symbol *copyto;
1315 if (mcount > 0)
1317 copyfrom = copyto = msymbol;
1318 while (copyfrom < msymbol + mcount - 1)
1320 if (copyfrom->unrelocated_address ()
1321 == (copyfrom + 1)->unrelocated_address ()
1322 && (copyfrom->section_index ()
1323 == (copyfrom + 1)->section_index ())
1324 && strcmp (copyfrom->linkage_name (),
1325 (copyfrom + 1)->linkage_name ()) == 0)
1327 if ((copyfrom + 1)->type () == mst_unknown)
1328 (copyfrom + 1)->set_type (copyfrom->type ());
1330 copyfrom++;
1332 else
1333 *copyto++ = *copyfrom++;
1335 *copyto++ = *copyfrom++;
1336 mcount = copyto - msymbol;
1338 return (mcount);
1341 static void
1342 clear_minimal_symbol_hash_tables (struct objfile *objfile)
1344 for (size_t i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
1346 objfile->per_bfd->msymbol_hash[i] = 0;
1347 objfile->per_bfd->msymbol_demangled_hash[i] = 0;
1351 /* This struct is used to store values we compute for msymbols on the
1352 background threads but don't need to keep around long term. */
1353 struct computed_hash_values
1355 /* Length of the linkage_name of the symbol. */
1356 size_t name_length;
1357 /* Hash code (using fast_hash) of the linkage_name. */
1358 hashval_t mangled_name_hash;
1359 /* The msymbol_hash of the linkage_name. */
1360 unsigned int minsym_hash;
1361 /* The msymbol_hash of the search_name. */
1362 unsigned int minsym_demangled_hash;
1365 /* Build (or rebuild) the minimal symbol hash tables. This is necessary
1366 after compacting or sorting the table since the entries move around
1367 thus causing the internal minimal_symbol pointers to become jumbled. */
1369 static void
1370 build_minimal_symbol_hash_tables
1371 (struct objfile *objfile,
1372 const std::vector<computed_hash_values>& hash_values)
1374 int i;
1375 struct minimal_symbol *msym;
1377 /* (Re)insert the actual entries. */
1378 int mcount = objfile->per_bfd->minimal_symbol_count;
1379 for ((i = 0,
1380 msym = objfile->per_bfd->msymbols.get ());
1381 i < mcount;
1382 i++, msym++)
1384 msym->hash_next = 0;
1385 add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash,
1386 hash_values[i].minsym_hash);
1388 msym->demangled_hash_next = 0;
1389 if (msym->search_name () != msym->linkage_name ())
1390 add_minsym_to_demangled_hash_table
1391 (msym, objfile, hash_values[i].minsym_demangled_hash);
1395 /* Add the minimal symbols in the existing bunches to the objfile's official
1396 minimal symbol table. In most cases there is no minimal symbol table yet
1397 for this objfile, and the existing bunches are used to create one. Once
1398 in a while (for shared libraries for example), we add symbols (e.g. common
1399 symbols) to an existing objfile. */
1401 void
1402 minimal_symbol_reader::install ()
1404 int mcount;
1405 struct msym_bunch *bunch;
1406 struct minimal_symbol *msymbols;
1407 int alloc_count;
1409 if (m_objfile->per_bfd->minsyms_read)
1410 return;
1412 if (m_msym_count > 0)
1414 symtab_create_debug_printf ("installing %d minimal symbols of objfile %s",
1415 m_msym_count, objfile_name (m_objfile));
1417 /* Allocate enough space, into which we will gather the bunches
1418 of new and existing minimal symbols, sort them, and then
1419 compact out the duplicate entries. Once we have a final
1420 table, we will give back the excess space. */
1422 alloc_count = m_msym_count + m_objfile->per_bfd->minimal_symbol_count;
1423 gdb::unique_xmalloc_ptr<minimal_symbol>
1424 msym_holder (XNEWVEC (minimal_symbol, alloc_count));
1425 msymbols = msym_holder.get ();
1427 /* Copy in the existing minimal symbols, if there are any. */
1429 if (m_objfile->per_bfd->minimal_symbol_count)
1430 memcpy (msymbols, m_objfile->per_bfd->msymbols.get (),
1431 m_objfile->per_bfd->minimal_symbol_count
1432 * sizeof (struct minimal_symbol));
1434 /* Walk through the list of minimal symbol bunches, adding each symbol
1435 to the new contiguous array of symbols. Note that we start with the
1436 current, possibly partially filled bunch (thus we use the current
1437 msym_bunch_index for the first bunch we copy over), and thereafter
1438 each bunch is full. */
1440 mcount = m_objfile->per_bfd->minimal_symbol_count;
1442 for (bunch = m_msym_bunch; bunch != NULL; bunch = bunch->next)
1444 memcpy (&msymbols[mcount], &bunch->contents[0],
1445 m_msym_bunch_index * sizeof (struct minimal_symbol));
1446 mcount += m_msym_bunch_index;
1447 m_msym_bunch_index = BUNCH_SIZE;
1450 /* Sort the minimal symbols by address. */
1452 std::sort (msymbols, msymbols + mcount, minimal_symbol_is_less_than);
1454 /* Compact out any duplicates, and free up whatever space we are
1455 no longer using. */
1457 mcount = compact_minimal_symbols (msymbols, mcount, m_objfile);
1458 msym_holder.reset (XRESIZEVEC (struct minimal_symbol,
1459 msym_holder.release (),
1460 mcount));
1462 /* Attach the minimal symbol table to the specified objfile.
1463 The strings themselves are also located in the storage_obstack
1464 of this objfile. */
1466 if (m_objfile->per_bfd->minimal_symbol_count != 0)
1467 clear_minimal_symbol_hash_tables (m_objfile);
1469 m_objfile->per_bfd->minimal_symbol_count = mcount;
1470 m_objfile->per_bfd->msymbols = std::move (msym_holder);
1472 #if CXX_STD_THREAD
1473 /* Mutex that is used when modifying or accessing the demangled
1474 hash table. */
1475 std::mutex demangled_mutex;
1476 #endif
1478 std::vector<computed_hash_values> hash_values (mcount);
1480 msymbols = m_objfile->per_bfd->msymbols.get ();
1481 /* Arbitrarily require at least 10 elements in a thread. */
1482 gdb::parallel_for_each (10, &msymbols[0], &msymbols[mcount],
1483 [&] (minimal_symbol *start, minimal_symbol *end)
1485 for (minimal_symbol *msym = start; msym < end; ++msym)
1487 size_t idx = msym - msymbols;
1488 hash_values[idx].name_length = strlen (msym->linkage_name ());
1489 if (!msym->name_set)
1491 /* This will be freed later, by compute_and_set_names. */
1492 gdb::unique_xmalloc_ptr<char> demangled_name
1493 = symbol_find_demangled_name (msym, msym->linkage_name ());
1494 msym->set_demangled_name
1495 (demangled_name.release (),
1496 &m_objfile->per_bfd->storage_obstack);
1497 msym->name_set = 1;
1499 /* This mangled_name_hash computation has to be outside of
1500 the name_set check, or compute_and_set_names below will
1501 be called with an invalid hash value. */
1502 hash_values[idx].mangled_name_hash
1503 = fast_hash (msym->linkage_name (),
1504 hash_values[idx].name_length);
1505 hash_values[idx].minsym_hash
1506 = msymbol_hash (msym->linkage_name ());
1507 /* We only use this hash code if the search name differs
1508 from the linkage name. See the code in
1509 build_minimal_symbol_hash_tables. */
1510 if (msym->search_name () != msym->linkage_name ())
1511 hash_values[idx].minsym_demangled_hash
1512 = search_name_hash (msym->language (), msym->search_name ());
1515 /* To limit how long we hold the lock, we only acquire it here
1516 and not while we demangle the names above. */
1517 #if CXX_STD_THREAD
1518 std::lock_guard<std::mutex> guard (demangled_mutex);
1519 #endif
1520 for (minimal_symbol *msym = start; msym < end; ++msym)
1522 size_t idx = msym - msymbols;
1523 msym->compute_and_set_names
1524 (std::string_view (msym->linkage_name (),
1525 hash_values[idx].name_length),
1526 false,
1527 m_objfile->per_bfd,
1528 hash_values[idx].mangled_name_hash);
1533 build_minimal_symbol_hash_tables (m_objfile, hash_values);
1537 /* Check if PC is in a shared library trampoline code stub.
1538 Return minimal symbol for the trampoline entry or NULL if PC is not
1539 in a trampoline code stub. */
1541 static struct minimal_symbol *
1542 lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
1544 bound_minimal_symbol msymbol
1545 = lookup_minimal_symbol_by_pc_section (pc, NULL,
1546 lookup_msym_prefer::TRAMPOLINE);
1548 if (msymbol.minsym != NULL
1549 && msymbol.minsym->type () == mst_solib_trampoline)
1550 return msymbol.minsym;
1551 return NULL;
1554 /* If PC is in a shared library trampoline code stub, return the
1555 address of the `real' function belonging to the stub.
1556 Return 0 if PC is not in a trampoline code stub or if the real
1557 function is not found in the minimal symbol table.
1559 We may fail to find the right function if a function with the
1560 same name is defined in more than one shared library, but this
1561 is considered bad programming style. We could return 0 if we find
1562 a duplicate function in case this matters someday. */
1564 CORE_ADDR
1565 find_solib_trampoline_target (const frame_info_ptr &frame, CORE_ADDR pc)
1567 struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
1569 if (tsymbol != NULL)
1571 for (objfile *objfile : current_program_space->objfiles ())
1573 for (minimal_symbol *msymbol : objfile->msymbols ())
1575 /* Also handle minimal symbols pointing to function
1576 descriptors. */
1577 if ((msymbol->type () == mst_text
1578 || msymbol->type () == mst_text_gnu_ifunc
1579 || msymbol->type () == mst_data
1580 || msymbol->type () == mst_data_gnu_ifunc)
1581 && strcmp (msymbol->linkage_name (),
1582 tsymbol->linkage_name ()) == 0)
1584 CORE_ADDR func;
1586 /* Ignore data symbols that are not function
1587 descriptors. */
1588 if (msymbol_is_function (objfile, msymbol, &func))
1589 return func;
1594 return 0;
1597 /* See minsyms.h. */
1599 CORE_ADDR
1600 minimal_symbol_upper_bound (bound_minimal_symbol minsym)
1602 struct obj_section *obj_section;
1603 CORE_ADDR result;
1604 struct minimal_symbol *iter, *msymbol;
1606 gdb_assert (minsym.minsym != NULL);
1608 /* If the minimal symbol has a size, use it. Otherwise use the
1609 lesser of the next minimal symbol in the same section, or the end
1610 of the section, as the end of the function. */
1612 if (minsym.minsym->size () != 0)
1613 return minsym.value_address () + minsym.minsym->size ();
1615 /* Step over other symbols at this same address, and symbols in
1616 other sections, to find the next symbol in this section with a
1617 different address. */
1619 struct minimal_symbol *past_the_end
1620 = (minsym.objfile->per_bfd->msymbols.get ()
1621 + minsym.objfile->per_bfd->minimal_symbol_count);
1622 msymbol = minsym.minsym;
1623 int section = msymbol->section_index ();
1624 for (iter = msymbol + 1; iter != past_the_end; ++iter)
1626 if ((iter->unrelocated_address ()
1627 != msymbol->unrelocated_address ())
1628 && iter->section_index () == section)
1629 break;
1632 obj_section = minsym.obj_section ();
1633 if (iter != past_the_end
1634 && (iter->value_address (minsym.objfile)
1635 < obj_section->endaddr ()))
1636 result = iter->value_address (minsym.objfile);
1637 else
1638 /* We got the start address from the last msymbol in the objfile.
1639 So the end address is the end of the section. */
1640 result = obj_section->endaddr ();
1642 return result;
1645 /* See minsyms.h. */
1647 type *
1648 find_minsym_type_and_address (minimal_symbol *msymbol,
1649 struct objfile *objfile,
1650 CORE_ADDR *address_p)
1652 bound_minimal_symbol bound_msym = {msymbol, objfile};
1653 struct obj_section *section = msymbol->obj_section (objfile);
1654 enum minimal_symbol_type type = msymbol->type ();
1656 bool is_tls = (section != NULL
1657 && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
1659 /* The minimal symbol might point to a function descriptor;
1660 resolve it to the actual code address instead. */
1661 CORE_ADDR addr;
1662 if (is_tls)
1664 /* Addresses of TLS symbols are really offsets into a
1665 per-objfile/per-thread storage block. */
1666 addr = CORE_ADDR (bound_msym.minsym->unrelocated_address ());
1668 else if (msymbol_is_function (objfile, msymbol, &addr))
1670 if (addr != bound_msym.value_address ())
1672 /* This means we resolved a function descriptor, and we now
1673 have an address for a code/text symbol instead of a data
1674 symbol. */
1675 if (msymbol->type () == mst_data_gnu_ifunc)
1676 type = mst_text_gnu_ifunc;
1677 else
1678 type = mst_text;
1679 section = NULL;
1682 else
1683 addr = bound_msym.value_address ();
1685 if (overlay_debugging)
1686 addr = symbol_overlayed_address (addr, section);
1688 if (is_tls)
1690 /* Skip translation if caller does not need the address. */
1691 if (address_p != NULL)
1692 *address_p = target_translate_tls_address (objfile, addr);
1693 return builtin_type (objfile)->nodebug_tls_symbol;
1696 if (address_p != NULL)
1697 *address_p = addr;
1699 switch (type)
1701 case mst_text:
1702 case mst_file_text:
1703 case mst_solib_trampoline:
1704 return builtin_type (objfile)->nodebug_text_symbol;
1706 case mst_text_gnu_ifunc:
1707 return builtin_type (objfile)->nodebug_text_gnu_ifunc_symbol;
1709 case mst_data:
1710 case mst_file_data:
1711 case mst_bss:
1712 case mst_file_bss:
1713 return builtin_type (objfile)->nodebug_data_symbol;
1715 case mst_slot_got_plt:
1716 return builtin_type (objfile)->nodebug_got_plt_symbol;
1718 default:
1719 return builtin_type (objfile)->nodebug_unknown_symbol;