1 //===---------- LazyReexports.cpp - Utilities for lazy reexports ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/LazyReexports.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
14 #define DEBUG_TYPE "orc"
19 void LazyCallThroughManager::NotifyResolvedFunction::anchor() {}
21 LazyCallThroughManager::LazyCallThroughManager(
22 ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr,
23 std::unique_ptr<TrampolinePool> TP)
24 : ES(ES), ErrorHandlerAddr(ErrorHandlerAddr), TP(std::move(TP)) {}
26 Expected<JITTargetAddress> LazyCallThroughManager::getCallThroughTrampoline(
27 JITDylib &SourceJD, SymbolStringPtr SymbolName,
28 std::shared_ptr<NotifyResolvedFunction> NotifyResolved) {
29 std::lock_guard<std::mutex> Lock(LCTMMutex);
30 auto Trampoline = TP->getTrampoline();
33 return Trampoline.takeError();
35 Reexports[*Trampoline] = std::make_pair(&SourceJD, std::move(SymbolName));
36 Notifiers[*Trampoline] = std::move(NotifyResolved);
41 LazyCallThroughManager::callThroughToSymbol(JITTargetAddress TrampolineAddr) {
42 JITDylib *SourceJD = nullptr;
43 SymbolStringPtr SymbolName;
46 std::lock_guard<std::mutex> Lock(LCTMMutex);
47 auto I = Reexports.find(TrampolineAddr);
48 if (I == Reexports.end())
49 return ErrorHandlerAddr;
50 SourceJD = I->second.first;
51 SymbolName = I->second.second;
55 ES.lookup(JITDylibSearchList({{SourceJD, true}}), SymbolName);
58 ES.reportError(LookupResult.takeError());
59 return ErrorHandlerAddr;
62 auto ResolvedAddr = LookupResult->getAddress();
64 std::shared_ptr<NotifyResolvedFunction> NotifyResolved = nullptr;
66 std::lock_guard<std::mutex> Lock(LCTMMutex);
67 auto I = Notifiers.find(TrampolineAddr);
68 if (I != Notifiers.end()) {
69 NotifyResolved = I->second;
75 if (auto Err = (*NotifyResolved)(*SourceJD, SymbolName, ResolvedAddr)) {
76 ES.reportError(std::move(Err));
77 return ErrorHandlerAddr;
84 Expected<std::unique_ptr<LazyCallThroughManager>>
85 createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES,
86 JITTargetAddress ErrorHandlerAddr) {
87 switch (T.getArch()) {
89 return make_error<StringError>(
90 std::string("No callback manager available for ") + T.str(),
91 inconvertibleErrorCode());
94 return LocalLazyCallThroughManager::Create<OrcAArch64>(ES,
98 return LocalLazyCallThroughManager::Create<OrcI386>(ES, ErrorHandlerAddr);
101 return LocalLazyCallThroughManager::Create<OrcMips32Be>(ES,
105 return LocalLazyCallThroughManager::Create<OrcMips32Le>(ES,
109 case Triple::mips64el:
110 return LocalLazyCallThroughManager::Create<OrcMips64>(ES, ErrorHandlerAddr);
113 if (T.getOS() == Triple::OSType::Win32)
114 return LocalLazyCallThroughManager::Create<OrcX86_64_Win32>(
115 ES, ErrorHandlerAddr);
117 return LocalLazyCallThroughManager::Create<OrcX86_64_SysV>(
118 ES, ErrorHandlerAddr);
122 LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit(
123 LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager,
124 JITDylib &SourceJD, SymbolAliasMap CallableAliases, VModuleKey K)
125 : MaterializationUnit(extractFlags(CallableAliases), std::move(K)),
126 LCTManager(LCTManager), ISManager(ISManager), SourceJD(SourceJD),
127 CallableAliases(std::move(CallableAliases)),
128 NotifyResolved(LazyCallThroughManager::createNotifyResolvedFunction(
129 [&ISManager](JITDylib &JD, const SymbolStringPtr &SymbolName,
130 JITTargetAddress ResolvedAddr) {
131 return ISManager.updatePointer(*SymbolName, ResolvedAddr);
134 StringRef LazyReexportsMaterializationUnit::getName() const {
135 return "<Lazy Reexports>";
138 void LazyReexportsMaterializationUnit::materialize(
139 MaterializationResponsibility R) {
140 auto RequestedSymbols = R.getRequestedSymbols();
142 SymbolAliasMap RequestedAliases;
143 for (auto &RequestedSymbol : RequestedSymbols) {
144 auto I = CallableAliases.find(RequestedSymbol);
145 assert(I != CallableAliases.end() && "Symbol not found in alias map?");
146 RequestedAliases[I->first] = std::move(I->second);
147 CallableAliases.erase(I);
150 if (!CallableAliases.empty())
151 R.replace(lazyReexports(LCTManager, ISManager, SourceJD,
152 std::move(CallableAliases)));
154 IndirectStubsManager::StubInitsMap StubInits;
155 for (auto &Alias : RequestedAliases) {
157 auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline(
158 SourceJD, Alias.second.Aliasee, NotifyResolved);
160 if (!CallThroughTrampoline) {
161 SourceJD.getExecutionSession().reportError(
162 CallThroughTrampoline.takeError());
163 R.failMaterialization();
167 StubInits[*Alias.first] =
168 std::make_pair(*CallThroughTrampoline, Alias.second.AliasFlags);
171 if (auto Err = ISManager.createStubs(StubInits)) {
172 SourceJD.getExecutionSession().reportError(std::move(Err));
173 R.failMaterialization();
178 for (auto &Alias : RequestedAliases)
179 Stubs[Alias.first] = ISManager.findStub(*Alias.first, false);
181 R.notifyResolved(Stubs);
185 void LazyReexportsMaterializationUnit::discard(const JITDylib &JD,
186 const SymbolStringPtr &Name) {
187 assert(CallableAliases.count(Name) &&
188 "Symbol not covered by this MaterializationUnit");
189 CallableAliases.erase(Name);
193 LazyReexportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) {
194 SymbolFlagsMap SymbolFlags;
195 for (auto &KV : Aliases) {
196 assert(KV.second.AliasFlags.isCallable() &&
197 "Lazy re-exports must be callable symbols");
198 SymbolFlags[KV.first] = KV.second.AliasFlags;
203 } // End namespace orc.
204 } // End namespace llvm.