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