ELFLinkGraphBuilder.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //===------- ELFLinkGraphBuilder.h - ELF LinkGraph builder ------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Generic ELF LinkGraph building code.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H
  13. #define LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H
  14. #include "llvm/ExecutionEngine/JITLink/JITLink.h"
  15. #include "llvm/Object/ELF.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #define DEBUG_TYPE "jitlink"
  20. namespace llvm {
  21. namespace jitlink {
  22. /// Common link-graph building code shared between all ELFFiles.
  23. class ELFLinkGraphBuilderBase {
  24. public:
  25. ELFLinkGraphBuilderBase(std::unique_ptr<LinkGraph> G) : G(std::move(G)) {}
  26. virtual ~ELFLinkGraphBuilderBase();
  27. protected:
  28. static bool isDwarfSection(StringRef SectionName) {
  29. return llvm::is_contained(DwarfSectionNames, SectionName);
  30. }
  31. Section &getCommonSection() {
  32. if (!CommonSection)
  33. CommonSection = &G->createSection(
  34. CommonSectionName, orc::MemProt::Read | orc::MemProt::Write);
  35. return *CommonSection;
  36. }
  37. std::unique_ptr<LinkGraph> G;
  38. private:
  39. static StringRef CommonSectionName;
  40. static ArrayRef<const char *> DwarfSectionNames;
  41. Section *CommonSection = nullptr;
  42. };
  43. /// Ling-graph building code that's specific to the given ELFT, but common
  44. /// across all architectures.
  45. template <typename ELFT>
  46. class ELFLinkGraphBuilder : public ELFLinkGraphBuilderBase {
  47. using ELFFile = object::ELFFile<ELFT>;
  48. public:
  49. ELFLinkGraphBuilder(const object::ELFFile<ELFT> &Obj, Triple TT,
  50. StringRef FileName,
  51. LinkGraph::GetEdgeKindNameFunction GetEdgeKindName);
  52. /// Attempt to construct and return the LinkGraph.
  53. Expected<std::unique_ptr<LinkGraph>> buildGraph();
  54. /// Call to derived class to handle relocations. These require
  55. /// architecture specific knowledge to map to JITLink edge kinds.
  56. virtual Error addRelocations() = 0;
  57. protected:
  58. using ELFSectionIndex = unsigned;
  59. using ELFSymbolIndex = unsigned;
  60. bool isRelocatable() const {
  61. return Obj.getHeader().e_type == llvm::ELF::ET_REL;
  62. }
  63. void setGraphBlock(ELFSectionIndex SecIndex, Block *B) {
  64. assert(!GraphBlocks.count(SecIndex) && "Duplicate section at index");
  65. GraphBlocks[SecIndex] = B;
  66. }
  67. Block *getGraphBlock(ELFSectionIndex SecIndex) {
  68. auto I = GraphBlocks.find(SecIndex);
  69. if (I == GraphBlocks.end())
  70. return nullptr;
  71. return I->second;
  72. }
  73. void setGraphSymbol(ELFSymbolIndex SymIndex, Symbol &Sym) {
  74. assert(!GraphSymbols.count(SymIndex) && "Duplicate symbol at index");
  75. GraphSymbols[SymIndex] = &Sym;
  76. }
  77. Symbol *getGraphSymbol(ELFSymbolIndex SymIndex) {
  78. auto I = GraphSymbols.find(SymIndex);
  79. if (I == GraphSymbols.end())
  80. return nullptr;
  81. return I->second;
  82. }
  83. Expected<std::pair<Linkage, Scope>>
  84. getSymbolLinkageAndScope(const typename ELFT::Sym &Sym, StringRef Name);
  85. Error prepare();
  86. Error graphifySections();
  87. Error graphifySymbols();
  88. /// Traverse all matching ELFT::Rela relocation records in the given section.
  89. /// The handler function Func should be callable with this signature:
  90. /// Error(const typename ELFT::Rela &,
  91. /// const typename ELFT::Shdr &, Section &)
  92. ///
  93. template <typename RelocHandlerMethod>
  94. Error forEachRelaRelocation(const typename ELFT::Shdr &RelSect,
  95. RelocHandlerMethod &&Func,
  96. bool ProcessDebugSections = false);
  97. /// Traverse all matching ELFT::Rel relocation records in the given section.
  98. /// The handler function Func should be callable with this signature:
  99. /// Error(const typename ELFT::Rel &,
  100. /// const typename ELFT::Shdr &, Section &)
  101. ///
  102. template <typename RelocHandlerMethod>
  103. Error forEachRelRelocation(const typename ELFT::Shdr &RelSect,
  104. RelocHandlerMethod &&Func,
  105. bool ProcessDebugSections = false);
  106. /// Traverse all matching rela relocation records in the given section.
  107. /// Convenience wrapper to allow passing a member function for the handler.
  108. ///
  109. template <typename ClassT, typename RelocHandlerMethod>
  110. Error forEachRelaRelocation(const typename ELFT::Shdr &RelSect,
  111. ClassT *Instance, RelocHandlerMethod &&Method,
  112. bool ProcessDebugSections = false) {
  113. return forEachRelaRelocation(
  114. RelSect,
  115. [Instance, Method](const auto &Rel, const auto &Target, auto &GS) {
  116. return (Instance->*Method)(Rel, Target, GS);
  117. },
  118. ProcessDebugSections);
  119. }
  120. /// Traverse all matching rel relocation records in the given section.
  121. /// Convenience wrapper to allow passing a member function for the handler.
  122. ///
  123. template <typename ClassT, typename RelocHandlerMethod>
  124. Error forEachRelRelocation(const typename ELFT::Shdr &RelSect,
  125. ClassT *Instance, RelocHandlerMethod &&Method,
  126. bool ProcessDebugSections = false) {
  127. return forEachRelRelocation(
  128. RelSect,
  129. [Instance, Method](const auto &Rel, const auto &Target, auto &GS) {
  130. return (Instance->*Method)(Rel, Target, GS);
  131. },
  132. ProcessDebugSections);
  133. }
  134. const ELFFile &Obj;
  135. typename ELFFile::Elf_Shdr_Range Sections;
  136. const typename ELFFile::Elf_Shdr *SymTabSec = nullptr;
  137. StringRef SectionStringTab;
  138. // Maps ELF section indexes to LinkGraph Blocks.
  139. // Only SHF_ALLOC sections will have graph blocks.
  140. DenseMap<ELFSectionIndex, Block *> GraphBlocks;
  141. DenseMap<ELFSymbolIndex, Symbol *> GraphSymbols;
  142. DenseMap<const typename ELFFile::Elf_Shdr *,
  143. ArrayRef<typename ELFFile::Elf_Word>>
  144. ShndxTables;
  145. };
  146. template <typename ELFT>
  147. ELFLinkGraphBuilder<ELFT>::ELFLinkGraphBuilder(
  148. const ELFFile &Obj, Triple TT, StringRef FileName,
  149. LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
  150. : ELFLinkGraphBuilderBase(std::make_unique<LinkGraph>(
  151. FileName.str(), Triple(std::move(TT)), ELFT::Is64Bits ? 8 : 4,
  152. support::endianness(ELFT::TargetEndianness),
  153. std::move(GetEdgeKindName))),
  154. Obj(Obj) {
  155. LLVM_DEBUG(
  156. { dbgs() << "Created ELFLinkGraphBuilder for \"" << FileName << "\""; });
  157. }
  158. template <typename ELFT>
  159. Expected<std::unique_ptr<LinkGraph>> ELFLinkGraphBuilder<ELFT>::buildGraph() {
  160. if (!isRelocatable())
  161. return make_error<JITLinkError>("Object is not a relocatable ELF file");
  162. if (auto Err = prepare())
  163. return std::move(Err);
  164. if (auto Err = graphifySections())
  165. return std::move(Err);
  166. if (auto Err = graphifySymbols())
  167. return std::move(Err);
  168. if (auto Err = addRelocations())
  169. return std::move(Err);
  170. return std::move(G);
  171. }
  172. template <typename ELFT>
  173. Expected<std::pair<Linkage, Scope>>
  174. ELFLinkGraphBuilder<ELFT>::getSymbolLinkageAndScope(
  175. const typename ELFT::Sym &Sym, StringRef Name) {
  176. Linkage L = Linkage::Strong;
  177. Scope S = Scope::Default;
  178. switch (Sym.getBinding()) {
  179. case ELF::STB_LOCAL:
  180. S = Scope::Local;
  181. break;
  182. case ELF::STB_GLOBAL:
  183. // Nothing to do here.
  184. break;
  185. case ELF::STB_WEAK:
  186. case ELF::STB_GNU_UNIQUE:
  187. L = Linkage::Weak;
  188. break;
  189. default:
  190. return make_error<StringError>(
  191. "Unrecognized symbol binding " +
  192. Twine(static_cast<int>(Sym.getBinding())) + " for " + Name,
  193. inconvertibleErrorCode());
  194. }
  195. switch (Sym.getVisibility()) {
  196. case ELF::STV_DEFAULT:
  197. case ELF::STV_PROTECTED:
  198. // FIXME: Make STV_DEFAULT symbols pre-emptible? This probably needs
  199. // Orc support.
  200. // Otherwise nothing to do here.
  201. break;
  202. case ELF::STV_HIDDEN:
  203. // Default scope -> Hidden scope. No effect on local scope.
  204. if (S == Scope::Default)
  205. S = Scope::Hidden;
  206. break;
  207. case ELF::STV_INTERNAL:
  208. return make_error<StringError>(
  209. "Unrecognized symbol visibility " +
  210. Twine(static_cast<int>(Sym.getVisibility())) + " for " + Name,
  211. inconvertibleErrorCode());
  212. }
  213. return std::make_pair(L, S);
  214. }
  215. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::prepare() {
  216. LLVM_DEBUG(dbgs() << " Preparing to build...\n");
  217. // Get the sections array.
  218. if (auto SectionsOrErr = Obj.sections())
  219. Sections = *SectionsOrErr;
  220. else
  221. return SectionsOrErr.takeError();
  222. // Get the section string table.
  223. if (auto SectionStringTabOrErr = Obj.getSectionStringTable(Sections))
  224. SectionStringTab = *SectionStringTabOrErr;
  225. else
  226. return SectionStringTabOrErr.takeError();
  227. // Get the SHT_SYMTAB section.
  228. for (auto &Sec : Sections) {
  229. if (Sec.sh_type == ELF::SHT_SYMTAB) {
  230. if (!SymTabSec)
  231. SymTabSec = &Sec;
  232. else
  233. return make_error<JITLinkError>("Multiple SHT_SYMTAB sections in " +
  234. G->getName());
  235. }
  236. // Extended table.
  237. if (Sec.sh_type == ELF::SHT_SYMTAB_SHNDX) {
  238. uint32_t SymtabNdx = Sec.sh_link;
  239. if (SymtabNdx >= Sections.size())
  240. return make_error<JITLinkError>("sh_link is out of bound");
  241. auto ShndxTable = Obj.getSHNDXTable(Sec);
  242. if (!ShndxTable)
  243. return ShndxTable.takeError();
  244. ShndxTables.insert({&Sections[SymtabNdx], *ShndxTable});
  245. }
  246. }
  247. return Error::success();
  248. }
  249. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySections() {
  250. LLVM_DEBUG(dbgs() << " Creating graph sections...\n");
  251. // For each section...
  252. for (ELFSectionIndex SecIndex = 0; SecIndex != Sections.size(); ++SecIndex) {
  253. auto &Sec = Sections[SecIndex];
  254. // Start by getting the section name.
  255. auto Name = Obj.getSectionName(Sec, SectionStringTab);
  256. if (!Name)
  257. return Name.takeError();
  258. // If the name indicates that it's a debug section then skip it: We don't
  259. // support those yet.
  260. if (isDwarfSection(*Name)) {
  261. LLVM_DEBUG({
  262. dbgs() << " " << SecIndex << ": \"" << *Name
  263. << "\" is a debug section: "
  264. "No graph section will be created.\n";
  265. });
  266. continue;
  267. }
  268. // Skip non-SHF_ALLOC sections
  269. if (!(Sec.sh_flags & ELF::SHF_ALLOC)) {
  270. LLVM_DEBUG({
  271. dbgs() << " " << SecIndex << ": \"" << *Name
  272. << "\" is not an SHF_ALLOC section: "
  273. "No graph section will be created.\n";
  274. });
  275. continue;
  276. }
  277. LLVM_DEBUG({
  278. dbgs() << " " << SecIndex << ": Creating section for \"" << *Name
  279. << "\"\n";
  280. });
  281. // Get the section's memory protection flags.
  282. orc::MemProt Prot;
  283. if (Sec.sh_flags & ELF::SHF_EXECINSTR)
  284. Prot = orc::MemProt::Read | orc::MemProt::Exec;
  285. else
  286. Prot = orc::MemProt::Read | orc::MemProt::Write;
  287. // Look for existing sections first.
  288. auto *GraphSec = G->findSectionByName(*Name);
  289. if (!GraphSec)
  290. GraphSec = &G->createSection(*Name, Prot);
  291. assert(GraphSec->getMemProt() == Prot && "MemProt should match");
  292. Block *B = nullptr;
  293. if (Sec.sh_type != ELF::SHT_NOBITS) {
  294. auto Data = Obj.template getSectionContentsAsArray<char>(Sec);
  295. if (!Data)
  296. return Data.takeError();
  297. B = &G->createContentBlock(*GraphSec, *Data,
  298. orc::ExecutorAddr(Sec.sh_addr),
  299. Sec.sh_addralign, 0);
  300. } else
  301. B = &G->createZeroFillBlock(*GraphSec, Sec.sh_size,
  302. orc::ExecutorAddr(Sec.sh_addr),
  303. Sec.sh_addralign, 0);
  304. setGraphBlock(SecIndex, B);
  305. }
  306. return Error::success();
  307. }
  308. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySymbols() {
  309. LLVM_DEBUG(dbgs() << " Creating graph symbols...\n");
  310. // No SYMTAB -- Bail out early.
  311. if (!SymTabSec)
  312. return Error::success();
  313. // Get the section content as a Symbols array.
  314. auto Symbols = Obj.symbols(SymTabSec);
  315. if (!Symbols)
  316. return Symbols.takeError();
  317. // Get the string table for this section.
  318. auto StringTab = Obj.getStringTableForSymtab(*SymTabSec, Sections);
  319. if (!StringTab)
  320. return StringTab.takeError();
  321. LLVM_DEBUG({
  322. StringRef SymTabName;
  323. if (auto SymTabNameOrErr = Obj.getSectionName(*SymTabSec, SectionStringTab))
  324. SymTabName = *SymTabNameOrErr;
  325. else {
  326. dbgs() << "Could not get ELF SHT_SYMTAB section name for logging: "
  327. << toString(SymTabNameOrErr.takeError()) << "\n";
  328. SymTabName = "<SHT_SYMTAB section with invalid name>";
  329. }
  330. dbgs() << " Adding symbols from symtab section \"" << SymTabName
  331. << "\"\n";
  332. });
  333. for (ELFSymbolIndex SymIndex = 0; SymIndex != Symbols->size(); ++SymIndex) {
  334. auto &Sym = (*Symbols)[SymIndex];
  335. // Check symbol type.
  336. switch (Sym.getType()) {
  337. case ELF::STT_FILE:
  338. LLVM_DEBUG({
  339. if (auto Name = Sym.getName(*StringTab))
  340. dbgs() << " " << SymIndex << ": Skipping STT_FILE symbol \""
  341. << *Name << "\"\n";
  342. else {
  343. dbgs() << "Could not get STT_FILE symbol name: "
  344. << toString(Name.takeError()) << "\n";
  345. dbgs() << " " << SymIndex
  346. << ": Skipping STT_FILE symbol with invalid name\n";
  347. }
  348. });
  349. continue;
  350. break;
  351. }
  352. // Get the symbol name.
  353. auto Name = Sym.getName(*StringTab);
  354. if (!Name)
  355. return Name.takeError();
  356. // Handle common symbols specially.
  357. if (Sym.isCommon()) {
  358. Symbol &GSym = G->addDefinedSymbol(
  359. G->createZeroFillBlock(getCommonSection(), Sym.st_size,
  360. orc::ExecutorAddr(), Sym.getValue(), 0),
  361. 0, *Name, Sym.st_size, Linkage::Strong, Scope::Default, false, false);
  362. setGraphSymbol(SymIndex, GSym);
  363. continue;
  364. }
  365. if (Sym.isDefined() &&
  366. (Sym.getType() == ELF::STT_NOTYPE || Sym.getType() == ELF::STT_FUNC ||
  367. Sym.getType() == ELF::STT_OBJECT ||
  368. Sym.getType() == ELF::STT_SECTION || Sym.getType() == ELF::STT_TLS)) {
  369. // Map Visibility and Binding to Scope and Linkage:
  370. Linkage L;
  371. Scope S;
  372. if (auto LSOrErr = getSymbolLinkageAndScope(Sym, *Name))
  373. std::tie(L, S) = *LSOrErr;
  374. else
  375. return LSOrErr.takeError();
  376. // Handle extended tables.
  377. unsigned Shndx = Sym.st_shndx;
  378. if (Shndx == ELF::SHN_XINDEX) {
  379. auto ShndxTable = ShndxTables.find(SymTabSec);
  380. if (ShndxTable == ShndxTables.end())
  381. continue;
  382. auto NdxOrErr = object::getExtendedSymbolTableIndex<ELFT>(
  383. Sym, SymIndex, ShndxTable->second);
  384. if (!NdxOrErr)
  385. return NdxOrErr.takeError();
  386. Shndx = *NdxOrErr;
  387. }
  388. if (auto *B = getGraphBlock(Shndx)) {
  389. LLVM_DEBUG({
  390. dbgs() << " " << SymIndex
  391. << ": Creating defined graph symbol for ELF symbol \"" << *Name
  392. << "\"\n";
  393. });
  394. // In RISCV, temporary symbols (Used to generate dwarf, eh_frame
  395. // sections...) will appear in object code's symbol table, and LLVM does
  396. // not use names on these temporary symbols (RISCV gnu toolchain uses
  397. // names on these temporary symbols). If the symbol is unnamed, add an
  398. // anonymous symbol.
  399. auto &GSym =
  400. Name->empty()
  401. ? G->addAnonymousSymbol(*B, Sym.getValue(), Sym.st_size,
  402. false, false)
  403. : G->addDefinedSymbol(*B, Sym.getValue(), *Name, Sym.st_size, L,
  404. S, Sym.getType() == ELF::STT_FUNC, false);
  405. setGraphSymbol(SymIndex, GSym);
  406. }
  407. } else if (Sym.isUndefined() && Sym.isExternal()) {
  408. LLVM_DEBUG({
  409. dbgs() << " " << SymIndex
  410. << ": Creating external graph symbol for ELF symbol \"" << *Name
  411. << "\"\n";
  412. });
  413. if (Sym.getBinding() != ELF::STB_GLOBAL &&
  414. Sym.getBinding() != ELF::STB_WEAK)
  415. return make_error<StringError>(
  416. "Invalid symbol binding " +
  417. Twine(static_cast<int>(Sym.getBinding())) +
  418. " for external symbol " + *Name,
  419. inconvertibleErrorCode());
  420. // If L is Linkage::Weak that means this is a weakly referenced symbol.
  421. auto &GSym = G->addExternalSymbol(*Name, Sym.st_size,
  422. Sym.getBinding() == ELF::STB_WEAK);
  423. setGraphSymbol(SymIndex, GSym);
  424. } else {
  425. LLVM_DEBUG({
  426. dbgs() << " " << SymIndex
  427. << ": Not creating graph symbol for ELF symbol \"" << *Name
  428. << "\" with unrecognized type\n";
  429. });
  430. }
  431. }
  432. return Error::success();
  433. }
  434. template <typename ELFT>
  435. template <typename RelocHandlerFunction>
  436. Error ELFLinkGraphBuilder<ELFT>::forEachRelaRelocation(
  437. const typename ELFT::Shdr &RelSect, RelocHandlerFunction &&Func,
  438. bool ProcessDebugSections) {
  439. // Only look into sections that store relocation entries.
  440. if (RelSect.sh_type != ELF::SHT_RELA)
  441. return Error::success();
  442. // sh_info contains the section header index of the target (FixupSection),
  443. // which is the section to which all relocations in RelSect apply.
  444. auto FixupSection = Obj.getSection(RelSect.sh_info);
  445. if (!FixupSection)
  446. return FixupSection.takeError();
  447. // Target sections have names in valid ELF object files.
  448. Expected<StringRef> Name = Obj.getSectionName(**FixupSection);
  449. if (!Name)
  450. return Name.takeError();
  451. LLVM_DEBUG(dbgs() << " " << *Name << ":\n");
  452. // Consider skipping these relocations.
  453. if (!ProcessDebugSections && isDwarfSection(*Name)) {
  454. LLVM_DEBUG(dbgs() << " skipped (dwarf section)\n\n");
  455. return Error::success();
  456. }
  457. // Lookup the link-graph node corresponding to the target section name.
  458. auto *BlockToFix = getGraphBlock(RelSect.sh_info);
  459. if (!BlockToFix)
  460. return make_error<StringError>(
  461. "Refencing a section that wasn't added to the graph: " + *Name,
  462. inconvertibleErrorCode());
  463. auto RelEntries = Obj.relas(RelSect);
  464. if (!RelEntries)
  465. return RelEntries.takeError();
  466. // Let the callee process relocation entries one by one.
  467. for (const typename ELFT::Rela &R : *RelEntries)
  468. if (Error Err = Func(R, **FixupSection, *BlockToFix))
  469. return Err;
  470. LLVM_DEBUG(dbgs() << "\n");
  471. return Error::success();
  472. }
  473. template <typename ELFT>
  474. template <typename RelocHandlerFunction>
  475. Error ELFLinkGraphBuilder<ELFT>::forEachRelRelocation(
  476. const typename ELFT::Shdr &RelSect, RelocHandlerFunction &&Func,
  477. bool ProcessDebugSections) {
  478. // Only look into sections that store relocation entries.
  479. if (RelSect.sh_type != ELF::SHT_REL)
  480. return Error::success();
  481. // sh_info contains the section header index of the target (FixupSection),
  482. // which is the section to which all relocations in RelSect apply.
  483. auto FixupSection = Obj.getSection(RelSect.sh_info);
  484. if (!FixupSection)
  485. return FixupSection.takeError();
  486. // Target sections have names in valid ELF object files.
  487. Expected<StringRef> Name = Obj.getSectionName(**FixupSection);
  488. if (!Name)
  489. return Name.takeError();
  490. LLVM_DEBUG(dbgs() << " " << *Name << ":\n");
  491. // Consider skipping these relocations.
  492. if (!ProcessDebugSections && isDwarfSection(*Name)) {
  493. LLVM_DEBUG(dbgs() << " skipped (dwarf section)\n\n");
  494. return Error::success();
  495. }
  496. // Lookup the link-graph node corresponding to the target section name.
  497. auto *BlockToFix = getGraphBlock(RelSect.sh_info);
  498. if (!BlockToFix)
  499. return make_error<StringError>(
  500. "Refencing a section that wasn't added to the graph: " + *Name,
  501. inconvertibleErrorCode());
  502. auto RelEntries = Obj.rels(RelSect);
  503. if (!RelEntries)
  504. return RelEntries.takeError();
  505. // Let the callee process relocation entries one by one.
  506. for (const typename ELFT::Rel &R : *RelEntries)
  507. if (Error Err = Func(R, **FixupSection, *BlockToFix))
  508. return Err;
  509. LLVM_DEBUG(dbgs() << "\n");
  510. return Error::success();
  511. }
  512. } // end namespace jitlink
  513. } // end namespace llvm
  514. #undef DEBUG_TYPE
  515. #endif // LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H