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