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