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