LLVM 20.0.0git
LazyReexports.cpp
Go to the documentation of this file.
1//===---------- LazyReexports.cpp - Utilities for lazy reexports ----------===//
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
13
14#define DEBUG_TYPE "orc"
15
16namespace llvm {
17namespace orc {
18
20 ExecutorAddr ErrorHandlerAddr,
22 : ES(ES), ErrorHandlerAddr(ErrorHandlerAddr), TP(TP) {}
23
25 JITDylib &SourceJD, SymbolStringPtr SymbolName,
26 NotifyResolvedFunction NotifyResolved) {
27 assert(TP && "TrampolinePool not set");
28
29 std::lock_guard<std::mutex> Lock(LCTMMutex);
30 auto Trampoline = TP->getTrampoline();
31
32 if (!Trampoline)
33 return Trampoline.takeError();
34
35 Reexports[*Trampoline] = ReexportsEntry{&SourceJD, std::move(SymbolName)};
36 Notifiers[*Trampoline] = std::move(NotifyResolved);
37 return *Trampoline;
38}
39
41 ES.reportError(std::move(Err));
42 return ErrorHandlerAddr;
43}
44
47 std::lock_guard<std::mutex> Lock(LCTMMutex);
48 auto I = Reexports.find(TrampolineAddr);
49 if (I == Reexports.end())
51 "Missing reexport for trampoline address %p" +
52 formatv("{0:x}", TrampolineAddr));
53 return I->second;
54}
55
57 ExecutorAddr ResolvedAddr) {
58 NotifyResolvedFunction NotifyResolved;
59 {
60 std::lock_guard<std::mutex> Lock(LCTMMutex);
61 auto I = Notifiers.find(TrampolineAddr);
62 if (I != Notifiers.end()) {
63 NotifyResolved = std::move(I->second);
64 Notifiers.erase(I);
65 }
66 }
67
68 return NotifyResolved ? NotifyResolved(ResolvedAddr) : Error::success();
69}
70
72 ExecutorAddr TrampolineAddr,
73 NotifyLandingResolvedFunction NotifyLandingResolved) {
74
75 auto Entry = findReexport(TrampolineAddr);
76 if (!Entry)
77 return NotifyLandingResolved(reportCallThroughError(Entry.takeError()));
78
79 // Declaring SLS and the callback outside of the call to ES.lookup is a
80 // workaround to fix build failures on AIX and on z/OS platforms.
81 SymbolLookupSet SLS({Entry->SymbolName});
82 auto Callback = [this, TrampolineAddr, SymbolName = Entry->SymbolName,
83 NotifyLandingResolved = std::move(NotifyLandingResolved)](
85 if (Result) {
86 assert(Result->size() == 1 && "Unexpected result size");
87 assert(Result->count(SymbolName) && "Unexpected result value");
88 ExecutorAddr LandingAddr = (*Result)[SymbolName].getAddress();
89
90 if (auto Err = notifyResolved(TrampolineAddr, LandingAddr))
91 NotifyLandingResolved(reportCallThroughError(std::move(Err)));
92 else
93 NotifyLandingResolved(LandingAddr);
94 } else {
95 NotifyLandingResolved(reportCallThroughError(Result.takeError()));
96 }
97 };
98
100 makeJITDylibSearchOrder(Entry->SourceJD,
102 std::move(SLS), SymbolState::Ready, std::move(Callback),
104}
105
108 ExecutorAddr ErrorHandlerAddr) {
109 switch (T.getArch()) {
110 default:
111 return make_error<StringError>(
112 std::string("No callback manager available for ") + T.str(),
114
115 case Triple::aarch64:
117 return LocalLazyCallThroughManager::Create<OrcAArch64>(ES,
118 ErrorHandlerAddr);
119
120 case Triple::x86:
121 return LocalLazyCallThroughManager::Create<OrcI386>(ES, ErrorHandlerAddr);
122
124 return LocalLazyCallThroughManager::Create<OrcLoongArch64>(
125 ES, ErrorHandlerAddr);
126
127 case Triple::mips:
128 return LocalLazyCallThroughManager::Create<OrcMips32Be>(ES,
129 ErrorHandlerAddr);
130
131 case Triple::mipsel:
132 return LocalLazyCallThroughManager::Create<OrcMips32Le>(ES,
133 ErrorHandlerAddr);
134
135 case Triple::mips64:
136 case Triple::mips64el:
137 return LocalLazyCallThroughManager::Create<OrcMips64>(ES, ErrorHandlerAddr);
138
139 case Triple::riscv64:
140 return LocalLazyCallThroughManager::Create<OrcRiscv64>(ES,
141 ErrorHandlerAddr);
142
143 case Triple::x86_64:
144 if (T.getOS() == Triple::OSType::Win32)
145 return LocalLazyCallThroughManager::Create<OrcX86_64_Win32>(
146 ES, ErrorHandlerAddr);
147 else
148 return LocalLazyCallThroughManager::Create<OrcX86_64_SysV>(
149 ES, ErrorHandlerAddr);
150 }
151}
152
154 LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager,
155 JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc)
156 : MaterializationUnit(extractFlags(CallableAliases)),
157 LCTManager(LCTManager), ISManager(ISManager), SourceJD(SourceJD),
158 CallableAliases(std::move(CallableAliases)), AliaseeTable(SrcJDLoc) {}
159
161 return "<Lazy Reexports>";
162}
163
164void LazyReexportsMaterializationUnit::materialize(
165 std::unique_ptr<MaterializationResponsibility> R) {
166 auto RequestedSymbols = R->getRequestedSymbols();
167
168 SymbolAliasMap RequestedAliases;
169 for (auto &RequestedSymbol : RequestedSymbols) {
170 auto I = CallableAliases.find(RequestedSymbol);
171 assert(I != CallableAliases.end() && "Symbol not found in alias map?");
172 RequestedAliases[I->first] = std::move(I->second);
173 CallableAliases.erase(I);
174 }
175
176 if (!CallableAliases.empty())
177 if (auto Err = R->replace(lazyReexports(LCTManager, ISManager, SourceJD,
178 std::move(CallableAliases),
179 AliaseeTable))) {
180 R->getExecutionSession().reportError(std::move(Err));
181 R->failMaterialization();
182 return;
183 }
184
186 for (auto &Alias : RequestedAliases) {
187
188 auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline(
189 SourceJD, Alias.second.Aliasee,
190 [&ISManager = this->ISManager,
191 StubSym = Alias.first](ExecutorAddr ResolvedAddr) -> Error {
192 return ISManager.updatePointer(*StubSym, ResolvedAddr);
193 });
194
195 if (!CallThroughTrampoline) {
197 CallThroughTrampoline.takeError());
198 R->failMaterialization();
199 return;
200 }
201
202 StubInits[*Alias.first] =
203 std::make_pair(*CallThroughTrampoline, Alias.second.AliasFlags);
204 }
205
206 if (AliaseeTable != nullptr && !RequestedAliases.empty())
207 AliaseeTable->trackImpls(RequestedAliases, &SourceJD);
208
209 if (auto Err = ISManager.createStubs(StubInits)) {
210 SourceJD.getExecutionSession().reportError(std::move(Err));
211 R->failMaterialization();
212 return;
213 }
214
215 SymbolMap Stubs;
216 for (auto &Alias : RequestedAliases)
217 Stubs[Alias.first] = ISManager.findStub(*Alias.first, false);
218
219 // No registered dependencies, so these calls cannot fail.
220 cantFail(R->notifyResolved(Stubs));
221 cantFail(R->notifyEmitted({}));
222}
223
224void LazyReexportsMaterializationUnit::discard(const JITDylib &JD,
225 const SymbolStringPtr &Name) {
226 assert(CallableAliases.count(Name) &&
227 "Symbol not covered by this MaterializationUnit");
228 CallableAliases.erase(Name);
229}
230
231MaterializationUnit::Interface
232LazyReexportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) {
234 for (auto &KV : Aliases) {
235 assert(KV.second.AliasFlags.isCallable() &&
236 "Lazy re-exports must be callable symbols");
237 SymbolFlags[KV.first] = KV.second.AliasFlags;
238 }
239 return MaterializationUnit::Interface(std::move(SymbolFlags), nullptr);
240}
241
242} // End namespace orc.
243} // End namespace llvm.
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool erase(const KeyT &Val)
Definition: DenseMap.h:336
bool empty() const
Definition: DenseMap.h:98
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ loongarch64
Definition: Triple.h:62
@ mips64el
Definition: Triple.h:67
@ aarch64_32
Definition: Triple.h:53
An ExecutionSession represents a running JIT program.
Definition: Core.h:1431
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1566
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:1810
Represents an address in the executor process.
void trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD)
Definition: Speculation.cpp:25
Base class for managing collections of named indirect stubs.
virtual ExecutorSymbolDef findStub(StringRef Name, bool ExportedStubsOnly)=0
Find the stub with the given name.
virtual Error createStubs(const StubInitsMap &StubInits)=0
Create StubInits.size() stubs with the given names, target addresses, and flags.
StringMap< std::pair< ExecutorAddr, JITSymbolFlags > > StubInitsMap
Map type for initializing the manager. See init.
Represents a JIT'd dynamic library.
Definition: Core.h:989
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:1008
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
ExecutorAddr reportCallThroughError(Error Err)
Expected< ReexportsEntry > findReexport(ExecutorAddr TrampolineAddr)
Error notifyResolved(ExecutorAddr TrampolineAddr, ExecutorAddr ResolvedAddr)
void resolveTrampolineLandingAddress(ExecutorAddr TrampolineAddr, TrampolinePool::NotifyLandingResolvedFunction NotifyLandingResolved)
LazyCallThroughManager(ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr, TrampolinePool *TP)
Expected< ExecutorAddr > getCallThroughTrampoline(JITDylib &SourceJD, SymbolStringPtr SymbolName, NotifyResolvedFunction NotifyResolved)
StringRef getName() const override
Return the name of this materialization unit.
LazyReexportsMaterializationUnit(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc)
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:693
SymbolFlagsMap SymbolFlags
Definition: Core.h:749
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:183
Pointer to a pooled string representing a symbol name.
Base class for pools of compiler re-entry trampolines.
Expected< ExecutorAddr > getTrampoline()
Get an available trampoline address.
unique_function is a type-erasing functor similar to std::function.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:166
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
Definition: Core.h:396
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:121
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
Definition: Core.h:124
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session.
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr)
Define lazy-reexports based on the given SymbolAliasMap.
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition: Core.cpp:37
@ Ready
Emitted to memory, but waiting on transitive dependencies.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
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:1856
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858