1 //===------ JITLinkGeneric.h - Generic JIT linker utilities -----*- 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 JITLinker utilities. E.g. graph pruning, eh-frame parsing. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LIB_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H 14 #define LIB_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H 15 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 18 19 #define DEBUG_TYPE "jitlink" 20 21 namespace llvm { 22 23 class MemoryBufferRef; 24 25 namespace jitlink { 26 27 /// Base class for a JIT linker. 28 /// 29 /// A JITLinkerBase instance links one object file into an ongoing JIT 30 /// session. Symbol resolution and finalization operations are pluggable, 31 /// and called using continuation passing (passing a continuation for the 32 /// remaining linker work) to allow them to be performed asynchronously. 33 class JITLinkerBase { 34 public: 35 JITLinkerBase(std::unique_ptr<JITLinkContext> Ctx, 36 std::unique_ptr<LinkGraph> G, PassConfiguration Passes) 37 : Ctx(std::move(Ctx)), G(std::move(G)), Passes(std::move(Passes)) { 38 assert(this->Ctx && "Ctx can not be null"); 39 assert(this->G && "G can not be null"); 40 } 41 42 virtual ~JITLinkerBase(); 43 44 protected: 45 using InFlightAlloc = JITLinkMemoryManager::InFlightAlloc; 46 using AllocResult = Expected<std::unique_ptr<InFlightAlloc>>; 47 using FinalizeResult = Expected<JITLinkMemoryManager::FinalizedAlloc>; 48 49 // Returns the PassConfiguration for this instance. This can be used by 50 // JITLinkerBase implementations to add late passes that reference their 51 // own data structures (e.g. for ELF implementations to locate / construct 52 // a GOT start symbol prior to fixup). 53 PassConfiguration &getPassConfig() { return Passes; } 54 55 // Phase 1: 56 // 1.1: Run pre-prune passes 57 // 1.2: Prune graph 58 // 1.3: Run post-prune passes 59 // 1.4: Allocate memory. 60 void linkPhase1(std::unique_ptr<JITLinkerBase> Self); 61 62 // Phase 2: 63 // 2.2: Run post-allocation passes 64 // 2.3: Notify context of final assigned symbol addresses 65 // 2.4: Identify external symbols and make an async call to resolve 66 void linkPhase2(std::unique_ptr<JITLinkerBase> Self, AllocResult AR); 67 68 // Phase 3: 69 // 3.1: Apply resolution results 70 // 3.2: Run pre-fixup passes 71 // 3.3: Fix up block contents 72 // 3.4: Run post-fixup passes 73 // 3.5: Make an async call to transfer and finalize memory. 74 void linkPhase3(std::unique_ptr<JITLinkerBase> Self, 75 Expected<AsyncLookupResult> LookupResult); 76 77 // Phase 4: 78 // 4.1: Call OnFinalized callback, handing off allocation. 79 void linkPhase4(std::unique_ptr<JITLinkerBase> Self, FinalizeResult FR); 80 81 private: 82 // Run all passes in the given pass list, bailing out immediately if any pass 83 // returns an error. 84 Error runPasses(LinkGraphPassList &Passes); 85 86 // Copy block contents and apply relocations. 87 // Implemented in JITLinker. 88 virtual Error fixUpBlocks(LinkGraph &G) const = 0; 89 90 JITLinkContext::LookupMap getExternalSymbolNames() const; 91 void applyLookupResult(AsyncLookupResult LR); 92 void abandonAllocAndBailOut(std::unique_ptr<JITLinkerBase> Self, Error Err); 93 94 std::unique_ptr<JITLinkContext> Ctx; 95 std::unique_ptr<LinkGraph> G; 96 PassConfiguration Passes; 97 std::unique_ptr<InFlightAlloc> Alloc; 98 }; 99 100 template <typename LinkerImpl> class JITLinker : public JITLinkerBase { 101 public: 102 using JITLinkerBase::JITLinkerBase; 103 104 /// Link constructs a LinkerImpl instance and calls linkPhase1. 105 /// Link should be called with the constructor arguments for LinkerImpl, which 106 /// will be forwarded to the constructor. 107 template <typename... ArgTs> static void link(ArgTs &&... Args) { 108 auto L = std::make_unique<LinkerImpl>(std::forward<ArgTs>(Args)...); 109 110 // Ownership of the linker is passed into the linker's doLink function to 111 // allow it to be passed on to async continuations. 112 // 113 // FIXME: Remove LTmp once we have c++17. 114 // C++17 sequencing rules guarantee that function name expressions are 115 // sequenced before arguments, so L->linkPhase1(std::move(L), ...) will be 116 // well formed. 117 auto <mp = *L; 118 LTmp.linkPhase1(std::move(L)); 119 } 120 121 private: 122 const LinkerImpl &impl() const { 123 return static_cast<const LinkerImpl &>(*this); 124 } 125 126 Error fixUpBlocks(LinkGraph &G) const override { 127 LLVM_DEBUG(dbgs() << "Fixing up blocks:\n"); 128 129 for (auto *B : G.blocks()) { 130 LLVM_DEBUG(dbgs() << " " << *B << ":\n"); 131 132 // Copy Block data and apply fixups. 133 LLVM_DEBUG(dbgs() << " Applying fixups.\n"); 134 assert((!B->isZeroFill() || B->edges_size() == 0) && 135 "Edges in zero-fill block?"); 136 for (auto &E : B->edges()) { 137 138 // Skip non-relocation edges. 139 if (!E.isRelocation()) 140 continue; 141 142 // Dispatch to LinkerImpl for fixup. 143 if (auto Err = impl().applyFixup(G, *B, E)) 144 return Err; 145 } 146 } 147 148 return Error::success(); 149 } 150 }; 151 152 /// Removes dead symbols/blocks/addressables. 153 /// 154 /// Finds the set of symbols and addressables reachable from any symbol 155 /// initially marked live. All symbols/addressables not marked live at the end 156 /// of this process are removed. 157 void prune(LinkGraph &G); 158 159 } // end namespace jitlink 160 } // end namespace llvm 161 162 #undef DEBUG_TYPE // "jitlink" 163 164 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H 165