ELFLinkGraphBuilder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 =
  34. &G->createSection(CommonSectionName, MemProt::Read | 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 relocation records in the given section. The handler
  89. /// function Func should be callable with this signature:
  90. /// Error(const typename ELFT::Rela &,
  91. /// const typename ELFT::Shdr &, Section &)
  92. ///
  93. template <typename RelocHandlerFunction>
  94. Error forEachRelocation(const typename ELFT::Shdr &RelSect,
  95. RelocHandlerFunction &&Func,
  96. bool ProcessDebugSections = false);
  97. /// Traverse all matching relocation records in the given section. Convenience
  98. /// wrapper to allow passing a member function for the handler.
  99. ///
  100. template <typename ClassT, typename RelocHandlerMethod>
  101. Error forEachRelocation(const typename ELFT::Shdr &RelSect, ClassT *Instance,
  102. RelocHandlerMethod &&Method,
  103. bool ProcessDebugSections = false) {
  104. return forEachRelocation(
  105. RelSect,
  106. [Instance, Method](const auto &Rel, const auto &Target, auto &GS) {
  107. return (Instance->*Method)(Rel, Target, GS);
  108. },
  109. ProcessDebugSections);
  110. }
  111. const ELFFile &Obj;
  112. typename ELFFile::Elf_Shdr_Range Sections;
  113. const typename ELFFile::Elf_Shdr *SymTabSec = nullptr;
  114. StringRef SectionStringTab;
  115. // Maps ELF section indexes to LinkGraph Blocks.
  116. // Only SHF_ALLOC sections will have graph blocks.
  117. DenseMap<ELFSectionIndex, Block *> GraphBlocks;
  118. DenseMap<ELFSymbolIndex, Symbol *> GraphSymbols;
  119. DenseMap<const typename ELFFile::Elf_Shdr *,
  120. ArrayRef<typename ELFFile::Elf_Word>>
  121. ShndxTables;
  122. };
  123. template <typename ELFT>
  124. ELFLinkGraphBuilder<ELFT>::ELFLinkGraphBuilder(
  125. const ELFFile &Obj, Triple TT, StringRef FileName,
  126. LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
  127. : ELFLinkGraphBuilderBase(std::make_unique<LinkGraph>(
  128. FileName.str(), Triple(std::move(TT)), ELFT::Is64Bits ? 8 : 4,
  129. support::endianness(ELFT::TargetEndianness),
  130. std::move(GetEdgeKindName))),
  131. Obj(Obj) {
  132. LLVM_DEBUG(
  133. { dbgs() << "Created ELFLinkGraphBuilder for \"" << FileName << "\""; });
  134. }
  135. template <typename ELFT>
  136. Expected<std::unique_ptr<LinkGraph>> ELFLinkGraphBuilder<ELFT>::buildGraph() {
  137. if (!isRelocatable())
  138. return make_error<JITLinkError>("Object is not a relocatable ELF file");
  139. if (auto Err = prepare())
  140. return std::move(Err);
  141. if (auto Err = graphifySections())
  142. return std::move(Err);
  143. if (auto Err = graphifySymbols())
  144. return std::move(Err);
  145. if (auto Err = addRelocations())
  146. return std::move(Err);
  147. return std::move(G);
  148. }
  149. template <typename ELFT>
  150. Expected<std::pair<Linkage, Scope>>
  151. ELFLinkGraphBuilder<ELFT>::getSymbolLinkageAndScope(
  152. const typename ELFT::Sym &Sym, StringRef Name) {
  153. Linkage L = Linkage::Strong;
  154. Scope S = Scope::Default;
  155. switch (Sym.getBinding()) {
  156. case ELF::STB_LOCAL:
  157. S = Scope::Local;
  158. break;
  159. case ELF::STB_GLOBAL:
  160. // Nothing to do here.
  161. break;
  162. case ELF::STB_WEAK:
  163. case ELF::STB_GNU_UNIQUE:
  164. L = Linkage::Weak;
  165. break;
  166. default:
  167. return make_error<StringError>(
  168. "Unrecognized symbol binding " +
  169. Twine(static_cast<int>(Sym.getBinding())) + " for " + Name,
  170. inconvertibleErrorCode());
  171. }
  172. switch (Sym.getVisibility()) {
  173. case ELF::STV_DEFAULT:
  174. case ELF::STV_PROTECTED:
  175. // FIXME: Make STV_DEFAULT symbols pre-emptible? This probably needs
  176. // Orc support.
  177. // Otherwise nothing to do here.
  178. break;
  179. case ELF::STV_HIDDEN:
  180. // Default scope -> Hidden scope. No effect on local scope.
  181. if (S == Scope::Default)
  182. S = Scope::Hidden;
  183. break;
  184. case ELF::STV_INTERNAL:
  185. return make_error<StringError>(
  186. "Unrecognized symbol visibility " +
  187. Twine(static_cast<int>(Sym.getVisibility())) + " for " + Name,
  188. inconvertibleErrorCode());
  189. }
  190. return std::make_pair(L, S);
  191. }
  192. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::prepare() {
  193. LLVM_DEBUG(dbgs() << " Preparing to build...\n");
  194. // Get the sections array.
  195. if (auto SectionsOrErr = Obj.sections())
  196. Sections = *SectionsOrErr;
  197. else
  198. return SectionsOrErr.takeError();
  199. // Get the section string table.
  200. if (auto SectionStringTabOrErr = Obj.getSectionStringTable(Sections))
  201. SectionStringTab = *SectionStringTabOrErr;
  202. else
  203. return SectionStringTabOrErr.takeError();
  204. // Get the SHT_SYMTAB section.
  205. for (auto &Sec : Sections) {
  206. if (Sec.sh_type == ELF::SHT_SYMTAB) {
  207. if (!SymTabSec)
  208. SymTabSec = &Sec;
  209. else
  210. return make_error<JITLinkError>("Multiple SHT_SYMTAB sections in " +
  211. G->getName());
  212. }
  213. // Extended table.
  214. if (Sec.sh_type == ELF::SHT_SYMTAB_SHNDX) {
  215. uint32_t SymtabNdx = Sec.sh_link;
  216. if (SymtabNdx >= Sections.size())
  217. return make_error<JITLinkError>("sh_link is out of bound");
  218. auto ShndxTable = Obj.getSHNDXTable(Sec);
  219. if (!ShndxTable)
  220. return ShndxTable.takeError();
  221. ShndxTables.insert({&Sections[SymtabNdx], *ShndxTable});
  222. }
  223. }
  224. return Error::success();
  225. }
  226. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySections() {
  227. LLVM_DEBUG(dbgs() << " Creating graph sections...\n");
  228. // For each section...
  229. for (ELFSectionIndex SecIndex = 0; SecIndex != Sections.size(); ++SecIndex) {
  230. auto &Sec = Sections[SecIndex];
  231. // Start by getting the section name.
  232. auto Name = Obj.getSectionName(Sec, SectionStringTab);
  233. if (!Name)
  234. return Name.takeError();
  235. // If the name indicates that it's a debug section then skip it: We don't
  236. // support those yet.
  237. if (isDwarfSection(*Name)) {
  238. LLVM_DEBUG({
  239. dbgs() << " " << SecIndex << ": \"" << *Name
  240. << "\" is a debug section: "
  241. "No graph section will be created.\n";
  242. });
  243. continue;
  244. }
  245. // Skip non-SHF_ALLOC sections
  246. if (!(Sec.sh_flags & ELF::SHF_ALLOC)) {
  247. LLVM_DEBUG({
  248. dbgs() << " " << SecIndex << ": \"" << *Name
  249. << "\" is not an SHF_ALLOC section: "
  250. "No graph section will be created.\n";
  251. });
  252. continue;
  253. }
  254. LLVM_DEBUG({
  255. dbgs() << " " << SecIndex << ": Creating section for \"" << *Name
  256. << "\"\n";
  257. });
  258. // Get the section's memory protection flags.
  259. MemProt Prot;
  260. if (Sec.sh_flags & ELF::SHF_EXECINSTR)
  261. Prot = MemProt::Read | MemProt::Exec;
  262. else
  263. Prot = MemProt::Read | MemProt::Write;
  264. // Look for existing sections first.
  265. auto *GraphSec = G->findSectionByName(*Name);
  266. if (!GraphSec)
  267. GraphSec = &G->createSection(*Name, Prot);
  268. assert(GraphSec->getMemProt() == Prot && "MemProt should match");
  269. Block *B = nullptr;
  270. if (Sec.sh_type != ELF::SHT_NOBITS) {
  271. auto Data = Obj.template getSectionContentsAsArray<char>(Sec);
  272. if (!Data)
  273. return Data.takeError();
  274. B = &G->createContentBlock(*GraphSec, *Data,
  275. orc::ExecutorAddr(Sec.sh_addr),
  276. Sec.sh_addralign, 0);
  277. } else
  278. B = &G->createZeroFillBlock(*GraphSec, Sec.sh_size,
  279. orc::ExecutorAddr(Sec.sh_addr),
  280. Sec.sh_addralign, 0);
  281. setGraphBlock(SecIndex, B);
  282. }
  283. return Error::success();
  284. }
  285. template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySymbols() {
  286. LLVM_DEBUG(dbgs() << " Creating graph symbols...\n");
  287. // No SYMTAB -- Bail out early.
  288. if (!SymTabSec)
  289. return Error::success();
  290. // Get the section content as a Symbols array.
  291. auto Symbols = Obj.symbols(SymTabSec);
  292. if (!Symbols)
  293. return Symbols.takeError();
  294. // Get the string table for this section.
  295. auto StringTab = Obj.getStringTableForSymtab(*SymTabSec, Sections);
  296. if (!StringTab)
  297. return StringTab.takeError();
  298. LLVM_DEBUG({
  299. StringRef SymTabName;
  300. if (auto SymTabNameOrErr = Obj.getSectionName(*SymTabSec, SectionStringTab))
  301. SymTabName = *SymTabNameOrErr;
  302. else {
  303. dbgs() << "Could not get ELF SHT_SYMTAB section name for logging: "
  304. << toString(SymTabNameOrErr.takeError()) << "\n";
  305. SymTabName = "<SHT_SYMTAB section with invalid name>";
  306. }
  307. dbgs() << " Adding symbols from symtab section \"" << SymTabName
  308. << "\"\n";
  309. });
  310. for (ELFSymbolIndex SymIndex = 0; SymIndex != Symbols->size(); ++SymIndex) {
  311. auto &Sym = (*Symbols)[SymIndex];
  312. // Check symbol type.
  313. switch (Sym.getType()) {
  314. case ELF::STT_FILE:
  315. LLVM_DEBUG({
  316. if (auto Name = Sym.getName(*StringTab))
  317. dbgs() << " " << SymIndex << ": Skipping STT_FILE symbol \""
  318. << *Name << "\"\n";
  319. else {
  320. dbgs() << "Could not get STT_FILE symbol name: "
  321. << toString(Name.takeError()) << "\n";
  322. dbgs() << " " << SymIndex
  323. << ": Skipping STT_FILE symbol with invalid name\n";
  324. }
  325. });
  326. continue;
  327. break;
  328. }
  329. // Get the symbol name.
  330. auto Name = Sym.getName(*StringTab);
  331. if (!Name)
  332. return Name.takeError();
  333. // Handle common symbols specially.
  334. if (Sym.isCommon()) {
  335. Symbol &GSym = G->addCommonSymbol(*Name, Scope::Default,
  336. getCommonSection(), orc::ExecutorAddr(),
  337. Sym.st_size, Sym.getValue(), false);
  338. setGraphSymbol(SymIndex, GSym);
  339. continue;
  340. }
  341. // Map Visibility and Binding to Scope and Linkage:
  342. Linkage L;
  343. Scope S;
  344. if (auto LSOrErr = getSymbolLinkageAndScope(Sym, *Name))
  345. std::tie(L, S) = *LSOrErr;
  346. else
  347. return LSOrErr.takeError();
  348. if (Sym.isDefined() &&
  349. (Sym.getType() == ELF::STT_NOTYPE || Sym.getType() == ELF::STT_FUNC ||
  350. Sym.getType() == ELF::STT_OBJECT ||
  351. Sym.getType() == ELF::STT_SECTION || Sym.getType() == ELF::STT_TLS)) {
  352. // Handle extended tables.
  353. unsigned Shndx = Sym.st_shndx;
  354. if (Shndx == ELF::SHN_XINDEX) {
  355. auto ShndxTable = ShndxTables.find(SymTabSec);
  356. if (ShndxTable == ShndxTables.end())
  357. continue;
  358. auto NdxOrErr = object::getExtendedSymbolTableIndex<ELFT>(
  359. Sym, SymIndex, ShndxTable->second);
  360. if (!NdxOrErr)
  361. return NdxOrErr.takeError();
  362. Shndx = *NdxOrErr;
  363. }
  364. if (auto *B = getGraphBlock(Shndx)) {
  365. LLVM_DEBUG({
  366. dbgs() << " " << SymIndex
  367. << ": Creating defined graph symbol for ELF symbol \"" << *Name
  368. << "\"\n";
  369. });
  370. // In RISCV, temporary symbols (Used to generate dwarf, eh_frame
  371. // sections...) will appear in object code's symbol table, and LLVM does
  372. // not use names on these temporary symbols (RISCV gnu toolchain uses
  373. // names on these temporary symbols). If the symbol is unnamed, add an
  374. // anonymous symbol.
  375. auto &GSym =
  376. Name->empty()
  377. ? G->addAnonymousSymbol(*B, Sym.getValue(), Sym.st_size,
  378. false, false)
  379. : G->addDefinedSymbol(*B, Sym.getValue(), *Name, Sym.st_size, L,
  380. S, Sym.getType() == ELF::STT_FUNC, false);
  381. setGraphSymbol(SymIndex, GSym);
  382. }
  383. } else if (Sym.isUndefined() && Sym.isExternal()) {
  384. LLVM_DEBUG({
  385. dbgs() << " " << SymIndex
  386. << ": Creating external graph symbol for ELF symbol \"" << *Name
  387. << "\"\n";
  388. });
  389. auto &GSym = G->addExternalSymbol(*Name, Sym.st_size, L);
  390. setGraphSymbol(SymIndex, GSym);
  391. } else {
  392. LLVM_DEBUG({
  393. dbgs() << " " << SymIndex
  394. << ": Not creating graph symbol for ELF symbol \"" << *Name
  395. << "\" with unrecognized type\n";
  396. });
  397. }
  398. }
  399. return Error::success();
  400. }
  401. template <typename ELFT>
  402. template <typename RelocHandlerFunction>
  403. Error ELFLinkGraphBuilder<ELFT>::forEachRelocation(
  404. const typename ELFT::Shdr &RelSect, RelocHandlerFunction &&Func,
  405. bool ProcessDebugSections) {
  406. // Only look into sections that store relocation entries.
  407. if (RelSect.sh_type != ELF::SHT_RELA && RelSect.sh_type != ELF::SHT_REL)
  408. return Error::success();
  409. // sh_info contains the section header index of the target (FixupSection),
  410. // which is the section to which all relocations in RelSect apply.
  411. auto FixupSection = Obj.getSection(RelSect.sh_info);
  412. if (!FixupSection)
  413. return FixupSection.takeError();
  414. // Target sections have names in valid ELF object files.
  415. Expected<StringRef> Name = Obj.getSectionName(**FixupSection);
  416. if (!Name)
  417. return Name.takeError();
  418. LLVM_DEBUG(dbgs() << " " << *Name << ":\n");
  419. // Consider skipping these relocations.
  420. if (!ProcessDebugSections && isDwarfSection(*Name)) {
  421. LLVM_DEBUG(dbgs() << " skipped (dwarf section)\n\n");
  422. return Error::success();
  423. }
  424. // Lookup the link-graph node corresponding to the target section name.
  425. auto *BlockToFix = getGraphBlock(RelSect.sh_info);
  426. if (!BlockToFix)
  427. return make_error<StringError>(
  428. "Refencing a section that wasn't added to the graph: " + *Name,
  429. inconvertibleErrorCode());
  430. auto RelEntries = Obj.relas(RelSect);
  431. if (!RelEntries)
  432. return RelEntries.takeError();
  433. // Let the callee process relocation entries one by one.
  434. for (const typename ELFT::Rela &R : *RelEntries)
  435. if (Error Err = Func(R, **FixupSection, *BlockToFix))
  436. return Err;
  437. LLVM_DEBUG(dbgs() << "\n");
  438. return Error::success();
  439. }
  440. } // end namespace jitlink
  441. } // end namespace llvm
  442. #undef DEBUG_TYPE
  443. #endif // LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H