]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/elfread.c
Remove most includes of psymtab.h
[thirdparty/binutils-gdb.git] / gdb / elfread.c
CommitLineData
c906108c 1/* Read ELF (Executable and Linking Format) object files for GDB.
1bac305b 2
213516ef 3 Copyright (C) 1991-2023 Free Software Foundation, Inc.
1bac305b 4
c906108c
SS
5 Written by Fred Fish at Cygnus Support.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
23#include "bfd.h"
c906108c 24#include "elf-bfd.h"
31d99776
DJ
25#include "elf/common.h"
26#include "elf/internal.h"
c906108c 27#include "elf/mips.h"
4de283e4
TT
28#include "symtab.h"
29#include "symfile.h"
30#include "objfiles.h"
31#include "stabsread.h"
4de283e4 32#include "demangle.h"
4de283e4
TT
33#include "filenames.h"
34#include "probe.h"
35#include "arch-utils.h"
07be84bf 36#include "gdbtypes.h"
4de283e4 37#include "value.h"
07be84bf 38#include "infcall.h"
4de283e4 39#include "gdbthread.h"
00431a78 40#include "inferior.h"
4de283e4
TT
41#include "regcache.h"
42#include "bcache.h"
43#include "gdb_bfd.h"
44#include "build-id.h"
f00aae0f 45#include "location.h"
4de283e4 46#include "auxv.h"
0e8f53ba 47#include "mdebugread.h"
30d1f018 48#include "ctfread.h"
31edb802 49#include "gdbsupport/gdb_string_view.h"
0d79cdc4
AM
50#include "gdbsupport/scoped_fd.h"
51#include "debuginfod-support.h"
70182375 52#include "dwarf2/public.h"
0d5adb56
TV
53#include "cli/cli-cmds.h"
54
55/* Whether ctf should always be read, or only if no dwarf is present. */
56static bool always_read_ctf;
c906108c
SS
57
58/* The struct elfinfo is available only during ELF symbol table and
6426a772 59 psymtab reading. It is destroyed at the completion of psymtab-reading.
c906108c
SS
60 It's local to elf_symfile_read. */
61
c5aa993b
JM
62struct elfinfo
63 {
c5aa993b 64 asection *stabsect; /* Section pointer for .stab section */
c5aa993b 65 asection *mdebugsect; /* Section pointer for .mdebug section */
30d1f018 66 asection *ctfsect; /* Section pointer for .ctf section */
c5aa993b 67 };
c906108c 68
814cf43a
TT
69/* Type for per-BFD data. */
70
71typedef std::vector<std::unique_ptr<probe>> elfread_data;
72
5d9cf8a4 73/* Per-BFD data for probe info. */
55aa24fb 74
08b8a139 75static const registry<bfd>::key<elfread_data> probe_key;
55aa24fb 76
07be84bf
JK
77/* Minimal symbols located at the GOT entries for .plt - that is the real
78 pointer where the given entry will jump to. It gets updated by the real
79 function address during lazy ld.so resolving in the inferior. These
80 minimal symbols are indexed for <tab>-completion. */
81
82#define SYMBOL_GOT_PLT_SUFFIX "@got.plt"
83
31d99776
DJ
84/* Locate the segments in ABFD. */
85
62982abd 86static symfile_segment_data_up
31d99776
DJ
87elf_symfile_segments (bfd *abfd)
88{
89 Elf_Internal_Phdr *phdrs, **segments;
90 long phdrs_size;
91 int num_phdrs, num_segments, num_sections, i;
92 asection *sect;
31d99776
DJ
93
94 phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
95 if (phdrs_size == -1)
96 return NULL;
97
224c3ddb 98 phdrs = (Elf_Internal_Phdr *) alloca (phdrs_size);
31d99776
DJ
99 num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
100 if (num_phdrs == -1)
101 return NULL;
102
103 num_segments = 0;
8d749320 104 segments = XALLOCAVEC (Elf_Internal_Phdr *, num_phdrs);
31d99776
DJ
105 for (i = 0; i < num_phdrs; i++)
106 if (phdrs[i].p_type == PT_LOAD)
107 segments[num_segments++] = &phdrs[i];
108
109 if (num_segments == 0)
110 return NULL;
111
62982abd 112 symfile_segment_data_up data (new symfile_segment_data);
68b888ff 113 data->segments.reserve (num_segments);
31d99776
DJ
114
115 for (i = 0; i < num_segments; i++)
68b888ff 116 data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz);
31d99776
DJ
117
118 num_sections = bfd_count_sections (abfd);
9005fbbb
SM
119
120 /* All elements are initialized to 0 (map to no segment). */
121 data->segment_info.resize (num_sections);
31d99776
DJ
122
123 for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
124 {
125 int j;
31d99776 126
fd361982 127 if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
31d99776
DJ
128 continue;
129
62b74cb8 130 Elf_Internal_Shdr *this_hdr = &elf_section_data (sect)->this_hdr;
31d99776
DJ
131
132 for (j = 0; j < num_segments; j++)
62b74cb8 133 if (ELF_SECTION_IN_SEGMENT (this_hdr, segments[j]))
31d99776
DJ
134 {
135 data->segment_info[i] = j + 1;
136 break;
137 }
138
ad09a548
DJ
139 /* We should have found a segment for every non-empty section.
140 If we haven't, we will not relocate this section by any
141 offsets we apply to the segments. As an exception, do not
142 warn about SHT_NOBITS sections; in normal ELF execution
143 environments, SHT_NOBITS means zero-initialized and belongs
144 in a segment, but in no-OS environments some tools (e.g. ARM
145 RealView) use SHT_NOBITS for uninitialized data. Since it is
146 uninitialized, it doesn't need a program header. Such
147 binaries are not relocatable. */
25f4c262
KS
148
149 /* Exclude debuginfo files from this warning, too, since those
150 are often not strictly compliant with the standard. See, e.g.,
151 ld/24717 for more discussion. */
152 if (!is_debuginfo_file (abfd)
153 && bfd_section_size (sect) > 0 && j == num_segments
fd361982 154 && (bfd_section_flags (sect) & SEC_LOAD) != 0)
9d3ab915
KS
155 warning (_("Loadable section \"%s\" outside of ELF segments\n in %s"),
156 bfd_section_name (sect), bfd_get_filename (abfd));
31d99776
DJ
157 }
158
159 return data;
160}
161
c906108c
SS
162/* We are called once per section from elf_symfile_read. We
163 need to examine each section we are passed, check to see
164 if it is something we are interested in processing, and
165 if so, stash away some access information for the section.
166
167 For now we recognize the dwarf debug information sections and
168 line number sections from matching their section names. The
169 ELF definition is no real help here since it has no direct
170 knowledge of DWARF (by design, so any debugging format can be
171 used).
172
173 We also recognize the ".stab" sections used by the Sun compilers
174 released with Solaris 2.
175
176 FIXME: The section names should not be hardwired strings (what
177 should they be? I don't think most object file formats have enough
0963b4bd 178 section flags to specify what kind of debug section it is.
c906108c
SS
179 -kingdon). */
180
181static void
08f93a1a 182elf_locate_sections (asection *sectp, struct elfinfo *ei)
c906108c 183{
7ce59000 184 if (strcmp (sectp->name, ".stab") == 0)
c906108c 185 {
c5aa993b 186 ei->stabsect = sectp;
c906108c 187 }
6314a349 188 else if (strcmp (sectp->name, ".mdebug") == 0)
c906108c 189 {
c5aa993b 190 ei->mdebugsect = sectp;
c906108c 191 }
30d1f018
WP
192 else if (strcmp (sectp->name, ".ctf") == 0)
193 {
194 ei->ctfsect = sectp;
195 }
c906108c
SS
196}
197
c906108c 198static struct minimal_symbol *
8dddcb8f 199record_minimal_symbol (minimal_symbol_reader &reader,
31edb802 200 gdb::string_view name, bool copy_name,
9675da25 201 unrelocated_addr address,
f594e5e9
MC
202 enum minimal_symbol_type ms_type,
203 asection *bfd_section, struct objfile *objfile)
c906108c 204{
08feed99 205 struct gdbarch *gdbarch = objfile->arch ();
5e2b427d 206
0875794a
JK
207 if (ms_type == mst_text || ms_type == mst_file_text
208 || ms_type == mst_text_gnu_ifunc)
9675da25
TT
209 address
210 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
211 CORE_ADDR (address)));
c906108c 212
44e4c775
AB
213 /* We only setup section information for allocatable sections. Usually
214 we'd only expect to find msymbols for allocatable sections, but if the
215 ELF is malformed then this might not be the case. In that case don't
216 create an msymbol that references an uninitialised section object. */
217 int section_index = 0;
dd2532ad
TT
218 if ((bfd_section_flags (bfd_section) & SEC_ALLOC) == SEC_ALLOC
219 || bfd_section == bfd_abs_section_ptr)
98badbfd 220 section_index = gdb_bfd_section_index (objfile->obfd.get (), bfd_section);
44e4c775 221
4b610737 222 struct minimal_symbol *result
44e4c775 223 = reader.record_full (name, copy_name, address, ms_type, section_index);
4b610737
TT
224 if ((objfile->flags & OBJF_MAINLINE) == 0
225 && (ms_type == mst_data || ms_type == mst_bss))
226 result->maybe_copied = 1;
227
228 return result;
c906108c
SS
229}
230
7f86f058 231/* Read the symbol table of an ELF file.
c906108c 232
62553543 233 Given an objfile, a symbol table, and a flag indicating whether the
6f610d07
UW
234 symbol table contains regular, dynamic, or synthetic symbols, add all
235 the global function and data symbols to the minimal symbol table.
c906108c 236
c5aa993b
JM
237 In stabs-in-ELF, as implemented by Sun, there are some local symbols
238 defined in the ELF symbol table, which can be used to locate
239 the beginnings of sections from each ".o" file that was linked to
240 form the executable objfile. We gather any such info and record it
7f86f058 241 in data structures hung off the objfile's private data. */
c906108c 242
6f610d07
UW
243#define ST_REGULAR 0
244#define ST_DYNAMIC 1
245#define ST_SYNTHETIC 2
246
c906108c 247static void
8dddcb8f
TT
248elf_symtab_read (minimal_symbol_reader &reader,
249 struct objfile *objfile, int type,
04a679b8 250 long number_of_symbols, asymbol **symbol_table,
ce6c454e 251 bool copy_names)
c906108c 252{
08feed99 253 struct gdbarch *gdbarch = objfile->arch ();
c906108c 254 asymbol *sym;
c906108c 255 long i;
c906108c
SS
256 CORE_ADDR symaddr;
257 enum minimal_symbol_type ms_type;
18a94d75
DE
258 /* Name of the last file symbol. This is either a constant string or is
259 saved on the objfile's filename cache. */
0af1e9a5 260 const char *filesymname = "";
98badbfd 261 int stripped = (bfd_get_symcount (objfile->obfd.get ()) == 0);
3e29f34a
MR
262 int elf_make_msymbol_special_p
263 = gdbarch_elf_make_msymbol_special_p (gdbarch);
c5aa993b 264
0cc7b392 265 for (i = 0; i < number_of_symbols; i++)
c906108c 266 {
0cc7b392
DJ
267 sym = symbol_table[i];
268 if (sym->name == NULL || *sym->name == '\0')
c906108c 269 {
0cc7b392 270 /* Skip names that don't exist (shouldn't happen), or names
0963b4bd 271 that are null strings (may happen). */
0cc7b392
DJ
272 continue;
273 }
c906108c 274
28a4e64d
TT
275 elf_symbol_type *elf_sym = (elf_symbol_type *) sym;
276
74763737
DJ
277 /* Skip "special" symbols, e.g. ARM mapping symbols. These are
278 symbols which do not correspond to objects in the symbol table,
279 but have some other target-specific meaning. */
98badbfd 280 if (bfd_is_target_special_symbol (objfile->obfd.get (), sym))
60c5725c
DJ
281 {
282 if (gdbarch_record_special_symbol_p (gdbarch))
283 gdbarch_record_special_symbol (gdbarch, objfile, sym);
284 continue;
285 }
74763737 286
6f610d07 287 if (type == ST_DYNAMIC
45dfa85a 288 && sym->section == bfd_und_section_ptr
0cc7b392
DJ
289 && (sym->flags & BSF_FUNCTION))
290 {
291 struct minimal_symbol *msym;
98badbfd 292 bfd *abfd = objfile->obfd.get ();
dea91a5c 293 asection *sect;
0cc7b392
DJ
294
295 /* Symbol is a reference to a function defined in
296 a shared library.
297 If its value is non zero then it is usually the address
298 of the corresponding entry in the procedure linkage table,
299 plus the desired section offset.
300 If its value is zero then the dynamic linker has to resolve
0963b4bd 301 the symbol. We are unable to find any meaningful address
0cc7b392
DJ
302 for this symbol in the executable file, so we skip it. */
303 symaddr = sym->value;
304 if (symaddr == 0)
305 continue;
02c75f72
UW
306
307 /* sym->section is the undefined section. However, we want to
308 record the section where the PLT stub resides with the
309 minimal symbol. Search the section table for the one that
310 covers the stub's address. */
311 for (sect = abfd->sections; sect != NULL; sect = sect->next)
312 {
fd361982 313 if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
02c75f72
UW
314 continue;
315
fd361982
AM
316 if (symaddr >= bfd_section_vma (sect)
317 && symaddr < bfd_section_vma (sect)
318 + bfd_section_size (sect))
02c75f72
UW
319 break;
320 }
321 if (!sect)
322 continue;
323
828cfa8d
JB
324 /* On ia64-hpux, we have discovered that the system linker
325 adds undefined symbols with nonzero addresses that cannot
326 be right (their address points inside the code of another
327 function in the .text section). This creates problems
328 when trying to determine which symbol corresponds to
329 a given address.
330
331 We try to detect those buggy symbols by checking which
332 section we think they correspond to. Normally, PLT symbols
333 are stored inside their own section, and the typical name
334 for that section is ".plt". So, if there is a ".plt"
335 section, and yet the section name of our symbol does not
336 start with ".plt", we ignore that symbol. */
61012eef 337 if (!startswith (sect->name, ".plt")
828cfa8d
JB
338 && bfd_get_section_by_name (abfd, ".plt") != NULL)
339 continue;
340
0cc7b392 341 msym = record_minimal_symbol
31edb802 342 (reader, sym->name, copy_names,
9675da25
TT
343 unrelocated_addr (symaddr),
344 mst_solib_trampoline, sect, objfile);
0cc7b392 345 if (msym != NULL)
9b807e7b
MR
346 {
347 msym->filename = filesymname;
3e29f34a
MR
348 if (elf_make_msymbol_special_p)
349 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
9b807e7b 350 }
0cc7b392
DJ
351 continue;
352 }
c906108c 353
0cc7b392
DJ
354 /* If it is a nonstripped executable, do not enter dynamic
355 symbols, as the dynamic symbol table is usually a subset
356 of the main symbol table. */
6f610d07 357 if (type == ST_DYNAMIC && !stripped)
0cc7b392
DJ
358 continue;
359 if (sym->flags & BSF_FILE)
be1e3d3e 360 filesymname = objfile->intern (sym->name);
0cc7b392
DJ
361 else if (sym->flags & BSF_SECTION_SYM)
362 continue;
bb869963
SDJ
363 else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK
364 | BSF_GNU_UNIQUE))
0cc7b392
DJ
365 {
366 struct minimal_symbol *msym;
367
368 /* Select global/local/weak symbols. Note that bfd puts abs
369 symbols in their own section, so all symbols we are
0963b4bd
MS
370 interested in will have a section. */
371 /* Bfd symbols are section relative. */
0cc7b392 372 symaddr = sym->value + sym->section->vma;
0cc7b392
DJ
373 /* For non-absolute symbols, use the type of the section
374 they are relative to, to intuit text/data. Bfd provides
0963b4bd 375 no way of figuring this out for absolute symbols. */
45dfa85a 376 if (sym->section == bfd_abs_section_ptr)
c906108c 377 {
0cc7b392
DJ
378 /* This is a hack to get the minimal symbol type
379 right for Irix 5, which has absolute addresses
6f610d07
UW
380 with special section indices for dynamic symbols.
381
382 NOTE: uweigand-20071112: Synthetic symbols do not
383 have an ELF-private part, so do not touch those. */
dea91a5c 384 unsigned int shndx = type == ST_SYNTHETIC ? 0 :
28a4e64d 385 elf_sym->internal_elf_sym.st_shndx;
0cc7b392
DJ
386
387 switch (shndx)
c906108c 388 {
0cc7b392
DJ
389 case SHN_MIPS_TEXT:
390 ms_type = mst_text;
391 break;
392 case SHN_MIPS_DATA:
393 ms_type = mst_data;
394 break;
395 case SHN_MIPS_ACOMMON:
396 ms_type = mst_bss;
397 break;
398 default:
399 ms_type = mst_abs;
400 }
401
402 /* If it is an Irix dynamic symbol, skip section name
0963b4bd 403 symbols, relocate all others by section offset. */
0cc7b392
DJ
404 if (ms_type != mst_abs)
405 {
406 if (sym->name[0] == '.')
407 continue;
c906108c 408 }
0cc7b392
DJ
409 }
410 else if (sym->section->flags & SEC_CODE)
411 {
bb869963 412 if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
c906108c 413 {
0875794a
JK
414 if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
415 ms_type = mst_text_gnu_ifunc;
416 else
417 ms_type = mst_text;
0cc7b392 418 }
90359a16
JK
419 /* The BSF_SYNTHETIC check is there to omit ppc64 function
420 descriptors mistaken for static functions starting with 'L'.
421 */
422 else if ((sym->name[0] == '.' && sym->name[1] == 'L'
423 && (sym->flags & BSF_SYNTHETIC) == 0)
0cc7b392
DJ
424 || ((sym->flags & BSF_LOCAL)
425 && sym->name[0] == '$'
426 && sym->name[1] == 'L'))
427 /* Looks like a compiler-generated label. Skip
428 it. The assembler should be skipping these (to
429 keep executables small), but apparently with
430 gcc on the (deleted) delta m88k SVR4, it loses.
431 So to have us check too should be harmless (but
432 I encourage people to fix this in the assembler
433 instead of adding checks here). */
434 continue;
435 else
436 {
437 ms_type = mst_file_text;
c906108c 438 }
0cc7b392
DJ
439 }
440 else if (sym->section->flags & SEC_ALLOC)
441 {
bb869963 442 if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
c906108c 443 {
f50776aa
PA
444 if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
445 {
446 ms_type = mst_data_gnu_ifunc;
447 }
448 else if (sym->section->flags & SEC_LOAD)
c906108c 449 {
0cc7b392 450 ms_type = mst_data;
c906108c 451 }
c906108c
SS
452 else
453 {
0cc7b392 454 ms_type = mst_bss;
c906108c
SS
455 }
456 }
0cc7b392 457 else if (sym->flags & BSF_LOCAL)
c906108c 458 {
0cc7b392
DJ
459 if (sym->section->flags & SEC_LOAD)
460 {
461 ms_type = mst_file_data;
c906108c
SS
462 }
463 else
464 {
0cc7b392 465 ms_type = mst_file_bss;
c906108c
SS
466 }
467 }
468 else
469 {
0cc7b392 470 ms_type = mst_unknown;
c906108c 471 }
0cc7b392
DJ
472 }
473 else
474 {
475 /* FIXME: Solaris2 shared libraries include lots of
dea91a5c 476 odd "absolute" and "undefined" symbols, that play
0cc7b392
DJ
477 hob with actions like finding what function the PC
478 is in. Ignore them if they aren't text, data, or bss. */
479 /* ms_type = mst_unknown; */
0963b4bd 480 continue; /* Skip this symbol. */
0cc7b392
DJ
481 }
482 msym = record_minimal_symbol
9675da25 483 (reader, sym->name, copy_names, unrelocated_addr (symaddr),
0cc7b392 484 ms_type, sym->section, objfile);
6f610d07 485
0cc7b392
DJ
486 if (msym)
487 {
6f610d07 488 /* NOTE: uweigand-20071112: A synthetic symbol does not have an
24c274a1 489 ELF-private part. */
6f610d07 490 if (type != ST_SYNTHETIC)
24c274a1
AM
491 {
492 /* Pass symbol size field in via BFD. FIXME!!! */
5bbfd12d 493 msym->set_size (elf_sym->internal_elf_sym.st_size);
24c274a1 494 }
dea91a5c 495
a103a963 496 msym->filename = filesymname;
3e29f34a
MR
497 if (elf_make_msymbol_special_p)
498 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
0cc7b392 499 }
2eaf8d2a 500
715c6909
TT
501 /* If we see a default versioned symbol, install it under
502 its version-less name. */
503 if (msym != NULL)
504 {
505 const char *atsign = strchr (sym->name, '@');
28a4e64d
TT
506 bool is_at_symbol = atsign != nullptr && atsign > sym->name;
507 bool is_plt = is_at_symbol && strcmp (atsign, "@plt") == 0;
508 int len = is_at_symbol ? atsign - sym->name : 0;
509
510 if (is_at_symbol
511 && !is_plt
512 && (elf_sym->version & VERSYM_HIDDEN) == 0)
513 record_minimal_symbol (reader,
514 gdb::string_view (sym->name, len),
9675da25
TT
515 true, unrelocated_addr (symaddr),
516 ms_type, sym->section, objfile);
28a4e64d 517 else if (is_plt)
2eaf8d2a 518 {
28a4e64d
TT
519 /* For @plt symbols, also record a trampoline to the
520 destination symbol. The @plt symbol will be used
521 in disassembly, and the trampoline will be used
522 when we are trying to find the target. */
523 if (ms_type == mst_text && type == ST_SYNTHETIC)
2eaf8d2a 524 {
28a4e64d
TT
525 struct minimal_symbol *mtramp;
526
527 mtramp = record_minimal_symbol
528 (reader, gdb::string_view (sym->name, len), true,
9675da25
TT
529 unrelocated_addr (symaddr),
530 mst_solib_trampoline, sym->section, objfile);
28a4e64d
TT
531 if (mtramp)
532 {
5bbfd12d 533 mtramp->set_size (msym->size());
28a4e64d
TT
534 mtramp->created_by_gdb = 1;
535 mtramp->filename = filesymname;
536 if (elf_make_msymbol_special_p)
537 gdbarch_elf_make_msymbol_special (gdbarch,
538 sym, mtramp);
539 }
2eaf8d2a
DJ
540 }
541 }
542 }
c906108c 543 }
c906108c
SS
544 }
545}
546
07be84bf
JK
547/* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
548 for later look ups of which function to call when user requests
549 a STT_GNU_IFUNC function. As the STT_GNU_IFUNC type is found at the target
550 library defining `function' we cannot yet know while reading OBJFILE which
551 of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
552 DYN_SYMBOL_TABLE is no longer easily available for OBJFILE. */
553
554static void
8dddcb8f
TT
555elf_rel_plt_read (minimal_symbol_reader &reader,
556 struct objfile *objfile, asymbol **dyn_symbol_table)
07be84bf 557{
98badbfd 558 bfd *obfd = objfile->obfd.get ();
07be84bf 559 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
02e169e2 560 asection *relplt, *got_plt;
07be84bf 561 bfd_size_type reloc_count, reloc;
08feed99 562 struct gdbarch *gdbarch = objfile->arch ();
07be84bf 563 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
df86565b 564 size_t ptr_size = ptr_type->length ();
07be84bf
JK
565
566 if (objfile->separate_debug_objfile_backlink)
567 return;
568
07be84bf
JK
569 got_plt = bfd_get_section_by_name (obfd, ".got.plt");
570 if (got_plt == NULL)
4b7d1f7f
WN
571 {
572 /* For platforms where there is no separate .got.plt. */
573 got_plt = bfd_get_section_by_name (obfd, ".got");
574 if (got_plt == NULL)
575 return;
576 }
07be84bf 577
02e169e2
PA
578 /* Depending on system, we may find jump slots in a relocation
579 section for either .got.plt or .plt. */
580 asection *plt = bfd_get_section_by_name (obfd, ".plt");
581 int plt_elf_idx = (plt != NULL) ? elf_section_data (plt)->this_idx : -1;
582
583 int got_plt_elf_idx = elf_section_data (got_plt)->this_idx;
584
07be84bf
JK
585 /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc. */
586 for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
02e169e2
PA
587 {
588 const auto &this_hdr = elf_section_data (relplt)->this_hdr;
589
590 if (this_hdr.sh_type == SHT_REL || this_hdr.sh_type == SHT_RELA)
591 {
592 if (this_hdr.sh_info == plt_elf_idx
593 || this_hdr.sh_info == got_plt_elf_idx)
594 break;
595 }
596 }
07be84bf
JK
597 if (relplt == NULL)
598 return;
599
600 if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
601 return;
602
26fcd5d7 603 std::string string_buffer;
07be84bf 604
02e169e2
PA
605 /* Does ADDRESS reside in SECTION of OBFD? */
606 auto within_section = [obfd] (asection *section, CORE_ADDR address)
607 {
608 if (section == NULL)
609 return false;
610
fd361982
AM
611 return (bfd_section_vma (section) <= address
612 && (address < bfd_section_vma (section)
613 + bfd_section_size (section)));
02e169e2
PA
614 };
615
07be84bf
JK
616 reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
617 for (reloc = 0; reloc < reloc_count; reloc++)
618 {
22e048c9 619 const char *name;
07be84bf
JK
620 struct minimal_symbol *msym;
621 CORE_ADDR address;
26fcd5d7 622 const char *got_suffix = SYMBOL_GOT_PLT_SUFFIX;
07be84bf 623 const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
07be84bf
JK
624
625 name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
07be84bf
JK
626 address = relplt->relocation[reloc].address;
627
02e169e2
PA
628 asection *msym_section;
629
630 /* Does the pointer reside in either the .got.plt or .plt
631 sections? */
632 if (within_section (got_plt, address))
633 msym_section = got_plt;
634 else if (within_section (plt, address))
635 msym_section = plt;
636 else
07be84bf
JK
637 continue;
638
f50776aa
PA
639 /* We cannot check if NAME is a reference to
640 mst_text_gnu_ifunc/mst_data_gnu_ifunc as in OBJFILE the
641 symbol is undefined and the objfile having NAME defined may
642 not yet have been loaded. */
07be84bf 643
26fcd5d7
TT
644 string_buffer.assign (name);
645 string_buffer.append (got_suffix, got_suffix + got_suffix_len);
07be84bf 646
31edb802 647 msym = record_minimal_symbol (reader, string_buffer,
9675da25
TT
648 true, unrelocated_addr (address),
649 mst_slot_got_plt, msym_section, objfile);
07be84bf 650 if (msym)
5bbfd12d 651 msym->set_size (ptr_size);
07be84bf 652 }
07be84bf
JK
653}
654
655/* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked. */
656
08b8a139 657static const registry<objfile>::key<htab, htab_deleter>
8127a2fa 658 elf_objfile_gnu_ifunc_cache_data;
07be84bf
JK
659
660/* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data. */
661
662struct elf_gnu_ifunc_cache
663{
664 /* This is always a function entry address, not a function descriptor. */
665 CORE_ADDR addr;
666
667 char name[1];
668};
669
670/* htab_hash for elf_objfile_gnu_ifunc_cache_data. */
671
672static hashval_t
673elf_gnu_ifunc_cache_hash (const void *a_voidp)
674{
9a3c8263
SM
675 const struct elf_gnu_ifunc_cache *a
676 = (const struct elf_gnu_ifunc_cache *) a_voidp;
07be84bf
JK
677
678 return htab_hash_string (a->name);
679}
680
681/* htab_eq for elf_objfile_gnu_ifunc_cache_data. */
682
683static int
684elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
685{
9a3c8263
SM
686 const struct elf_gnu_ifunc_cache *a
687 = (const struct elf_gnu_ifunc_cache *) a_voidp;
688 const struct elf_gnu_ifunc_cache *b
689 = (const struct elf_gnu_ifunc_cache *) b_voidp;
07be84bf
JK
690
691 return strcmp (a->name, b->name) == 0;
692}
693
694/* Record the target function address of a STT_GNU_IFUNC function NAME is the
695 function entry address ADDR. Return 1 if NAME and ADDR are considered as
696 valid and therefore they were successfully recorded, return 0 otherwise.
697
698 Function does not expect a duplicate entry. Use
699 elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
700 exists. */
701
702static int
703elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
704{
7cbd4a93 705 struct bound_minimal_symbol msym;
07be84bf
JK
706 struct objfile *objfile;
707 htab_t htab;
708 struct elf_gnu_ifunc_cache entry_local, *entry_p;
709 void **slot;
710
711 msym = lookup_minimal_symbol_by_pc (addr);
7cbd4a93 712 if (msym.minsym == NULL)
07be84bf 713 return 0;
4aeddc50 714 if (msym.value_address () != addr)
07be84bf 715 return 0;
e27d198c 716 objfile = msym.objfile;
07be84bf
JK
717
718 /* If .plt jumps back to .plt the symbol is still deferred for later
1adeb822 719 resolution and it has no use for GDB. */
c9d95fa3 720 const char *target_name = msym.minsym->linkage_name ();
1adeb822
PA
721 size_t len = strlen (target_name);
722
723 /* Note we check the symbol's name instead of checking whether the
724 symbol is in the .plt section because some systems have @plt
725 symbols in the .text section. */
726 if (len > 4 && strcmp (target_name + len - 4, "@plt") == 0)
07be84bf
JK
727 return 0;
728
600b5fc2
HL
729 if (strcmp (target_name, "_PROCEDURE_LINKAGE_TABLE_") == 0)
730 return 0;
731
8127a2fa 732 htab = elf_objfile_gnu_ifunc_cache_data.get (objfile);
07be84bf
JK
733 if (htab == NULL)
734 {
8127a2fa
TT
735 htab = htab_create_alloc (1, elf_gnu_ifunc_cache_hash,
736 elf_gnu_ifunc_cache_eq,
737 NULL, xcalloc, xfree);
738 elf_objfile_gnu_ifunc_cache_data.set (objfile, htab);
07be84bf
JK
739 }
740
741 entry_local.addr = addr;
742 obstack_grow (&objfile->objfile_obstack, &entry_local,
743 offsetof (struct elf_gnu_ifunc_cache, name));
744 obstack_grow_str0 (&objfile->objfile_obstack, name);
224c3ddb
SM
745 entry_p
746 = (struct elf_gnu_ifunc_cache *) obstack_finish (&objfile->objfile_obstack);
07be84bf
JK
747
748 slot = htab_find_slot (htab, entry_p, INSERT);
749 if (*slot != NULL)
750 {
9a3c8263
SM
751 struct elf_gnu_ifunc_cache *entry_found_p
752 = (struct elf_gnu_ifunc_cache *) *slot;
08feed99 753 struct gdbarch *gdbarch = objfile->arch ();
07be84bf
JK
754
755 if (entry_found_p->addr != addr)
756 {
757 /* This case indicates buggy inferior program, the resolved address
758 should never change. */
759
760 warning (_("gnu-indirect-function \"%s\" has changed its resolved "
761 "function_address from %s to %s"),
762 name, paddress (gdbarch, entry_found_p->addr),
763 paddress (gdbarch, addr));
764 }
765
766 /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack. */
767 }
768 *slot = entry_p;
769
770 return 1;
771}
772
773/* Try to find the target resolved function entry address of a STT_GNU_IFUNC
774 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
775 is not NULL) and the function returns 1. It returns 0 otherwise.
776
777 Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
778 function. */
779
780static int
781elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
782{
1dc9084f
MM
783 int found = 0;
784
785 /* FIXME: we only search the initial namespace.
786
787 To search other namespaces, we would need to provide context, e.g. in
788 form of an objfile in that namespace. */
789 gdbarch_iterate_over_objfiles_in_search_order
790 (target_gdbarch (),
791 [name, &addr_p, &found] (struct objfile *objfile)
792 {
793 htab_t htab;
794 elf_gnu_ifunc_cache *entry_p;
795 void **slot;
796
797 htab = elf_objfile_gnu_ifunc_cache_data.get (objfile);
798 if (htab == NULL)
799 return 0;
800
801 entry_p = ((elf_gnu_ifunc_cache *)
802 alloca (sizeof (*entry_p) + strlen (name)));
803 strcpy (entry_p->name, name);
804
805 slot = htab_find_slot (htab, entry_p, NO_INSERT);
806 if (slot == NULL)
807 return 0;
808 entry_p = (elf_gnu_ifunc_cache *) *slot;
809 gdb_assert (entry_p != NULL);
810
811 if (addr_p)
812 *addr_p = entry_p->addr;
813
814 found = 1;
815 return 1;
816 }, nullptr);
817
818 return found;
07be84bf
JK
819}
820
821/* Try to find the target resolved function entry address of a STT_GNU_IFUNC
822 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
823 is not NULL) and the function returns 1. It returns 0 otherwise.
824
825 Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
826 elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
827 prevent cache entries duplicates. */
828
829static int
830elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
831{
832 char *name_got_plt;
07be84bf 833 const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
1dc9084f 834 int found = 0;
07be84bf 835
224c3ddb 836 name_got_plt = (char *) alloca (strlen (name) + got_suffix_len + 1);
07be84bf
JK
837 sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
838
1dc9084f
MM
839 /* FIXME: we only search the initial namespace.
840
841 To search other namespaces, we would need to provide context, e.g. in
842 form of an objfile in that namespace. */
843 gdbarch_iterate_over_objfiles_in_search_order
844 (target_gdbarch (),
845 [name, name_got_plt, &addr_p, &found] (struct objfile *objfile)
846 {
847 bfd *obfd = objfile->obfd.get ();
848 struct gdbarch *gdbarch = objfile->arch ();
849 type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
850 size_t ptr_size = ptr_type->length ();
851 CORE_ADDR pointer_address, addr;
852 asection *plt;
853 gdb_byte *buf = (gdb_byte *) alloca (ptr_size);
854 bound_minimal_symbol msym;
855
856 msym = lookup_minimal_symbol (name_got_plt, NULL, objfile);
857 if (msym.minsym == NULL)
858 return 0;
859 if (msym.minsym->type () != mst_slot_got_plt)
860 return 0;
861 pointer_address = msym.value_address ();
862
863 plt = bfd_get_section_by_name (obfd, ".plt");
864 if (plt == NULL)
865 return 0;
866
867 if (msym.minsym->size () != ptr_size)
868 return 0;
869 if (target_read_memory (pointer_address, buf, ptr_size) != 0)
870 return 0;
871 addr = extract_typed_address (buf, ptr_type);
872 addr = gdbarch_convert_from_func_ptr_addr
873 (gdbarch, addr, current_inferior ()->top_target ());
874 addr = gdbarch_addr_bits_remove (gdbarch, addr);
875
876 if (elf_gnu_ifunc_record_cache (name, addr))
877 {
878 if (addr_p != NULL)
879 *addr_p = addr;
880
881 found = 1;
882 return 1;
883 }
884
885 return 0;
886 }, nullptr);
887
888 return found;
07be84bf
JK
889}
890
891/* Try to find the target resolved function entry address of a STT_GNU_IFUNC
892 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
ececd218 893 is not NULL) and the function returns true. It returns false otherwise.
07be84bf
JK
894
895 Both the elf_objfile_gnu_ifunc_cache_data hash table and
896 SYMBOL_GOT_PLT_SUFFIX locations are searched by this function. */
897
ececd218 898static bool
07be84bf
JK
899elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
900{
901 if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
ececd218 902 return true;
dea91a5c 903
07be84bf 904 if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
ececd218 905 return true;
07be84bf 906
ececd218 907 return false;
07be84bf
JK
908}
909
910/* Call STT_GNU_IFUNC - a function returning addresss of a real function to
911 call. PC is theSTT_GNU_IFUNC resolving function entry. The value returned
912 is the entry point of the resolved STT_GNU_IFUNC target function to call.
913 */
914
915static CORE_ADDR
916elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
917{
2c02bd72 918 const char *name_at_pc;
07be84bf
JK
919 CORE_ADDR start_at_pc, address;
920 struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
921 struct value *function, *address_val;
e1b2624a
AA
922 CORE_ADDR hwcap = 0;
923 struct value *hwcap_val;
07be84bf
JK
924
925 /* Try first any non-intrusive methods without an inferior call. */
926
927 if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
928 && start_at_pc == pc)
929 {
930 if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
931 return address;
932 }
933 else
934 name_at_pc = NULL;
935
317c3ed9 936 function = value::allocate (func_func_type);
6f9c9d71 937 function->set_lval (lval_memory);
9feb2d07 938 function->set_address (pc);
07be84bf 939
e1b2624a
AA
940 /* STT_GNU_IFUNC resolver functions usually receive the HWCAP vector as
941 parameter. FUNCTION is the function entry address. ADDRESS may be a
942 function descriptor. */
07be84bf 943
82d23ca8 944 target_auxv_search (AT_HWCAP, &hwcap);
e1b2624a
AA
945 hwcap_val = value_from_longest (builtin_type (gdbarch)
946 ->builtin_unsigned_long, hwcap);
e71585ff 947 address_val = call_function_by_hand (function, NULL, hwcap_val);
07be84bf 948 address = value_as_address (address_val);
328d42d8
SM
949 address = gdbarch_convert_from_func_ptr_addr
950 (gdbarch, address, current_inferior ()->top_target ());
4b7d1f7f 951 address = gdbarch_addr_bits_remove (gdbarch, address);
07be84bf
JK
952
953 if (name_at_pc)
954 elf_gnu_ifunc_record_cache (name_at_pc, address);
955
956 return address;
957}
958
0e30163f
JK
959/* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition. */
960
961static void
74421c0b 962elf_gnu_ifunc_resolver_stop (code_breakpoint *b)
0e30163f
JK
963{
964 struct breakpoint *b_return;
bd2b40ac 965 frame_info_ptr prev_frame = get_prev_frame (get_current_frame ());
0e30163f
JK
966 struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
967 CORE_ADDR prev_pc = get_frame_pc (prev_frame);
00431a78 968 int thread_id = inferior_thread ()->global_num;
0e30163f
JK
969
970 gdb_assert (b->type == bp_gnu_ifunc_resolver);
971
972 for (b_return = b->related_breakpoint; b_return != b;
973 b_return = b_return->related_breakpoint)
974 {
975 gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
9dc1523b 976 gdb_assert (b_return->has_single_location ());
0e30163f
JK
977 gdb_assert (frame_id_p (b_return->frame_id));
978
979 if (b_return->thread == thread_id
f5951b9f 980 && b_return->first_loc ().requested_address == prev_pc
a0cbd650 981 && b_return->frame_id == prev_frame_id)
0e30163f
JK
982 break;
983 }
984
985 if (b_return == b)
986 {
0e30163f
JK
987 /* No need to call find_pc_line for symbols resolving as this is only
988 a helper breakpointer never shown to the user. */
989
51abb421 990 symtab_and_line sal;
0e30163f
JK
991 sal.pspace = current_inferior ()->pspace;
992 sal.pc = prev_pc;
993 sal.section = find_pc_overlay (sal.pc);
994 sal.explicit_pc = 1;
454dafbd
TT
995 b_return
996 = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
997 prev_frame_id,
998 bp_gnu_ifunc_resolver_return).release ();
0e30163f 999
c70a6932
JK
1000 /* set_momentary_breakpoint invalidates PREV_FRAME. */
1001 prev_frame = NULL;
1002
0e30163f
JK
1003 /* Add new b_return to the ring list b->related_breakpoint. */
1004 gdb_assert (b_return->related_breakpoint == b_return);
1005 b_return->related_breakpoint = b->related_breakpoint;
1006 b->related_breakpoint = b_return;
1007 }
1008}
1009
1010/* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition. */
1011
1012static void
74421c0b 1013elf_gnu_ifunc_resolver_return_stop (code_breakpoint *b)
0e30163f 1014{
00431a78 1015 thread_info *thread = inferior_thread ();
0e30163f
JK
1016 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
1017 struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
27710edb 1018 struct type *value_type = func_func_type->target_type ();
00431a78 1019 struct regcache *regcache = get_thread_regcache (thread);
6a3a010b 1020 struct value *func_func;
0e30163f
JK
1021 struct value *value;
1022 CORE_ADDR resolved_address, resolved_pc;
0e30163f
JK
1023
1024 gdb_assert (b->type == bp_gnu_ifunc_resolver_return);
1025
0e30163f
JK
1026 while (b->related_breakpoint != b)
1027 {
1028 struct breakpoint *b_next = b->related_breakpoint;
1029
1030 switch (b->type)
1031 {
1032 case bp_gnu_ifunc_resolver:
1033 break;
1034 case bp_gnu_ifunc_resolver_return:
1035 delete_breakpoint (b);
1036 break;
1037 default:
f34652de 1038 internal_error (_("handle_inferior_event: Invalid "
0e30163f
JK
1039 "gnu-indirect-function breakpoint type %d"),
1040 (int) b->type);
1041 }
74421c0b 1042 b = (code_breakpoint *) b_next;
0e30163f
JK
1043 }
1044 gdb_assert (b->type == bp_gnu_ifunc_resolver);
9dc1523b 1045 gdb_assert (b->has_single_location ());
6a3a010b 1046
317c3ed9 1047 func_func = value::allocate (func_func_type);
6f9c9d71 1048 func_func->set_lval (lval_memory);
f5951b9f 1049 func_func->set_address (b->first_loc ().related_address);
6a3a010b 1050
317c3ed9 1051 value = value::allocate (value_type);
4e1d2f58
TT
1052 gdbarch_return_value_as_value (gdbarch, func_func, value_type, regcache,
1053 &value, NULL);
6a3a010b 1054 resolved_address = value_as_address (value);
328d42d8
SM
1055 resolved_pc = gdbarch_convert_from_func_ptr_addr
1056 (gdbarch, resolved_address, current_inferior ()->top_target ());
4b7d1f7f 1057 resolved_pc = gdbarch_addr_bits_remove (gdbarch, resolved_pc);
0e30163f 1058
f8eba3c6 1059 gdb_assert (current_program_space == b->pspace || b->pspace == NULL);
709438c7 1060 elf_gnu_ifunc_record_cache (b->locspec->to_string (), resolved_pc);
0e30163f 1061
0e30163f 1062 b->type = bp_breakpoint;
6c5b2ebe 1063 update_breakpoint_locations (b, current_program_space,
79188d8d
PA
1064 find_function_start_sal (resolved_pc, NULL, true),
1065 {});
0e30163f
JK
1066}
1067
2750ef27
TT
1068/* A helper function for elf_symfile_read that reads the minimal
1069 symbols. */
c906108c
SS
1070
1071static void
5f6cac40
TT
1072elf_read_minimal_symbols (struct objfile *objfile, int symfile_flags,
1073 const struct elfinfo *ei)
c906108c 1074{
98badbfd 1075 bfd *synth_abfd, *abfd = objfile->obfd.get ();
62553543
EZ
1076 long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
1077 asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
1078 asymbol *synthsyms;
c906108c 1079
2ab317fb
SM
1080 symtab_create_debug_printf ("reading minimal symbols of objfile %s",
1081 objfile_name (objfile));
45cfd468 1082
5f6cac40
TT
1083 /* If we already have minsyms, then we can skip some work here.
1084 However, if there were stabs or mdebug sections, we go ahead and
1085 redo all the work anyway, because the psym readers for those
1086 kinds of debuginfo need extra information found here. This can
1087 go away once all types of symbols are in the per-BFD object. */
1088 if (objfile->per_bfd->minsyms_read
1089 && ei->stabsect == NULL
30d1f018
WP
1090 && ei->mdebugsect == NULL
1091 && ei->ctfsect == NULL)
5f6cac40 1092 {
2ab317fb 1093 symtab_create_debug_printf ("minimal symbols were previously read");
5f6cac40
TT
1094 return;
1095 }
1096
d25e8719 1097 minimal_symbol_reader reader (objfile);
c906108c 1098
18a94d75 1099 /* Process the normal ELF symbol table first. */
c906108c 1100
98badbfd 1101 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
62553543 1102 if (storage_needed < 0)
3e43a32a 1103 error (_("Can't read symbols from %s: %s"),
98badbfd 1104 bfd_get_filename (objfile->obfd.get ()),
62553543
EZ
1105 bfd_errmsg (bfd_get_error ()));
1106
1107 if (storage_needed > 0)
1108 {
80c57053
JK
1109 /* Memory gets permanently referenced from ABFD after
1110 bfd_canonicalize_symtab so it must not get freed before ABFD gets. */
1111
224c3ddb 1112 symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
98badbfd 1113 symcount = bfd_canonicalize_symtab (objfile->obfd.get (), symbol_table);
62553543
EZ
1114
1115 if (symcount < 0)
3e43a32a 1116 error (_("Can't read symbols from %s: %s"),
98badbfd 1117 bfd_get_filename (objfile->obfd.get ()),
62553543
EZ
1118 bfd_errmsg (bfd_get_error ()));
1119
ce6c454e
TT
1120 elf_symtab_read (reader, objfile, ST_REGULAR, symcount, symbol_table,
1121 false);
62553543 1122 }
c906108c
SS
1123
1124 /* Add the dynamic symbols. */
1125
98badbfd 1126 storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd.get ());
62553543
EZ
1127
1128 if (storage_needed > 0)
1129 {
3f1eff0a
JK
1130 /* Memory gets permanently referenced from ABFD after
1131 bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
1132 It happens only in the case when elf_slurp_reloc_table sees
1133 asection->relocation NULL. Determining which section is asection is
1134 done by _bfd_elf_get_synthetic_symtab which is all a bfd
1135 implementation detail, though. */
1136
224c3ddb 1137 dyn_symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
98badbfd 1138 dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd.get (),
62553543
EZ
1139 dyn_symbol_table);
1140
1141 if (dynsymcount < 0)
3e43a32a 1142 error (_("Can't read symbols from %s: %s"),
98badbfd 1143 bfd_get_filename (objfile->obfd.get ()),
62553543
EZ
1144 bfd_errmsg (bfd_get_error ()));
1145
8dddcb8f 1146 elf_symtab_read (reader, objfile, ST_DYNAMIC, dynsymcount,
ce6c454e 1147 dyn_symbol_table, false);
07be84bf 1148
8dddcb8f 1149 elf_rel_plt_read (reader, objfile, dyn_symbol_table);
62553543
EZ
1150 }
1151
63524580
JK
1152 /* Contrary to binutils --strip-debug/--only-keep-debug the strip command from
1153 elfutils (eu-strip) moves even the .symtab section into the .debug file.
1154
1155 bfd_get_synthetic_symtab on ppc64 for each function descriptor ELF symbol
1156 'name' creates a new BSF_SYNTHETIC ELF symbol '.name' with its code
1157 address. But with eu-strip files bfd_get_synthetic_symtab would fail to
1158 read the code address from .opd while it reads the .symtab section from
1159 a separate debug info file as the .opd section is SHT_NOBITS there.
1160
1161 With SYNTH_ABFD the .opd section will be read from the original
1162 backlinked binary where it is valid. */
1163
1164 if (objfile->separate_debug_objfile_backlink)
98badbfd 1165 synth_abfd = objfile->separate_debug_objfile_backlink->obfd.get ();
63524580
JK
1166 else
1167 synth_abfd = abfd;
1168
62553543
EZ
1169 /* Add synthetic symbols - for instance, names for any PLT entries. */
1170
63524580 1171 synthcount = bfd_get_synthetic_symtab (synth_abfd, symcount, symbol_table,
62553543
EZ
1172 dynsymcount, dyn_symbol_table,
1173 &synthsyms);
1174 if (synthcount > 0)
1175 {
62553543
EZ
1176 long i;
1177
b22e99fd 1178 std::unique_ptr<asymbol *[]>
d1e4a624 1179 synth_symbol_table (new asymbol *[synthcount]);
62553543 1180 for (i = 0; i < synthcount; i++)
9f20e3da 1181 synth_symbol_table[i] = synthsyms + i;
8dddcb8f 1182 elf_symtab_read (reader, objfile, ST_SYNTHETIC, synthcount,
ce6c454e 1183 synth_symbol_table.get (), true);
ba713918
AL
1184
1185 xfree (synthsyms);
1186 synthsyms = NULL;
62553543 1187 }
c906108c 1188
7134143f
DJ
1189 /* Install any minimal symbols that have been collected as the current
1190 minimal symbols for this objfile. The debug readers below this point
1191 should not generate new minimal symbols; if they do it's their
1192 responsibility to install them. "mdebug" appears to be the only one
1193 which will do this. */
1194
d25e8719 1195 reader.install ();
7134143f 1196
2ab317fb 1197 symtab_create_debug_printf ("done reading minimal symbols");
2750ef27
TT
1198}
1199
d955d179
TV
1200/* Dwarf-specific helper for elf_symfile_read. Return true if we managed to
1201 load dwarf debug info. */
1202
1203static bool
1204elf_symfile_read_dwarf2 (struct objfile *objfile,
1205 symfile_add_flags symfile_flags)
1206{
1207 bool has_dwarf2 = true;
1208
1209 if (dwarf2_has_info (objfile, NULL, true))
1210 dwarf2_initialize_objfile (objfile);
1211 /* If the file has its own symbol tables it has no separate debug
1212 info. `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
1213 SYMTABS/PSYMTABS. `.gnu_debuglink' may no longer be present with
1214 `.note.gnu.build-id'.
1215
1216 .gnu_debugdata is !objfile::has_partial_symbols because it contains only
1217 .symtab, not .debug_* section. But if we already added .gnu_debugdata as
1218 an objfile via find_separate_debug_file_in_section there was no separate
1219 debug info available. Therefore do not attempt to search for another one,
1220 objfile->separate_debug_objfile->separate_debug_objfile GDB guarantees to
1221 be NULL and we would possibly violate it. */
1222
1223 else if (!objfile->has_partial_symbols ()
1224 && objfile->separate_debug_objfile == NULL
1225 && objfile->separate_debug_objfile_backlink == NULL)
1226 {
34f997c8 1227 deferred_warnings warnings;
6647f05d
AH
1228
1229 std::string debugfile
34f997c8 1230 = find_separate_debug_file_by_buildid (objfile, &warnings);
d955d179
TV
1231
1232 if (debugfile.empty ())
34f997c8 1233 debugfile = find_separate_debug_file_by_debuglink (objfile, &warnings);
d955d179
TV
1234
1235 if (!debugfile.empty ())
1236 {
f96328ac
TT
1237 gdb_bfd_ref_ptr debug_bfd
1238 (symfile_bfd_open_no_error (debugfile.c_str ()));
d955d179 1239
f96328ac
TT
1240 if (debug_bfd != nullptr)
1241 symbol_file_add_separate (debug_bfd, debugfile.c_str (),
1242 symfile_flags, objfile);
d955d179
TV
1243 }
1244 else
1245 {
1246 has_dwarf2 = false;
1247 const struct bfd_build_id *build_id
1248 = build_id_bfd_get (objfile->obfd.get ());
1249 const char *filename = bfd_get_filename (objfile->obfd.get ());
1250
1251 if (build_id != nullptr)
1252 {
1253 gdb::unique_xmalloc_ptr<char> symfile_path;
1254 scoped_fd fd (debuginfod_debuginfo_query (build_id->data,
1255 build_id->size,
1256 filename,
1257 &symfile_path));
1258
1259 if (fd.get () >= 0)
1260 {
1261 /* File successfully retrieved from server. */
f96328ac
TT
1262 gdb_bfd_ref_ptr debug_bfd
1263 (symfile_bfd_open_no_error (symfile_path.get ()));
d955d179 1264
f96328ac
TT
1265 if (debug_bfd != nullptr
1266 && build_id_verify (debug_bfd.get (), build_id->size,
1267 build_id->data))
d955d179
TV
1268 {
1269 symbol_file_add_separate (debug_bfd, symfile_path.get (),
1270 symfile_flags, objfile);
1271 has_dwarf2 = true;
1272 }
1273 }
1274 }
1275 }
34f997c8
AB
1276 /* If all the methods to collect the debuginfo failed, print the
1277 warnings, this is a no-op if there are no warnings. */
1278 if (debugfile.empty () && !has_dwarf2)
1279 warnings.emit ();
d955d179
TV
1280 }
1281
1282 return has_dwarf2;
1283}
1284
2750ef27
TT
1285/* Scan and build partial symbols for a symbol file.
1286 We have been initialized by a call to elf_symfile_init, which
1287 currently does nothing.
1288
2750ef27
TT
1289 This function only does the minimum work necessary for letting the
1290 user "name" things symbolically; it does not read the entire symtab.
1291 Instead, it reads the external and static symbols and puts them in partial
1292 symbol tables. When more extensive information is requested of a
1293 file, the corresponding partial symbol table is mutated into a full
1294 fledged symbol table by going back and reading the symbols
1295 for real.
1296
1297 We look for sections with specific names, to tell us what debug
1298 format to look for: FIXME!!!
1299
1300 elfstab_build_psymtabs() handles STABS symbols;
1301 mdebug_build_psymtabs() handles ECOFF debugging information.
1302
1303 Note that ELF files have a "minimal" symbol table, which looks a lot
1304 like a COFF symbol table, but has only the minimal information necessary
1305 for linking. We process this also, and use the information to
1306 build gdb's minimal symbol table. This gives us some minimal debugging
1307 capability even for files compiled without -g. */
1308
1309static void
b15cc25c 1310elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
2750ef27 1311{
98badbfd 1312 bfd *abfd = objfile->obfd.get ();
2750ef27
TT
1313 struct elfinfo ei;
1314
2750ef27 1315 memset ((char *) &ei, 0, sizeof (ei));
97cbe998 1316 if (!(objfile->flags & OBJF_READNEVER))
08f93a1a
TT
1317 {
1318 for (asection *sect : gdb_bfd_sections (abfd))
1319 elf_locate_sections (sect, &ei);
1320 }
c906108c 1321
5f6cac40
TT
1322 elf_read_minimal_symbols (objfile, symfile_flags, &ei);
1323
c906108c
SS
1324 /* ELF debugging information is inserted into the psymtab in the
1325 order of least informative first - most informative last. Since
1326 the psymtab table is searched `most recent insertion first' this
1327 increases the probability that more detailed debug information
1328 for a section is found.
1329
1330 For instance, an object file might contain both .mdebug (XCOFF)
1331 and .debug_info (DWARF2) sections then .mdebug is inserted first
1332 (searched last) and DWARF2 is inserted last (searched first). If
1333 we don't do this then the XCOFF info is found first - for code in
0963b4bd 1334 an included file XCOFF info is useless. */
c906108c
SS
1335
1336 if (ei.mdebugsect)
1337 {
1338 const struct ecoff_debug_swap *swap;
1339
1340 /* .mdebug section, presumably holding ECOFF debugging
dda83cd7 1341 information. */
c906108c
SS
1342 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1343 if (swap)
d4f3574e 1344 elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
c906108c
SS
1345 }
1346 if (ei.stabsect)
1347 {
1348 asection *str_sect;
1349
1350 /* Stab sections have an associated string table that looks like
dda83cd7 1351 a separate section. */
c906108c
SS
1352 str_sect = bfd_get_section_by_name (abfd, ".stabstr");
1353
1354 /* FIXME should probably warn about a stab section without a stabstr. */
1355 if (str_sect)
1356 elfstab_build_psymtabs (objfile,
086df311 1357 ei.stabsect,
c906108c 1358 str_sect->filepos,
fd361982 1359 bfd_section_size (str_sect));
c906108c 1360 }
9291a0cd 1361
0d5adb56
TV
1362 /* Read the CTF section only if there is no DWARF info. */
1363 if (always_read_ctf && ei.ctfsect)
1364 {
1365 elfctf_build_psymtabs (objfile);
1366 }
1367
d955d179 1368 bool has_dwarf2 = elf_symfile_read_dwarf2 (objfile, symfile_flags);
30d1f018
WP
1369
1370 /* Read the CTF section only if there is no DWARF info. */
0d5adb56 1371 if (!always_read_ctf && !has_dwarf2 && ei.ctfsect)
30d1f018
WP
1372 {
1373 elfctf_build_psymtabs (objfile);
9cce227f 1374 }
c906108c
SS
1375}
1376
c906108c
SS
1377/* Initialize anything that needs initializing when a completely new symbol
1378 file is specified (not just adding some symbols from another file, e.g. a
caa429d8 1379 shared library). */
c906108c
SS
1380
1381static void
fba45db2 1382elf_new_init (struct objfile *ignore)
c906108c 1383{
c906108c
SS
1384}
1385
1386/* Perform any local cleanups required when we are done with a particular
1387 objfile. I.E, we are in the process of discarding all symbol information
1388 for an objfile, freeing up all memory held for it, and unlinking the
0963b4bd 1389 objfile struct from the global list of known objfiles. */
c906108c
SS
1390
1391static void
fba45db2 1392elf_symfile_finish (struct objfile *objfile)
c906108c 1393{
c906108c
SS
1394}
1395
db7a9bcd 1396/* ELF specific initialization routine for reading symbols. */
c906108c
SS
1397
1398static void
fba45db2 1399elf_symfile_init (struct objfile *objfile)
c906108c 1400{
c906108c
SS
1401}
1402
55aa24fb
SDJ
1403/* Implementation of `sym_get_probes', as documented in symfile.h. */
1404
814cf43a 1405static const elfread_data &
55aa24fb
SDJ
1406elf_get_probes (struct objfile *objfile)
1407{
98badbfd 1408 elfread_data *probes_per_bfd = probe_key.get (objfile->obfd.get ());
55aa24fb 1409
aaa63a31 1410 if (probes_per_bfd == NULL)
55aa24fb 1411 {
98badbfd 1412 probes_per_bfd = probe_key.emplace (objfile->obfd.get ());
55aa24fb
SDJ
1413
1414 /* Here we try to gather information about all types of probes from the
1415 objfile. */
935676c9 1416 for (const static_probe_ops *ops : all_static_probe_ops)
0782db84 1417 ops->get_probes (probes_per_bfd, objfile);
55aa24fb
SDJ
1418 }
1419
aaa63a31 1420 return *probes_per_bfd;
55aa24fb
SDJ
1421}
1422
c906108c 1423\f
55aa24fb
SDJ
1424
1425/* Implementation `sym_probe_fns', as documented in symfile.h. */
1426
1427static const struct sym_probe_fns elf_probe_fns =
1428{
25f9533e 1429 elf_get_probes, /* sym_get_probes */
55aa24fb
SDJ
1430};
1431
c906108c
SS
1432/* Register that we are able to handle ELF object file formats. */
1433
00b5771c 1434static const struct sym_fns elf_sym_fns =
c906108c 1435{
3e43a32a
MS
1436 elf_new_init, /* init anything gbl to entire symtab */
1437 elf_symfile_init, /* read initial info, setup for sym_read() */
1438 elf_symfile_read, /* read a symbol file into symtab */
1439 elf_symfile_finish, /* finished with file, cleanup */
1440 default_symfile_offsets, /* Translate ext. to int. relocation */
1441 elf_symfile_segments, /* Get segment information from a file. */
1442 NULL,
1443 default_symfile_relocate, /* Relocate a debug section. */
55aa24fb 1444 &elf_probe_fns, /* sym_probe_fns */
927aa2e7
JK
1445};
1446
07be84bf
JK
1447/* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p. */
1448
1449static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
1450{
1451 elf_gnu_ifunc_resolve_addr,
1452 elf_gnu_ifunc_resolve_name,
0e30163f
JK
1453 elf_gnu_ifunc_resolver_stop,
1454 elf_gnu_ifunc_resolver_return_stop
07be84bf
JK
1455};
1456
6c265988 1457void _initialize_elfread ();
c906108c 1458void
6c265988 1459_initialize_elfread ()
c906108c 1460{
c256e171 1461 add_symtab_fns (bfd_target_elf_flavour, &elf_sym_fns);
07be84bf 1462
07be84bf 1463 gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
0d5adb56
TV
1464
1465 /* Add "set always-read-ctf on/off". */
1466 add_setshow_boolean_cmd ("always-read-ctf", class_support, &always_read_ctf,
1467 _("\
1468Set whether CTF is always read."),
1469 _("\
1470Show whether CTF is always read."),
1471 _("\
1472When off, CTF is only read if DWARF is not present. When on, CTF is read\
1473 regardless of whether DWARF is present."),
1474 nullptr /* set_func */, nullptr /* show_func */,
1475 &setlist, &showlist);
c906108c 1476}