blob: db88ec8517bff47dbc002c2fab75ec8ee69ceb8d [file] [log] [blame]
Lang Hames11c8dfa52019-04-20 17:10:34 +00001//===------- ObjectLinkingLayer.cpp - JITLink 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
9#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
10
11#include "llvm/ADT/Optional.h"
Lang Hames1233c152019-04-22 03:03:09 +000012#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
Lang Hames11c8dfa52019-04-20 17:10:34 +000013
14#include <vector>
15
16#define DEBUG_TYPE "orc"
17
18using namespace llvm;
19using namespace llvm::jitlink;
20using namespace llvm::orc;
21
22namespace llvm {
23namespace orc {
24
25class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
26public:
27 ObjectLinkingLayerJITLinkContext(ObjectLinkingLayer &Layer,
28 MaterializationResponsibility MR,
29 std::unique_ptr<MemoryBuffer> ObjBuffer)
30 : Layer(Layer), MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {}
31
Lang Hamesa98546eb2019-10-15 21:41:12 +000032 ~ObjectLinkingLayerJITLinkContext() {
33 // If there is an object buffer return function then use it to
34 // return ownership of the buffer.
35 if (Layer.ReturnObjectBuffer)
36 Layer.ReturnObjectBuffer(std::move(ObjBuffer));
37 }
38
Lang Hamesc0143f32019-12-15 17:23:36 -080039 JITLinkMemoryManager &getMemoryManager() override { return *Layer.MemMgr; }
Lang Hames11c8dfa52019-04-20 17:10:34 +000040
41 MemoryBufferRef getObjectBuffer() const override {
42 return ObjBuffer->getMemBufferRef();
43 }
44
45 void notifyFailed(Error Err) override {
46 Layer.getExecutionSession().reportError(std::move(Err));
47 MR.failMaterialization();
48 }
49
Lang Hames674df132019-11-25 21:57:27 -080050 void lookup(const LookupMap &Symbols,
Lang Hames4e920e52019-10-04 03:55:26 +000051 std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override {
Lang Hames11c8dfa52019-04-20 17:10:34 +000052
Lang Hames674df132019-11-25 21:57:27 -080053 JITDylibSearchOrder SearchOrder;
Lang Hames11c8dfa52019-04-20 17:10:34 +000054 MR.getTargetJITDylib().withSearchOrderDo(
Lang Hames674df132019-11-25 21:57:27 -080055 [&](const JITDylibSearchOrder &O) { SearchOrder = O; });
Lang Hames11c8dfa52019-04-20 17:10:34 +000056
57 auto &ES = Layer.getExecutionSession();
58
Lang Hames674df132019-11-25 21:57:27 -080059 SymbolLookupSet LookupSet;
60 for (auto &KV : Symbols) {
61 orc::SymbolLookupFlags LookupFlags;
62 switch (KV.second) {
63 case jitlink::SymbolLookupFlags::RequiredSymbol:
64 LookupFlags = orc::SymbolLookupFlags::RequiredSymbol;
65 break;
66 case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol:
67 LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol;
68 break;
69 }
70 LookupSet.add(ES.intern(KV.first), LookupFlags);
71 }
Lang Hames11c8dfa52019-04-20 17:10:34 +000072
73 // OnResolve -- De-intern the symbols and pass the result to the linker.
Lang Hames4e920e52019-10-04 03:55:26 +000074 auto OnResolve = [this, LookupContinuation = std::move(LC)](
75 Expected<SymbolMap> Result) mutable {
Lang Hamesc48f1f62019-08-27 15:50:32 +000076 auto Main = Layer.getExecutionSession().intern("_main");
Lang Hames11c8dfa52019-04-20 17:10:34 +000077 if (!Result)
Lang Hames4e920e52019-10-04 03:55:26 +000078 LookupContinuation->run(Result.takeError());
Lang Hames11c8dfa52019-04-20 17:10:34 +000079 else {
80 AsyncLookupResult LR;
81 for (auto &KV : *Result)
82 LR[*KV.first] = KV.second;
Lang Hames4e920e52019-10-04 03:55:26 +000083 LookupContinuation->run(std::move(LR));
Lang Hames11c8dfa52019-04-20 17:10:34 +000084 }
85 };
86
Lang Hamesca6f5842020-02-11 09:02:22 -080087 for (auto &KV : InternalNamedSymbolDeps) {
88 SymbolDependenceMap InternalDeps;
89 InternalDeps[&MR.getTargetJITDylib()] = std::move(KV.second);
90 MR.addDependencies(KV.first, InternalDeps);
91 }
92
Lang Hames674df132019-11-25 21:57:27 -080093 ES.lookup(LookupKind::Static, SearchOrder, std::move(LookupSet),
94 SymbolState::Resolved, std::move(OnResolve),
95 [this](const SymbolDependenceMap &Deps) {
Lang Hamesd4a80892019-06-07 19:33:51 +000096 registerDependencies(Deps);
97 });
Lang Hames11c8dfa52019-04-20 17:10:34 +000098 }
99
Lang Hames4e920e52019-10-04 03:55:26 +0000100 void notifyResolved(LinkGraph &G) override {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000101 auto &ES = Layer.getExecutionSession();
102
103 SymbolFlagsMap ExtraSymbolsToClaim;
104 bool AutoClaim = Layer.AutoClaimObjectSymbols;
105
106 SymbolMap InternedResult;
Lang Hames4e920e52019-10-04 03:55:26 +0000107 for (auto *Sym : G.defined_symbols())
108 if (Sym->hasName() && Sym->getScope() != Scope::Local) {
109 auto InternedName = ES.intern(Sym->getName());
Lang Hames11c8dfa52019-04-20 17:10:34 +0000110 JITSymbolFlags Flags;
111
Lang Hames4e920e52019-10-04 03:55:26 +0000112 if (Sym->isCallable())
Lang Hames11c8dfa52019-04-20 17:10:34 +0000113 Flags |= JITSymbolFlags::Callable;
Lang Hames4e920e52019-10-04 03:55:26 +0000114 if (Sym->getScope() == Scope::Default)
115 Flags |= JITSymbolFlags::Exported;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000116
117 InternedResult[InternedName] =
Lang Hames4e920e52019-10-04 03:55:26 +0000118 JITEvaluatedSymbol(Sym->getAddress(), Flags);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000119 if (AutoClaim && !MR.getSymbols().count(InternedName)) {
120 assert(!ExtraSymbolsToClaim.count(InternedName) &&
121 "Duplicate symbol to claim?");
122 ExtraSymbolsToClaim[InternedName] = Flags;
123 }
124 }
125
Lang Hames4e920e52019-10-04 03:55:26 +0000126 for (auto *Sym : G.absolute_symbols())
127 if (Sym->hasName()) {
128 auto InternedName = ES.intern(Sym->getName());
Lang Hames11c8dfa52019-04-20 17:10:34 +0000129 JITSymbolFlags Flags;
130 Flags |= JITSymbolFlags::Absolute;
Lang Hames4e920e52019-10-04 03:55:26 +0000131 if (Sym->isCallable())
Lang Hames11c8dfa52019-04-20 17:10:34 +0000132 Flags |= JITSymbolFlags::Callable;
Lang Hames4e920e52019-10-04 03:55:26 +0000133 if (Sym->getLinkage() == Linkage::Weak)
134 Flags |= JITSymbolFlags::Weak;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000135 InternedResult[InternedName] =
Lang Hames4e920e52019-10-04 03:55:26 +0000136 JITEvaluatedSymbol(Sym->getAddress(), Flags);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000137 if (AutoClaim && !MR.getSymbols().count(InternedName)) {
138 assert(!ExtraSymbolsToClaim.count(InternedName) &&
139 "Duplicate symbol to claim?");
140 ExtraSymbolsToClaim[InternedName] = Flags;
141 }
142 }
143
144 if (!ExtraSymbolsToClaim.empty())
145 if (auto Err = MR.defineMaterializing(ExtraSymbolsToClaim))
146 return notifyFailed(std::move(Err));
Lang Hames85fb9972019-12-16 02:50:40 -0800147
148 if (const auto &InitSym = MR.getInitializerSymbol())
149 InternedResult[InitSym] = JITEvaluatedSymbol();
150
Lang Hamese00585c2019-08-23 20:37:31 +0000151 if (auto Err = MR.notifyResolved(InternedResult)) {
152 Layer.getExecutionSession().reportError(std::move(Err));
153 MR.failMaterialization();
154 return;
155 }
Lang Hamesa9fdf372019-04-26 22:58:39 +0000156 Layer.notifyLoaded(MR);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000157 }
158
159 void notifyFinalized(
160 std::unique_ptr<JITLinkMemoryManager::Allocation> A) override {
Lang Hamesa9fdf372019-04-26 22:58:39 +0000161 if (auto Err = Layer.notifyEmitted(MR, std::move(A))) {
162 Layer.getExecutionSession().reportError(std::move(Err));
163 MR.failMaterialization();
Lang Hamesa9fdf372019-04-26 22:58:39 +0000164 return;
165 }
Lang Hamese00585c2019-08-23 20:37:31 +0000166 if (auto Err = MR.notifyEmitted()) {
167 Layer.getExecutionSession().reportError(std::move(Err));
168 MR.failMaterialization();
169 }
Lang Hames11c8dfa52019-04-20 17:10:34 +0000170 }
171
Lang Hames4e920e52019-10-04 03:55:26 +0000172 LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override {
173 return [this](LinkGraph &G) { return markResponsibilitySymbolsLive(G); };
Lang Hames11c8dfa52019-04-20 17:10:34 +0000174 }
175
176 Error modifyPassConfig(const Triple &TT, PassConfiguration &Config) override {
177 // Add passes to mark duplicate defs as should-discard, and to walk the
Lang Hames4e920e52019-10-04 03:55:26 +0000178 // link graph to build the symbol dependence graph.
Lang Hames11c8dfa52019-04-20 17:10:34 +0000179 Config.PrePrunePasses.push_back(
Lang Hames4e920e52019-10-04 03:55:26 +0000180 [this](LinkGraph &G) { return externalizeWeakAndCommonSymbols(G); });
Lang Hames11c8dfa52019-04-20 17:10:34 +0000181
Lang Hamesa9fdf372019-04-26 22:58:39 +0000182 Layer.modifyPassConfig(MR, TT, Config);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000183
Lang Hamesca6f5842020-02-11 09:02:22 -0800184 Config.PostPrunePasses.push_back(
185 [this](LinkGraph &G) { return computeNamedSymbolDependencies(G); });
186
Lang Hames11c8dfa52019-04-20 17:10:34 +0000187 return Error::success();
188 }
189
190private:
Lang Hames85fb9972019-12-16 02:50:40 -0800191 struct LocalSymbolNamedDependencies {
192 SymbolNameSet Internal, External;
193 };
194
195 using LocalSymbolNamedDependenciesMap =
196 DenseMap<const Symbol *, LocalSymbolNamedDependencies>;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000197
Lang Hames4e920e52019-10-04 03:55:26 +0000198 Error externalizeWeakAndCommonSymbols(LinkGraph &G) {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000199 auto &ES = Layer.getExecutionSession();
Lang Hames4e920e52019-10-04 03:55:26 +0000200 for (auto *Sym : G.defined_symbols())
201 if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak) {
202 if (!MR.getSymbols().count(ES.intern(Sym->getName())))
203 G.makeExternal(*Sym);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000204 }
205
Lang Hames4e920e52019-10-04 03:55:26 +0000206 for (auto *Sym : G.absolute_symbols())
207 if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak) {
208 if (!MR.getSymbols().count(ES.intern(Sym->getName())))
209 G.makeExternal(*Sym);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000210 }
211
212 return Error::success();
213 }
214
Lang Hames4e920e52019-10-04 03:55:26 +0000215 Error markResponsibilitySymbolsLive(LinkGraph &G) const {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000216 auto &ES = Layer.getExecutionSession();
Lang Hames4e920e52019-10-04 03:55:26 +0000217 for (auto *Sym : G.defined_symbols())
218 if (Sym->hasName() && MR.getSymbols().count(ES.intern(Sym->getName())))
219 Sym->setLive(true);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000220 return Error::success();
221 }
222
Lang Hames4e920e52019-10-04 03:55:26 +0000223 Error computeNamedSymbolDependencies(LinkGraph &G) {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000224 auto &ES = MR.getTargetJITDylib().getExecutionSession();
Lang Hamesca6f5842020-02-11 09:02:22 -0800225 auto LocalDeps = computeLocalDeps(G);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000226
Lang Hames85fb9972019-12-16 02:50:40 -0800227 // Compute dependencies for symbols defined in the JITLink graph.
Lang Hames4e920e52019-10-04 03:55:26 +0000228 for (auto *Sym : G.defined_symbols()) {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000229
Lang Hamesca6f5842020-02-11 09:02:22 -0800230 // Skip local symbols: we do not track dependencies for these.
Lang Hames4e920e52019-10-04 03:55:26 +0000231 if (Sym->getScope() == Scope::Local)
Lang Hames11c8dfa52019-04-20 17:10:34 +0000232 continue;
Lang Hamesca6f5842020-02-11 09:02:22 -0800233 assert(Sym->hasName() &&
234 "Defined non-local jitlink::Symbol should have a name");
Lang Hames11c8dfa52019-04-20 17:10:34 +0000235
Lang Hamesca6f5842020-02-11 09:02:22 -0800236 SymbolNameSet ExternalSymDeps, InternalSymDeps;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000237
Lang Hamesca6f5842020-02-11 09:02:22 -0800238 // Find internal and external named symbol dependencies.
Lang Hames4e920e52019-10-04 03:55:26 +0000239 for (auto &E : Sym->getBlock().edges()) {
240 auto &TargetSym = E.getTarget();
Lang Hames11c8dfa52019-04-20 17:10:34 +0000241
Lang Hamesca6f5842020-02-11 09:02:22 -0800242 if (TargetSym.getScope() != Scope::Local) {
243 if (TargetSym.isExternal())
244 ExternalSymDeps.insert(ES.intern(TargetSym.getName()));
245 else if (&TargetSym != Sym)
246 InternalSymDeps.insert(ES.intern(TargetSym.getName()));
247 } else {
Lang Hames4e920e52019-10-04 03:55:26 +0000248 assert(TargetSym.isDefined() &&
Lang Hamesca6f5842020-02-11 09:02:22 -0800249 "local symbols must be defined");
250 auto I = LocalDeps.find(&TargetSym);
Lang Hames85fb9972019-12-16 02:50:40 -0800251 if (I != LocalDeps.end()) {
252 for (auto &S : I->second.External)
253 ExternalSymDeps.insert(S);
254 for (auto &S : I->second.Internal)
255 InternalSymDeps.insert(S);
256 }
Lang Hames11c8dfa52019-04-20 17:10:34 +0000257 }
258 }
Lang Hamesca6f5842020-02-11 09:02:22 -0800259
260 if (ExternalSymDeps.empty() && InternalSymDeps.empty())
261 continue;
262
263 auto SymName = ES.intern(Sym->getName());
264 if (!ExternalSymDeps.empty())
265 ExternalNamedSymbolDeps[SymName] = std::move(ExternalSymDeps);
266 if (!InternalSymDeps.empty())
267 InternalNamedSymbolDeps[SymName] = std::move(InternalSymDeps);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000268 }
269
Lang Hames85fb9972019-12-16 02:50:40 -0800270 for (auto &P : Layer.Plugins) {
271 auto SyntheticLocalDeps = P->getSyntheticSymbolLocalDependencies(MR);
272 if (SyntheticLocalDeps.empty())
273 continue;
274
275 for (auto &KV : SyntheticLocalDeps) {
276 auto &Name = KV.first;
277 auto &LocalDepsForName = KV.second;
278 for (auto *Local : LocalDepsForName) {
279 assert(Local->getScope() == Scope::Local &&
280 "Dependence on non-local symbol");
281 auto LocalNamedDepsItr = LocalDeps.find(Local);
282 if (LocalNamedDepsItr == LocalDeps.end())
283 continue;
284 for (auto &S : LocalNamedDepsItr->second.Internal)
285 InternalNamedSymbolDeps[Name].insert(S);
286 for (auto &S : LocalNamedDepsItr->second.External)
287 ExternalNamedSymbolDeps[Name].insert(S);
288 }
289 }
290 }
291
Lang Hames11c8dfa52019-04-20 17:10:34 +0000292 return Error::success();
293 }
294
Lang Hames85fb9972019-12-16 02:50:40 -0800295 LocalSymbolNamedDependenciesMap computeLocalDeps(LinkGraph &G) {
296 DenseMap<jitlink::Symbol *, DenseSet<jitlink::Symbol *>> DepMap;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000297
Lang Hamesca6f5842020-02-11 09:02:22 -0800298 // For all local symbols:
Lang Hames11c8dfa52019-04-20 17:10:34 +0000299 // (1) Add their named dependencies.
300 // (2) Add them to the worklist for further iteration if they have any
Lang Hamesca6f5842020-02-11 09:02:22 -0800301 // depend on any other local symbols.
Lang Hames11c8dfa52019-04-20 17:10:34 +0000302 struct WorklistEntry {
Lang Hamesca6f5842020-02-11 09:02:22 -0800303 WorklistEntry(Symbol *Sym, DenseSet<Symbol *> LocalDeps)
304 : Sym(Sym), LocalDeps(std::move(LocalDeps)) {}
Lang Hames11c8dfa52019-04-20 17:10:34 +0000305
Lang Hames4e920e52019-10-04 03:55:26 +0000306 Symbol *Sym = nullptr;
Lang Hamesca6f5842020-02-11 09:02:22 -0800307 DenseSet<Symbol *> LocalDeps;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000308 };
309 std::vector<WorklistEntry> Worklist;
Lang Hames4e920e52019-10-04 03:55:26 +0000310 for (auto *Sym : G.defined_symbols())
Lang Hamesca6f5842020-02-11 09:02:22 -0800311 if (Sym->getScope() == Scope::Local) {
Lang Hames4e920e52019-10-04 03:55:26 +0000312 auto &SymNamedDeps = DepMap[Sym];
Lang Hamesca6f5842020-02-11 09:02:22 -0800313 DenseSet<Symbol *> LocalDeps;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000314
Lang Hames4e920e52019-10-04 03:55:26 +0000315 for (auto &E : Sym->getBlock().edges()) {
316 auto &TargetSym = E.getTarget();
Lang Hamesca6f5842020-02-11 09:02:22 -0800317 if (TargetSym.getScope() != Scope::Local)
318 SymNamedDeps.insert(&TargetSym);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000319 else {
Lang Hames4e920e52019-10-04 03:55:26 +0000320 assert(TargetSym.isDefined() &&
Lang Hamesca6f5842020-02-11 09:02:22 -0800321 "local symbols must be defined");
322 LocalDeps.insert(&TargetSym);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000323 }
324 }
325
Lang Hamesca6f5842020-02-11 09:02:22 -0800326 if (!LocalDeps.empty())
327 Worklist.push_back(WorklistEntry(Sym, std::move(LocalDeps)));
Lang Hames11c8dfa52019-04-20 17:10:34 +0000328 }
329
Lang Hamesca6f5842020-02-11 09:02:22 -0800330 // Loop over all local symbols with local dependencies, propagating
331 // their respective non-local dependencies. Iterate until we hit a stable
Lang Hames11c8dfa52019-04-20 17:10:34 +0000332 // state.
333 bool Changed;
334 do {
335 Changed = false;
336 for (auto &WLEntry : Worklist) {
Lang Hames4e920e52019-10-04 03:55:26 +0000337 auto *Sym = WLEntry.Sym;
Lang Hamesca6f5842020-02-11 09:02:22 -0800338 auto &NamedDeps = DepMap[Sym];
339 auto &LocalDeps = WLEntry.LocalDeps;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000340
Lang Hamesca6f5842020-02-11 09:02:22 -0800341 for (auto *TargetSym : LocalDeps) {
Lang Hames4e920e52019-10-04 03:55:26 +0000342 auto I = DepMap.find(TargetSym);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000343 if (I != DepMap.end())
344 for (const auto &S : I->second)
Lang Hamesca6f5842020-02-11 09:02:22 -0800345 Changed |= NamedDeps.insert(S).second;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000346 }
347 }
348 } while (Changed);
349
Lang Hames85fb9972019-12-16 02:50:40 -0800350 // Intern the results to produce a mapping of jitlink::Symbol* to internal
351 // and external symbol names.
352 auto &ES = Layer.getExecutionSession();
353 LocalSymbolNamedDependenciesMap Result;
354 for (auto &KV : DepMap) {
355 auto *Local = KV.first;
356 assert(Local->getScope() == Scope::Local &&
357 "DepMap keys should all be local symbols");
358 auto &LocalNamedDeps = Result[Local];
359 for (auto *Named : KV.second) {
360 assert(Named->getScope() != Scope::Local &&
361 "DepMap values should all be non-local symbol sets");
362 if (Named->isExternal())
363 LocalNamedDeps.External.insert(ES.intern(Named->getName()));
364 else
365 LocalNamedDeps.Internal.insert(ES.intern(Named->getName()));
366 }
367 }
368
369 return Result;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000370 }
371
372 void registerDependencies(const SymbolDependenceMap &QueryDeps) {
Lang Hamesca6f5842020-02-11 09:02:22 -0800373 for (auto &NamedDepsEntry : ExternalNamedSymbolDeps) {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000374 auto &Name = NamedDepsEntry.first;
375 auto &NameDeps = NamedDepsEntry.second;
376 SymbolDependenceMap SymbolDeps;
377
378 for (const auto &QueryDepsEntry : QueryDeps) {
379 JITDylib &SourceJD = *QueryDepsEntry.first;
380 const SymbolNameSet &Symbols = QueryDepsEntry.second;
381 auto &DepsForJD = SymbolDeps[&SourceJD];
382
383 for (const auto &S : Symbols)
384 if (NameDeps.count(S))
385 DepsForJD.insert(S);
386
387 if (DepsForJD.empty())
388 SymbolDeps.erase(&SourceJD);
389 }
390
391 MR.addDependencies(Name, SymbolDeps);
392 }
393 }
394
395 ObjectLinkingLayer &Layer;
396 MaterializationResponsibility MR;
397 std::unique_ptr<MemoryBuffer> ObjBuffer;
Lang Hamesca6f5842020-02-11 09:02:22 -0800398 DenseMap<SymbolStringPtr, SymbolNameSet> ExternalNamedSymbolDeps;
399 DenseMap<SymbolStringPtr, SymbolNameSet> InternalNamedSymbolDeps;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000400};
401
Lang Hamesa9fdf372019-04-26 22:58:39 +0000402ObjectLinkingLayer::Plugin::~Plugin() {}
403
Lang Hamesc0143f32019-12-15 17:23:36 -0800404ObjectLinkingLayer::ObjectLinkingLayer(
405 ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
406 : ObjectLayer(ES), MemMgr(std::move(MemMgr)) {}
Lang Hamesa9fdf372019-04-26 22:58:39 +0000407
408ObjectLinkingLayer::~ObjectLinkingLayer() {
409 if (auto Err = removeAllModules())
410 getExecutionSession().reportError(std::move(Err));
411}
Lang Hames11c8dfa52019-04-20 17:10:34 +0000412
413void ObjectLinkingLayer::emit(MaterializationResponsibility R,
414 std::unique_ptr<MemoryBuffer> O) {
415 assert(O && "Object must not be null");
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000416 jitLink(std::make_unique<ObjectLinkingLayerJITLinkContext>(
Lang Hames11c8dfa52019-04-20 17:10:34 +0000417 *this, std::move(R), std::move(O)));
418}
419
Lang Hamesa9fdf372019-04-26 22:58:39 +0000420void ObjectLinkingLayer::modifyPassConfig(MaterializationResponsibility &MR,
421 const Triple &TT,
422 PassConfiguration &PassConfig) {
423 for (auto &P : Plugins)
424 P->modifyPassConfig(MR, TT, PassConfig);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000425}
426
Lang Hamesa9fdf372019-04-26 22:58:39 +0000427void ObjectLinkingLayer::notifyLoaded(MaterializationResponsibility &MR) {
428 for (auto &P : Plugins)
429 P->notifyLoaded(MR);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000430}
431
Lang Hamesa9fdf372019-04-26 22:58:39 +0000432Error ObjectLinkingLayer::notifyEmitted(MaterializationResponsibility &MR,
433 AllocPtr Alloc) {
434 Error Err = Error::success();
435 for (auto &P : Plugins)
436 Err = joinErrors(std::move(Err), P->notifyEmitted(MR));
Lang Hames11c8dfa52019-04-20 17:10:34 +0000437
Lang Hamesa9fdf372019-04-26 22:58:39 +0000438 if (Err)
439 return Err;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000440
Lang Hamesa9fdf372019-04-26 22:58:39 +0000441 {
442 std::lock_guard<std::mutex> Lock(LayerMutex);
443 UntrackedAllocs.push_back(std::move(Alloc));
444 }
Lang Hames11c8dfa52019-04-20 17:10:34 +0000445
Lang Hamesa9fdf372019-04-26 22:58:39 +0000446 return Error::success();
447}
448
449Error ObjectLinkingLayer::removeModule(VModuleKey K) {
450 Error Err = Error::success();
451
452 for (auto &P : Plugins)
453 Err = joinErrors(std::move(Err), P->notifyRemovingModule(K));
454
455 AllocPtr Alloc;
456
457 {
458 std::lock_guard<std::mutex> Lock(LayerMutex);
459 auto AllocItr = TrackedAllocs.find(K);
460 Alloc = std::move(AllocItr->second);
461 TrackedAllocs.erase(AllocItr);
462 }
463
464 assert(Alloc && "No allocation for key K");
465
466 return joinErrors(std::move(Err), Alloc->deallocate());
467}
468
469Error ObjectLinkingLayer::removeAllModules() {
470
471 Error Err = Error::success();
472
473 for (auto &P : Plugins)
474 Err = joinErrors(std::move(Err), P->notifyRemovingAllModules());
475
476 std::vector<AllocPtr> Allocs;
477 {
478 std::lock_guard<std::mutex> Lock(LayerMutex);
479 Allocs = std::move(UntrackedAllocs);
480
481 for (auto &KV : TrackedAllocs)
482 Allocs.push_back(std::move(KV.second));
483
484 TrackedAllocs.clear();
485 }
486
487 while (!Allocs.empty()) {
488 Err = joinErrors(std::move(Err), Allocs.back()->deallocate());
489 Allocs.pop_back();
490 }
491
492 return Err;
493}
494
Lang Hamesf5a885f2019-07-04 00:05:12 +0000495EHFrameRegistrationPlugin::EHFrameRegistrationPlugin(
Lang Hames4e920e52019-10-04 03:55:26 +0000496 EHFrameRegistrar &Registrar)
Lang Hamesf5a885f2019-07-04 00:05:12 +0000497 : Registrar(Registrar) {}
498
499void EHFrameRegistrationPlugin::modifyPassConfig(
Lang Hamesa9fdf372019-04-26 22:58:39 +0000500 MaterializationResponsibility &MR, const Triple &TT,
501 PassConfiguration &PassConfig) {
502 assert(!InProcessLinks.count(&MR) && "Link for MR already being tracked?");
503
504 PassConfig.PostFixupPasses.push_back(
Lang Hamesc48f1f62019-08-27 15:50:32 +0000505 createEHFrameRecorderPass(TT, [this, &MR](JITTargetAddress Addr,
506 size_t Size) {
Lang Hamesa9fdf372019-04-26 22:58:39 +0000507 if (Addr)
Lang Hamesc48f1f62019-08-27 15:50:32 +0000508 InProcessLinks[&MR] = { Addr, Size };
Lang Hamesa9fdf372019-04-26 22:58:39 +0000509 }));
510}
511
Lang Hamesf5a885f2019-07-04 00:05:12 +0000512Error EHFrameRegistrationPlugin::notifyEmitted(
Lang Hamesa9fdf372019-04-26 22:58:39 +0000513 MaterializationResponsibility &MR) {
514
Lang Hamesc48f1f62019-08-27 15:50:32 +0000515 auto EHFrameRangeItr = InProcessLinks.find(&MR);
516 if (EHFrameRangeItr == InProcessLinks.end())
Lang Hamesa9fdf372019-04-26 22:58:39 +0000517 return Error::success();
518
Lang Hamesc48f1f62019-08-27 15:50:32 +0000519 auto EHFrameRange = EHFrameRangeItr->second;
520 assert(EHFrameRange.Addr &&
521 "eh-frame addr to register can not be null");
Lang Hamesa9fdf372019-04-26 22:58:39 +0000522
Lang Hamesc48f1f62019-08-27 15:50:32 +0000523 InProcessLinks.erase(EHFrameRangeItr);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000524 if (auto Key = MR.getVModuleKey())
Lang Hamesc48f1f62019-08-27 15:50:32 +0000525 TrackedEHFrameRanges[Key] = EHFrameRange;
Lang Hamesa9fdf372019-04-26 22:58:39 +0000526 else
Lang Hamesc48f1f62019-08-27 15:50:32 +0000527 UntrackedEHFrameRanges.push_back(EHFrameRange);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000528
Lang Hamesc48f1f62019-08-27 15:50:32 +0000529 return Registrar.registerEHFrames(EHFrameRange.Addr, EHFrameRange.Size);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000530}
531
Lang Hamesf5a885f2019-07-04 00:05:12 +0000532Error EHFrameRegistrationPlugin::notifyRemovingModule(VModuleKey K) {
Lang Hamesc48f1f62019-08-27 15:50:32 +0000533 auto EHFrameRangeItr = TrackedEHFrameRanges.find(K);
534 if (EHFrameRangeItr == TrackedEHFrameRanges.end())
Lang Hamesa9fdf372019-04-26 22:58:39 +0000535 return Error::success();
536
Lang Hamesc48f1f62019-08-27 15:50:32 +0000537 auto EHFrameRange = EHFrameRangeItr->second;
538 assert(EHFrameRange.Addr && "Tracked eh-frame range must not be null");
Lang Hamesa9fdf372019-04-26 22:58:39 +0000539
Lang Hamesc48f1f62019-08-27 15:50:32 +0000540 TrackedEHFrameRanges.erase(EHFrameRangeItr);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000541
Lang Hamesc48f1f62019-08-27 15:50:32 +0000542 return Registrar.deregisterEHFrames(EHFrameRange.Addr, EHFrameRange.Size);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000543}
544
Lang Hamesf5a885f2019-07-04 00:05:12 +0000545Error EHFrameRegistrationPlugin::notifyRemovingAllModules() {
Lang Hamesa9fdf372019-04-26 22:58:39 +0000546
Lang Hamesc48f1f62019-08-27 15:50:32 +0000547 std::vector<EHFrameRange> EHFrameRanges =
548 std::move(UntrackedEHFrameRanges);
549 EHFrameRanges.reserve(EHFrameRanges.size() + TrackedEHFrameRanges.size());
Lang Hamesa9fdf372019-04-26 22:58:39 +0000550
Lang Hamesc48f1f62019-08-27 15:50:32 +0000551 for (auto &KV : TrackedEHFrameRanges)
552 EHFrameRanges.push_back(KV.second);
Lang Hamesa9fdf372019-04-26 22:58:39 +0000553
Lang Hamesc48f1f62019-08-27 15:50:32 +0000554 TrackedEHFrameRanges.clear();
Lang Hamesa9fdf372019-04-26 22:58:39 +0000555
556 Error Err = Error::success();
557
Lang Hamesc48f1f62019-08-27 15:50:32 +0000558 while (!EHFrameRanges.empty()) {
559 auto EHFrameRange = EHFrameRanges.back();
560 assert(EHFrameRange.Addr && "Untracked eh-frame range must not be null");
561 EHFrameRanges.pop_back();
562 Err = joinErrors(std::move(Err),
563 Registrar.deregisterEHFrames(EHFrameRange.Addr,
564 EHFrameRange.Size));
Lang Hamesa9fdf372019-04-26 22:58:39 +0000565 }
566
567 return Err;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000568}
569
570} // End namespace orc.
571} // End namespace llvm.