elfread.c 45 KB

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