xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp (revision f331ccca26733857fef4dd3272db6aaeed7907b9)
1 //===------- 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 #include "llvm/ADT/Optional.h"
11 #include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
12 #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include <string>
15 #include <vector>
16 
17 #define DEBUG_TYPE "orc"
18 
19 using namespace llvm;
20 using namespace llvm::jitlink;
21 using namespace llvm::orc;
22 
23 namespace {
24 
25 class LinkGraphMaterializationUnit : public MaterializationUnit {
26 public:
27   static std::unique_ptr<LinkGraphMaterializationUnit>
28   Create(ObjectLinkingLayer &ObjLinkingLayer, std::unique_ptr<LinkGraph> G) {
29     auto LGI = scanLinkGraph(ObjLinkingLayer.getExecutionSession(), *G);
30     return std::unique_ptr<LinkGraphMaterializationUnit>(
31         new LinkGraphMaterializationUnit(ObjLinkingLayer, std::move(G),
32                                          std::move(LGI)));
33   }
34 
35   StringRef getName() const override { return G->getName(); }
36   void materialize(std::unique_ptr<MaterializationResponsibility> MR) override {
37     ObjLinkingLayer.emit(std::move(MR), std::move(G));
38   }
39 
40 private:
41   static Interface scanLinkGraph(ExecutionSession &ES, LinkGraph &G) {
42 
43     Interface LGI;
44 
45     for (auto *Sym : G.defined_symbols()) {
46       // Skip local symbols.
47       if (Sym->getScope() == Scope::Local)
48         continue;
49       assert(Sym->hasName() && "Anonymous non-local symbol?");
50 
51       JITSymbolFlags Flags;
52       if (Sym->getScope() == Scope::Default)
53         Flags |= JITSymbolFlags::Exported;
54 
55       if (Sym->isCallable())
56         Flags |= JITSymbolFlags::Callable;
57 
58       LGI.SymbolFlags[ES.intern(Sym->getName())] = Flags;
59     }
60 
61     if ((G.getTargetTriple().isOSBinFormatMachO() && hasMachOInitSection(G)) ||
62         (G.getTargetTriple().isOSBinFormatELF() && hasELFInitSection(G)))
63       LGI.InitSymbol = makeInitSymbol(ES, G);
64 
65     return LGI;
66   }
67 
68   static bool hasMachOInitSection(LinkGraph &G) {
69     for (auto &Sec : G.sections())
70       if (Sec.getName() == "__DATA,__objc_selrefs" ||
71           Sec.getName() == "__DATA,__objc_classlist" ||
72           Sec.getName() == "__TEXT,__swift5_protos" ||
73           Sec.getName() == "__TEXT,__swift5_proto" ||
74           Sec.getName() == "__TEXT,__swift5_types" ||
75           Sec.getName() == "__DATA,__mod_init_func")
76         return true;
77     return false;
78   }
79 
80   static bool hasELFInitSection(LinkGraph &G) {
81     for (auto &Sec : G.sections()) {
82       auto SecName = Sec.getName();
83       if (SecName.consume_front(".init_array") &&
84           (SecName.empty() || SecName[0] == '.'))
85         return true;
86     }
87     return false;
88   }
89 
90   static SymbolStringPtr makeInitSymbol(ExecutionSession &ES, LinkGraph &G) {
91     std::string InitSymString;
92     raw_string_ostream(InitSymString)
93         << "$." << G.getName() << ".__inits" << Counter++;
94     return ES.intern(InitSymString);
95   }
96 
97   LinkGraphMaterializationUnit(ObjectLinkingLayer &ObjLinkingLayer,
98                                std::unique_ptr<LinkGraph> G, Interface LGI)
99       : MaterializationUnit(std::move(LGI)), ObjLinkingLayer(ObjLinkingLayer),
100         G(std::move(G)) {}
101 
102   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
103     for (auto *Sym : G->defined_symbols())
104       if (Sym->getName() == *Name) {
105         assert(Sym->getLinkage() == Linkage::Weak &&
106                "Discarding non-weak definition");
107         G->makeExternal(*Sym);
108         break;
109       }
110   }
111 
112   ObjectLinkingLayer &ObjLinkingLayer;
113   std::unique_ptr<LinkGraph> G;
114   static std::atomic<uint64_t> Counter;
115 };
116 
117 std::atomic<uint64_t> LinkGraphMaterializationUnit::Counter{0};
118 
119 } // end anonymous namespace
120 
121 namespace llvm {
122 namespace orc {
123 
124 class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
125 public:
126   ObjectLinkingLayerJITLinkContext(
127       ObjectLinkingLayer &Layer,
128       std::unique_ptr<MaterializationResponsibility> MR,
129       std::unique_ptr<MemoryBuffer> ObjBuffer)
130       : JITLinkContext(&MR->getTargetJITDylib()), Layer(Layer),
131         MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {}
132 
133   ~ObjectLinkingLayerJITLinkContext() {
134     // If there is an object buffer return function then use it to
135     // return ownership of the buffer.
136     if (Layer.ReturnObjectBuffer && ObjBuffer)
137       Layer.ReturnObjectBuffer(std::move(ObjBuffer));
138   }
139 
140   JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }
141 
142   void notifyMaterializing(LinkGraph &G) {
143     for (auto &P : Layer.Plugins)
144       P->notifyMaterializing(*MR, G, *this,
145                              ObjBuffer ? ObjBuffer->getMemBufferRef()
146                              : MemoryBufferRef());
147   }
148 
149   void notifyFailed(Error Err) override {
150     for (auto &P : Layer.Plugins)
151       Err = joinErrors(std::move(Err), P->notifyFailed(*MR));
152     Layer.getExecutionSession().reportError(std::move(Err));
153     MR->failMaterialization();
154   }
155 
156   void lookup(const LookupMap &Symbols,
157               std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override {
158 
159     JITDylibSearchOrder LinkOrder;
160     MR->getTargetJITDylib().withLinkOrderDo(
161         [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
162 
163     auto &ES = Layer.getExecutionSession();
164 
165     SymbolLookupSet LookupSet;
166     for (auto &KV : Symbols) {
167       orc::SymbolLookupFlags LookupFlags;
168       switch (KV.second) {
169       case jitlink::SymbolLookupFlags::RequiredSymbol:
170         LookupFlags = orc::SymbolLookupFlags::RequiredSymbol;
171         break;
172       case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol:
173         LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol;
174         break;
175       }
176       LookupSet.add(ES.intern(KV.first), LookupFlags);
177     }
178 
179     // OnResolve -- De-intern the symbols and pass the result to the linker.
180     auto OnResolve = [LookupContinuation =
181                           std::move(LC)](Expected<SymbolMap> Result) mutable {
182       if (!Result)
183         LookupContinuation->run(Result.takeError());
184       else {
185         AsyncLookupResult LR;
186         for (auto &KV : *Result)
187           LR[*KV.first] = KV.second;
188         LookupContinuation->run(std::move(LR));
189       }
190     };
191 
192     for (auto &KV : InternalNamedSymbolDeps) {
193       SymbolDependenceMap InternalDeps;
194       InternalDeps[&MR->getTargetJITDylib()] = std::move(KV.second);
195       MR->addDependencies(KV.first, InternalDeps);
196     }
197 
198     ES.lookup(LookupKind::Static, LinkOrder, std::move(LookupSet),
199               SymbolState::Resolved, std::move(OnResolve),
200               [this](const SymbolDependenceMap &Deps) {
201                 registerDependencies(Deps);
202               });
203   }
204 
205   Error notifyResolved(LinkGraph &G) override {
206     auto &ES = Layer.getExecutionSession();
207 
208     SymbolFlagsMap ExtraSymbolsToClaim;
209     bool AutoClaim = Layer.AutoClaimObjectSymbols;
210 
211     SymbolMap InternedResult;
212     for (auto *Sym : G.defined_symbols())
213       if (Sym->hasName() && Sym->getScope() != Scope::Local) {
214         auto InternedName = ES.intern(Sym->getName());
215         JITSymbolFlags Flags;
216 
217         if (Sym->isCallable())
218           Flags |= JITSymbolFlags::Callable;
219         if (Sym->getScope() == Scope::Default)
220           Flags |= JITSymbolFlags::Exported;
221 
222         InternedResult[InternedName] =
223             JITEvaluatedSymbol(Sym->getAddress().getValue(), Flags);
224         if (AutoClaim && !MR->getSymbols().count(InternedName)) {
225           assert(!ExtraSymbolsToClaim.count(InternedName) &&
226                  "Duplicate symbol to claim?");
227           ExtraSymbolsToClaim[InternedName] = Flags;
228         }
229       }
230 
231     for (auto *Sym : G.absolute_symbols())
232       if (Sym->hasName() && Sym->getScope() != Scope::Local) {
233         auto InternedName = ES.intern(Sym->getName());
234         JITSymbolFlags Flags;
235         if (Sym->isCallable())
236           Flags |= JITSymbolFlags::Callable;
237         if (Sym->getScope() == Scope::Default)
238           Flags |= JITSymbolFlags::Exported;
239         if (Sym->getLinkage() == Linkage::Weak)
240           Flags |= JITSymbolFlags::Weak;
241         InternedResult[InternedName] =
242             JITEvaluatedSymbol(Sym->getAddress().getValue(), Flags);
243         if (AutoClaim && !MR->getSymbols().count(InternedName)) {
244           assert(!ExtraSymbolsToClaim.count(InternedName) &&
245                  "Duplicate symbol to claim?");
246           ExtraSymbolsToClaim[InternedName] = Flags;
247         }
248       }
249 
250     if (!ExtraSymbolsToClaim.empty())
251       if (auto Err = MR->defineMaterializing(ExtraSymbolsToClaim))
252         return Err;
253 
254     {
255 
256       // Check that InternedResult matches up with MR->getSymbols(), overriding
257       // flags if requested.
258       // This guards against faulty transformations / compilers / object caches.
259 
260       // First check that there aren't any missing symbols.
261       size_t NumMaterializationSideEffectsOnlySymbols = 0;
262       SymbolNameVector ExtraSymbols;
263       SymbolNameVector MissingSymbols;
264       for (auto &KV : MR->getSymbols()) {
265 
266         auto I = InternedResult.find(KV.first);
267 
268         // If this is a materialization-side-effects only symbol then bump
269         // the counter and make sure it's *not* defined, otherwise make
270         // sure that it is defined.
271         if (KV.second.hasMaterializationSideEffectsOnly()) {
272           ++NumMaterializationSideEffectsOnlySymbols;
273           if (I != InternedResult.end())
274             ExtraSymbols.push_back(KV.first);
275           continue;
276         } else if (I == InternedResult.end())
277           MissingSymbols.push_back(KV.first);
278         else if (Layer.OverrideObjectFlags)
279           I->second.setFlags(KV.second);
280       }
281 
282       // If there were missing symbols then report the error.
283       if (!MissingSymbols.empty())
284         return make_error<MissingSymbolDefinitions>(
285             Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
286             std::move(MissingSymbols));
287 
288       // If there are more definitions than expected, add them to the
289       // ExtraSymbols vector.
290       if (InternedResult.size() >
291           MR->getSymbols().size() - NumMaterializationSideEffectsOnlySymbols) {
292         for (auto &KV : InternedResult)
293           if (!MR->getSymbols().count(KV.first))
294             ExtraSymbols.push_back(KV.first);
295       }
296 
297       // If there were extra definitions then report the error.
298       if (!ExtraSymbols.empty())
299         return make_error<UnexpectedSymbolDefinitions>(
300             Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
301             std::move(ExtraSymbols));
302     }
303 
304     if (auto Err = MR->notifyResolved(InternedResult))
305       return Err;
306 
307     Layer.notifyLoaded(*MR);
308     return Error::success();
309   }
310 
311   void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc A) override {
312     if (auto Err = Layer.notifyEmitted(*MR, std::move(A))) {
313       Layer.getExecutionSession().reportError(std::move(Err));
314       MR->failMaterialization();
315       return;
316     }
317     if (auto Err = MR->notifyEmitted()) {
318       Layer.getExecutionSession().reportError(std::move(Err));
319       MR->failMaterialization();
320     }
321   }
322 
323   LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override {
324     return [this](LinkGraph &G) { return markResponsibilitySymbolsLive(G); };
325   }
326 
327   Error modifyPassConfig(LinkGraph &LG, PassConfiguration &Config) override {
328     // Add passes to mark duplicate defs as should-discard, and to walk the
329     // link graph to build the symbol dependence graph.
330     Config.PrePrunePasses.push_back([this](LinkGraph &G) {
331       return claimOrExternalizeWeakAndCommonSymbols(G);
332     });
333 
334     Layer.modifyPassConfig(*MR, LG, Config);
335 
336     Config.PostPrunePasses.push_back(
337         [this](LinkGraph &G) { return computeNamedSymbolDependencies(G); });
338 
339     return Error::success();
340   }
341 
342 private:
343   // Symbol name dependencies:
344   // Internal: Defined in this graph.
345   // External: Defined externally.
346   struct BlockSymbolDependencies {
347     SymbolNameSet Internal, External;
348   };
349 
350   // Lazily populated map of blocks to BlockSymbolDependencies values.
351   class BlockDependenciesMap {
352   public:
353     BlockDependenciesMap(ExecutionSession &ES,
354                          DenseMap<const Block *, DenseSet<Block *>> BlockDeps)
355         : ES(ES), BlockDeps(std::move(BlockDeps)) {}
356 
357     const BlockSymbolDependencies &operator[](const Block &B) {
358       // Check the cache first.
359       auto I = BlockTransitiveDepsCache.find(&B);
360       if (I != BlockTransitiveDepsCache.end())
361         return I->second;
362 
363       // No value. Populate the cache.
364       BlockSymbolDependencies BTDCacheVal;
365       auto BDI = BlockDeps.find(&B);
366       assert(BDI != BlockDeps.end() && "No block dependencies");
367 
368       for (auto *BDep : BDI->second) {
369         auto &BID = getBlockImmediateDeps(*BDep);
370         for (auto &ExternalDep : BID.External)
371           BTDCacheVal.External.insert(ExternalDep);
372         for (auto &InternalDep : BID.Internal)
373           BTDCacheVal.Internal.insert(InternalDep);
374       }
375 
376       return BlockTransitiveDepsCache
377           .insert(std::make_pair(&B, std::move(BTDCacheVal)))
378           .first->second;
379     }
380 
381     SymbolStringPtr &getInternedName(Symbol &Sym) {
382       auto I = NameCache.find(&Sym);
383       if (I != NameCache.end())
384         return I->second;
385 
386       return NameCache.insert(std::make_pair(&Sym, ES.intern(Sym.getName())))
387           .first->second;
388     }
389 
390   private:
391     BlockSymbolDependencies &getBlockImmediateDeps(Block &B) {
392       // Check the cache first.
393       auto I = BlockImmediateDepsCache.find(&B);
394       if (I != BlockImmediateDepsCache.end())
395         return I->second;
396 
397       BlockSymbolDependencies BIDCacheVal;
398       for (auto &E : B.edges()) {
399         auto &Tgt = E.getTarget();
400         if (Tgt.getScope() != Scope::Local) {
401           if (Tgt.isExternal())
402             BIDCacheVal.External.insert(getInternedName(Tgt));
403           else
404             BIDCacheVal.Internal.insert(getInternedName(Tgt));
405         }
406       }
407 
408       return BlockImmediateDepsCache
409           .insert(std::make_pair(&B, std::move(BIDCacheVal)))
410           .first->second;
411     }
412 
413     ExecutionSession &ES;
414     DenseMap<const Block *, DenseSet<Block *>> BlockDeps;
415     DenseMap<const Symbol *, SymbolStringPtr> NameCache;
416     DenseMap<const Block *, BlockSymbolDependencies> BlockImmediateDepsCache;
417     DenseMap<const Block *, BlockSymbolDependencies> BlockTransitiveDepsCache;
418   };
419 
420   Error claimOrExternalizeWeakAndCommonSymbols(LinkGraph &G) {
421     auto &ES = Layer.getExecutionSession();
422 
423     SymbolFlagsMap NewSymbolsToClaim;
424     std::vector<std::pair<SymbolStringPtr, Symbol *>> NameToSym;
425 
426     auto ProcessSymbol = [&](Symbol *Sym) {
427       if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak &&
428           Sym->getScope() != Scope::Local) {
429         auto Name = ES.intern(Sym->getName());
430         if (!MR->getSymbols().count(ES.intern(Sym->getName()))) {
431           JITSymbolFlags SF = JITSymbolFlags::Weak;
432           if (Sym->getScope() == Scope::Default)
433             SF |= JITSymbolFlags::Exported;
434           NewSymbolsToClaim[Name] = SF;
435           NameToSym.push_back(std::make_pair(std::move(Name), Sym));
436         }
437       }
438     };
439 
440     for (auto *Sym : G.defined_symbols())
441       ProcessSymbol(Sym);
442     for (auto *Sym : G.absolute_symbols())
443       ProcessSymbol(Sym);
444 
445     // Attempt to claim all weak defs that we're not already responsible for.
446     // This cannot fail -- any clashes will just result in rejection of our
447     // claim, at which point we'll externalize that symbol.
448     cantFail(MR->defineMaterializing(std::move(NewSymbolsToClaim)));
449 
450     for (auto &KV : NameToSym)
451       if (!MR->getSymbols().count(KV.first))
452         G.makeExternal(*KV.second);
453 
454     return Error::success();
455   }
456 
457   Error markResponsibilitySymbolsLive(LinkGraph &G) const {
458     auto &ES = Layer.getExecutionSession();
459     for (auto *Sym : G.defined_symbols())
460       if (Sym->hasName() && MR->getSymbols().count(ES.intern(Sym->getName())))
461         Sym->setLive(true);
462     return Error::success();
463   }
464 
465   Error computeNamedSymbolDependencies(LinkGraph &G) {
466     auto &ES = MR->getTargetJITDylib().getExecutionSession();
467     auto BlockDeps = computeBlockNonLocalDeps(G);
468 
469     // Compute dependencies for symbols defined in the JITLink graph.
470     for (auto *Sym : G.defined_symbols()) {
471 
472       // Skip local symbols: we do not track dependencies for these.
473       if (Sym->getScope() == Scope::Local)
474         continue;
475       assert(Sym->hasName() &&
476              "Defined non-local jitlink::Symbol should have a name");
477 
478       auto &SymDeps = BlockDeps[Sym->getBlock()];
479       if (SymDeps.External.empty() && SymDeps.Internal.empty())
480         continue;
481 
482       auto SymName = ES.intern(Sym->getName());
483       if (!SymDeps.External.empty())
484         ExternalNamedSymbolDeps[SymName] = SymDeps.External;
485       if (!SymDeps.Internal.empty())
486         InternalNamedSymbolDeps[SymName] = SymDeps.Internal;
487     }
488 
489     for (auto &P : Layer.Plugins) {
490       auto SynthDeps = P->getSyntheticSymbolDependencies(*MR);
491       if (SynthDeps.empty())
492         continue;
493 
494       DenseSet<Block *> BlockVisited;
495       for (auto &KV : SynthDeps) {
496         auto &Name = KV.first;
497         auto &DepsForName = KV.second;
498         for (auto *Sym : DepsForName) {
499           if (Sym->getScope() == Scope::Local) {
500             auto &BDeps = BlockDeps[Sym->getBlock()];
501             for (auto &S : BDeps.Internal)
502               InternalNamedSymbolDeps[Name].insert(S);
503             for (auto &S : BDeps.External)
504               ExternalNamedSymbolDeps[Name].insert(S);
505           } else {
506             if (Sym->isExternal())
507               ExternalNamedSymbolDeps[Name].insert(
508                   BlockDeps.getInternedName(*Sym));
509             else
510               InternalNamedSymbolDeps[Name].insert(
511                   BlockDeps.getInternedName(*Sym));
512           }
513         }
514       }
515     }
516 
517     return Error::success();
518   }
519 
520   BlockDependenciesMap computeBlockNonLocalDeps(LinkGraph &G) {
521     // First calculate the reachable-via-non-local-symbol blocks for each block.
522     struct BlockInfo {
523       DenseSet<Block *> Dependencies;
524       DenseSet<Block *> Dependants;
525       bool DependenciesChanged = true;
526     };
527     DenseMap<Block *, BlockInfo> BlockInfos;
528     SmallVector<Block *> WorkList;
529 
530     // Pre-allocate map entries. This prevents any iterator/reference
531     // invalidation in the next loop.
532     for (auto *B : G.blocks())
533       (void)BlockInfos[B];
534 
535     // Build initial worklist, record block dependencies/dependants and
536     // non-local symbol dependencies.
537     for (auto *B : G.blocks()) {
538       auto &BI = BlockInfos[B];
539       for (auto &E : B->edges()) {
540         if (E.getTarget().getScope() == Scope::Local &&
541             !E.getTarget().isAbsolute()) {
542           auto &TgtB = E.getTarget().getBlock();
543           if (&TgtB != B) {
544             BI.Dependencies.insert(&TgtB);
545             BlockInfos[&TgtB].Dependants.insert(B);
546           }
547         }
548       }
549 
550       // If this node has both dependants and dependencies then add it to the
551       // worklist to propagate the dependencies to the dependants.
552       if (!BI.Dependants.empty() && !BI.Dependencies.empty())
553         WorkList.push_back(B);
554     }
555 
556     // Propagate block-level dependencies through the block-dependence graph.
557     while (!WorkList.empty()) {
558       auto *B = WorkList.pop_back_val();
559 
560       auto &BI = BlockInfos[B];
561       assert(BI.DependenciesChanged &&
562              "Block in worklist has unchanged dependencies");
563       BI.DependenciesChanged = false;
564       for (auto *Dependant : BI.Dependants) {
565         auto &DependantBI = BlockInfos[Dependant];
566         for (auto *Dependency : BI.Dependencies) {
567           if (Dependant != Dependency &&
568               DependantBI.Dependencies.insert(Dependency).second)
569             if (!DependantBI.DependenciesChanged) {
570               DependantBI.DependenciesChanged = true;
571               WorkList.push_back(Dependant);
572             }
573         }
574       }
575     }
576 
577     DenseMap<const Block *, DenseSet<Block *>> BlockDeps;
578     for (auto &KV : BlockInfos)
579       BlockDeps[KV.first] = std::move(KV.second.Dependencies);
580 
581     return BlockDependenciesMap(Layer.getExecutionSession(),
582                                 std::move(BlockDeps));
583   }
584 
585   void registerDependencies(const SymbolDependenceMap &QueryDeps) {
586     for (auto &NamedDepsEntry : ExternalNamedSymbolDeps) {
587       auto &Name = NamedDepsEntry.first;
588       auto &NameDeps = NamedDepsEntry.second;
589       SymbolDependenceMap SymbolDeps;
590 
591       for (const auto &QueryDepsEntry : QueryDeps) {
592         JITDylib &SourceJD = *QueryDepsEntry.first;
593         const SymbolNameSet &Symbols = QueryDepsEntry.second;
594         auto &DepsForJD = SymbolDeps[&SourceJD];
595 
596         for (const auto &S : Symbols)
597           if (NameDeps.count(S))
598             DepsForJD.insert(S);
599 
600         if (DepsForJD.empty())
601           SymbolDeps.erase(&SourceJD);
602       }
603 
604       MR->addDependencies(Name, SymbolDeps);
605     }
606   }
607 
608   ObjectLinkingLayer &Layer;
609   std::unique_ptr<MaterializationResponsibility> MR;
610   std::unique_ptr<MemoryBuffer> ObjBuffer;
611   DenseMap<SymbolStringPtr, SymbolNameSet> ExternalNamedSymbolDeps;
612   DenseMap<SymbolStringPtr, SymbolNameSet> InternalNamedSymbolDeps;
613 };
614 
615 ObjectLinkingLayer::Plugin::~Plugin() = default;
616 
617 char ObjectLinkingLayer::ID;
618 
619 using BaseT = RTTIExtends<ObjectLinkingLayer, ObjectLayer>;
620 
621 ObjectLinkingLayer::ObjectLinkingLayer(ExecutionSession &ES)
622     : BaseT(ES), MemMgr(ES.getExecutorProcessControl().getMemMgr()) {
623   ES.registerResourceManager(*this);
624 }
625 
626 ObjectLinkingLayer::ObjectLinkingLayer(ExecutionSession &ES,
627                                        JITLinkMemoryManager &MemMgr)
628     : BaseT(ES), MemMgr(MemMgr) {
629   ES.registerResourceManager(*this);
630 }
631 
632 ObjectLinkingLayer::ObjectLinkingLayer(
633     ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
634     : BaseT(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) {
635   ES.registerResourceManager(*this);
636 }
637 
638 ObjectLinkingLayer::~ObjectLinkingLayer() {
639   assert(Allocs.empty() && "Layer destroyed with resources still attached");
640   getExecutionSession().deregisterResourceManager(*this);
641 }
642 
643 Error ObjectLinkingLayer::add(ResourceTrackerSP RT,
644                               std::unique_ptr<LinkGraph> G) {
645   auto &JD = RT->getJITDylib();
646   return JD.define(LinkGraphMaterializationUnit::Create(*this, std::move(G)),
647                    std::move(RT));
648 }
649 
650 void ObjectLinkingLayer::emit(std::unique_ptr<MaterializationResponsibility> R,
651                               std::unique_ptr<MemoryBuffer> O) {
652   assert(O && "Object must not be null");
653   MemoryBufferRef ObjBuffer = O->getMemBufferRef();
654 
655   auto Ctx = std::make_unique<ObjectLinkingLayerJITLinkContext>(
656       *this, std::move(R), std::move(O));
657   if (auto G = createLinkGraphFromObject(ObjBuffer)) {
658     Ctx->notifyMaterializing(**G);
659     link(std::move(*G), std::move(Ctx));
660   } else {
661     Ctx->notifyFailed(G.takeError());
662   }
663 }
664 
665 void ObjectLinkingLayer::emit(std::unique_ptr<MaterializationResponsibility> R,
666                               std::unique_ptr<LinkGraph> G) {
667   auto Ctx = std::make_unique<ObjectLinkingLayerJITLinkContext>(
668       *this, std::move(R), nullptr);
669   Ctx->notifyMaterializing(*G);
670   link(std::move(G), std::move(Ctx));
671 }
672 
673 void ObjectLinkingLayer::modifyPassConfig(MaterializationResponsibility &MR,
674                                           LinkGraph &G,
675                                           PassConfiguration &PassConfig) {
676   for (auto &P : Plugins)
677     P->modifyPassConfig(MR, G, PassConfig);
678 }
679 
680 void ObjectLinkingLayer::notifyLoaded(MaterializationResponsibility &MR) {
681   for (auto &P : Plugins)
682     P->notifyLoaded(MR);
683 }
684 
685 Error ObjectLinkingLayer::notifyEmitted(MaterializationResponsibility &MR,
686                                         FinalizedAlloc FA) {
687   Error Err = Error::success();
688   for (auto &P : Plugins)
689     Err = joinErrors(std::move(Err), P->notifyEmitted(MR));
690 
691   if (Err)
692     return Err;
693 
694   return MR.withResourceKeyDo(
695       [&](ResourceKey K) { Allocs[K].push_back(std::move(FA)); });
696 }
697 
698 Error ObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
699 
700   {
701     Error Err = Error::success();
702     for (auto &P : Plugins)
703       Err = joinErrors(std::move(Err), P->notifyRemovingResources(K));
704     if (Err)
705       return Err;
706   }
707 
708   std::vector<FinalizedAlloc> AllocsToRemove;
709   getExecutionSession().runSessionLocked([&] {
710     auto I = Allocs.find(K);
711     if (I != Allocs.end()) {
712       std::swap(AllocsToRemove, I->second);
713       Allocs.erase(I);
714     }
715   });
716 
717   if (AllocsToRemove.empty())
718     return Error::success();
719 
720   return MemMgr.deallocate(std::move(AllocsToRemove));
721 }
722 
723 void ObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
724                                                  ResourceKey SrcKey) {
725   auto I = Allocs.find(SrcKey);
726   if (I != Allocs.end()) {
727     auto &SrcAllocs = I->second;
728     auto &DstAllocs = Allocs[DstKey];
729     DstAllocs.reserve(DstAllocs.size() + SrcAllocs.size());
730     for (auto &Alloc : SrcAllocs)
731       DstAllocs.push_back(std::move(Alloc));
732 
733     // Erase SrcKey entry using value rather than iterator I: I may have been
734     // invalidated when we looked up DstKey.
735     Allocs.erase(SrcKey);
736   }
737 
738   for (auto &P : Plugins)
739     P->notifyTransferringResources(DstKey, SrcKey);
740 }
741 
742 EHFrameRegistrationPlugin::EHFrameRegistrationPlugin(
743     ExecutionSession &ES, std::unique_ptr<EHFrameRegistrar> Registrar)
744     : ES(ES), Registrar(std::move(Registrar)) {}
745 
746 void EHFrameRegistrationPlugin::modifyPassConfig(
747     MaterializationResponsibility &MR, LinkGraph &G,
748     PassConfiguration &PassConfig) {
749 
750   PassConfig.PostFixupPasses.push_back(createEHFrameRecorderPass(
751       G.getTargetTriple(), [this, &MR](ExecutorAddr Addr, size_t Size) {
752         if (Addr) {
753           std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
754           assert(!InProcessLinks.count(&MR) &&
755                  "Link for MR already being tracked?");
756           InProcessLinks[&MR] = {Addr, Size};
757         }
758       }));
759 }
760 
761 Error EHFrameRegistrationPlugin::notifyEmitted(
762     MaterializationResponsibility &MR) {
763 
764   ExecutorAddrRange EmittedRange;
765   {
766     std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
767 
768     auto EHFrameRangeItr = InProcessLinks.find(&MR);
769     if (EHFrameRangeItr == InProcessLinks.end())
770       return Error::success();
771 
772     EmittedRange = EHFrameRangeItr->second;
773     assert(EmittedRange.Start && "eh-frame addr to register can not be null");
774     InProcessLinks.erase(EHFrameRangeItr);
775   }
776 
777   if (auto Err = MR.withResourceKeyDo(
778           [&](ResourceKey K) { EHFrameRanges[K].push_back(EmittedRange); }))
779     return Err;
780 
781   return Registrar->registerEHFrames(EmittedRange);
782 }
783 
784 Error EHFrameRegistrationPlugin::notifyFailed(
785     MaterializationResponsibility &MR) {
786   std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
787   InProcessLinks.erase(&MR);
788   return Error::success();
789 }
790 
791 Error EHFrameRegistrationPlugin::notifyRemovingResources(ResourceKey K) {
792   std::vector<ExecutorAddrRange> RangesToRemove;
793 
794   ES.runSessionLocked([&] {
795     auto I = EHFrameRanges.find(K);
796     if (I != EHFrameRanges.end()) {
797       RangesToRemove = std::move(I->second);
798       EHFrameRanges.erase(I);
799     }
800   });
801 
802   Error Err = Error::success();
803   while (!RangesToRemove.empty()) {
804     auto RangeToRemove = RangesToRemove.back();
805     RangesToRemove.pop_back();
806     assert(RangeToRemove.Start && "Untracked eh-frame range must not be null");
807     Err = joinErrors(std::move(Err),
808                      Registrar->deregisterEHFrames(RangeToRemove));
809   }
810 
811   return Err;
812 }
813 
814 void EHFrameRegistrationPlugin::notifyTransferringResources(
815     ResourceKey DstKey, ResourceKey SrcKey) {
816   auto SI = EHFrameRanges.find(SrcKey);
817   if (SI == EHFrameRanges.end())
818     return;
819 
820   auto DI = EHFrameRanges.find(DstKey);
821   if (DI != EHFrameRanges.end()) {
822     auto &SrcRanges = SI->second;
823     auto &DstRanges = DI->second;
824     DstRanges.reserve(DstRanges.size() + SrcRanges.size());
825     for (auto &SrcRange : SrcRanges)
826       DstRanges.push_back(std::move(SrcRange));
827     EHFrameRanges.erase(SI);
828   } else {
829     // We need to move SrcKey's ranges over without invalidating the SI
830     // iterator.
831     auto Tmp = std::move(SI->second);
832     EHFrameRanges.erase(SI);
833     EHFrameRanges[DstKey] = std::move(Tmp);
834   }
835 }
836 
837 } // End namespace orc.
838 } // End namespace llvm.
839