10b57cec5SDimitry Andric //===------- ObjectLinkingLayer.cpp - JITLink backed ORC ObjectLayer ------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 100b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 110b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h" 12fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h" 13fe6060f1SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 14fe6060f1SDimitry Andric #include <string> 150b57cec5SDimitry Andric #include <vector> 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #define DEBUG_TYPE "orc" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric using namespace llvm; 200b57cec5SDimitry Andric using namespace llvm::jitlink; 210b57cec5SDimitry Andric using namespace llvm::orc; 220b57cec5SDimitry Andric 23fe6060f1SDimitry Andric namespace { 24fe6060f1SDimitry Andric 25fe6060f1SDimitry Andric class LinkGraphMaterializationUnit : public MaterializationUnit { 26fe6060f1SDimitry Andric public: 27fe6060f1SDimitry Andric static std::unique_ptr<LinkGraphMaterializationUnit> 28fe6060f1SDimitry Andric Create(ObjectLinkingLayer &ObjLinkingLayer, std::unique_ptr<LinkGraph> G) { 29fe6060f1SDimitry Andric auto LGI = scanLinkGraph(ObjLinkingLayer.getExecutionSession(), *G); 30fe6060f1SDimitry Andric return std::unique_ptr<LinkGraphMaterializationUnit>( 31fe6060f1SDimitry Andric new LinkGraphMaterializationUnit(ObjLinkingLayer, std::move(G), 32fe6060f1SDimitry Andric std::move(LGI))); 33fe6060f1SDimitry Andric } 34fe6060f1SDimitry Andric 35fe6060f1SDimitry Andric StringRef getName() const override { return G->getName(); } 36fe6060f1SDimitry Andric void materialize(std::unique_ptr<MaterializationResponsibility> MR) override { 37fe6060f1SDimitry Andric ObjLinkingLayer.emit(std::move(MR), std::move(G)); 38fe6060f1SDimitry Andric } 39fe6060f1SDimitry Andric 40fe6060f1SDimitry Andric private: 410eae32dcSDimitry Andric static Interface scanLinkGraph(ExecutionSession &ES, LinkGraph &G) { 42fe6060f1SDimitry Andric 430eae32dcSDimitry Andric Interface LGI; 44fe6060f1SDimitry Andric 45fe6060f1SDimitry Andric for (auto *Sym : G.defined_symbols()) { 46fe6060f1SDimitry Andric // Skip local symbols. 47fe6060f1SDimitry Andric if (Sym->getScope() == Scope::Local) 48fe6060f1SDimitry Andric continue; 49fe6060f1SDimitry Andric assert(Sym->hasName() && "Anonymous non-local symbol?"); 50fe6060f1SDimitry Andric 51fe6060f1SDimitry Andric JITSymbolFlags Flags; 52fe6060f1SDimitry Andric if (Sym->getScope() == Scope::Default) 53fe6060f1SDimitry Andric Flags |= JITSymbolFlags::Exported; 54fe6060f1SDimitry Andric 55fe6060f1SDimitry Andric if (Sym->isCallable()) 56fe6060f1SDimitry Andric Flags |= JITSymbolFlags::Callable; 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric LGI.SymbolFlags[ES.intern(Sym->getName())] = Flags; 59fe6060f1SDimitry Andric } 60fe6060f1SDimitry Andric 61349cc55cSDimitry Andric if ((G.getTargetTriple().isOSBinFormatMachO() && hasMachOInitSection(G)) || 62349cc55cSDimitry Andric (G.getTargetTriple().isOSBinFormatELF() && hasELFInitSection(G))) 63fe6060f1SDimitry Andric LGI.InitSymbol = makeInitSymbol(ES, G); 64fe6060f1SDimitry Andric 65fe6060f1SDimitry Andric return LGI; 66fe6060f1SDimitry Andric } 67fe6060f1SDimitry Andric 68fe6060f1SDimitry Andric static bool hasMachOInitSection(LinkGraph &G) { 69fe6060f1SDimitry Andric for (auto &Sec : G.sections()) 70fe6060f1SDimitry Andric if (Sec.getName() == "__DATA,__obj_selrefs" || 71fe6060f1SDimitry Andric Sec.getName() == "__DATA,__objc_classlist" || 72fe6060f1SDimitry Andric Sec.getName() == "__TEXT,__swift5_protos" || 73fe6060f1SDimitry Andric Sec.getName() == "__TEXT,__swift5_proto" || 74349cc55cSDimitry Andric Sec.getName() == "__TEXT,__swift5_types" || 75fe6060f1SDimitry Andric Sec.getName() == "__DATA,__mod_init_func") 76fe6060f1SDimitry Andric return true; 77fe6060f1SDimitry Andric return false; 78fe6060f1SDimitry Andric } 79fe6060f1SDimitry Andric 80349cc55cSDimitry Andric static bool hasELFInitSection(LinkGraph &G) { 81*81ad6265SDimitry Andric for (auto &Sec : G.sections()) { 82*81ad6265SDimitry Andric auto SecName = Sec.getName(); 83*81ad6265SDimitry Andric if (SecName.consume_front(".init_array") && 84*81ad6265SDimitry Andric (SecName.empty() || SecName[0] == '.')) 85349cc55cSDimitry Andric return true; 86*81ad6265SDimitry Andric } 87349cc55cSDimitry Andric return false; 88349cc55cSDimitry Andric } 89349cc55cSDimitry Andric 90fe6060f1SDimitry Andric static SymbolStringPtr makeInitSymbol(ExecutionSession &ES, LinkGraph &G) { 91fe6060f1SDimitry Andric std::string InitSymString; 92fe6060f1SDimitry Andric raw_string_ostream(InitSymString) 93fe6060f1SDimitry Andric << "$." << G.getName() << ".__inits" << Counter++; 94fe6060f1SDimitry Andric return ES.intern(InitSymString); 95fe6060f1SDimitry Andric } 96fe6060f1SDimitry Andric 97fe6060f1SDimitry Andric LinkGraphMaterializationUnit(ObjectLinkingLayer &ObjLinkingLayer, 980eae32dcSDimitry Andric std::unique_ptr<LinkGraph> G, Interface LGI) 990eae32dcSDimitry Andric : MaterializationUnit(std::move(LGI)), ObjLinkingLayer(ObjLinkingLayer), 1000eae32dcSDimitry Andric G(std::move(G)) {} 101fe6060f1SDimitry Andric 102fe6060f1SDimitry Andric void discard(const JITDylib &JD, const SymbolStringPtr &Name) override { 103fe6060f1SDimitry Andric for (auto *Sym : G->defined_symbols()) 104fe6060f1SDimitry Andric if (Sym->getName() == *Name) { 105fe6060f1SDimitry Andric assert(Sym->getLinkage() == Linkage::Weak && 106fe6060f1SDimitry Andric "Discarding non-weak definition"); 107fe6060f1SDimitry Andric G->makeExternal(*Sym); 108fe6060f1SDimitry Andric break; 109fe6060f1SDimitry Andric } 110fe6060f1SDimitry Andric } 111fe6060f1SDimitry Andric 112fe6060f1SDimitry Andric ObjectLinkingLayer &ObjLinkingLayer; 113fe6060f1SDimitry Andric std::unique_ptr<LinkGraph> G; 114fe6060f1SDimitry Andric static std::atomic<uint64_t> Counter; 115fe6060f1SDimitry Andric }; 116fe6060f1SDimitry Andric 117fe6060f1SDimitry Andric std::atomic<uint64_t> LinkGraphMaterializationUnit::Counter{0}; 118fe6060f1SDimitry Andric 119fe6060f1SDimitry Andric } // end anonymous namespace 120fe6060f1SDimitry Andric 1210b57cec5SDimitry Andric namespace llvm { 1220b57cec5SDimitry Andric namespace orc { 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric class ObjectLinkingLayerJITLinkContext final : public JITLinkContext { 1250b57cec5SDimitry Andric public: 126e8d8bef9SDimitry Andric ObjectLinkingLayerJITLinkContext( 127e8d8bef9SDimitry Andric ObjectLinkingLayer &Layer, 128e8d8bef9SDimitry Andric std::unique_ptr<MaterializationResponsibility> MR, 1290b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> ObjBuffer) 130e8d8bef9SDimitry Andric : JITLinkContext(&MR->getTargetJITDylib()), Layer(Layer), 131e8d8bef9SDimitry Andric MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {} 1320b57cec5SDimitry Andric 1338bcb0991SDimitry Andric ~ObjectLinkingLayerJITLinkContext() { 1348bcb0991SDimitry Andric // If there is an object buffer return function then use it to 1358bcb0991SDimitry Andric // return ownership of the buffer. 136e8d8bef9SDimitry Andric if (Layer.ReturnObjectBuffer && ObjBuffer) 1378bcb0991SDimitry Andric Layer.ReturnObjectBuffer(std::move(ObjBuffer)); 1388bcb0991SDimitry Andric } 1398bcb0991SDimitry Andric 140e8d8bef9SDimitry Andric JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; } 1410b57cec5SDimitry Andric 142fe6060f1SDimitry Andric void notifyMaterializing(LinkGraph &G) { 143fe6060f1SDimitry Andric for (auto &P : Layer.Plugins) 144fe6060f1SDimitry Andric P->notifyMaterializing(*MR, G, *this, 145fe6060f1SDimitry Andric ObjBuffer ? ObjBuffer->getMemBufferRef() 146fe6060f1SDimitry Andric : MemoryBufferRef()); 147fe6060f1SDimitry Andric } 148fe6060f1SDimitry Andric 1490b57cec5SDimitry Andric void notifyFailed(Error Err) override { 150e8d8bef9SDimitry Andric for (auto &P : Layer.Plugins) 151e8d8bef9SDimitry Andric Err = joinErrors(std::move(Err), P->notifyFailed(*MR)); 1520b57cec5SDimitry Andric Layer.getExecutionSession().reportError(std::move(Err)); 153e8d8bef9SDimitry Andric MR->failMaterialization(); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 156480093f4SDimitry Andric void lookup(const LookupMap &Symbols, 1578bcb0991SDimitry Andric std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override { 1580b57cec5SDimitry Andric 1595ffd83dbSDimitry Andric JITDylibSearchOrder LinkOrder; 160e8d8bef9SDimitry Andric MR->getTargetJITDylib().withLinkOrderDo( 1615ffd83dbSDimitry Andric [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; }); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric auto &ES = Layer.getExecutionSession(); 1640b57cec5SDimitry Andric 165480093f4SDimitry Andric SymbolLookupSet LookupSet; 166480093f4SDimitry Andric for (auto &KV : Symbols) { 167480093f4SDimitry Andric orc::SymbolLookupFlags LookupFlags; 168480093f4SDimitry Andric switch (KV.second) { 169480093f4SDimitry Andric case jitlink::SymbolLookupFlags::RequiredSymbol: 170480093f4SDimitry Andric LookupFlags = orc::SymbolLookupFlags::RequiredSymbol; 171480093f4SDimitry Andric break; 172480093f4SDimitry Andric case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol: 173480093f4SDimitry Andric LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol; 174480093f4SDimitry Andric break; 175480093f4SDimitry Andric } 176480093f4SDimitry Andric LookupSet.add(ES.intern(KV.first), LookupFlags); 177480093f4SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric // OnResolve -- De-intern the symbols and pass the result to the linker. 180e8d8bef9SDimitry Andric auto OnResolve = [LookupContinuation = 181e8d8bef9SDimitry Andric std::move(LC)](Expected<SymbolMap> Result) mutable { 1820b57cec5SDimitry Andric if (!Result) 1838bcb0991SDimitry Andric LookupContinuation->run(Result.takeError()); 1840b57cec5SDimitry Andric else { 1850b57cec5SDimitry Andric AsyncLookupResult LR; 1860b57cec5SDimitry Andric for (auto &KV : *Result) 1870b57cec5SDimitry Andric LR[*KV.first] = KV.second; 1888bcb0991SDimitry Andric LookupContinuation->run(std::move(LR)); 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric }; 1910b57cec5SDimitry Andric 1925ffd83dbSDimitry Andric for (auto &KV : InternalNamedSymbolDeps) { 1935ffd83dbSDimitry Andric SymbolDependenceMap InternalDeps; 194e8d8bef9SDimitry Andric InternalDeps[&MR->getTargetJITDylib()] = std::move(KV.second); 195e8d8bef9SDimitry Andric MR->addDependencies(KV.first, InternalDeps); 1965ffd83dbSDimitry Andric } 1975ffd83dbSDimitry Andric 1985ffd83dbSDimitry Andric ES.lookup(LookupKind::Static, LinkOrder, std::move(LookupSet), 199480093f4SDimitry Andric SymbolState::Resolved, std::move(OnResolve), 200480093f4SDimitry Andric [this](const SymbolDependenceMap &Deps) { 2010b57cec5SDimitry Andric registerDependencies(Deps); 2020b57cec5SDimitry Andric }); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 205e8d8bef9SDimitry Andric Error notifyResolved(LinkGraph &G) override { 2060b57cec5SDimitry Andric auto &ES = Layer.getExecutionSession(); 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric SymbolFlagsMap ExtraSymbolsToClaim; 2090b57cec5SDimitry Andric bool AutoClaim = Layer.AutoClaimObjectSymbols; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric SymbolMap InternedResult; 2128bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) 2138bcb0991SDimitry Andric if (Sym->hasName() && Sym->getScope() != Scope::Local) { 2148bcb0991SDimitry Andric auto InternedName = ES.intern(Sym->getName()); 2150b57cec5SDimitry Andric JITSymbolFlags Flags; 2160b57cec5SDimitry Andric 2178bcb0991SDimitry Andric if (Sym->isCallable()) 2180b57cec5SDimitry Andric Flags |= JITSymbolFlags::Callable; 2198bcb0991SDimitry Andric if (Sym->getScope() == Scope::Default) 2208bcb0991SDimitry Andric Flags |= JITSymbolFlags::Exported; 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric InternedResult[InternedName] = 22304eeddc0SDimitry Andric JITEvaluatedSymbol(Sym->getAddress().getValue(), Flags); 224e8d8bef9SDimitry Andric if (AutoClaim && !MR->getSymbols().count(InternedName)) { 2250b57cec5SDimitry Andric assert(!ExtraSymbolsToClaim.count(InternedName) && 2260b57cec5SDimitry Andric "Duplicate symbol to claim?"); 2270b57cec5SDimitry Andric ExtraSymbolsToClaim[InternedName] = Flags; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2318bcb0991SDimitry Andric for (auto *Sym : G.absolute_symbols()) 232*81ad6265SDimitry Andric if (Sym->hasName() && Sym->getScope() != Scope::Local) { 2338bcb0991SDimitry Andric auto InternedName = ES.intern(Sym->getName()); 2340b57cec5SDimitry Andric JITSymbolFlags Flags; 2358bcb0991SDimitry Andric if (Sym->isCallable()) 2360b57cec5SDimitry Andric Flags |= JITSymbolFlags::Callable; 237*81ad6265SDimitry Andric if (Sym->getScope() == Scope::Default) 238*81ad6265SDimitry Andric Flags |= JITSymbolFlags::Exported; 2398bcb0991SDimitry Andric if (Sym->getLinkage() == Linkage::Weak) 2408bcb0991SDimitry Andric Flags |= JITSymbolFlags::Weak; 2410b57cec5SDimitry Andric InternedResult[InternedName] = 24204eeddc0SDimitry Andric JITEvaluatedSymbol(Sym->getAddress().getValue(), Flags); 243e8d8bef9SDimitry Andric if (AutoClaim && !MR->getSymbols().count(InternedName)) { 2440b57cec5SDimitry Andric assert(!ExtraSymbolsToClaim.count(InternedName) && 2450b57cec5SDimitry Andric "Duplicate symbol to claim?"); 2460b57cec5SDimitry Andric ExtraSymbolsToClaim[InternedName] = Flags; 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric if (!ExtraSymbolsToClaim.empty()) 251e8d8bef9SDimitry Andric if (auto Err = MR->defineMaterializing(ExtraSymbolsToClaim)) 252e8d8bef9SDimitry Andric return Err; 2535ffd83dbSDimitry Andric 2545ffd83dbSDimitry Andric { 2555ffd83dbSDimitry Andric 2560eae32dcSDimitry Andric // Check that InternedResult matches up with MR->getSymbols(), overriding 2570eae32dcSDimitry Andric // flags if requested. 2585ffd83dbSDimitry Andric // This guards against faulty transformations / compilers / object caches. 2595ffd83dbSDimitry Andric 2605ffd83dbSDimitry Andric // First check that there aren't any missing symbols. 2615ffd83dbSDimitry Andric size_t NumMaterializationSideEffectsOnlySymbols = 0; 2625ffd83dbSDimitry Andric SymbolNameVector ExtraSymbols; 2635ffd83dbSDimitry Andric SymbolNameVector MissingSymbols; 264e8d8bef9SDimitry Andric for (auto &KV : MR->getSymbols()) { 2655ffd83dbSDimitry Andric 2660eae32dcSDimitry Andric auto I = InternedResult.find(KV.first); 2670eae32dcSDimitry Andric 2685ffd83dbSDimitry Andric // If this is a materialization-side-effects only symbol then bump 2695ffd83dbSDimitry Andric // the counter and make sure it's *not* defined, otherwise make 2705ffd83dbSDimitry Andric // sure that it is defined. 2715ffd83dbSDimitry Andric if (KV.second.hasMaterializationSideEffectsOnly()) { 2725ffd83dbSDimitry Andric ++NumMaterializationSideEffectsOnlySymbols; 2730eae32dcSDimitry Andric if (I != InternedResult.end()) 2745ffd83dbSDimitry Andric ExtraSymbols.push_back(KV.first); 2755ffd83dbSDimitry Andric continue; 2760eae32dcSDimitry Andric } else if (I == InternedResult.end()) 2775ffd83dbSDimitry Andric MissingSymbols.push_back(KV.first); 2780eae32dcSDimitry Andric else if (Layer.OverrideObjectFlags) 2790eae32dcSDimitry Andric I->second.setFlags(KV.second); 2805ffd83dbSDimitry Andric } 2815ffd83dbSDimitry Andric 2825ffd83dbSDimitry Andric // If there were missing symbols then report the error. 283e8d8bef9SDimitry Andric if (!MissingSymbols.empty()) 284349cc55cSDimitry Andric return make_error<MissingSymbolDefinitions>( 285349cc55cSDimitry Andric Layer.getExecutionSession().getSymbolStringPool(), G.getName(), 286e8d8bef9SDimitry Andric std::move(MissingSymbols)); 2875ffd83dbSDimitry Andric 2885ffd83dbSDimitry Andric // If there are more definitions than expected, add them to the 2895ffd83dbSDimitry Andric // ExtraSymbols vector. 2905ffd83dbSDimitry Andric if (InternedResult.size() > 291e8d8bef9SDimitry Andric MR->getSymbols().size() - NumMaterializationSideEffectsOnlySymbols) { 2925ffd83dbSDimitry Andric for (auto &KV : InternedResult) 293e8d8bef9SDimitry Andric if (!MR->getSymbols().count(KV.first)) 2945ffd83dbSDimitry Andric ExtraSymbols.push_back(KV.first); 2955ffd83dbSDimitry Andric } 2965ffd83dbSDimitry Andric 2975ffd83dbSDimitry Andric // If there were extra definitions then report the error. 298e8d8bef9SDimitry Andric if (!ExtraSymbols.empty()) 299349cc55cSDimitry Andric return make_error<UnexpectedSymbolDefinitions>( 300349cc55cSDimitry Andric Layer.getExecutionSession().getSymbolStringPool(), G.getName(), 301e8d8bef9SDimitry Andric std::move(ExtraSymbols)); 3025ffd83dbSDimitry Andric } 3035ffd83dbSDimitry Andric 304e8d8bef9SDimitry Andric if (auto Err = MR->notifyResolved(InternedResult)) 305e8d8bef9SDimitry Andric return Err; 306e8d8bef9SDimitry Andric 307e8d8bef9SDimitry Andric Layer.notifyLoaded(*MR); 308e8d8bef9SDimitry Andric return Error::success(); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 311349cc55cSDimitry Andric void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc A) override { 312e8d8bef9SDimitry Andric if (auto Err = Layer.notifyEmitted(*MR, std::move(A))) { 3130b57cec5SDimitry Andric Layer.getExecutionSession().reportError(std::move(Err)); 314e8d8bef9SDimitry Andric MR->failMaterialization(); 3150b57cec5SDimitry Andric return; 3160b57cec5SDimitry Andric } 317e8d8bef9SDimitry Andric if (auto Err = MR->notifyEmitted()) { 3188bcb0991SDimitry Andric Layer.getExecutionSession().reportError(std::move(Err)); 319e8d8bef9SDimitry Andric MR->failMaterialization(); 3208bcb0991SDimitry Andric } 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 3238bcb0991SDimitry Andric LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override { 3248bcb0991SDimitry Andric return [this](LinkGraph &G) { return markResponsibilitySymbolsLive(G); }; 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 327fe6060f1SDimitry Andric Error modifyPassConfig(LinkGraph &LG, PassConfiguration &Config) override { 3280b57cec5SDimitry Andric // Add passes to mark duplicate defs as should-discard, and to walk the 3298bcb0991SDimitry Andric // link graph to build the symbol dependence graph. 330e8d8bef9SDimitry Andric Config.PrePrunePasses.push_back([this](LinkGraph &G) { 331e8d8bef9SDimitry Andric return claimOrExternalizeWeakAndCommonSymbols(G); 332e8d8bef9SDimitry Andric }); 3330b57cec5SDimitry Andric 334fe6060f1SDimitry Andric Layer.modifyPassConfig(*MR, LG, Config); 3350b57cec5SDimitry Andric 3365ffd83dbSDimitry Andric Config.PostPrunePasses.push_back( 3375ffd83dbSDimitry Andric [this](LinkGraph &G) { return computeNamedSymbolDependencies(G); }); 3385ffd83dbSDimitry Andric 3390b57cec5SDimitry Andric return Error::success(); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric private: 343fe6060f1SDimitry Andric // Symbol name dependencies: 344fe6060f1SDimitry Andric // Internal: Defined in this graph. 345fe6060f1SDimitry Andric // External: Defined externally. 346fe6060f1SDimitry Andric struct BlockSymbolDependencies { 3475ffd83dbSDimitry Andric SymbolNameSet Internal, External; 3485ffd83dbSDimitry Andric }; 3495ffd83dbSDimitry Andric 350fe6060f1SDimitry Andric // Lazily populated map of blocks to BlockSymbolDependencies values. 351fe6060f1SDimitry Andric class BlockDependenciesMap { 352fe6060f1SDimitry Andric public: 353fe6060f1SDimitry Andric BlockDependenciesMap(ExecutionSession &ES, 354fe6060f1SDimitry Andric DenseMap<const Block *, DenseSet<Block *>> BlockDeps) 355fe6060f1SDimitry Andric : ES(ES), BlockDeps(std::move(BlockDeps)) {} 356fe6060f1SDimitry Andric 357fe6060f1SDimitry Andric const BlockSymbolDependencies &operator[](const Block &B) { 358fe6060f1SDimitry Andric // Check the cache first. 359fe6060f1SDimitry Andric auto I = BlockTransitiveDepsCache.find(&B); 360fe6060f1SDimitry Andric if (I != BlockTransitiveDepsCache.end()) 361fe6060f1SDimitry Andric return I->second; 362fe6060f1SDimitry Andric 363fe6060f1SDimitry Andric // No value. Populate the cache. 364fe6060f1SDimitry Andric BlockSymbolDependencies BTDCacheVal; 365fe6060f1SDimitry Andric auto BDI = BlockDeps.find(&B); 366fe6060f1SDimitry Andric assert(BDI != BlockDeps.end() && "No block dependencies"); 367fe6060f1SDimitry Andric 368fe6060f1SDimitry Andric for (auto *BDep : BDI->second) { 369fe6060f1SDimitry Andric auto &BID = getBlockImmediateDeps(*BDep); 370fe6060f1SDimitry Andric for (auto &ExternalDep : BID.External) 371fe6060f1SDimitry Andric BTDCacheVal.External.insert(ExternalDep); 372fe6060f1SDimitry Andric for (auto &InternalDep : BID.Internal) 373fe6060f1SDimitry Andric BTDCacheVal.Internal.insert(InternalDep); 374fe6060f1SDimitry Andric } 375fe6060f1SDimitry Andric 376fe6060f1SDimitry Andric return BlockTransitiveDepsCache 377fe6060f1SDimitry Andric .insert(std::make_pair(&B, std::move(BTDCacheVal))) 378fe6060f1SDimitry Andric .first->second; 379fe6060f1SDimitry Andric } 380fe6060f1SDimitry Andric 381fe6060f1SDimitry Andric SymbolStringPtr &getInternedName(Symbol &Sym) { 382fe6060f1SDimitry Andric auto I = NameCache.find(&Sym); 383fe6060f1SDimitry Andric if (I != NameCache.end()) 384fe6060f1SDimitry Andric return I->second; 385fe6060f1SDimitry Andric 386fe6060f1SDimitry Andric return NameCache.insert(std::make_pair(&Sym, ES.intern(Sym.getName()))) 387fe6060f1SDimitry Andric .first->second; 388fe6060f1SDimitry Andric } 389fe6060f1SDimitry Andric 390fe6060f1SDimitry Andric private: 391fe6060f1SDimitry Andric BlockSymbolDependencies &getBlockImmediateDeps(Block &B) { 392fe6060f1SDimitry Andric // Check the cache first. 393fe6060f1SDimitry Andric auto I = BlockImmediateDepsCache.find(&B); 394fe6060f1SDimitry Andric if (I != BlockImmediateDepsCache.end()) 395fe6060f1SDimitry Andric return I->second; 396fe6060f1SDimitry Andric 397fe6060f1SDimitry Andric BlockSymbolDependencies BIDCacheVal; 398fe6060f1SDimitry Andric for (auto &E : B.edges()) { 399fe6060f1SDimitry Andric auto &Tgt = E.getTarget(); 400fe6060f1SDimitry Andric if (Tgt.getScope() != Scope::Local) { 401fe6060f1SDimitry Andric if (Tgt.isExternal()) 402fe6060f1SDimitry Andric BIDCacheVal.External.insert(getInternedName(Tgt)); 403fe6060f1SDimitry Andric else 404fe6060f1SDimitry Andric BIDCacheVal.Internal.insert(getInternedName(Tgt)); 405fe6060f1SDimitry Andric } 406fe6060f1SDimitry Andric } 407fe6060f1SDimitry Andric 408fe6060f1SDimitry Andric return BlockImmediateDepsCache 409fe6060f1SDimitry Andric .insert(std::make_pair(&B, std::move(BIDCacheVal))) 410fe6060f1SDimitry Andric .first->second; 411fe6060f1SDimitry Andric } 412fe6060f1SDimitry Andric 413fe6060f1SDimitry Andric ExecutionSession &ES; 414fe6060f1SDimitry Andric DenseMap<const Block *, DenseSet<Block *>> BlockDeps; 415fe6060f1SDimitry Andric DenseMap<const Symbol *, SymbolStringPtr> NameCache; 416fe6060f1SDimitry Andric DenseMap<const Block *, BlockSymbolDependencies> BlockImmediateDepsCache; 417fe6060f1SDimitry Andric DenseMap<const Block *, BlockSymbolDependencies> BlockTransitiveDepsCache; 418fe6060f1SDimitry Andric }; 4190b57cec5SDimitry Andric 420e8d8bef9SDimitry Andric Error claimOrExternalizeWeakAndCommonSymbols(LinkGraph &G) { 4210b57cec5SDimitry Andric auto &ES = Layer.getExecutionSession(); 4220b57cec5SDimitry Andric 423e8d8bef9SDimitry Andric SymbolFlagsMap NewSymbolsToClaim; 424e8d8bef9SDimitry Andric std::vector<std::pair<SymbolStringPtr, Symbol *>> NameToSym; 425e8d8bef9SDimitry Andric 426e8d8bef9SDimitry Andric auto ProcessSymbol = [&](Symbol *Sym) { 427349cc55cSDimitry Andric if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak && 428349cc55cSDimitry Andric Sym->getScope() != Scope::Local) { 429e8d8bef9SDimitry Andric auto Name = ES.intern(Sym->getName()); 430e8d8bef9SDimitry Andric if (!MR->getSymbols().count(ES.intern(Sym->getName()))) { 431e8d8bef9SDimitry Andric JITSymbolFlags SF = JITSymbolFlags::Weak; 432e8d8bef9SDimitry Andric if (Sym->getScope() == Scope::Default) 433e8d8bef9SDimitry Andric SF |= JITSymbolFlags::Exported; 434e8d8bef9SDimitry Andric NewSymbolsToClaim[Name] = SF; 435e8d8bef9SDimitry Andric NameToSym.push_back(std::make_pair(std::move(Name), Sym)); 4360b57cec5SDimitry Andric } 437e8d8bef9SDimitry Andric } 438e8d8bef9SDimitry Andric }; 439e8d8bef9SDimitry Andric 440e8d8bef9SDimitry Andric for (auto *Sym : G.defined_symbols()) 441e8d8bef9SDimitry Andric ProcessSymbol(Sym); 442e8d8bef9SDimitry Andric for (auto *Sym : G.absolute_symbols()) 443e8d8bef9SDimitry Andric ProcessSymbol(Sym); 444e8d8bef9SDimitry Andric 445e8d8bef9SDimitry Andric // Attempt to claim all weak defs that we're not already responsible for. 446e8d8bef9SDimitry Andric // This cannot fail -- any clashes will just result in rejection of our 447e8d8bef9SDimitry Andric // claim, at which point we'll externalize that symbol. 448e8d8bef9SDimitry Andric cantFail(MR->defineMaterializing(std::move(NewSymbolsToClaim))); 449e8d8bef9SDimitry Andric 450e8d8bef9SDimitry Andric for (auto &KV : NameToSym) 451e8d8bef9SDimitry Andric if (!MR->getSymbols().count(KV.first)) 452e8d8bef9SDimitry Andric G.makeExternal(*KV.second); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric return Error::success(); 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric 4578bcb0991SDimitry Andric Error markResponsibilitySymbolsLive(LinkGraph &G) const { 4580b57cec5SDimitry Andric auto &ES = Layer.getExecutionSession(); 4598bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) 460e8d8bef9SDimitry Andric if (Sym->hasName() && MR->getSymbols().count(ES.intern(Sym->getName()))) 4618bcb0991SDimitry Andric Sym->setLive(true); 4620b57cec5SDimitry Andric return Error::success(); 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4658bcb0991SDimitry Andric Error computeNamedSymbolDependencies(LinkGraph &G) { 466e8d8bef9SDimitry Andric auto &ES = MR->getTargetJITDylib().getExecutionSession(); 467fe6060f1SDimitry Andric auto BlockDeps = computeBlockNonLocalDeps(G); 4680b57cec5SDimitry Andric 4695ffd83dbSDimitry Andric // Compute dependencies for symbols defined in the JITLink graph. 4708bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) { 4710b57cec5SDimitry Andric 4725ffd83dbSDimitry Andric // Skip local symbols: we do not track dependencies for these. 4738bcb0991SDimitry Andric if (Sym->getScope() == Scope::Local) 4740b57cec5SDimitry Andric continue; 4755ffd83dbSDimitry Andric assert(Sym->hasName() && 4765ffd83dbSDimitry Andric "Defined non-local jitlink::Symbol should have a name"); 4770b57cec5SDimitry Andric 478fe6060f1SDimitry Andric auto &SymDeps = BlockDeps[Sym->getBlock()]; 479fe6060f1SDimitry Andric if (SymDeps.External.empty() && SymDeps.Internal.empty()) 4805ffd83dbSDimitry Andric continue; 4815ffd83dbSDimitry Andric 4825ffd83dbSDimitry Andric auto SymName = ES.intern(Sym->getName()); 483fe6060f1SDimitry Andric if (!SymDeps.External.empty()) 484fe6060f1SDimitry Andric ExternalNamedSymbolDeps[SymName] = SymDeps.External; 485fe6060f1SDimitry Andric if (!SymDeps.Internal.empty()) 486fe6060f1SDimitry Andric InternalNamedSymbolDeps[SymName] = SymDeps.Internal; 4875ffd83dbSDimitry Andric } 4885ffd83dbSDimitry Andric 4895ffd83dbSDimitry Andric for (auto &P : Layer.Plugins) { 490fe6060f1SDimitry Andric auto SynthDeps = P->getSyntheticSymbolDependencies(*MR); 491fe6060f1SDimitry Andric if (SynthDeps.empty()) 4925ffd83dbSDimitry Andric continue; 4935ffd83dbSDimitry Andric 494fe6060f1SDimitry Andric DenseSet<Block *> BlockVisited; 495fe6060f1SDimitry Andric for (auto &KV : SynthDeps) { 4965ffd83dbSDimitry Andric auto &Name = KV.first; 497fe6060f1SDimitry Andric auto &DepsForName = KV.second; 498fe6060f1SDimitry Andric for (auto *Sym : DepsForName) { 499fe6060f1SDimitry Andric if (Sym->getScope() == Scope::Local) { 500fe6060f1SDimitry Andric auto &BDeps = BlockDeps[Sym->getBlock()]; 501fe6060f1SDimitry Andric for (auto &S : BDeps.Internal) 5025ffd83dbSDimitry Andric InternalNamedSymbolDeps[Name].insert(S); 503fe6060f1SDimitry Andric for (auto &S : BDeps.External) 5045ffd83dbSDimitry Andric ExternalNamedSymbolDeps[Name].insert(S); 505fe6060f1SDimitry Andric } else { 506fe6060f1SDimitry Andric if (Sym->isExternal()) 507fe6060f1SDimitry Andric ExternalNamedSymbolDeps[Name].insert( 508fe6060f1SDimitry Andric BlockDeps.getInternedName(*Sym)); 509fe6060f1SDimitry Andric else 510fe6060f1SDimitry Andric InternalNamedSymbolDeps[Name].insert( 511fe6060f1SDimitry Andric BlockDeps.getInternedName(*Sym)); 512fe6060f1SDimitry Andric } 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric return Error::success(); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 520fe6060f1SDimitry Andric BlockDependenciesMap computeBlockNonLocalDeps(LinkGraph &G) { 521fe6060f1SDimitry Andric // First calculate the reachable-via-non-local-symbol blocks for each block. 522fe6060f1SDimitry Andric struct BlockInfo { 523fe6060f1SDimitry Andric DenseSet<Block *> Dependencies; 524fe6060f1SDimitry Andric DenseSet<Block *> Dependants; 525fe6060f1SDimitry Andric bool DependenciesChanged = true; 5260b57cec5SDimitry Andric }; 527fe6060f1SDimitry Andric DenseMap<Block *, BlockInfo> BlockInfos; 528fe6060f1SDimitry Andric SmallVector<Block *> WorkList; 5290b57cec5SDimitry Andric 530fe6060f1SDimitry Andric // Pre-allocate map entries. This prevents any iterator/reference 531fe6060f1SDimitry Andric // invalidation in the next loop. 532fe6060f1SDimitry Andric for (auto *B : G.blocks()) 533fe6060f1SDimitry Andric (void)BlockInfos[B]; 534fe6060f1SDimitry Andric 535fe6060f1SDimitry Andric // Build initial worklist, record block dependencies/dependants and 536fe6060f1SDimitry Andric // non-local symbol dependencies. 537fe6060f1SDimitry Andric for (auto *B : G.blocks()) { 538fe6060f1SDimitry Andric auto &BI = BlockInfos[B]; 539fe6060f1SDimitry Andric for (auto &E : B->edges()) { 540fe6060f1SDimitry Andric if (E.getTarget().getScope() == Scope::Local) { 541fe6060f1SDimitry Andric auto &TgtB = E.getTarget().getBlock(); 542fe6060f1SDimitry Andric if (&TgtB != B) { 543fe6060f1SDimitry Andric BI.Dependencies.insert(&TgtB); 544fe6060f1SDimitry Andric BlockInfos[&TgtB].Dependants.insert(B); 545fe6060f1SDimitry Andric } 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric 549fe6060f1SDimitry Andric // If this node has both dependants and dependencies then add it to the 550fe6060f1SDimitry Andric // worklist to propagate the dependencies to the dependants. 551fe6060f1SDimitry Andric if (!BI.Dependants.empty() && !BI.Dependencies.empty()) 552fe6060f1SDimitry Andric WorkList.push_back(B); 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 555fe6060f1SDimitry Andric // Propagate block-level dependencies through the block-dependence graph. 556fe6060f1SDimitry Andric while (!WorkList.empty()) { 557349cc55cSDimitry Andric auto *B = WorkList.pop_back_val(); 5580b57cec5SDimitry Andric 559fe6060f1SDimitry Andric auto &BI = BlockInfos[B]; 560fe6060f1SDimitry Andric assert(BI.DependenciesChanged && 561fe6060f1SDimitry Andric "Block in worklist has unchanged dependencies"); 562fe6060f1SDimitry Andric BI.DependenciesChanged = false; 563fe6060f1SDimitry Andric for (auto *Dependant : BI.Dependants) { 564fe6060f1SDimitry Andric auto &DependantBI = BlockInfos[Dependant]; 565fe6060f1SDimitry Andric for (auto *Dependency : BI.Dependencies) { 566fe6060f1SDimitry Andric if (Dependant != Dependency && 567fe6060f1SDimitry Andric DependantBI.Dependencies.insert(Dependency).second) 568fe6060f1SDimitry Andric if (!DependantBI.DependenciesChanged) { 569fe6060f1SDimitry Andric DependantBI.DependenciesChanged = true; 570fe6060f1SDimitry Andric WorkList.push_back(Dependant); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric } 5735ffd83dbSDimitry Andric } 5745ffd83dbSDimitry Andric } 5755ffd83dbSDimitry Andric 576fe6060f1SDimitry Andric DenseMap<const Block *, DenseSet<Block *>> BlockDeps; 577fe6060f1SDimitry Andric for (auto &KV : BlockInfos) 578fe6060f1SDimitry Andric BlockDeps[KV.first] = std::move(KV.second.Dependencies); 579fe6060f1SDimitry Andric 580fe6060f1SDimitry Andric return BlockDependenciesMap(Layer.getExecutionSession(), 581fe6060f1SDimitry Andric std::move(BlockDeps)); 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric void registerDependencies(const SymbolDependenceMap &QueryDeps) { 5855ffd83dbSDimitry Andric for (auto &NamedDepsEntry : ExternalNamedSymbolDeps) { 5860b57cec5SDimitry Andric auto &Name = NamedDepsEntry.first; 5870b57cec5SDimitry Andric auto &NameDeps = NamedDepsEntry.second; 5880b57cec5SDimitry Andric SymbolDependenceMap SymbolDeps; 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric for (const auto &QueryDepsEntry : QueryDeps) { 5910b57cec5SDimitry Andric JITDylib &SourceJD = *QueryDepsEntry.first; 5920b57cec5SDimitry Andric const SymbolNameSet &Symbols = QueryDepsEntry.second; 5930b57cec5SDimitry Andric auto &DepsForJD = SymbolDeps[&SourceJD]; 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric for (const auto &S : Symbols) 5960b57cec5SDimitry Andric if (NameDeps.count(S)) 5970b57cec5SDimitry Andric DepsForJD.insert(S); 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric if (DepsForJD.empty()) 6000b57cec5SDimitry Andric SymbolDeps.erase(&SourceJD); 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric 603e8d8bef9SDimitry Andric MR->addDependencies(Name, SymbolDeps); 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric } 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric ObjectLinkingLayer &Layer; 608e8d8bef9SDimitry Andric std::unique_ptr<MaterializationResponsibility> MR; 6090b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> ObjBuffer; 6105ffd83dbSDimitry Andric DenseMap<SymbolStringPtr, SymbolNameSet> ExternalNamedSymbolDeps; 6115ffd83dbSDimitry Andric DenseMap<SymbolStringPtr, SymbolNameSet> InternalNamedSymbolDeps; 6120b57cec5SDimitry Andric }; 6130b57cec5SDimitry Andric 614*81ad6265SDimitry Andric ObjectLinkingLayer::Plugin::~Plugin() = default; 6150b57cec5SDimitry Andric 616fe6060f1SDimitry Andric char ObjectLinkingLayer::ID; 617fe6060f1SDimitry Andric 618fe6060f1SDimitry Andric using BaseT = RTTIExtends<ObjectLinkingLayer, ObjectLayer>; 619fe6060f1SDimitry Andric 620fe6060f1SDimitry Andric ObjectLinkingLayer::ObjectLinkingLayer(ExecutionSession &ES) 621fe6060f1SDimitry Andric : BaseT(ES), MemMgr(ES.getExecutorProcessControl().getMemMgr()) { 622fe6060f1SDimitry Andric ES.registerResourceManager(*this); 623fe6060f1SDimitry Andric } 624fe6060f1SDimitry Andric 625e8d8bef9SDimitry Andric ObjectLinkingLayer::ObjectLinkingLayer(ExecutionSession &ES, 626e8d8bef9SDimitry Andric JITLinkMemoryManager &MemMgr) 627fe6060f1SDimitry Andric : BaseT(ES), MemMgr(MemMgr) { 628e8d8bef9SDimitry Andric ES.registerResourceManager(*this); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 631e8d8bef9SDimitry Andric ObjectLinkingLayer::ObjectLinkingLayer( 632e8d8bef9SDimitry Andric ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr) 633fe6060f1SDimitry Andric : BaseT(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) { 634e8d8bef9SDimitry Andric ES.registerResourceManager(*this); 635e8d8bef9SDimitry Andric } 636e8d8bef9SDimitry Andric 637e8d8bef9SDimitry Andric ObjectLinkingLayer::~ObjectLinkingLayer() { 638e8d8bef9SDimitry Andric assert(Allocs.empty() && "Layer destroyed with resources still attached"); 639e8d8bef9SDimitry Andric getExecutionSession().deregisterResourceManager(*this); 640e8d8bef9SDimitry Andric } 641e8d8bef9SDimitry Andric 642fe6060f1SDimitry Andric Error ObjectLinkingLayer::add(ResourceTrackerSP RT, 643fe6060f1SDimitry Andric std::unique_ptr<LinkGraph> G) { 644fe6060f1SDimitry Andric auto &JD = RT->getJITDylib(); 645fe6060f1SDimitry Andric return JD.define(LinkGraphMaterializationUnit::Create(*this, std::move(G)), 646fe6060f1SDimitry Andric std::move(RT)); 647fe6060f1SDimitry Andric } 648fe6060f1SDimitry Andric 649e8d8bef9SDimitry Andric void ObjectLinkingLayer::emit(std::unique_ptr<MaterializationResponsibility> R, 6500b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> O) { 6510b57cec5SDimitry Andric assert(O && "Object must not be null"); 652fe6060f1SDimitry Andric MemoryBufferRef ObjBuffer = O->getMemBufferRef(); 653fe6060f1SDimitry Andric 654e8d8bef9SDimitry Andric auto Ctx = std::make_unique<ObjectLinkingLayerJITLinkContext>( 655e8d8bef9SDimitry Andric *this, std::move(R), std::move(O)); 656fe6060f1SDimitry Andric if (auto G = createLinkGraphFromObject(ObjBuffer)) { 657fe6060f1SDimitry Andric Ctx->notifyMaterializing(**G); 658e8d8bef9SDimitry Andric link(std::move(*G), std::move(Ctx)); 659fe6060f1SDimitry Andric } else { 660e8d8bef9SDimitry Andric Ctx->notifyFailed(G.takeError()); 661e8d8bef9SDimitry Andric } 662fe6060f1SDimitry Andric } 663e8d8bef9SDimitry Andric 664e8d8bef9SDimitry Andric void ObjectLinkingLayer::emit(std::unique_ptr<MaterializationResponsibility> R, 665e8d8bef9SDimitry Andric std::unique_ptr<LinkGraph> G) { 666fe6060f1SDimitry Andric auto Ctx = std::make_unique<ObjectLinkingLayerJITLinkContext>( 667fe6060f1SDimitry Andric *this, std::move(R), nullptr); 668fe6060f1SDimitry Andric Ctx->notifyMaterializing(*G); 669fe6060f1SDimitry Andric link(std::move(G), std::move(Ctx)); 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric void ObjectLinkingLayer::modifyPassConfig(MaterializationResponsibility &MR, 673fe6060f1SDimitry Andric LinkGraph &G, 6740b57cec5SDimitry Andric PassConfiguration &PassConfig) { 6750b57cec5SDimitry Andric for (auto &P : Plugins) 676fe6060f1SDimitry Andric P->modifyPassConfig(MR, G, PassConfig); 6770b57cec5SDimitry Andric } 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric void ObjectLinkingLayer::notifyLoaded(MaterializationResponsibility &MR) { 6800b57cec5SDimitry Andric for (auto &P : Plugins) 6810b57cec5SDimitry Andric P->notifyLoaded(MR); 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric Error ObjectLinkingLayer::notifyEmitted(MaterializationResponsibility &MR, 685349cc55cSDimitry Andric FinalizedAlloc FA) { 6860b57cec5SDimitry Andric Error Err = Error::success(); 6870b57cec5SDimitry Andric for (auto &P : Plugins) 6880b57cec5SDimitry Andric Err = joinErrors(std::move(Err), P->notifyEmitted(MR)); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric if (Err) 6910b57cec5SDimitry Andric return Err; 6920b57cec5SDimitry Andric 693e8d8bef9SDimitry Andric return MR.withResourceKeyDo( 694349cc55cSDimitry Andric [&](ResourceKey K) { Allocs[K].push_back(std::move(FA)); }); 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric 697e8d8bef9SDimitry Andric Error ObjectLinkingLayer::handleRemoveResources(ResourceKey K) { 6980b57cec5SDimitry Andric 699349cc55cSDimitry Andric { 7000b57cec5SDimitry Andric Error Err = Error::success(); 7010b57cec5SDimitry Andric for (auto &P : Plugins) 702e8d8bef9SDimitry Andric Err = joinErrors(std::move(Err), P->notifyRemovingResources(K)); 703349cc55cSDimitry Andric if (Err) 704349cc55cSDimitry Andric return Err; 705349cc55cSDimitry Andric } 7060b57cec5SDimitry Andric 707349cc55cSDimitry Andric std::vector<FinalizedAlloc> AllocsToRemove; 708e8d8bef9SDimitry Andric getExecutionSession().runSessionLocked([&] { 709e8d8bef9SDimitry Andric auto I = Allocs.find(K); 710e8d8bef9SDimitry Andric if (I != Allocs.end()) { 711e8d8bef9SDimitry Andric std::swap(AllocsToRemove, I->second); 712e8d8bef9SDimitry Andric Allocs.erase(I); 7130b57cec5SDimitry Andric } 714e8d8bef9SDimitry Andric }); 7150b57cec5SDimitry Andric 716349cc55cSDimitry Andric if (AllocsToRemove.empty()) 717349cc55cSDimitry Andric return Error::success(); 7180b57cec5SDimitry Andric 719349cc55cSDimitry Andric return MemMgr.deallocate(std::move(AllocsToRemove)); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 722e8d8bef9SDimitry Andric void ObjectLinkingLayer::handleTransferResources(ResourceKey DstKey, 723e8d8bef9SDimitry Andric ResourceKey SrcKey) { 724e8d8bef9SDimitry Andric auto I = Allocs.find(SrcKey); 725e8d8bef9SDimitry Andric if (I != Allocs.end()) { 726e8d8bef9SDimitry Andric auto &SrcAllocs = I->second; 727e8d8bef9SDimitry Andric auto &DstAllocs = Allocs[DstKey]; 728e8d8bef9SDimitry Andric DstAllocs.reserve(DstAllocs.size() + SrcAllocs.size()); 729e8d8bef9SDimitry Andric for (auto &Alloc : SrcAllocs) 730e8d8bef9SDimitry Andric DstAllocs.push_back(std::move(Alloc)); 731e8d8bef9SDimitry Andric 732e8d8bef9SDimitry Andric // Erase SrcKey entry using value rather than iterator I: I may have been 733e8d8bef9SDimitry Andric // invalidated when we looked up DstKey. 734e8d8bef9SDimitry Andric Allocs.erase(SrcKey); 735e8d8bef9SDimitry Andric } 736e8d8bef9SDimitry Andric 737e8d8bef9SDimitry Andric for (auto &P : Plugins) 738e8d8bef9SDimitry Andric P->notifyTransferringResources(DstKey, SrcKey); 739e8d8bef9SDimitry Andric } 740e8d8bef9SDimitry Andric 7410b57cec5SDimitry Andric EHFrameRegistrationPlugin::EHFrameRegistrationPlugin( 742e8d8bef9SDimitry Andric ExecutionSession &ES, std::unique_ptr<EHFrameRegistrar> Registrar) 743e8d8bef9SDimitry Andric : ES(ES), Registrar(std::move(Registrar)) {} 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric void EHFrameRegistrationPlugin::modifyPassConfig( 746fe6060f1SDimitry Andric MaterializationResponsibility &MR, LinkGraph &G, 7470b57cec5SDimitry Andric PassConfiguration &PassConfig) { 7480b57cec5SDimitry Andric 7495ffd83dbSDimitry Andric PassConfig.PostFixupPasses.push_back(createEHFrameRecorderPass( 75004eeddc0SDimitry Andric G.getTargetTriple(), [this, &MR](ExecutorAddr Addr, size_t Size) { 7515ffd83dbSDimitry Andric if (Addr) { 7525ffd83dbSDimitry Andric std::lock_guard<std::mutex> Lock(EHFramePluginMutex); 7535ffd83dbSDimitry Andric assert(!InProcessLinks.count(&MR) && 7545ffd83dbSDimitry Andric "Link for MR already being tracked?"); 7558bcb0991SDimitry Andric InProcessLinks[&MR] = {Addr, Size}; 7565ffd83dbSDimitry Andric } 7570b57cec5SDimitry Andric })); 7580b57cec5SDimitry Andric } 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric Error EHFrameRegistrationPlugin::notifyEmitted( 7610b57cec5SDimitry Andric MaterializationResponsibility &MR) { 762e8d8bef9SDimitry Andric 76304eeddc0SDimitry Andric ExecutorAddrRange EmittedRange; 764e8d8bef9SDimitry Andric { 7655ffd83dbSDimitry Andric std::lock_guard<std::mutex> Lock(EHFramePluginMutex); 7660b57cec5SDimitry Andric 7678bcb0991SDimitry Andric auto EHFrameRangeItr = InProcessLinks.find(&MR); 7688bcb0991SDimitry Andric if (EHFrameRangeItr == InProcessLinks.end()) 7690b57cec5SDimitry Andric return Error::success(); 7700b57cec5SDimitry Andric 771e8d8bef9SDimitry Andric EmittedRange = EHFrameRangeItr->second; 77204eeddc0SDimitry Andric assert(EmittedRange.Start && "eh-frame addr to register can not be null"); 7738bcb0991SDimitry Andric InProcessLinks.erase(EHFrameRangeItr); 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric 776e8d8bef9SDimitry Andric if (auto Err = MR.withResourceKeyDo( 777e8d8bef9SDimitry Andric [&](ResourceKey K) { EHFrameRanges[K].push_back(EmittedRange); })) 778e8d8bef9SDimitry Andric return Err; 7795ffd83dbSDimitry Andric 78004eeddc0SDimitry Andric return Registrar->registerEHFrames(EmittedRange); 781e8d8bef9SDimitry Andric } 782e8d8bef9SDimitry Andric 783e8d8bef9SDimitry Andric Error EHFrameRegistrationPlugin::notifyFailed( 784e8d8bef9SDimitry Andric MaterializationResponsibility &MR) { 785e8d8bef9SDimitry Andric std::lock_guard<std::mutex> Lock(EHFramePluginMutex); 786e8d8bef9SDimitry Andric InProcessLinks.erase(&MR); 7870b57cec5SDimitry Andric return Error::success(); 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 790e8d8bef9SDimitry Andric Error EHFrameRegistrationPlugin::notifyRemovingResources(ResourceKey K) { 79104eeddc0SDimitry Andric std::vector<ExecutorAddrRange> RangesToRemove; 7920b57cec5SDimitry Andric 793e8d8bef9SDimitry Andric ES.runSessionLocked([&] { 794e8d8bef9SDimitry Andric auto I = EHFrameRanges.find(K); 795e8d8bef9SDimitry Andric if (I != EHFrameRanges.end()) { 796e8d8bef9SDimitry Andric RangesToRemove = std::move(I->second); 797e8d8bef9SDimitry Andric EHFrameRanges.erase(I); 798e8d8bef9SDimitry Andric } 799e8d8bef9SDimitry Andric }); 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric Error Err = Error::success(); 802e8d8bef9SDimitry Andric while (!RangesToRemove.empty()) { 803e8d8bef9SDimitry Andric auto RangeToRemove = RangesToRemove.back(); 804e8d8bef9SDimitry Andric RangesToRemove.pop_back(); 80504eeddc0SDimitry Andric assert(RangeToRemove.Start && "Untracked eh-frame range must not be null"); 80604eeddc0SDimitry Andric Err = joinErrors(std::move(Err), 80704eeddc0SDimitry Andric Registrar->deregisterEHFrames(RangeToRemove)); 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric return Err; 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 813e8d8bef9SDimitry Andric void EHFrameRegistrationPlugin::notifyTransferringResources( 814e8d8bef9SDimitry Andric ResourceKey DstKey, ResourceKey SrcKey) { 815e8d8bef9SDimitry Andric auto SI = EHFrameRanges.find(SrcKey); 816fe6060f1SDimitry Andric if (SI == EHFrameRanges.end()) 817fe6060f1SDimitry Andric return; 818fe6060f1SDimitry Andric 819fe6060f1SDimitry Andric auto DI = EHFrameRanges.find(DstKey); 820fe6060f1SDimitry Andric if (DI != EHFrameRanges.end()) { 821e8d8bef9SDimitry Andric auto &SrcRanges = SI->second; 822fe6060f1SDimitry Andric auto &DstRanges = DI->second; 823e8d8bef9SDimitry Andric DstRanges.reserve(DstRanges.size() + SrcRanges.size()); 824e8d8bef9SDimitry Andric for (auto &SrcRange : SrcRanges) 825e8d8bef9SDimitry Andric DstRanges.push_back(std::move(SrcRange)); 826e8d8bef9SDimitry Andric EHFrameRanges.erase(SI); 827fe6060f1SDimitry Andric } else { 828fe6060f1SDimitry Andric // We need to move SrcKey's ranges over without invalidating the SI 829fe6060f1SDimitry Andric // iterator. 830fe6060f1SDimitry Andric auto Tmp = std::move(SI->second); 831fe6060f1SDimitry Andric EHFrameRanges.erase(SI); 832fe6060f1SDimitry Andric EHFrameRanges[DstKey] = std::move(Tmp); 833e8d8bef9SDimitry Andric } 834e8d8bef9SDimitry Andric } 835e8d8bef9SDimitry Andric 8360b57cec5SDimitry Andric } // End namespace orc. 8370b57cec5SDimitry Andric } // End namespace llvm. 838