LLVM 17.0.0git
RTDyldObjectLinkingLayer.cpp
Go to the documentation of this file.
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
10#include "llvm/Object/COFF.h"
11
12namespace {
13
14using namespace llvm;
15using namespace llvm::orc;
16
17class JITDylibSearchOrderResolver : public JITSymbolResolver {
18public:
19 JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20
21 void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
22 auto &ES = MR.getTargetJITDylib().getExecutionSession();
23 SymbolLookupSet InternedSymbols;
24
25 // Intern the requested symbols: lookup takes interned strings.
26 for (auto &S : Symbols)
27 InternedSymbols.add(ES.intern(S));
28
29 // Build an OnResolve callback to unwrap the interned strings and pass them
30 // to the OnResolved callback.
31 auto OnResolvedWithUnwrap =
32 [OnResolved = std::move(OnResolved)](
33 Expected<SymbolMap> InternedResult) mutable {
34 if (!InternedResult) {
35 OnResolved(InternedResult.takeError());
36 return;
37 }
38
40 for (auto &KV : *InternedResult)
41 Result[*KV.first] = std::move(KV.second);
42 OnResolved(Result);
43 };
44
45 // Register dependencies for all symbols contained in this set.
46 auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
47 MR.addDependenciesForAll(Deps);
48 };
49
50 JITDylibSearchOrder LinkOrder;
51 MR.getTargetJITDylib().withLinkOrderDo(
52 [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
53 ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
54 SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
55 RegisterDependencies);
56 }
57
58 Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
59 LookupSet Result;
60
61 for (auto &KV : MR.getSymbols()) {
62 if (Symbols.count(*KV.first))
63 Result.insert(*KV.first);
64 }
65
66 return Result;
67 }
68
69private:
71};
72
73} // end anonymous namespace
74
75namespace llvm {
76namespace orc {
77
79
81
83 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
84 : BaseT(ES), GetMemoryManager(std::move(GetMemoryManager)) {
86}
87
89 assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
90}
91
93 std::unique_ptr<MaterializationResponsibility> R,
94 std::unique_ptr<MemoryBuffer> O) {
95 assert(O && "Object must not be null");
96
97 auto &ES = getExecutionSession();
98
100
101 if (!Obj) {
102 getExecutionSession().reportError(Obj.takeError());
103 R->failMaterialization();
104 return;
105 }
106
107 // Collect the internal symbols from the object file: We will need to
108 // filter these later.
109 auto InternalSymbols = std::make_shared<std::set<StringRef>>();
110 {
111 SymbolFlagsMap ExtraSymbolsToClaim;
112 for (auto &Sym : (*Obj)->symbols()) {
113
114 // Skip file symbols.
115 if (auto SymType = Sym.getType()) {
116 if (*SymType == object::SymbolRef::ST_File)
117 continue;
118 } else {
119 ES.reportError(SymType.takeError());
120 R->failMaterialization();
121 return;
122 }
123
124 Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
125 if (!SymFlagsOrErr) {
126 // TODO: Test this error.
127 ES.reportError(SymFlagsOrErr.takeError());
128 R->failMaterialization();
129 return;
130 }
131
132 // Try to claim responsibility of weak symbols
133 // if AutoClaimObjectSymbols flag is set.
134 if (AutoClaimObjectSymbols &&
135 (*SymFlagsOrErr & object::BasicSymbolRef::SF_Weak)) {
136 auto SymName = Sym.getName();
137 if (!SymName) {
138 ES.reportError(SymName.takeError());
139 R->failMaterialization();
140 return;
141 }
142
143 // Already included in responsibility set, skip it
144 SymbolStringPtr SymbolName = ES.intern(*SymName);
145 if (R->getSymbols().count(SymbolName))
146 continue;
147
148 auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
149 if (!SymFlags) {
150 ES.reportError(SymFlags.takeError());
151 R->failMaterialization();
152 return;
153 }
154
155 ExtraSymbolsToClaim[SymbolName] = *SymFlags;
156 continue;
157 }
158
159 // Don't include symbols that aren't global.
160 if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
161 if (auto SymName = Sym.getName())
162 InternalSymbols->insert(*SymName);
163 else {
164 ES.reportError(SymName.takeError());
165 R->failMaterialization();
166 return;
167 }
168 }
169 }
170
171 if (!ExtraSymbolsToClaim.empty()) {
172 if (auto Err = R->defineMaterializing(ExtraSymbolsToClaim)) {
173 ES.reportError(std::move(Err));
174 R->failMaterialization();
175 }
176 }
177 }
178
179 auto MemMgr = GetMemoryManager();
180 auto &MemMgrRef = *MemMgr;
181
182 // Switch to shared ownership of MR so that it can be captured by both
183 // lambdas below.
184 std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
185
186 JITDylibSearchOrderResolver Resolver(*SharedR);
187
189 object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
190 MemMgrRef, Resolver, ProcessAllSections,
191 [this, SharedR, &MemMgrRef, InternalSymbols](
192 const object::ObjectFile &Obj,
193 RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
194 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
195 return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
196 ResolvedSymbols, *InternalSymbols);
197 },
198 [this, SharedR, MemMgr = std::move(MemMgr)](
200 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
201 Error Err) mutable {
202 onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
203 std::move(LoadedObjInfo), std::move(Err));
204 });
205}
206
208 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
209 assert(!llvm::is_contained(EventListeners, &L) &&
210 "Listener has already been registered");
211 EventListeners.push_back(&L);
212}
213
215 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
216 auto I = llvm::find(EventListeners, &L);
217 assert(I != EventListeners.end() && "Listener not registered");
218 EventListeners.erase(I);
219}
220
221Error RTDyldObjectLinkingLayer::onObjLoad(
224 RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
225 std::map<StringRef, JITEvaluatedSymbol> Resolved,
226 std::set<StringRef> &InternalSymbols) {
227 SymbolFlagsMap ExtraSymbolsToClaim;
228 SymbolMap Symbols;
229
230 // Hack to support COFF constant pool comdats introduced during compilation:
231 // (See http://llvm.org/PR40074)
232 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
233 auto &ES = getExecutionSession();
234
235 // For all resolved symbols that are not already in the responsibilty set:
236 // check whether the symbol is in a comdat section and if so mark it as
237 // weak.
238 for (auto &Sym : COFFObj->symbols()) {
239 // getFlags() on COFF symbols can't fail.
240 uint32_t SymFlags = cantFail(Sym.getFlags());
242 continue;
243 auto Name = Sym.getName();
244 if (!Name)
245 return Name.takeError();
246 auto I = Resolved.find(*Name);
247
248 // Skip unresolved symbols, internal symbols, and symbols that are
249 // already in the responsibility set.
250 if (I == Resolved.end() || InternalSymbols.count(*Name) ||
251 R.getSymbols().count(ES.intern(*Name)))
252 continue;
253 auto Sec = Sym.getSection();
254 if (!Sec)
255 return Sec.takeError();
256 if (*Sec == COFFObj->section_end())
257 continue;
258 auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
259 if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
260 I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
261 }
262
263 // Handle any aliases.
264 for (auto &Sym : COFFObj->symbols()) {
265 uint32_t SymFlags = cantFail(Sym.getFlags());
267 continue;
268 auto Name = Sym.getName();
269 if (!Name)
270 return Name.takeError();
271 auto I = Resolved.find(*Name);
272
273 // Skip already-resolved symbols, and symbols that we're not responsible
274 // for.
275 if (I != Resolved.end() || !R.getSymbols().count(ES.intern(*Name)))
276 continue;
277
278 // Skip anything other than weak externals.
279 auto COFFSym = COFFObj->getCOFFSymbol(Sym);
280 if (!COFFSym.isWeakExternal())
281 continue;
282 auto *WeakExternal = COFFSym.getAux<object::coff_aux_weak_external>();
283 if (WeakExternal->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS)
284 continue;
285
286 // We found an alias. Reuse the resolution of the alias target for the
287 // alias itself.
289 COFFObj->getSymbol(WeakExternal->TagIndex);
290 if (!TargetSymbol)
291 return TargetSymbol.takeError();
292 Expected<StringRef> TargetName = COFFObj->getSymbolName(*TargetSymbol);
293 if (!TargetName)
294 return TargetName.takeError();
295 auto J = Resolved.find(*TargetName);
296 if (J == Resolved.end())
297 return make_error<StringError>("Could alias target " + *TargetName +
298 " not resolved",
300 Resolved[*Name] = J->second;
301 }
302 }
303
304 for (auto &KV : Resolved) {
305 // Scan the symbols and add them to the Symbols map for resolution.
306
307 // We never claim internal symbols.
308 if (InternalSymbols.count(KV.first))
309 continue;
310
311 auto InternedName = getExecutionSession().intern(KV.first);
312 auto Flags = KV.second.getFlags();
313 auto I = R.getSymbols().find(InternedName);
314 if (I != R.getSymbols().end()) {
315 // Override object flags and claim responsibility for symbols if
316 // requested.
317 if (OverrideObjectFlags)
318 Flags = I->second;
319 else {
320 // RuntimeDyld/MCJIT's weak tracking isn't compatible with ORC's. Even
321 // if we're not overriding flags in general we should set the weak flag
322 // according to the MaterializationResponsibility object symbol table.
323 if (I->second.isWeak())
325 }
326 } else if (AutoClaimObjectSymbols)
327 ExtraSymbolsToClaim[InternedName] = Flags;
328
329 Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
330 }
331
332 if (!ExtraSymbolsToClaim.empty()) {
333 if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
334 return Err;
335
336 // If we claimed responsibility for any weak symbols but were rejected then
337 // we need to remove them from the resolved set.
338 for (auto &KV : ExtraSymbolsToClaim)
339 if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
340 Symbols.erase(KV.first);
341 }
342
343 if (auto Err = R.notifyResolved(Symbols)) {
344 R.failMaterialization();
345 return Err;
346 }
347
348 if (NotifyLoaded)
349 NotifyLoaded(R, Obj, LoadedObjInfo);
350
351 return Error::success();
352}
353
354void RTDyldObjectLinkingLayer::onObjEmit(
357 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
358 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
359 if (Err) {
360 getExecutionSession().reportError(std::move(Err));
361 R.failMaterialization();
362 return;
363 }
364
365 if (auto Err = R.notifyEmitted()) {
366 getExecutionSession().reportError(std::move(Err));
367 R.failMaterialization();
368 return;
369 }
370
371 std::unique_ptr<object::ObjectFile> Obj;
372 std::unique_ptr<MemoryBuffer> ObjBuffer;
373 std::tie(Obj, ObjBuffer) = O.takeBinary();
374
375 // Run EventListener notifyLoaded callbacks.
376 {
377 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
378 for (auto *L : EventListeners)
379 L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
380 *LoadedObjInfo);
381 }
382
383 if (NotifyEmitted)
384 NotifyEmitted(R, std::move(ObjBuffer));
385
386 if (auto Err = R.withResourceKeyDo(
387 [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
388 getExecutionSession().reportError(std::move(Err));
389 R.failMaterialization();
390 }
391}
392
393Error RTDyldObjectLinkingLayer::handleRemoveResources(JITDylib &JD,
394 ResourceKey K) {
395
396 std::vector<MemoryManagerUP> MemMgrsToRemove;
397
398 getExecutionSession().runSessionLocked([&] {
399 auto I = MemMgrs.find(K);
400 if (I != MemMgrs.end()) {
401 std::swap(MemMgrsToRemove, I->second);
402 MemMgrs.erase(I);
403 }
404 });
405
406 {
407 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
408 for (auto &MemMgr : MemMgrsToRemove) {
409 for (auto *L : EventListeners)
410 L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
411 MemMgr->deregisterEHFrames();
412 }
413 }
414
415 return Error::success();
416}
417
418void RTDyldObjectLinkingLayer::handleTransferResources(JITDylib &JD,
419 ResourceKey DstKey,
420 ResourceKey SrcKey) {
421 auto I = MemMgrs.find(SrcKey);
422 if (I != MemMgrs.end()) {
423 auto &SrcMemMgrs = I->second;
424 auto &DstMemMgrs = MemMgrs[DstKey];
425 DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
426 for (auto &MemMgr : SrcMemMgrs)
427 DstMemMgrs.push_back(std::move(MemMgr));
428
429 // Erase SrcKey entry using value rather than iterator I: I may have been
430 // invalidated when we looked up DstKey.
431 MemMgrs.erase(SrcKey);
432 }
433}
434
435} // End namespace orc.
436} // End namespace llvm.
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
@ Flags
Definition: TextStubV5.cpp:93
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool erase(const KeyT &Val)
Definition: DenseMap.h:315
bool empty() const
Definition: DenseMap.h:98
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:69
Symbol resolution interface.
Definition: JITSymbol.h:371
virtual void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved)=0
Returns the fully resolved address and flags for each of the given symbols.
virtual Expected< LookupSet > getResponsibilitySet(const LookupSet &Symbols)=0
Returns the subset of the given symbols that should be materialized by the caller.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2148
Information about the loaded object.
Definition: RuntimeDyld.h:69
This class is the base class for all object file types.
Definition: ObjectFile.h:228
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:194
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
Definition: Core.cpp:1915
Represents a JIT'd dynamic library.
Definition: Core.h:962
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:524
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:180
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:241
Pointer to a pooled string representing a symbol name.
@ IMAGE_SCN_LNK_COMDAT
Definition: COFF.h:294
@ IMAGE_WEAK_EXTERN_SEARCH_ALIAS
Definition: COFF.h:442
std::vector< ExecutorAddr > LookupResult
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition: Core.h:159
@ Resolved
Queried, materialization begun.
uintptr_t ResourceKey
Definition: Core.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1802
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
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.
Definition: Error.h:745
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1909
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1939
JITTargetAddress pointerToJITTargetAddress(T *Ptr)
Convert a pointer to a JITTargetAddress.
Definition: JITSymbol.h:69
Definition: BitVector.h:851