Re: nios2: Remove binutils support for Nios II target
[binutils-gdb.git] / gdb / elfread.c
blobe81233c6be893cb5681c5aa74841b1f877a381a7
1 /* Read ELF (Executable and Linking Format) object files for GDB.
3 Copyright (C) 1991-2024 Free Software Foundation, Inc.
5 Written by Fred Fish at Cygnus Support.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "bfd.h"
23 #include "elf-bfd.h"
24 #include "elf/common.h"
25 #include "elf/internal.h"
26 #include "elf/mips.h"
27 #include "extract-store-integer.h"
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "stabsread.h"
32 #include "probe.h"
33 #include "arch-utils.h"
34 #include "gdbtypes.h"
35 #include "value.h"
36 #include "infcall.h"
37 #include "gdbthread.h"
38 #include "inferior.h"
39 #include "regcache.h"
40 #include "gdb_bfd.h"
41 #include "location.h"
42 #include "auxv.h"
43 #include "mdebugread.h"
44 #include "ctfread.h"
45 #include <string_view>
46 #include "dwarf2/public.h"
47 #include "cli/cli-cmds.h"
48 #include "gdb-stabs.h"
50 /* Whether ctf should always be read, or only if no dwarf is present. */
51 static bool always_read_ctf;
53 /* The struct elfinfo is available only during ELF symbol table and
54 psymtab reading. It is destroyed at the completion of psymtab-reading.
55 It's local to elf_symfile_read. */
57 struct elfinfo
59 asection *stabsect; /* Section pointer for .stab section */
60 asection *mdebugsect; /* Section pointer for .mdebug section */
61 asection *ctfsect; /* Section pointer for .ctf section */
64 /* Type for per-BFD data. */
66 typedef std::vector<std::unique_ptr<probe>> elfread_data;
68 /* Per-BFD data for probe info. */
70 static const registry<bfd>::key<elfread_data> probe_key;
72 /* Minimal symbols located at the GOT entries for .plt - that is the real
73 pointer where the given entry will jump to. It gets updated by the real
74 function address during lazy ld.so resolving in the inferior. These
75 minimal symbols are indexed for <tab>-completion. */
77 #define SYMBOL_GOT_PLT_SUFFIX "@got.plt"
79 /* Locate the segments in ABFD. */
81 static symfile_segment_data_up
82 elf_symfile_segments (bfd *abfd)
84 Elf_Internal_Phdr *phdrs, **segments;
85 long phdrs_size;
86 int num_phdrs, num_segments, num_sections, i;
87 asection *sect;
89 phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
90 if (phdrs_size == -1)
91 return NULL;
93 phdrs = (Elf_Internal_Phdr *) alloca (phdrs_size);
94 num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
95 if (num_phdrs == -1)
96 return NULL;
98 num_segments = 0;
99 segments = XALLOCAVEC (Elf_Internal_Phdr *, num_phdrs);
100 for (i = 0; i < num_phdrs; i++)
101 if (phdrs[i].p_type == PT_LOAD)
102 segments[num_segments++] = &phdrs[i];
104 if (num_segments == 0)
105 return NULL;
107 symfile_segment_data_up data (new symfile_segment_data);
108 data->segments.reserve (num_segments);
110 for (i = 0; i < num_segments; i++)
111 data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz);
113 num_sections = bfd_count_sections (abfd);
115 /* All elements are initialized to 0 (map to no segment). */
116 data->segment_info.resize (num_sections);
118 for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
120 int j;
122 if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
123 continue;
125 Elf_Internal_Shdr *this_hdr = &elf_section_data (sect)->this_hdr;
127 for (j = 0; j < num_segments; j++)
128 if (ELF_SECTION_IN_SEGMENT (this_hdr, segments[j]))
130 data->segment_info[i] = j + 1;
131 break;
134 /* We should have found a segment for every non-empty section.
135 If we haven't, we will not relocate this section by any
136 offsets we apply to the segments. As an exception, do not
137 warn about SHT_NOBITS sections; in normal ELF execution
138 environments, SHT_NOBITS means zero-initialized and belongs
139 in a segment, but in no-OS environments some tools (e.g. ARM
140 RealView) use SHT_NOBITS for uninitialized data. Since it is
141 uninitialized, it doesn't need a program header. Such
142 binaries are not relocatable. */
144 /* Exclude debuginfo files from this warning, too, since those
145 are often not strictly compliant with the standard. See, e.g.,
146 ld/24717 for more discussion. */
147 if (!is_debuginfo_file (abfd)
148 && bfd_section_size (sect) > 0 && j == num_segments
149 && (bfd_section_flags (sect) & SEC_LOAD) != 0)
150 warning (_("Loadable section \"%s\" outside of ELF segments\n in %s"),
151 bfd_section_name (sect), bfd_get_filename (abfd));
154 return data;
157 /* We are called once per section from elf_symfile_read. We
158 need to examine each section we are passed, check to see
159 if it is something we are interested in processing, and
160 if so, stash away some access information for the section.
162 For now we recognize the dwarf debug information sections and
163 line number sections from matching their section names. The
164 ELF definition is no real help here since it has no direct
165 knowledge of DWARF (by design, so any debugging format can be
166 used).
168 We also recognize the ".stab" sections used by the Sun compilers
169 released with Solaris 2.
171 FIXME: The section names should not be hardwired strings (what
172 should they be? I don't think most object file formats have enough
173 section flags to specify what kind of debug section it is.
174 -kingdon). */
176 static void
177 elf_locate_sections (asection *sectp, struct elfinfo *ei)
179 if (strcmp (sectp->name, ".stab") == 0)
181 ei->stabsect = sectp;
183 else if (strcmp (sectp->name, ".mdebug") == 0)
185 ei->mdebugsect = sectp;
187 else if (strcmp (sectp->name, ".ctf") == 0)
189 ei->ctfsect = sectp;
193 static struct minimal_symbol *
194 record_minimal_symbol (minimal_symbol_reader &reader,
195 std::string_view name, bool copy_name,
196 unrelocated_addr address,
197 enum minimal_symbol_type ms_type,
198 asection *bfd_section, struct objfile *objfile)
200 struct gdbarch *gdbarch = objfile->arch ();
202 if (ms_type == mst_text || ms_type == mst_file_text
203 || ms_type == mst_text_gnu_ifunc)
204 address
205 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
206 CORE_ADDR (address)));
208 /* We only setup section information for allocatable sections. Usually
209 we'd only expect to find msymbols for allocatable sections, but if the
210 ELF is malformed then this might not be the case. In that case don't
211 create an msymbol that references an uninitialised section object. */
212 int section_index = 0;
213 if ((bfd_section_flags (bfd_section) & SEC_ALLOC) == SEC_ALLOC
214 || bfd_section == bfd_abs_section_ptr)
215 section_index = gdb_bfd_section_index (objfile->obfd.get (), bfd_section);
217 return reader.record_full (name, copy_name, address, ms_type, section_index);
220 /* Read the symbol table of an ELF file.
222 Given an objfile, a symbol table, and a flag indicating whether the
223 symbol table contains regular, dynamic, or synthetic symbols, add all
224 the global function and data symbols to the minimal symbol table.
226 In stabs-in-ELF, as implemented by Sun, there are some local symbols
227 defined in the ELF symbol table, which can be used to locate
228 the beginnings of sections from each ".o" file that was linked to
229 form the executable objfile. We gather any such info and record it
230 in data structures hung off the objfile's private data. */
232 #define ST_REGULAR 0
233 #define ST_DYNAMIC 1
234 #define ST_SYNTHETIC 2
236 static void
237 elf_symtab_read (minimal_symbol_reader &reader,
238 struct objfile *objfile, int type,
239 long number_of_symbols, asymbol **symbol_table,
240 bool copy_names)
242 struct gdbarch *gdbarch = objfile->arch ();
243 asymbol *sym;
244 long i;
245 CORE_ADDR symaddr;
246 enum minimal_symbol_type ms_type;
247 /* Name of the last file symbol. This is either a constant string or is
248 saved on the objfile's filename cache. */
249 const char *filesymname = "";
250 int stripped = (bfd_get_symcount (objfile->obfd.get ()) == 0);
251 int elf_make_msymbol_special_p
252 = gdbarch_elf_make_msymbol_special_p (gdbarch);
254 for (i = 0; i < number_of_symbols; i++)
256 sym = symbol_table[i];
257 if (sym->name == NULL || *sym->name == '\0')
259 /* Skip names that don't exist (shouldn't happen), or names
260 that are null strings (may happen). */
261 continue;
264 elf_symbol_type *elf_sym = (elf_symbol_type *) sym;
266 /* Skip "special" symbols, e.g. ARM mapping symbols. These are
267 symbols which do not correspond to objects in the symbol table,
268 but have some other target-specific meaning. */
269 if (bfd_is_target_special_symbol (objfile->obfd.get (), sym))
271 if (gdbarch_record_special_symbol_p (gdbarch))
272 gdbarch_record_special_symbol (gdbarch, objfile, sym);
273 continue;
276 if (type == ST_DYNAMIC
277 && sym->section == bfd_und_section_ptr
278 && (sym->flags & BSF_FUNCTION))
280 struct minimal_symbol *msym;
281 bfd *abfd = objfile->obfd.get ();
282 asection *sect;
284 /* Symbol is a reference to a function defined in
285 a shared library.
286 If its value is non zero then it is usually the address
287 of the corresponding entry in the procedure linkage table,
288 plus the desired section offset.
289 If its value is zero then the dynamic linker has to resolve
290 the symbol. We are unable to find any meaningful address
291 for this symbol in the executable file, so we skip it. */
292 symaddr = sym->value;
293 if (symaddr == 0)
294 continue;
296 /* sym->section is the undefined section. However, we want to
297 record the section where the PLT stub resides with the
298 minimal symbol. Search the section table for the one that
299 covers the stub's address. */
300 for (sect = abfd->sections; sect != NULL; sect = sect->next)
302 if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
303 continue;
305 if (symaddr >= bfd_section_vma (sect)
306 && symaddr < bfd_section_vma (sect)
307 + bfd_section_size (sect))
308 break;
310 if (!sect)
311 continue;
313 /* On ia64-hpux, we have discovered that the system linker
314 adds undefined symbols with nonzero addresses that cannot
315 be right (their address points inside the code of another
316 function in the .text section). This creates problems
317 when trying to determine which symbol corresponds to
318 a given address.
320 We try to detect those buggy symbols by checking which
321 section we think they correspond to. Normally, PLT symbols
322 are stored inside their own section, and the typical name
323 for that section is ".plt". So, if there is a ".plt"
324 section, and yet the section name of our symbol does not
325 start with ".plt", we ignore that symbol. */
326 if (!startswith (sect->name, ".plt")
327 && bfd_get_section_by_name (abfd, ".plt") != NULL)
328 continue;
330 msym = record_minimal_symbol
331 (reader, sym->name, copy_names,
332 unrelocated_addr (symaddr),
333 mst_solib_trampoline, sect, objfile);
334 if (msym != NULL)
336 msym->filename = filesymname;
337 if (elf_make_msymbol_special_p)
338 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
340 continue;
343 /* If it is a nonstripped executable, do not enter dynamic
344 symbols, as the dynamic symbol table is usually a subset
345 of the main symbol table. */
346 if (type == ST_DYNAMIC && !stripped)
347 continue;
348 if (sym->flags & BSF_FILE)
349 filesymname = objfile->intern (sym->name);
350 else if (sym->flags & BSF_SECTION_SYM)
351 continue;
352 else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK
353 | BSF_GNU_UNIQUE))
355 struct minimal_symbol *msym;
357 /* Select global/local/weak symbols. Note that bfd puts abs
358 symbols in their own section, so all symbols we are
359 interested in will have a section. */
360 /* Bfd symbols are section relative. */
361 symaddr = sym->value + sym->section->vma;
362 /* For non-absolute symbols, use the type of the section
363 they are relative to, to intuit text/data. Bfd provides
364 no way of figuring this out for absolute symbols. */
365 if (sym->section == bfd_abs_section_ptr)
367 /* This is a hack to get the minimal symbol type
368 right for Irix 5, which has absolute addresses
369 with special section indices for dynamic symbols.
371 NOTE: uweigand-20071112: Synthetic symbols do not
372 have an ELF-private part, so do not touch those. */
373 unsigned int shndx = type == ST_SYNTHETIC ? 0 :
374 elf_sym->internal_elf_sym.st_shndx;
376 switch (shndx)
378 case SHN_MIPS_TEXT:
379 ms_type = mst_text;
380 break;
381 case SHN_MIPS_DATA:
382 ms_type = mst_data;
383 break;
384 case SHN_MIPS_ACOMMON:
385 ms_type = mst_bss;
386 break;
387 default:
388 ms_type = mst_abs;
391 /* If it is an Irix dynamic symbol, skip section name
392 symbols, relocate all others by section offset. */
393 if (ms_type != mst_abs)
395 if (sym->name[0] == '.')
396 continue;
399 else if (sym->section->flags & SEC_CODE)
401 if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
403 if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
404 ms_type = mst_text_gnu_ifunc;
405 else
406 ms_type = mst_text;
408 /* The BSF_SYNTHETIC check is there to omit ppc64 function
409 descriptors mistaken for static functions starting with 'L'.
411 else if ((sym->name[0] == '.' && sym->name[1] == 'L'
412 && (sym->flags & BSF_SYNTHETIC) == 0)
413 || ((sym->flags & BSF_LOCAL)
414 && sym->name[0] == '$'
415 && sym->name[1] == 'L'))
416 /* Looks like a compiler-generated label. Skip
417 it. The assembler should be skipping these (to
418 keep executables small), but apparently with
419 gcc on the (deleted) delta m88k SVR4, it loses.
420 So to have us check too should be harmless (but
421 I encourage people to fix this in the assembler
422 instead of adding checks here). */
423 continue;
424 else
426 ms_type = mst_file_text;
429 else if (sym->section->flags & SEC_ALLOC)
431 if (sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE))
433 if (sym->flags & BSF_GNU_INDIRECT_FUNCTION)
435 ms_type = mst_data_gnu_ifunc;
437 else if (sym->section->flags & SEC_LOAD)
439 ms_type = mst_data;
441 else
443 ms_type = mst_bss;
446 else if (sym->flags & BSF_LOCAL)
448 if (sym->section->flags & SEC_LOAD)
450 ms_type = mst_file_data;
452 else
454 ms_type = mst_file_bss;
457 else
459 ms_type = mst_unknown;
462 else
464 /* FIXME: Solaris2 shared libraries include lots of
465 odd "absolute" and "undefined" symbols, that play
466 hob with actions like finding what function the PC
467 is in. Ignore them if they aren't text, data, or bss. */
468 /* ms_type = mst_unknown; */
469 continue; /* Skip this symbol. */
471 msym = record_minimal_symbol
472 (reader, sym->name, copy_names, unrelocated_addr (symaddr),
473 ms_type, sym->section, objfile);
475 if (msym)
477 /* NOTE: uweigand-20071112: A synthetic symbol does not have an
478 ELF-private part. */
479 if (type != ST_SYNTHETIC)
481 /* Pass symbol size field in via BFD. FIXME!!! */
482 msym->set_size (elf_sym->internal_elf_sym.st_size);
485 msym->filename = filesymname;
486 if (elf_make_msymbol_special_p)
487 gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
490 /* If we see a default versioned symbol, install it under
491 its version-less name. */
492 if (msym != NULL)
494 const char *atsign = strchr (sym->name, '@');
495 bool is_at_symbol = atsign != nullptr && atsign > sym->name;
496 bool is_plt = is_at_symbol && strcmp (atsign, "@plt") == 0;
497 int len = is_at_symbol ? atsign - sym->name : 0;
499 if (is_at_symbol
500 && !is_plt
501 && (elf_sym->version & VERSYM_HIDDEN) == 0)
502 record_minimal_symbol (reader,
503 std::string_view (sym->name, len),
504 true, unrelocated_addr (symaddr),
505 ms_type, sym->section, objfile);
506 else if (is_plt)
508 /* For @plt symbols, also record a trampoline to the
509 destination symbol. The @plt symbol will be used
510 in disassembly, and the trampoline will be used
511 when we are trying to find the target. */
512 if (ms_type == mst_text && type == ST_SYNTHETIC)
514 struct minimal_symbol *mtramp;
516 mtramp = record_minimal_symbol
517 (reader, std::string_view (sym->name, len), true,
518 unrelocated_addr (symaddr),
519 mst_solib_trampoline, sym->section, objfile);
520 if (mtramp)
522 mtramp->set_size (msym->size());
523 mtramp->created_by_gdb = 1;
524 mtramp->filename = filesymname;
525 if (elf_make_msymbol_special_p)
526 gdbarch_elf_make_msymbol_special (gdbarch,
527 sym, mtramp);
536 /* Build minimal symbols named `function@got.plt' (see SYMBOL_GOT_PLT_SUFFIX)
537 for later look ups of which function to call when user requests
538 a STT_GNU_IFUNC function. As the STT_GNU_IFUNC type is found at the target
539 library defining `function' we cannot yet know while reading OBJFILE which
540 of the SYMBOL_GOT_PLT_SUFFIX entries will be needed and later
541 DYN_SYMBOL_TABLE is no longer easily available for OBJFILE. */
543 static void
544 elf_rel_plt_read (minimal_symbol_reader &reader,
545 struct objfile *objfile, asymbol **dyn_symbol_table)
547 bfd *obfd = objfile->obfd.get ();
548 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
549 asection *relplt, *got_plt;
550 bfd_size_type reloc_count, reloc;
551 struct gdbarch *gdbarch = objfile->arch ();
552 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
553 size_t ptr_size = ptr_type->length ();
555 if (objfile->separate_debug_objfile_backlink)
556 return;
558 got_plt = bfd_get_section_by_name (obfd, ".got.plt");
559 if (got_plt == NULL)
561 /* For platforms where there is no separate .got.plt. */
562 got_plt = bfd_get_section_by_name (obfd, ".got");
563 if (got_plt == NULL)
564 return;
567 /* Depending on system, we may find jump slots in a relocation
568 section for either .got.plt or .plt. */
569 asection *plt = bfd_get_section_by_name (obfd, ".plt");
570 int plt_elf_idx = (plt != NULL) ? elf_section_data (plt)->this_idx : -1;
572 int got_plt_elf_idx = elf_section_data (got_plt)->this_idx;
574 /* This search algorithm is from _bfd_elf_canonicalize_dynamic_reloc. */
575 for (relplt = obfd->sections; relplt != NULL; relplt = relplt->next)
577 const auto &this_hdr = elf_section_data (relplt)->this_hdr;
579 if (this_hdr.sh_type == SHT_REL || this_hdr.sh_type == SHT_RELA)
581 if (this_hdr.sh_info == plt_elf_idx
582 || this_hdr.sh_info == got_plt_elf_idx)
583 break;
586 if (relplt == NULL)
587 return;
589 if (! bed->s->slurp_reloc_table (obfd, relplt, dyn_symbol_table, TRUE))
590 return;
592 std::string string_buffer;
594 /* Does ADDRESS reside in SECTION of OBFD? */
595 auto within_section = [obfd] (asection *section, CORE_ADDR address)
597 if (section == NULL)
598 return false;
600 return (bfd_section_vma (section) <= address
601 && (address < bfd_section_vma (section)
602 + bfd_section_size (section)));
605 reloc_count = relplt->size / elf_section_data (relplt)->this_hdr.sh_entsize;
606 for (reloc = 0; reloc < reloc_count; reloc++)
608 const char *name;
609 struct minimal_symbol *msym;
610 CORE_ADDR address;
611 const char *got_suffix = SYMBOL_GOT_PLT_SUFFIX;
612 const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
614 name = bfd_asymbol_name (*relplt->relocation[reloc].sym_ptr_ptr);
615 if (!name)
616 continue;
617 address = relplt->relocation[reloc].address;
619 asection *msym_section;
621 /* Does the pointer reside in either the .got.plt or .plt
622 sections? */
623 if (within_section (got_plt, address))
624 msym_section = got_plt;
625 else if (within_section (plt, address))
626 msym_section = plt;
627 else
628 continue;
630 /* We cannot check if NAME is a reference to
631 mst_text_gnu_ifunc/mst_data_gnu_ifunc as in OBJFILE the
632 symbol is undefined and the objfile having NAME defined may
633 not yet have been loaded. */
635 string_buffer.assign (name);
636 string_buffer.append (got_suffix, got_suffix + got_suffix_len);
638 msym = record_minimal_symbol (reader, string_buffer,
639 true, unrelocated_addr (address),
640 mst_slot_got_plt, msym_section, objfile);
641 if (msym)
642 msym->set_size (ptr_size);
646 /* The data pointer is htab_t for gnu_ifunc_record_cache_unchecked. */
648 static const registry<objfile>::key<htab, htab_deleter>
649 elf_objfile_gnu_ifunc_cache_data;
651 /* Map function names to CORE_ADDR in elf_objfile_gnu_ifunc_cache_data. */
653 struct elf_gnu_ifunc_cache
655 /* This is always a function entry address, not a function descriptor. */
656 CORE_ADDR addr;
658 char name[1];
661 /* htab_hash for elf_objfile_gnu_ifunc_cache_data. */
663 static hashval_t
664 elf_gnu_ifunc_cache_hash (const void *a_voidp)
666 const struct elf_gnu_ifunc_cache *a
667 = (const struct elf_gnu_ifunc_cache *) a_voidp;
669 return htab_hash_string (a->name);
672 /* htab_eq for elf_objfile_gnu_ifunc_cache_data. */
674 static int
675 elf_gnu_ifunc_cache_eq (const void *a_voidp, const void *b_voidp)
677 const struct elf_gnu_ifunc_cache *a
678 = (const struct elf_gnu_ifunc_cache *) a_voidp;
679 const struct elf_gnu_ifunc_cache *b
680 = (const struct elf_gnu_ifunc_cache *) b_voidp;
682 return strcmp (a->name, b->name) == 0;
685 /* Record the target function address of a STT_GNU_IFUNC function NAME is the
686 function entry address ADDR. Return 1 if NAME and ADDR are considered as
687 valid and therefore they were successfully recorded, return 0 otherwise.
689 Function does not expect a duplicate entry. Use
690 elf_gnu_ifunc_resolve_by_cache first to check if the entry for NAME already
691 exists. */
693 static int
694 elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
696 struct objfile *objfile;
697 htab_t htab;
698 struct elf_gnu_ifunc_cache entry_local, *entry_p;
699 void **slot;
701 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr);
702 if (msym.minsym == NULL)
703 return 0;
704 if (msym.value_address () != addr)
705 return 0;
706 objfile = msym.objfile;
708 /* If .plt jumps back to .plt the symbol is still deferred for later
709 resolution and it has no use for GDB. */
710 const char *target_name = msym.minsym->linkage_name ();
711 size_t len = strlen (target_name);
713 /* Note we check the symbol's name instead of checking whether the
714 symbol is in the .plt section because some systems have @plt
715 symbols in the .text section. */
716 if (len > 4 && strcmp (target_name + len - 4, "@plt") == 0)
717 return 0;
719 if (strcmp (target_name, "_PROCEDURE_LINKAGE_TABLE_") == 0)
720 return 0;
722 htab = elf_objfile_gnu_ifunc_cache_data.get (objfile);
723 if (htab == NULL)
725 htab = htab_create_alloc (1, elf_gnu_ifunc_cache_hash,
726 elf_gnu_ifunc_cache_eq,
727 NULL, xcalloc, xfree);
728 elf_objfile_gnu_ifunc_cache_data.set (objfile, htab);
731 entry_local.addr = addr;
732 obstack_grow (&objfile->objfile_obstack, &entry_local,
733 offsetof (struct elf_gnu_ifunc_cache, name));
734 obstack_grow_str0 (&objfile->objfile_obstack, name);
735 entry_p
736 = (struct elf_gnu_ifunc_cache *) obstack_finish (&objfile->objfile_obstack);
738 slot = htab_find_slot (htab, entry_p, INSERT);
739 if (*slot != NULL)
741 struct elf_gnu_ifunc_cache *entry_found_p
742 = (struct elf_gnu_ifunc_cache *) *slot;
743 struct gdbarch *gdbarch = objfile->arch ();
745 if (entry_found_p->addr != addr)
747 /* This case indicates buggy inferior program, the resolved address
748 should never change. */
750 warning (_("gnu-indirect-function \"%s\" has changed its resolved "
751 "function_address from %s to %s"),
752 name, paddress (gdbarch, entry_found_p->addr),
753 paddress (gdbarch, addr));
756 /* New ENTRY_P is here leaked/duplicate in the OBJFILE obstack. */
758 *slot = entry_p;
760 return 1;
763 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
764 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
765 is not NULL) and the function returns 1. It returns 0 otherwise.
767 Only the elf_objfile_gnu_ifunc_cache_data hash table is searched by this
768 function. */
770 static int
771 elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
773 int found = 0;
775 /* FIXME: we only search the initial namespace.
777 To search other namespaces, we would need to provide context, e.g. in
778 form of an objfile in that namespace. */
779 gdbarch_iterate_over_objfiles_in_search_order
780 (current_inferior ()->arch (),
781 [name, &addr_p, &found] (struct objfile *objfile)
783 htab_t htab;
784 elf_gnu_ifunc_cache *entry_p;
785 void **slot;
787 htab = elf_objfile_gnu_ifunc_cache_data.get (objfile);
788 if (htab == NULL)
789 return 0;
791 entry_p = ((elf_gnu_ifunc_cache *)
792 alloca (sizeof (*entry_p) + strlen (name)));
793 strcpy (entry_p->name, name);
795 slot = htab_find_slot (htab, entry_p, NO_INSERT);
796 if (slot == NULL)
797 return 0;
798 entry_p = (elf_gnu_ifunc_cache *) *slot;
799 gdb_assert (entry_p != NULL);
801 if (addr_p)
802 *addr_p = entry_p->addr;
804 found = 1;
805 return 1;
806 }, nullptr);
808 return found;
811 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
812 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
813 is not NULL) and the function returns 1. It returns 0 otherwise.
815 Only the SYMBOL_GOT_PLT_SUFFIX locations are searched by this function.
816 elf_gnu_ifunc_resolve_by_cache must have been already called for NAME to
817 prevent cache entries duplicates. */
819 static int
820 elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
822 char *name_got_plt;
823 const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
824 int found = 0;
826 name_got_plt = (char *) alloca (strlen (name) + got_suffix_len + 1);
827 sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
829 /* FIXME: we only search the initial namespace.
831 To search other namespaces, we would need to provide context, e.g. in
832 form of an objfile in that namespace. */
833 gdbarch_iterate_over_objfiles_in_search_order
834 (current_inferior ()->arch (),
835 [name, name_got_plt, &addr_p, &found] (struct objfile *objfile)
837 bfd *obfd = objfile->obfd.get ();
838 struct gdbarch *gdbarch = objfile->arch ();
839 type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
840 size_t ptr_size = ptr_type->length ();
841 CORE_ADDR pointer_address, addr;
842 asection *plt;
843 gdb_byte *buf = (gdb_byte *) alloca (ptr_size);
845 bound_minimal_symbol msym
846 = lookup_minimal_symbol (current_program_space, name_got_plt,
847 objfile);
848 if (msym.minsym == NULL)
849 return 0;
850 if (msym.minsym->type () != mst_slot_got_plt)
851 return 0;
852 pointer_address = msym.value_address ();
854 plt = bfd_get_section_by_name (obfd, ".plt");
855 if (plt == NULL)
856 return 0;
858 if (msym.minsym->size () != ptr_size)
859 return 0;
860 if (target_read_memory (pointer_address, buf, ptr_size) != 0)
861 return 0;
862 addr = extract_typed_address (buf, ptr_type);
863 addr = gdbarch_convert_from_func_ptr_addr
864 (gdbarch, addr, current_inferior ()->top_target ());
865 addr = gdbarch_addr_bits_remove (gdbarch, addr);
867 if (elf_gnu_ifunc_record_cache (name, addr))
869 if (addr_p != NULL)
870 *addr_p = addr;
872 found = 1;
873 return 1;
876 return 0;
877 }, nullptr);
879 return found;
882 /* Try to find the target resolved function entry address of a STT_GNU_IFUNC
883 function NAME. If the address is found it is stored to *ADDR_P (if ADDR_P
884 is not NULL) and the function returns true. It returns false otherwise.
886 Both the elf_objfile_gnu_ifunc_cache_data hash table and
887 SYMBOL_GOT_PLT_SUFFIX locations are searched by this function. */
889 static bool
890 elf_gnu_ifunc_resolve_name (const char *name, CORE_ADDR *addr_p)
892 if (elf_gnu_ifunc_resolve_by_cache (name, addr_p))
893 return true;
895 if (elf_gnu_ifunc_resolve_by_got (name, addr_p))
896 return true;
898 return false;
901 /* Call STT_GNU_IFUNC - a function returning addresss of a real function to
902 call. PC is theSTT_GNU_IFUNC resolving function entry. The value returned
903 is the entry point of the resolved STT_GNU_IFUNC target function to call.
906 static CORE_ADDR
907 elf_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
909 const char *name_at_pc;
910 CORE_ADDR start_at_pc, address;
911 struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
912 struct value *function, *address_val;
913 CORE_ADDR hwcap = 0;
914 struct value *hwcap_val;
916 /* Try first any non-intrusive methods without an inferior call. */
918 if (find_pc_partial_function (pc, &name_at_pc, &start_at_pc, NULL)
919 && start_at_pc == pc)
921 if (elf_gnu_ifunc_resolve_name (name_at_pc, &address))
922 return address;
924 else
925 name_at_pc = NULL;
927 function = value::allocate (func_func_type);
928 function->set_lval (lval_memory);
929 function->set_address (pc);
931 /* STT_GNU_IFUNC resolver functions usually receive the HWCAP vector as
932 parameter. FUNCTION is the function entry address. ADDRESS may be a
933 function descriptor. */
935 target_auxv_search (AT_HWCAP, &hwcap);
936 hwcap_val = value_from_longest (builtin_type (gdbarch)
937 ->builtin_unsigned_long, hwcap);
938 address_val = call_function_by_hand (function, NULL, hwcap_val);
939 address = value_as_address (address_val);
940 address = gdbarch_convert_from_func_ptr_addr
941 (gdbarch, address, current_inferior ()->top_target ());
942 address = gdbarch_addr_bits_remove (gdbarch, address);
944 if (name_at_pc)
945 elf_gnu_ifunc_record_cache (name_at_pc, address);
947 return address;
950 /* Handle inferior hit of bp_gnu_ifunc_resolver, see its definition. */
952 static void
953 elf_gnu_ifunc_resolver_stop (code_breakpoint *b)
955 struct breakpoint *b_return;
956 frame_info_ptr prev_frame = get_prev_frame (get_current_frame ());
957 struct frame_id prev_frame_id = get_stack_frame_id (prev_frame);
958 CORE_ADDR prev_pc = get_frame_pc (prev_frame);
959 int thread_id = inferior_thread ()->global_num;
961 gdb_assert (b->type == bp_gnu_ifunc_resolver);
963 for (b_return = b->related_breakpoint; b_return != b;
964 b_return = b_return->related_breakpoint)
966 gdb_assert (b_return->type == bp_gnu_ifunc_resolver_return);
967 gdb_assert (b_return->has_single_location ());
968 gdb_assert (frame_id_p (b_return->frame_id));
970 if (b_return->thread == thread_id
971 && b_return->first_loc ().requested_address == prev_pc
972 && b_return->frame_id == prev_frame_id)
973 break;
976 if (b_return == b)
978 /* No need to call find_pc_line for symbols resolving as this is only
979 a helper breakpoint never shown to the user. */
981 symtab_and_line sal;
982 sal.pspace = current_inferior ()->pspace;
983 sal.pc = prev_pc;
984 sal.section = find_pc_overlay (sal.pc);
985 sal.explicit_pc = 1;
986 b_return
987 = set_momentary_breakpoint (get_frame_arch (prev_frame), sal,
988 prev_frame_id,
989 bp_gnu_ifunc_resolver_return).release ();
992 /* Add new b_return to the ring list b->related_breakpoint. */
993 gdb_assert (b_return->related_breakpoint == b_return);
994 b_return->related_breakpoint = b->related_breakpoint;
995 b->related_breakpoint = b_return;
999 /* Handle inferior hit of bp_gnu_ifunc_resolver_return, see its definition. */
1001 static void
1002 elf_gnu_ifunc_resolver_return_stop (code_breakpoint *b)
1004 thread_info *thread = inferior_thread ();
1005 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
1006 struct type *func_func_type = builtin_type (gdbarch)->builtin_func_func;
1007 struct type *value_type = func_func_type->target_type ();
1008 struct regcache *regcache = get_thread_regcache (thread);
1009 struct value *func_func;
1010 struct value *value;
1011 CORE_ADDR resolved_address, resolved_pc;
1013 gdb_assert (b->type == bp_gnu_ifunc_resolver_return);
1015 while (b->related_breakpoint != b)
1017 struct breakpoint *b_next = b->related_breakpoint;
1019 switch (b->type)
1021 case bp_gnu_ifunc_resolver:
1022 break;
1023 case bp_gnu_ifunc_resolver_return:
1024 delete_breakpoint (b);
1025 break;
1026 default:
1027 internal_error (_("handle_inferior_event: Invalid "
1028 "gnu-indirect-function breakpoint type %d"),
1029 (int) b->type);
1031 b = gdb::checked_static_cast<code_breakpoint *> (b_next);
1033 gdb_assert (b->type == bp_gnu_ifunc_resolver);
1034 gdb_assert (b->has_single_location ());
1036 func_func = value::allocate (func_func_type);
1037 func_func->set_lval (lval_memory);
1038 func_func->set_address (b->first_loc ().related_address);
1040 value = value::allocate (value_type);
1041 gdbarch_return_value_as_value (gdbarch, func_func, value_type, regcache,
1042 &value, NULL);
1043 resolved_address = value_as_address (value);
1044 resolved_pc = gdbarch_convert_from_func_ptr_addr
1045 (gdbarch, resolved_address, current_inferior ()->top_target ());
1046 resolved_pc = gdbarch_addr_bits_remove (gdbarch, resolved_pc);
1048 gdb_assert (current_program_space == b->pspace || b->pspace == NULL);
1049 elf_gnu_ifunc_record_cache (b->locspec->to_string (), resolved_pc);
1051 b->type = bp_breakpoint;
1052 update_breakpoint_locations (b, current_program_space,
1053 find_function_start_sal (resolved_pc, NULL, true),
1054 {});
1057 /* A helper function for elf_symfile_read that reads the minimal
1058 symbols. */
1060 static void
1061 elf_read_minimal_symbols (struct objfile *objfile, int symfile_flags,
1062 const struct elfinfo *ei)
1064 bfd *synth_abfd, *abfd = objfile->obfd.get ();
1065 long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
1066 asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
1067 asymbol *synthsyms;
1069 symtab_create_debug_printf ("reading minimal symbols of objfile %s",
1070 objfile_name (objfile));
1072 /* If we already have minsyms, then we can skip some work here.
1073 However, if there were stabs or mdebug sections, we go ahead and
1074 redo all the work anyway, because the psym readers for those
1075 kinds of debuginfo need extra information found here. This can
1076 go away once all types of symbols are in the per-BFD object. */
1077 if (objfile->per_bfd->minsyms_read
1078 && ei->stabsect == NULL
1079 && ei->mdebugsect == NULL
1080 && ei->ctfsect == NULL)
1082 symtab_create_debug_printf ("minimal symbols were previously read");
1083 return;
1086 minimal_symbol_reader reader (objfile);
1088 /* Process the normal ELF symbol table first. */
1090 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
1091 if (storage_needed < 0)
1092 error (_("Can't read symbols from %s: %s"),
1093 bfd_get_filename (objfile->obfd.get ()),
1094 bfd_errmsg (bfd_get_error ()));
1096 if (storage_needed > 0)
1098 /* Memory gets permanently referenced from ABFD after
1099 bfd_canonicalize_symtab so it must not get freed before ABFD gets. */
1101 symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
1102 symcount = bfd_canonicalize_symtab (objfile->obfd.get (), symbol_table);
1104 if (symcount < 0)
1105 error (_("Can't read symbols from %s: %s"),
1106 bfd_get_filename (objfile->obfd.get ()),
1107 bfd_errmsg (bfd_get_error ()));
1109 elf_symtab_read (reader, objfile, ST_REGULAR, symcount, symbol_table,
1110 false);
1113 /* Add the dynamic symbols. */
1115 storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd.get ());
1117 if (storage_needed > 0)
1119 /* Memory gets permanently referenced from ABFD after
1120 bfd_get_synthetic_symtab so it must not get freed before ABFD gets.
1121 It happens only in the case when elf_slurp_reloc_table sees
1122 asection->relocation NULL. Determining which section is asection is
1123 done by _bfd_elf_get_synthetic_symtab which is all a bfd
1124 implementation detail, though. */
1126 dyn_symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
1127 dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd.get (),
1128 dyn_symbol_table);
1130 if (dynsymcount < 0)
1131 error (_("Can't read symbols from %s: %s"),
1132 bfd_get_filename (objfile->obfd.get ()),
1133 bfd_errmsg (bfd_get_error ()));
1135 elf_symtab_read (reader, objfile, ST_DYNAMIC, dynsymcount,
1136 dyn_symbol_table, false);
1138 elf_rel_plt_read (reader, objfile, dyn_symbol_table);
1141 /* Contrary to binutils --strip-debug/--only-keep-debug the strip command from
1142 elfutils (eu-strip) moves even the .symtab section into the .debug file.
1144 bfd_get_synthetic_symtab on ppc64 for each function descriptor ELF symbol
1145 'name' creates a new BSF_SYNTHETIC ELF symbol '.name' with its code
1146 address. But with eu-strip files bfd_get_synthetic_symtab would fail to
1147 read the code address from .opd while it reads the .symtab section from
1148 a separate debug info file as the .opd section is SHT_NOBITS there.
1150 With SYNTH_ABFD the .opd section will be read from the original
1151 backlinked binary where it is valid. */
1153 if (objfile->separate_debug_objfile_backlink)
1154 synth_abfd = objfile->separate_debug_objfile_backlink->obfd.get ();
1155 else
1156 synth_abfd = abfd;
1158 /* Add synthetic symbols - for instance, names for any PLT entries. */
1160 synthcount = bfd_get_synthetic_symtab (synth_abfd, symcount, symbol_table,
1161 dynsymcount, dyn_symbol_table,
1162 &synthsyms);
1163 if (synthcount > 0)
1165 long i;
1167 std::unique_ptr<asymbol *[]>
1168 synth_symbol_table (new asymbol *[synthcount]);
1169 for (i = 0; i < synthcount; i++)
1170 synth_symbol_table[i] = synthsyms + i;
1171 elf_symtab_read (reader, objfile, ST_SYNTHETIC, synthcount,
1172 synth_symbol_table.get (), true);
1174 xfree (synthsyms);
1175 synthsyms = NULL;
1178 /* Install any minimal symbols that have been collected as the current
1179 minimal symbols for this objfile. The debug readers below this point
1180 should not generate new minimal symbols; if they do it's their
1181 responsibility to install them. "mdebug" appears to be the only one
1182 which will do this. */
1184 reader.install ();
1186 symtab_create_debug_printf ("done reading minimal symbols");
1189 /* Dwarf-specific helper for elf_symfile_read. Return true if we managed to
1190 load dwarf debug info. */
1192 static bool
1193 elf_symfile_read_dwarf2 (struct objfile *objfile,
1194 symfile_add_flags symfile_flags)
1196 bool has_dwarf2 = true;
1198 if (dwarf2_initialize_objfile (objfile, nullptr, true))
1200 /* Nothing. */
1202 /* If the file has its own symbol tables it has no separate debug
1203 info. `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
1204 SYMTABS/PSYMTABS. `.gnu_debuglink' may no longer be present with
1205 `.note.gnu.build-id'.
1207 .gnu_debugdata is !objfile::has_partial_symbols because it contains only
1208 .symtab, not .debug_* section. But if we already added .gnu_debugdata as
1209 an objfile via find_separate_debug_file_in_section there was no separate
1210 debug info available. Therefore do not attempt to search for another one,
1211 objfile->separate_debug_objfile->separate_debug_objfile GDB guarantees to
1212 be NULL and we would possibly violate it. */
1214 else if (!objfile->has_partial_symbols ()
1215 && objfile->separate_debug_objfile == NULL
1216 && objfile->separate_debug_objfile_backlink == NULL)
1218 if (objfile->find_and_add_separate_symbol_file (symfile_flags))
1219 gdb_assert (objfile->separate_debug_objfile != nullptr);
1220 else
1221 has_dwarf2 = false;
1224 return has_dwarf2;
1227 /* find_text_range --- find start and end of loadable code sections
1229 The find_text_range function finds the shortest address range that
1230 encloses all sections containing executable code, and stores it in
1231 objfile's text_addr and text_size members.
1233 dbx_symfile_read will use this to finish off the partial symbol
1234 table, in some cases. */
1236 static void
1237 find_text_range (bfd * sym_bfd, struct objfile *objfile)
1239 asection *sec;
1240 int found_any = 0;
1241 CORE_ADDR start = 0;
1242 CORE_ADDR end = 0;
1244 for (sec = sym_bfd->sections; sec; sec = sec->next)
1245 if (bfd_section_flags (sec) & SEC_CODE)
1247 CORE_ADDR sec_start = bfd_section_vma (sec);
1248 CORE_ADDR sec_end = sec_start + bfd_section_size (sec);
1250 if (found_any)
1252 if (sec_start < start)
1253 start = sec_start;
1254 if (sec_end > end)
1255 end = sec_end;
1257 else
1259 start = sec_start;
1260 end = sec_end;
1263 found_any = 1;
1266 if (!found_any)
1267 error (_("Can't find any code sections in symbol file"));
1269 DBX_TEXT_ADDR (objfile) = start;
1270 DBX_TEXT_SIZE (objfile) = end - start;
1273 /* Scan and build partial symbols for an ELF symbol file.
1274 This ELF file has already been processed to get its minimal symbols.
1276 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
1277 rolled into one.
1279 OBJFILE is the object file we are reading symbols from.
1280 ADDR is the address relative to which the symbols are (e.g.
1281 the base address of the text segment).
1282 STABSECT is the BFD section information for the .stab section.
1283 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
1284 .stabstr section exists.
1286 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
1287 adjusted for elf details. */
1289 void
1290 elfstab_build_psymtabs (struct objfile *objfile, asection *stabsect,
1291 file_ptr stabstroffset, unsigned int stabstrsize)
1293 int val;
1294 bfd *sym_bfd = objfile->obfd.get ();
1295 const char *name = bfd_get_filename (sym_bfd);
1297 stabsread_new_init ();
1299 /* Allocate struct to keep track of stab reading. */
1300 dbx_objfile_data_key.emplace (objfile);
1301 dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
1303 /* Find the first and last text address. dbx_symfile_read seems to
1304 want this. */
1305 find_text_range (sym_bfd, objfile);
1307 #define ELF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
1308 DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
1309 DBX_SYMCOUNT (objfile)
1310 = bfd_section_size (stabsect) / DBX_SYMBOL_SIZE (objfile);
1311 DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
1312 DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
1313 DBX_STAB_SECTION (objfile) = stabsect;
1315 if (stabstrsize > bfd_get_size (sym_bfd))
1316 error (_("ridiculous string table size: %d bytes"), stabstrsize);
1317 DBX_STRINGTAB (objfile) = (char *)
1318 obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
1319 OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
1321 /* Now read in the string table in one big gulp. */
1323 val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
1324 if (val < 0)
1325 perror_with_name (name);
1326 val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
1327 if (val != stabstrsize)
1328 perror_with_name (name);
1330 stabsread_new_init ();
1331 free_header_files ();
1332 init_header_files ();
1334 key->ctx.processing_acc_compilation = 1;
1336 key->ctx.symbuf_read = 0;
1337 key->ctx.symbuf_left = bfd_section_size (stabsect);
1339 scoped_restore restore_stabs_data = make_scoped_restore (&key->ctx.stabs_data);
1340 gdb::unique_xmalloc_ptr<gdb_byte> data_holder;
1342 key->ctx.stabs_data = symfile_relocate_debug_section (objfile, stabsect, NULL);
1343 if (key->ctx.stabs_data)
1344 data_holder.reset (key->ctx.stabs_data);
1346 /* In an elf file, we've already installed the minimal symbols that came
1347 from the elf (non-stab) symbol table, so always act like an
1348 incremental load here. dbx_symfile_read should not generate any new
1349 minimal symbols, since we will have already read the ELF dynamic symbol
1350 table and normal symbol entries won't be in the ".stab" section; but in
1351 case it does, it will install them itself. */
1352 read_stabs_symtab (objfile, 0);
1355 /* Scan and build partial symbols for a symbol file.
1356 We have been initialized by a call to elf_symfile_init, which
1357 currently does nothing.
1359 This function only does the minimum work necessary for letting the
1360 user "name" things symbolically; it does not read the entire symtab.
1361 Instead, it reads the external and static symbols and puts them in partial
1362 symbol tables. When more extensive information is requested of a
1363 file, the corresponding partial symbol table is mutated into a full
1364 fledged symbol table by going back and reading the symbols
1365 for real.
1367 We look for sections with specific names, to tell us what debug
1368 format to look for: FIXME!!!
1370 elfstab_build_psymtabs() handles STABS symbols;
1371 mdebug_build_psymtabs() handles ECOFF debugging information.
1373 Note that ELF files have a "minimal" symbol table, which looks a lot
1374 like a COFF symbol table, but has only the minimal information necessary
1375 for linking. We process this also, and use the information to
1376 build gdb's minimal symbol table. This gives us some minimal debugging
1377 capability even for files compiled without -g. */
1379 static void
1380 elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
1382 bfd *abfd = objfile->obfd.get ();
1383 struct elfinfo ei;
1385 memset ((char *) &ei, 0, sizeof (ei));
1386 if (!(objfile->flags & OBJF_READNEVER))
1388 for (asection *sect : gdb_bfd_sections (abfd))
1389 elf_locate_sections (sect, &ei);
1392 elf_read_minimal_symbols (objfile, symfile_flags, &ei);
1394 /* ELF debugging information is inserted into the psymtab in the
1395 order of least informative first - most informative last. Since
1396 the psymtab table is searched `most recent insertion first' this
1397 increases the probability that more detailed debug information
1398 for a section is found.
1400 For instance, an object file might contain both .mdebug (XCOFF)
1401 and .debug_info (DWARF2) sections then .mdebug is inserted first
1402 (searched last) and DWARF2 is inserted last (searched first). If
1403 we don't do this then the XCOFF info is found first - for code in
1404 an included file XCOFF info is useless. */
1406 if (ei.mdebugsect)
1408 const struct ecoff_debug_swap *swap;
1410 /* .mdebug section, presumably holding ECOFF debugging
1411 information. */
1412 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1413 if (swap)
1414 elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
1416 if (ei.stabsect)
1418 asection *str_sect;
1420 /* Stab sections have an associated string table that looks like
1421 a separate section. */
1422 str_sect = bfd_get_section_by_name (abfd, ".stabstr");
1424 /* FIXME should probably warn about a stab section without a stabstr. */
1425 if (str_sect)
1426 elfstab_build_psymtabs (objfile,
1427 ei.stabsect,
1428 str_sect->filepos,
1429 bfd_section_size (str_sect));
1432 /* Read the CTF section only if there is no DWARF info. */
1433 if (always_read_ctf && ei.ctfsect)
1435 elfctf_build_psymtabs (objfile);
1438 bool has_dwarf2 = elf_symfile_read_dwarf2 (objfile, symfile_flags);
1440 /* Read the CTF section only if there is no DWARF info. */
1441 if (!always_read_ctf && !has_dwarf2 && ei.ctfsect)
1443 elfctf_build_psymtabs (objfile);
1446 /* Copy relocations are used by some ABIs using the ELF format, so
1447 set the objfile flag indicating this fact. */
1448 objfile->object_format_has_copy_relocs = true;
1451 /* Initialize anything that needs initializing when a completely new symbol
1452 file is specified (not just adding some symbols from another file, e.g. a
1453 shared library). */
1455 static void
1456 elf_new_init (struct objfile *ignore)
1460 /* Perform any local cleanups required when we are done with a particular
1461 objfile. I.E, we are in the process of discarding all symbol information
1462 for an objfile, freeing up all memory held for it, and unlinking the
1463 objfile struct from the global list of known objfiles. */
1465 static void
1466 elf_symfile_finish (struct objfile *objfile)
1470 /* ELF specific initialization routine for reading symbols. */
1472 static void
1473 elf_symfile_init (struct objfile *objfile)
1477 /* Implementation of `sym_get_probes', as documented in symfile.h. */
1479 static const elfread_data &
1480 elf_get_probes (struct objfile *objfile)
1482 elfread_data *probes_per_bfd = probe_key.get (objfile->obfd.get ());
1484 if (probes_per_bfd == NULL)
1486 probes_per_bfd = probe_key.emplace (objfile->obfd.get ());
1488 /* Here we try to gather information about all types of probes from the
1489 objfile. */
1490 for (const static_probe_ops *ops : all_static_probe_ops)
1491 ops->get_probes (probes_per_bfd, objfile);
1494 return *probes_per_bfd;
1499 /* Implementation `sym_probe_fns', as documented in symfile.h. */
1501 static const struct sym_probe_fns elf_probe_fns =
1503 elf_get_probes, /* sym_get_probes */
1506 /* Register that we are able to handle ELF object file formats. */
1508 static const struct sym_fns elf_sym_fns =
1510 elf_new_init, /* init anything gbl to entire symtab */
1511 elf_symfile_init, /* read initial info, setup for sym_read() */
1512 elf_symfile_read, /* read a symbol file into symtab */
1513 elf_symfile_finish, /* finished with file, cleanup */
1514 default_symfile_offsets, /* Translate ext. to int. relocation */
1515 elf_symfile_segments, /* Get segment information from a file. */
1516 NULL,
1517 default_symfile_relocate, /* Relocate a debug section. */
1518 &elf_probe_fns, /* sym_probe_fns */
1521 /* STT_GNU_IFUNC resolver vector to be installed to gnu_ifunc_fns_p. */
1523 static const struct gnu_ifunc_fns elf_gnu_ifunc_fns =
1525 elf_gnu_ifunc_resolve_addr,
1526 elf_gnu_ifunc_resolve_name,
1527 elf_gnu_ifunc_resolver_stop,
1528 elf_gnu_ifunc_resolver_return_stop
1531 void _initialize_elfread ();
1532 void
1533 _initialize_elfread ()
1535 add_symtab_fns (bfd_target_elf_flavour, &elf_sym_fns);
1537 gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
1539 /* Add "set always-read-ctf on/off". */
1540 add_setshow_boolean_cmd ("always-read-ctf", class_support, &always_read_ctf,
1541 _("\
1542 Set whether CTF is always read."),
1543 _("\
1544 Show whether CTF is always read."),
1545 _("\
1546 When off, CTF is only read if DWARF is not present. When on, CTF is read\n\
1547 regardless of whether DWARF is present."),
1548 nullptr /* set_func */, nullptr /* show_func */,
1549 &setlist, &showlist);