LLVM: lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9#include
10
13
14namespace {
15
16using namespace llvm;
18
20public:
21 JITDylibSearchOrderResolver(MaterializationResponsibility &MR,
23 : MR(MR), Deps(Deps) {}
24
25 void lookup(const LookupSet &Symbols,
26 OnResolvedFunction OnResolved) override {
27 auto &ES = MR.getTargetJITDylib().getExecutionSession();
28 SymbolLookupSet InternedSymbols;
29
30
31 for (auto &S : Symbols)
32 InternedSymbols.add(ES.intern(S));
33
34
35
36 auto OnResolvedWithUnwrap =
37 [OnResolved = std::move(OnResolved)](
38 Expected InternedResult) mutable {
39 if (!InternedResult) {
40 OnResolved(InternedResult.takeError());
41 return;
42 }
43
45 for (auto &KV : *InternedResult)
46 Result[*KV.first] = {KV.second.getAddress().getValue(),
47 KV.second.getFlags()};
49 };
50
52 MR.getTargetJITDylib().withLinkOrderDo(
54 ES.lookup(
55 LookupKind::Static, LinkOrder, InternedSymbols, SymbolState::Resolved,
56 std::move(OnResolvedWithUnwrap),
58 }
59
60 Expected getResponsibilitySet(const LookupSet &Symbols) override {
62
63 for (auto &KV : MR.getSymbols()) {
64 if (Symbols.count(*KV.first))
65 Result.insert(*KV.first);
66 }
67
69 }
70
71private:
72 MaterializationResponsibility &MR;
74};
75
76}
77
78namespace llvm {
79namespace orc {
80
82
83using BaseT = RTTIExtends<RTDyldObjectLinkingLayer, ObjectLayer>;
84
87 : BaseT(ES), GetMemoryManager(std::move(GetMemoryManager)) {
89}
90
92 assert(MemMgrs.empty() &&
93 "Layer destroyed with resources still attached"
94 "(ExecutionSession::endSession() must be called prior to "
95 "destruction)");
96}
97
99 std::unique_ptr R,
100 std::unique_ptr O) {
101 assert(O && "Object must not be null");
102
103 auto &ES = getExecutionSession();
104
106
107 if (!Obj) {
108 getExecutionSession().reportError(Obj.takeError());
109 R->failMaterialization();
110 return;
111 }
112
113
114
115 auto InternalSymbols = std::make_shared<std::set>();
116 {
118 for (auto &Sym : (*Obj)->symbols()) {
119
120
121 if (auto SymType = Sym.getType()) {
123 continue;
124 } else {
125 ES.reportError(SymType.takeError());
126 R->failMaterialization();
127 return;
128 }
129
131 if (!SymFlagsOrErr) {
132
133 ES.reportError(SymFlagsOrErr.takeError());
134 R->failMaterialization();
135 return;
136 }
137
138
139
140 if (AutoClaimObjectSymbols &&
142 auto SymName = Sym.getName();
143 if (!SymName) {
144 ES.reportError(SymName.takeError());
145 R->failMaterialization();
146 return;
147 }
148
149
151 if (R->getSymbols().count(SymbolName))
152 continue;
153
155 if (!SymFlags) {
156 ES.reportError(SymFlags.takeError());
157 R->failMaterialization();
158 return;
159 }
160
161 ExtraSymbolsToClaim[SymbolName] = *SymFlags;
162 continue;
163 }
164
165
167 if (auto SymName = Sym.getName())
168 InternalSymbols->insert(*SymName);
169 else {
170 ES.reportError(SymName.takeError());
171 R->failMaterialization();
172 return;
173 }
174 }
175 }
176
177 if (!ExtraSymbolsToClaim.empty()) {
178 if (auto Err = R->defineMaterializing(ExtraSymbolsToClaim)) {
179 ES.reportError(std::move(Err));
180 R->failMaterialization();
181 }
182 }
183 }
184
185 auto MemMgr = GetMemoryManager(*O);
186 auto &MemMgrRef = *MemMgr;
187
188
189
190 std::shared_ptr SharedR(std::move(R));
191 auto Deps = std::make_unique();
192
194 std::make_unique(*SharedR, *Deps);
195 auto *ResolverPtr = Resolver.get();
196
199 MemMgrRef, *ResolverPtr, ProcessAllSections,
200 [this, SharedR, &MemMgrRef, InternalSymbols](
203 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
204 return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
205 ResolvedSymbols, *InternalSymbols);
206 },
207 [this, SharedR, MemMgr = std::move(MemMgr), Deps = std::move(Deps),
210 std::unique_ptrRuntimeDyld::LoadedObjectInfo LoadedObjInfo,
211 Error Err) mutable {
212 onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
213 std::move(LoadedObjInfo), std::move(Deps), std::move(Err));
214 });
215}
216
218 std::lock_guardstd::mutex Lock(RTDyldLayerMutex);
220 "Listener has already been registered");
221 EventListeners.push_back(&L);
222}
223
225 std::lock_guardstd::mutex Lock(RTDyldLayerMutex);
227 assert(I != EventListeners.end() && "Listener not registered");
228 EventListeners.erase(I);
229}
230
231Error RTDyldObjectLinkingLayer::onObjLoad(
235 std::map<StringRef, JITEvaluatedSymbol> Resolved,
236 std::set &InternalSymbols) {
239
240
241
243 auto &ES = getExecutionSession();
244
245
246
247
248 for (auto &Sym : COFFObj->symbols()) {
249
252 continue;
253 auto Name = Sym.getName();
254 if (!Name)
255 return Name.takeError();
257
258
259
260 if (I == Resolved.end() || InternalSymbols.count(*Name) ||
261 R.getSymbols().count(ES.intern(*Name)))
262 continue;
263 auto Sec = Sym.getSection();
264 if (!Sec)
265 return Sec.takeError();
266 if (*Sec == COFFObj->section_end())
267 continue;
268 auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
271 }
272
273
274 for (auto &Sym : COFFObj->symbols()) {
277 continue;
278 auto Name = Sym.getName();
279 if (!Name)
280 return Name.takeError();
282
283
284
285 if (I != Resolved.end() || !R.getSymbols().count(ES.intern(*Name)))
286 continue;
287
288
289 auto COFFSym = COFFObj->getCOFFSymbol(Sym);
290 if (!COFFSym.isWeakExternal())
291 continue;
294 continue;
295
296
297
299 COFFObj->getSymbol(WeakExternal->TagIndex);
300 if (!TargetSymbol)
303 if (!TargetName)
305 auto J = Resolved.find(*TargetName);
308 " not resolved",
311 }
312 }
313
315
316
317
318 if (InternalSymbols.count(KV.first))
319 continue;
320
321 auto InternedName = getExecutionSession().intern(KV.first);
322 auto Flags = KV.second.getFlags();
323 auto I = R.getSymbols().find(InternedName);
324 if (I != R.getSymbols().end()) {
325
326
327 if (OverrideObjectFlags)
329 else {
330
331
332
333 if (I->second.isWeak())
335 }
336 } else if (AutoClaimObjectSymbols)
337 ExtraSymbolsToClaim[InternedName] = Flags;
338
339 Symbols[InternedName] = {ExecutorAddr(KV.second.getAddress()), Flags};
340 }
341
342 if (!ExtraSymbolsToClaim.empty()) {
343 if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
344 return Err;
345
346
347
348 for (auto &KV : ExtraSymbolsToClaim)
349 if (KV.second.isWeak() && .getSymbols().count(KV.first))
351 }
352
353 if (auto Err = R.notifyResolved(Symbols)) {
354 R.failMaterialization();
355 return Err;
356 }
357
358 if (NotifyLoaded)
359 NotifyLoaded(R, Obj, LoadedObjInfo);
360
362}
363
364void RTDyldObjectLinkingLayer::onObjEmit(
366 object::OwningBinaryobject::ObjectFile O,
367 std::unique_ptrRuntimeDyld::MemoryManager MemMgr,
368 std::unique_ptrRuntimeDyld::LoadedObjectInfo LoadedObjInfo,
369 std::unique_ptr Deps, Error Err) {
370 if (Err) {
371 getExecutionSession().reportError(std::move(Err));
372 R.failMaterialization();
373 return;
374 }
375
376 SymbolDependenceGroup SDG;
377 for (auto &[Sym, Flags] : R.getSymbols())
380
381 if (auto Err = R.notifyEmitted(SDG)) {
382 getExecutionSession().reportError(std::move(Err));
383 R.failMaterialization();
384 return;
385 }
386
387 std::unique_ptrobject::ObjectFile Obj;
388 std::unique_ptr ObjBuffer;
389 std::tie(Obj, ObjBuffer) = O.takeBinary();
390
391
392 {
393 std::lock_guardstd::mutex Lock(RTDyldLayerMutex);
394 for (auto *L : EventListeners)
396 *LoadedObjInfo);
397 }
398
399 if (NotifyEmitted)
400 NotifyEmitted(R, std::move(ObjBuffer));
401
402 if (auto Err = R.withResourceKeyDo(
403 [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
404 getExecutionSession().reportError(std::move(Err));
405 R.failMaterialization();
406 }
407}
408
409Error RTDyldObjectLinkingLayer::handleRemoveResources(JITDylib &JD,
411
412 std::vector MemMgrsToRemove;
413
414 getExecutionSession().runSessionLocked([&] {
415 auto I = MemMgrs.find(K);
416 if (I != MemMgrs.end()) {
417 std::swap(MemMgrsToRemove, I->second);
418 MemMgrs.erase(I);
419 }
420 });
421
422 {
423 std::lock_guardstd::mutex Lock(RTDyldLayerMutex);
424 for (auto &MemMgr : MemMgrsToRemove) {
425 for (auto *L : EventListeners)
427 MemMgr->deregisterEHFrames();
428 }
429 }
430
432}
433
434void RTDyldObjectLinkingLayer::handleTransferResources(JITDylib &JD,
437 if (MemMgrs.contains(SrcKey)) {
438
439
440 auto &DstMemMgrs = MemMgrs[DstKey];
441 auto &SrcMemMgrs = MemMgrs[SrcKey];
442 DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
443 for (auto &MemMgr : SrcMemMgrs)
444 DstMemMgrs.push_back(std::move(MemMgr));
445
446 MemMgrs.erase(SrcKey);
447 }
448}
449
450}
451}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static LLVM_ABI Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Symbol resolution interface.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Information about the loaded object.
std::pair< iterator, bool > insert(const ValueT &V)
This class is the base class for all object file types.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
An ExecutionSession represents a running JIT program.
LLVM_ABI void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
Represents a JIT'd dynamic library.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
Definition RTDyldObjectLinkingLayer.cpp:85
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Definition RTDyldObjectLinkingLayer.cpp:98
~RTDyldObjectLinkingLayer() override
Definition RTDyldObjectLinkingLayer.cpp:91
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
Definition RTDyldObjectLinkingLayer.cpp:224
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
Definition RTDyldObjectLinkingLayer.cpp:217
unique_function< std::unique_ptr< RuntimeDyld::MemoryManager >( const MemoryBuffer &)> GetMemoryManagerFunction
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Pointer to a pooled string representing a symbol name.
@ IMAGE_WEAK_EXTERN_SEARCH_ALIAS
std::vector< std::optional< ExecutorSymbolDef > > LookupResult
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
@ Resolved
Queried, materialization begun.
RTTIExtends< ObjectTransformLayer, ObjectLayer > BaseT
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
LLVM_ABI void jitLinkForORC(object::OwningBinary< object::ObjectFile > O, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(const object::ObjectFile &Obj, RuntimeDyld::LoadedObjectInfo &, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted)
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
JITTargetAddress pointerToJITTargetAddress(T *Ptr)
Convert a pointer to a JITTargetAddress.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
SymbolDependenceMap Dependencies