1 //===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===// 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 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 10 11 namespace { 12 13 using namespace llvm; 14 using namespace llvm::orc; 15 16 class JITDylibSearchOrderResolver : public JITSymbolResolver { 17 public: 18 JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {} 19 20 void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) { 21 auto &ES = MR.getTargetJITDylib().getExecutionSession(); 22 SymbolLookupSet InternedSymbols; 23 24 // Intern the requested symbols: lookup takes interned strings. 25 for (auto &S : Symbols) 26 InternedSymbols.add(ES.intern(S)); 27 28 // Build an OnResolve callback to unwrap the interned strings and pass them 29 // to the OnResolved callback. 30 auto OnResolvedWithUnwrap = 31 [OnResolved = std::move(OnResolved)]( 32 Expected<SymbolMap> InternedResult) mutable { 33 if (!InternedResult) { 34 OnResolved(InternedResult.takeError()); 35 return; 36 } 37 38 LookupResult Result; 39 for (auto &KV : *InternedResult) 40 Result[*KV.first] = std::move(KV.second); 41 OnResolved(Result); 42 }; 43 44 // Register dependencies for all symbols contained in this set. 45 auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) { 46 MR.addDependenciesForAll(Deps); 47 }; 48 49 JITDylibSearchOrder SearchOrder; 50 MR.getTargetJITDylib().withSearchOrderDo( 51 [&](const JITDylibSearchOrder &JDs) { SearchOrder = JDs; }); 52 ES.lookup(LookupKind::Static, SearchOrder, InternedSymbols, 53 SymbolState::Resolved, std::move(OnResolvedWithUnwrap), 54 RegisterDependencies); 55 } 56 57 Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) { 58 LookupSet Result; 59 60 for (auto &KV : MR.getSymbols()) { 61 if (Symbols.count(*KV.first)) 62 Result.insert(*KV.first); 63 } 64 65 return Result; 66 } 67 68 private: 69 MaterializationResponsibility &MR; 70 }; 71 72 } // end anonymous namespace 73 74 namespace llvm { 75 namespace orc { 76 77 RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer( 78 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager) 79 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {} 80 81 RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() { 82 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex); 83 for (auto &MemMgr : MemMgrs) 84 MemMgr->deregisterEHFrames(); 85 } 86 87 void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R, 88 std::unique_ptr<MemoryBuffer> O) { 89 assert(O && "Object must not be null"); 90 91 // This method launches an asynchronous link step that will fulfill our 92 // materialization responsibility. We need to switch R to be heap 93 // allocated before that happens so it can live as long as the asynchronous 94 // link needs it to (i.e. it must be able to outlive this method). 95 auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R)); 96 97 auto &ES = getExecutionSession(); 98 99 // Create a MemoryBufferRef backed MemoryBuffer (i.e. shallow) copy of the 100 // the underlying buffer to pass into RuntimeDyld. This allows us to hold 101 // ownership of the real underlying buffer and return it to the user once 102 // the object has been emitted. 103 auto ObjBuffer = MemoryBuffer::getMemBuffer(O->getMemBufferRef(), false); 104 105 auto Obj = object::ObjectFile::createObjectFile(*ObjBuffer); 106 107 if (!Obj) { 108 getExecutionSession().reportError(Obj.takeError()); 109 SharedR->failMaterialization(); 110 return; 111 } 112 113 // Collect the internal symbols from the object file: We will need to 114 // filter these later. 115 auto InternalSymbols = std::make_shared<std::set<StringRef>>(); 116 { 117 for (auto &Sym : (*Obj)->symbols()) { 118 if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) { 119 if (auto SymName = Sym.getName()) 120 InternalSymbols->insert(*SymName); 121 else { 122 ES.reportError(SymName.takeError()); 123 R.failMaterialization(); 124 return; 125 } 126 } 127 } 128 } 129 130 auto K = R.getVModuleKey(); 131 RuntimeDyld::MemoryManager *MemMgr = nullptr; 132 133 // Create a record a memory manager for this object. 134 { 135 auto Tmp = GetMemoryManager(); 136 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex); 137 MemMgrs.push_back(std::move(Tmp)); 138 MemMgr = MemMgrs.back().get(); 139 } 140 141 JITDylibSearchOrderResolver Resolver(*SharedR); 142 143 jitLinkForORC( 144 **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections, 145 [this, K, SharedR, &Obj, InternalSymbols]( 146 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, 147 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) { 148 return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo), 149 ResolvedSymbols, *InternalSymbols); 150 }, 151 [this, K, SharedR, O = std::move(O)](Error Err) mutable { 152 onObjEmit(K, std::move(O), *SharedR, std::move(Err)); 153 }); 154 } 155 156 Error RTDyldObjectLinkingLayer::onObjLoad( 157 VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj, 158 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, 159 std::map<StringRef, JITEvaluatedSymbol> Resolved, 160 std::set<StringRef> &InternalSymbols) { 161 SymbolFlagsMap ExtraSymbolsToClaim; 162 SymbolMap Symbols; 163 for (auto &KV : Resolved) { 164 // Scan the symbols and add them to the Symbols map for resolution. 165 166 // We never claim internal symbols. 167 if (InternalSymbols.count(KV.first)) 168 continue; 169 170 auto InternedName = getExecutionSession().intern(KV.first); 171 auto Flags = KV.second.getFlags(); 172 173 // Override object flags and claim responsibility for symbols if 174 // requested. 175 if (OverrideObjectFlags || AutoClaimObjectSymbols) { 176 auto I = R.getSymbols().find(InternedName); 177 178 if (OverrideObjectFlags && I != R.getSymbols().end()) 179 Flags = I->second; 180 else if (AutoClaimObjectSymbols && I == R.getSymbols().end()) 181 ExtraSymbolsToClaim[InternedName] = Flags; 182 } 183 184 Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags); 185 } 186 187 if (!ExtraSymbolsToClaim.empty()) 188 if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim)) 189 return Err; 190 191 if (auto Err = R.notifyResolved(Symbols)) { 192 R.failMaterialization(); 193 return Err; 194 } 195 196 if (NotifyLoaded) 197 NotifyLoaded(K, Obj, *LoadedObjInfo); 198 199 return Error::success(); 200 } 201 202 void RTDyldObjectLinkingLayer::onObjEmit( 203 VModuleKey K, std::unique_ptr<MemoryBuffer> ObjBuffer, 204 MaterializationResponsibility &R, Error Err) { 205 if (Err) { 206 getExecutionSession().reportError(std::move(Err)); 207 R.failMaterialization(); 208 return; 209 } 210 211 if (auto Err = R.notifyEmitted()) { 212 getExecutionSession().reportError(std::move(Err)); 213 R.failMaterialization(); 214 return; 215 } 216 217 if (NotifyEmitted) 218 NotifyEmitted(K, std::move(ObjBuffer)); 219 } 220 221 LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer( 222 ExecutionSession &ES, ResourcesGetter GetResources, 223 NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized, 224 NotifyFreedFtor NotifyFreed) 225 : ES(ES), GetResources(std::move(GetResources)), 226 NotifyLoaded(std::move(NotifyLoaded)), 227 NotifyFinalized(std::move(NotifyFinalized)), 228 NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {} 229 230 } // End namespace orc. 231 } // End namespace llvm. 232