diff options
Diffstat (limited to 'lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp')
-rw-r--r-- | lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp | 235 |
1 files changed, 137 insertions, 98 deletions
diff --git a/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp b/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp index 71b4b73ca6d3..299d76183cd4 100644 --- a/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp +++ b/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp @@ -14,57 +14,56 @@ namespace { using namespace llvm; using namespace llvm::orc; -class VSOSearchOrderResolver : public JITSymbolResolver { +class JITDylibSearchOrderResolver : public JITSymbolResolver { public: - VSOSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {} + JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {} - Expected<LookupResult> lookup(const LookupSet &Symbols) { - auto &ES = MR.getTargetVSO().getExecutionSession(); + void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) { + auto &ES = MR.getTargetJITDylib().getExecutionSession(); SymbolNameSet InternedSymbols; + // Intern the requested symbols: lookup takes interned strings. for (auto &S : Symbols) - InternedSymbols.insert(ES.getSymbolStringPool().intern(S)); - + InternedSymbols.insert(ES.intern(S)); + + // Build an OnResolve callback to unwrap the interned strings and pass them + // to the OnResolved callback. + // FIXME: Switch to move capture of OnResolved once we have c++14. + auto OnResolvedWithUnwrap = + [OnResolved](Expected<SymbolMap> InternedResult) { + if (!InternedResult) { + OnResolved(InternedResult.takeError()); + return; + } + + LookupResult Result; + for (auto &KV : *InternedResult) + Result[*KV.first] = std::move(KV.second); + OnResolved(Result); + }; + + // We're not waiting for symbols to be ready. Just log any errors. + auto OnReady = [&ES](Error Err) { ES.reportError(std::move(Err)); }; + + // Register dependencies for all symbols contained in this set. auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) { MR.addDependenciesForAll(Deps); }; - auto InternedResult = - MR.getTargetVSO().withSearchOrderDo([&](const VSOList &VSOs) { - return ES.lookup(VSOs, InternedSymbols, RegisterDependencies, false); - }); - - if (!InternedResult) - return InternedResult.takeError(); - - LookupResult Result; - for (auto &KV : *InternedResult) - Result[*KV.first] = std::move(KV.second); - - return Result; + JITDylibSearchList SearchOrder; + MR.getTargetJITDylib().withSearchOrderDo( + [&](const JITDylibSearchList &JDs) { SearchOrder = JDs; }); + ES.lookup(SearchOrder, InternedSymbols, OnResolvedWithUnwrap, OnReady, + RegisterDependencies); } - Expected<LookupFlagsResult> lookupFlags(const LookupSet &Symbols) { - auto &ES = MR.getTargetVSO().getExecutionSession(); - - SymbolNameSet InternedSymbols; - - for (auto &S : Symbols) - InternedSymbols.insert(ES.getSymbolStringPool().intern(S)); - - SymbolFlagsMap InternedResult; - MR.getTargetVSO().withSearchOrderDo([&](const VSOList &VSOs) { - // An empty search order is pathalogical, but allowed. - if (VSOs.empty()) - return; + Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) { + LookupSet Result; - assert(VSOs.front() && "VSOList entry can not be null"); - InternedResult = VSOs.front()->lookupFlags(InternedSymbols); - }); - - LookupFlagsResult Result; - for (auto &KV : InternedResult) - Result[*KV.first] = std::move(KV.second); + for (auto &KV : MR.getSymbols()) { + if (Symbols.count(*KV.first)) + Result.insert(*KV.first); + } return Result; } @@ -78,52 +77,41 @@ private: namespace llvm { namespace orc { -RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2( +RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer( ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager, - NotifyLoadedFunction NotifyLoaded, NotifyFinalizedFunction NotifyFinalized) + NotifyLoadedFunction NotifyLoaded, NotifyEmittedFunction NotifyEmitted) : ObjectLayer(ES), GetMemoryManager(GetMemoryManager), NotifyLoaded(std::move(NotifyLoaded)), - NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {} + NotifyEmitted(std::move(NotifyEmitted)) {} -void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R, - VModuleKey K, - std::unique_ptr<MemoryBuffer> O) { +void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R, + std::unique_ptr<MemoryBuffer> O) { assert(O && "Object must not be null"); - auto &ES = getExecutionSession(); - - auto ObjFile = object::ObjectFile::createObjectFile(*O); - if (!ObjFile) { - getExecutionSession().reportError(ObjFile.takeError()); - R.failMaterialization(); - } - - auto MemoryManager = GetMemoryManager(K); - - VSOSearchOrderResolver Resolver(R); - auto RTDyld = llvm::make_unique<RuntimeDyld>(*MemoryManager, Resolver); - RTDyld->setProcessAllSections(ProcessAllSections); + // This method launches an asynchronous link step that will fulfill our + // materialization responsibility. We need to switch R to be heap + // allocated before that happens so it can live as long as the asynchronous + // link needs it to (i.e. it must be able to outlive this method). + auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R)); - { - std::lock_guard<std::mutex> Lock(RTDyldLayerMutex); + auto &ES = getExecutionSession(); - assert(!ActiveRTDylds.count(K) && - "An active RTDyld already exists for this key?"); - ActiveRTDylds[K] = RTDyld.get(); + auto Obj = object::ObjectFile::createObjectFile(*O); - assert(!MemMgrs.count(K) && - "A memory manager already exists for this key?"); - MemMgrs[K] = std::move(MemoryManager); + if (!Obj) { + getExecutionSession().reportError(Obj.takeError()); + SharedR->failMaterialization(); + return; } - auto Info = RTDyld->loadObject(**ObjFile); - + // Collect the internal symbols from the object file: We will need to + // filter these later. + auto InternalSymbols = std::make_shared<std::set<StringRef>>(); { - std::set<StringRef> InternalSymbols; - for (auto &Sym : (*ObjFile)->symbols()) { + for (auto &Sym : (*Obj)->symbols()) { if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) { if (auto SymName = Sym.getName()) - InternalSymbols.insert(*SymName); + InternalSymbols->insert(*SymName); else { ES.reportError(SymName.takeError()); R.failMaterialization(); @@ -131,46 +119,97 @@ void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R, } } } - - SymbolMap Symbols; - for (auto &KV : RTDyld->getSymbolTable()) - if (!InternalSymbols.count(KV.first)) - Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second; - - R.resolve(Symbols); } - if (NotifyLoaded) - NotifyLoaded(K, **ObjFile, *Info); - - RTDyld->finalizeWithMemoryManagerLocking(); + auto K = R.getVModuleKey(); + RuntimeDyld::MemoryManager *MemMgr = nullptr; + // Create a record a memory manager for this object. { + auto Tmp = GetMemoryManager(); std::lock_guard<std::mutex> Lock(RTDyldLayerMutex); - ActiveRTDylds.erase(K); + MemMgrs.push_back(std::move(Tmp)); + MemMgr = MemMgrs.back().get(); } - if (RTDyld->hasError()) { - ES.reportError(make_error<StringError>(RTDyld->getErrorString(), - inconvertibleErrorCode())); - R.failMaterialization(); - return; + JITDylibSearchOrderResolver Resolver(*SharedR); + + /* Thoughts on proper cross-dylib weak symbol handling: + * + * Change selection of canonical defs to be a manually triggered process, and + * add a 'canonical' bit to symbol definitions. When canonical def selection + * is triggered, sweep the JITDylibs to mark defs as canonical, discard + * duplicate defs. + */ + jitLinkForORC( + **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections, + [this, K, SharedR, &Obj, InternalSymbols]( + std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, + std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) { + return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo), + ResolvedSymbols, *InternalSymbols); + }, + [this, K, SharedR](Error Err) { + onObjEmit(K, *SharedR, std::move(Err)); + }); +} + +Error RTDyldObjectLinkingLayer::onObjLoad( + VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj, + std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, + std::map<StringRef, JITEvaluatedSymbol> Resolved, + std::set<StringRef> &InternalSymbols) { + SymbolFlagsMap ExtraSymbolsToClaim; + SymbolMap Symbols; + for (auto &KV : Resolved) { + // Scan the symbols and add them to the Symbols map for resolution. + + // We never claim internal symbols. + if (InternalSymbols.count(KV.first)) + continue; + + auto InternedName = getExecutionSession().intern(KV.first); + auto Flags = KV.second.getFlags(); + + // Override object flags and claim responsibility for symbols if + // requested. + if (OverrideObjectFlags || AutoClaimObjectSymbols) { + auto I = R.getSymbols().find(InternedName); + + if (OverrideObjectFlags && I != R.getSymbols().end()) + Flags = JITSymbolFlags::stripTransientFlags(I->second); + else if (AutoClaimObjectSymbols && I == R.getSymbols().end()) + ExtraSymbolsToClaim[InternedName] = Flags; + } + + Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags); } - R.finalize(); + if (!ExtraSymbolsToClaim.empty()) + if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim)) + return Err; + + R.resolve(Symbols); + + if (NotifyLoaded) + NotifyLoaded(K, Obj, *LoadedObjInfo); - if (NotifyFinalized) - NotifyFinalized(K); + return Error::success(); } -void RTDyldObjectLinkingLayer2::mapSectionAddress( - VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const { - std::lock_guard<std::mutex> Lock(RTDyldLayerMutex); - auto ActiveRTDyldItr = ActiveRTDylds.find(K); +void RTDyldObjectLinkingLayer::onObjEmit(VModuleKey K, + MaterializationResponsibility &R, + Error Err) { + if (Err) { + getExecutionSession().reportError(std::move(Err)); + R.failMaterialization(); + return; + } + + R.emit(); - assert(ActiveRTDyldItr != ActiveRTDylds.end() && - "No active RTDyld instance found for key"); - ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr); + if (NotifyEmitted) + NotifyEmitted(K); } } // End namespace orc. |