1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
namespace {
using namespace llvm;
using namespace llvm::orc;
class VSOSearchOrderResolver : public JITSymbolResolver {
public:
VSOSearchOrderResolver(ExecutionSession &ES,
MaterializationResponsibility &MR)
: ES(ES), MR(MR) {}
Expected<LookupResult> lookup(const LookupSet &Symbols) {
SymbolNameSet InternedSymbols;
for (auto &S : Symbols)
InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
auto AsyncLookup = [&](std::shared_ptr<AsynchronousSymbolQuery> Q,
SymbolNameSet Names) {
SymbolNameSet Unresolved = std::move(Names);
MR.getTargetVSO().withSearchOrderDo([&](const VSOList &SearchOrder) {
for (auto *V : SearchOrder) {
assert(V && "VSOList entry can not be null");
Unresolved = V->lookup(Q, std::move(Unresolved));
}
});
return Unresolved;
};
auto InternedResult = blockingLookup(
ES, std::move(AsyncLookup), std::move(InternedSymbols), false, &MR);
if (!InternedResult)
return InternedResult.takeError();
LookupResult Result;
for (auto &KV : *InternedResult)
Result[*KV.first] = std::move(KV.second);
return Result;
}
Expected<LookupFlagsResult> lookupFlags(const LookupSet &Symbols) {
SymbolNameSet InternedSymbols;
for (auto &S : Symbols)
InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
SymbolFlagsMap InternedResult;
MR.getTargetVSO().withSearchOrderDo([&](const VSOList &VSOs) {
// An empty search order is pathalogical, but allowed.
if (VSOs.empty())
return;
assert(VSOs.front() && "VSOList entry can not be null");
InternedResult = VSOs.front()->lookupFlags(InternedSymbols);
});
LookupFlagsResult Result;
for (auto &KV : InternedResult)
Result[*KV.first] = std::move(KV.second);
return Result;
}
private:
ExecutionSession &ES;
MaterializationResponsibility &MR;
};
} // end anonymous namespace
namespace llvm {
namespace orc {
RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2(
ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
NotifyLoadedFunction NotifyLoaded, NotifyFinalizedFunction NotifyFinalized)
: ObjectLayer(ES), GetMemoryManager(GetMemoryManager),
NotifyLoaded(std::move(NotifyLoaded)),
NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {}
void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R,
VModuleKey K,
std::unique_ptr<MemoryBuffer> O) {
assert(O && "Object must not be null");
auto &ES = getExecutionSession();
auto ObjFile = object::ObjectFile::createObjectFile(*O);
if (!ObjFile) {
getExecutionSession().reportError(ObjFile.takeError());
R.failMaterialization();
}
auto MemoryManager = GetMemoryManager(K);
VSOSearchOrderResolver Resolver(ES, R);
auto RTDyld = llvm::make_unique<RuntimeDyld>(*MemoryManager, Resolver);
RTDyld->setProcessAllSections(ProcessAllSections);
{
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
assert(!ActiveRTDylds.count(K) &&
"An active RTDyld already exists for this key?");
ActiveRTDylds[K] = RTDyld.get();
assert(!MemMgrs.count(K) &&
"A memory manager already exists for this key?");
MemMgrs[K] = std::move(MemoryManager);
}
auto Info = RTDyld->loadObject(**ObjFile);
{
std::set<StringRef> InternalSymbols;
for (auto &Sym : (*ObjFile)->symbols()) {
if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
if (auto SymName = Sym.getName())
InternalSymbols.insert(*SymName);
else {
ES.reportError(SymName.takeError());
R.failMaterialization();
return;
}
}
}
SymbolMap Symbols;
for (auto &KV : RTDyld->getSymbolTable())
if (!InternalSymbols.count(KV.first))
Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second;
R.resolve(Symbols);
}
if (NotifyLoaded)
NotifyLoaded(K, **ObjFile, *Info);
RTDyld->finalizeWithMemoryManagerLocking();
{
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
ActiveRTDylds.erase(K);
}
if (RTDyld->hasError()) {
ES.reportError(make_error<StringError>(RTDyld->getErrorString(),
inconvertibleErrorCode()));
R.failMaterialization();
return;
}
R.finalize();
if (NotifyFinalized)
NotifyFinalized(K);
}
void RTDyldObjectLinkingLayer2::mapSectionAddress(
VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const {
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
auto ActiveRTDyldItr = ActiveRTDylds.find(K);
assert(ActiveRTDyldItr != ActiveRTDylds.end() &&
"No active RTDyld instance found for key");
ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr);
}
} // End namespace orc.
} // End namespace llvm.
|