LLVM  7.0.0
RTDyldObjectLinkingLayer.cpp
Go to the documentation of this file.
1 //===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
12 namespace {
13 
14 using namespace llvm;
15 using namespace llvm::orc;
16 
17 class VSOSearchOrderResolver : public JITSymbolResolver {
18 public:
19  VSOSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20 
21  Expected<LookupResult> lookup(const LookupSet &Symbols) {
22  auto &ES = MR.getTargetVSO().getExecutionSession();
23  SymbolNameSet InternedSymbols;
24 
25  for (auto &S : Symbols)
26  InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
27 
28  auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
29  MR.addDependenciesForAll(Deps);
30  };
31 
32  auto InternedResult =
33  MR.getTargetVSO().withSearchOrderDo([&](const VSOList &VSOs) {
34  return ES.lookup(VSOs, InternedSymbols, RegisterDependencies, false);
35  });
36 
37  if (!InternedResult)
38  return InternedResult.takeError();
39 
40  LookupResult Result;
41  for (auto &KV : *InternedResult)
42  Result[*KV.first] = std::move(KV.second);
43 
44  return Result;
45  }
46 
47  Expected<LookupFlagsResult> lookupFlags(const LookupSet &Symbols) {
48  auto &ES = MR.getTargetVSO().getExecutionSession();
49 
50  SymbolNameSet InternedSymbols;
51 
52  for (auto &S : Symbols)
53  InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
54 
55  SymbolFlagsMap InternedResult;
56  MR.getTargetVSO().withSearchOrderDo([&](const VSOList &VSOs) {
57  // An empty search order is pathalogical, but allowed.
58  if (VSOs.empty())
59  return;
60 
61  assert(VSOs.front() && "VSOList entry can not be null");
62  InternedResult = VSOs.front()->lookupFlags(InternedSymbols);
63  });
64 
65  LookupFlagsResult Result;
66  for (auto &KV : InternedResult)
67  Result[*KV.first] = std::move(KV.second);
68 
69  return Result;
70  }
71 
72 private:
74 };
75 
76 } // end anonymous namespace
77 
78 namespace llvm {
79 namespace orc {
80 
82  ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
83  NotifyLoadedFunction NotifyLoaded, NotifyFinalizedFunction NotifyFinalized)
84  : ObjectLayer(ES), GetMemoryManager(GetMemoryManager),
85  NotifyLoaded(std::move(NotifyLoaded)),
86  NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {}
87 
89  VModuleKey K,
90  std::unique_ptr<MemoryBuffer> O) {
91  assert(O && "Object must not be null");
92 
93  auto &ES = getExecutionSession();
94 
95  auto ObjFile = object::ObjectFile::createObjectFile(*O);
96  if (!ObjFile) {
97  getExecutionSession().reportError(ObjFile.takeError());
99  }
100 
101  auto MemoryManager = GetMemoryManager(K);
102 
103  VSOSearchOrderResolver Resolver(R);
104  auto RTDyld = llvm::make_unique<RuntimeDyld>(*MemoryManager, Resolver);
105  RTDyld->setProcessAllSections(ProcessAllSections);
106 
107  {
108  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
109 
110  assert(!ActiveRTDylds.count(K) &&
111  "An active RTDyld already exists for this key?");
112  ActiveRTDylds[K] = RTDyld.get();
113 
114  assert(!MemMgrs.count(K) &&
115  "A memory manager already exists for this key?");
116  MemMgrs[K] = std::move(MemoryManager);
117  }
118 
119  auto Info = RTDyld->loadObject(**ObjFile);
120 
121  {
122  std::set<StringRef> InternalSymbols;
123  for (auto &Sym : (*ObjFile)->symbols()) {
124  if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
125  if (auto SymName = Sym.getName())
126  InternalSymbols.insert(*SymName);
127  else {
128  ES.reportError(SymName.takeError());
130  return;
131  }
132  }
133  }
134 
135  SymbolMap Symbols;
136  for (auto &KV : RTDyld->getSymbolTable())
137  if (!InternalSymbols.count(KV.first))
138  Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second;
139 
140  R.resolve(Symbols);
141  }
142 
143  if (NotifyLoaded)
144  NotifyLoaded(K, **ObjFile, *Info);
145 
146  RTDyld->finalizeWithMemoryManagerLocking();
147 
148  {
149  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
150  ActiveRTDylds.erase(K);
151  }
152 
153  if (RTDyld->hasError()) {
154  ES.reportError(make_error<StringError>(RTDyld->getErrorString(),
157  return;
158  }
159 
160  R.finalize();
161 
162  if (NotifyFinalized)
163  NotifyFinalized(K);
164 }
165 
167  VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const {
168  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
169  auto ActiveRTDyldItr = ActiveRTDylds.find(K);
170 
171  assert(ActiveRTDyldItr != ActiveRTDylds.end() &&
172  "No active RTDyld instance found for key");
173  ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr);
174 }
175 
176 } // End namespace orc.
177 } // End namespace llvm.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:153
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static sys::Mutex Lock
std::map< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:51
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Definition: BitVector.h:921
std::map< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
Definition: Core.h:57
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:124
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:92
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:40
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:391
RTDyldObjectLinkingLayer2(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager, NotifyLoadedFunction NotifyLoaded=NotifyLoadedFunction(), NotifyFinalizedFunction NotifyFinalized=NotifyFinalizedFunction())
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyFinalized functors.
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const
Map section addresses for the object associated with the VModuleKey K.
void emit(MaterializationResponsibility R, VModuleKey K, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
Symbol resolution interface.
Definition: JITSymbol.h:292
std::function< std::shared_ptr< RuntimeDyld::MemoryManager >(VModuleKey)> GetMemoryManagerFunction
std::function< void(VModuleKey)> NotifyFinalizedFunction
Functor for receiving finalization notifications.
void resolve(const SymbolMap &Symbols)
Resolves the given symbols.
Definition: Core.cpp:605
std::map< VSO *, SymbolNameSet > SymbolDependenceMap
A base class for materialization failures that allows the failing symbols to be obtained for logging...
Definition: Core.h:64
Expected< SymbolMap > lookup(const VSOList &VSOs, SymbolNameSet Names)
Look up the given names in the given VSOs.
Definition: Core.cpp:1655
std::vector< VSO * > VSOList
A list of VSO pointers.
Definition: Core.h:70
An ExecutionSession represents a running JIT program.
Definition: Core.h:735
void finalize()
Finalizes all symbols tracked by this instance.
Definition: Core.cpp:625
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void failMaterialization()
Notify all unfinalized symbols that an error has occurred.
Definition: Core.cpp:653
Interface for Layers that accept object files.
Definition: Layer.h:86
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFunction
Functor for receiving object-loaded notifications.
std::set< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
Definition: Core.h:44
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73