xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp (revision 2a739f2789c3549aec3d77807e452dc22e4ef9f9)
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() == "__TEXT,__swift5_types" ||
81           Sec.getName() == "__DATA,__mod_init_func")
82         return true;
83     return false;
84   }
85 
86   static bool hasELFInitSection(LinkGraph &G) {
87     for (auto &Sec : G.sections())
88       if (Sec.getName() == ".init_array")
89         return true;
90     return false;
91   }
92 
93   static SymbolStringPtr makeInitSymbol(ExecutionSession &ES, LinkGraph &G) {
94     std::string InitSymString;
95     raw_string_ostream(InitSymString)
96         << "$." << G.getName() << ".__inits" << Counter++;
97     return ES.intern(InitSymString);
98   }
99 
100   LinkGraphMaterializationUnit(ObjectLinkingLayer &ObjLinkingLayer,
101                                std::unique_ptr<LinkGraph> G,
102                                LinkGraphInterface LGI)
103       : MaterializationUnit(std::move(LGI.SymbolFlags),
104                             std::move(LGI.InitSymbol)),
105         ObjLinkingLayer(ObjLinkingLayer), G(std::move(G)) {}
106 
107   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
108     for (auto *Sym : G->defined_symbols())
109       if (Sym->getName() == *Name) {
110         assert(Sym->getLinkage() == Linkage::Weak &&
111                "Discarding non-weak definition");
112         G->makeExternal(*Sym);
113         break;
114       }
115   }
116 
117   ObjectLinkingLayer &ObjLinkingLayer;
118   std::unique_ptr<LinkGraph> G;
119   static std::atomic<uint64_t> Counter;
120 };
121 
122 std::atomic<uint64_t> LinkGraphMaterializationUnit::Counter{0};
123 
124 } // end anonymous namespace
125 
126 namespace llvm {
127 namespace orc {
128 
129 class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
130 public:
131   ObjectLinkingLayerJITLinkContext(
132       ObjectLinkingLayer &Layer,
133       std::unique_ptr<MaterializationResponsibility> MR,
134       std::unique_ptr<MemoryBuffer> ObjBuffer)
135       : JITLinkContext(&MR->getTargetJITDylib()), Layer(Layer),
136         MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {}
137 
138   ~ObjectLinkingLayerJITLinkContext() {
139     // If there is an object buffer return function then use it to
140     // return ownership of the buffer.
141     if (Layer.ReturnObjectBuffer && ObjBuffer)
142       Layer.ReturnObjectBuffer(std::move(ObjBuffer));
143   }
144 
145   JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }
146 
147   void notifyMaterializing(LinkGraph &G) {
148     for (auto &P : Layer.Plugins)
149       P->notifyMaterializing(*MR, G, *this,
150                              ObjBuffer ? ObjBuffer->getMemBufferRef()
151                              : MemoryBufferRef());
152   }
153 
154   void notifyFailed(Error Err) override {
155     for (auto &P : Layer.Plugins)
156       Err = joinErrors(std::move(Err), P->notifyFailed(*MR));
157     Layer.getExecutionSession().reportError(std::move(Err));
158     MR->failMaterialization();
159   }
160 
161   void lookup(const LookupMap &Symbols,
162               std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override {
163 
164     JITDylibSearchOrder LinkOrder;
165     MR->getTargetJITDylib().withLinkOrderDo(
166         [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
167 
168     auto &ES = Layer.getExecutionSession();
169 
170     SymbolLookupSet LookupSet;
171     for (auto &KV : Symbols) {
172       orc::SymbolLookupFlags LookupFlags;
173       switch (KV.second) {
174       case jitlink::SymbolLookupFlags::RequiredSymbol:
175         LookupFlags = orc::SymbolLookupFlags::RequiredSymbol;
176         break;
177       case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol:
178         LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol;
179         break;
180       }
181       LookupSet.add(ES.intern(KV.first), LookupFlags);
182     }
183 
184     // OnResolve -- De-intern the symbols and pass the result to the linker.
185     auto OnResolve = [LookupContinuation =
186                           std::move(LC)](Expected<SymbolMap> Result) mutable {
187       if (!Result)
188         LookupContinuation->run(Result.takeError());
189       else {
190         AsyncLookupResult LR;
191         for (auto &KV : *Result)
192           LR[*KV.first] = KV.second;
193         LookupContinuation->run(std::move(LR));
194       }
195     };
196 
197     for (auto &KV : InternalNamedSymbolDeps) {
198       SymbolDependenceMap InternalDeps;
199       InternalDeps[&MR->getTargetJITDylib()] = std::move(KV.second);
200       MR->addDependencies(KV.first, InternalDeps);
201     }
202 
203     ES.lookup(LookupKind::Static, LinkOrder, std::move(LookupSet),
204               SymbolState::Resolved, std::move(OnResolve),
205               [this](const SymbolDependenceMap &Deps) {
206                 registerDependencies(Deps);
207               });
208   }
209 
210   Error notifyResolved(LinkGraph &G) override {
211     auto &ES = Layer.getExecutionSession();
212 
213     SymbolFlagsMap ExtraSymbolsToClaim;
214     bool AutoClaim = Layer.AutoClaimObjectSymbols;
215 
216     SymbolMap InternedResult;
217     for (auto *Sym : G.defined_symbols())
218       if (Sym->hasName() && Sym->getScope() != Scope::Local) {
219         auto InternedName = ES.intern(Sym->getName());
220         JITSymbolFlags Flags;
221 
222         if (Sym->isCallable())
223           Flags |= JITSymbolFlags::Callable;
224         if (Sym->getScope() == Scope::Default)
225           Flags |= JITSymbolFlags::Exported;
226 
227         InternedResult[InternedName] =
228             JITEvaluatedSymbol(Sym->getAddress(), Flags);
229         if (AutoClaim && !MR->getSymbols().count(InternedName)) {
230           assert(!ExtraSymbolsToClaim.count(InternedName) &&
231                  "Duplicate symbol to claim?");
232           ExtraSymbolsToClaim[InternedName] = Flags;
233         }
234       }
235 
236     for (auto *Sym : G.absolute_symbols())
237       if (Sym->hasName()) {
238         auto InternedName = ES.intern(Sym->getName());
239         JITSymbolFlags Flags;
240         Flags |= JITSymbolFlags::Absolute;
241         if (Sym->isCallable())
242           Flags |= JITSymbolFlags::Callable;
243         if (Sym->getLinkage() == Linkage::Weak)
244           Flags |= JITSymbolFlags::Weak;
245         InternedResult[InternedName] =
246             JITEvaluatedSymbol(Sym->getAddress(), Flags);
247         if (AutoClaim && !MR->getSymbols().count(InternedName)) {
248           assert(!ExtraSymbolsToClaim.count(InternedName) &&
249                  "Duplicate symbol to claim?");
250           ExtraSymbolsToClaim[InternedName] = Flags;
251         }
252       }
253 
254     if (!ExtraSymbolsToClaim.empty())
255       if (auto Err = MR->defineMaterializing(ExtraSymbolsToClaim))
256         return Err;
257 
258     {
259 
260       // Check that InternedResult matches up with MR->getSymbols().
261       // This guards against faulty transformations / compilers / object caches.
262 
263       // First check that there aren't any missing symbols.
264       size_t NumMaterializationSideEffectsOnlySymbols = 0;
265       SymbolNameVector ExtraSymbols;
266       SymbolNameVector MissingSymbols;
267       for (auto &KV : MR->getSymbols()) {
268 
269         // If this is a materialization-side-effects only symbol then bump
270         // the counter and make sure it's *not* defined, otherwise make
271         // sure that it is defined.
272         if (KV.second.hasMaterializationSideEffectsOnly()) {
273           ++NumMaterializationSideEffectsOnlySymbols;
274           if (InternedResult.count(KV.first))
275             ExtraSymbols.push_back(KV.first);
276           continue;
277         } else if (!InternedResult.count(KV.first))
278           MissingSymbols.push_back(KV.first);
279       }
280 
281       // If there were missing symbols then report the error.
282       if (!MissingSymbols.empty())
283         return make_error<MissingSymbolDefinitions>(
284             Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
285             std::move(MissingSymbols));
286 
287       // If there are more definitions than expected, add them to the
288       // ExtraSymbols vector.
289       if (InternedResult.size() >
290           MR->getSymbols().size() - NumMaterializationSideEffectsOnlySymbols) {
291         for (auto &KV : InternedResult)
292           if (!MR->getSymbols().count(KV.first))
293             ExtraSymbols.push_back(KV.first);
294       }
295 
296       // If there were extra definitions then report the error.
297       if (!ExtraSymbols.empty())
298         return make_error<UnexpectedSymbolDefinitions>(
299             Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
300             std::move(ExtraSymbols));
301     }
302 
303     if (auto Err = MR->notifyResolved(InternedResult))
304       return Err;
305 
306     Layer.notifyLoaded(*MR);
307     return Error::success();
308   }
309 
310   void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc 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                                         FinalizedAlloc FA) {
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(FA)); });
693 }
694 
695 Error ObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
696 
697   {
698     Error Err = Error::success();
699     for (auto &P : Plugins)
700       Err = joinErrors(std::move(Err), P->notifyRemovingResources(K));
701     if (Err)
702       return Err;
703   }
704 
705   std::vector<FinalizedAlloc> AllocsToRemove;
706   getExecutionSession().runSessionLocked([&] {
707     auto I = Allocs.find(K);
708     if (I != Allocs.end()) {
709       std::swap(AllocsToRemove, I->second);
710       Allocs.erase(I);
711     }
712   });
713 
714   if (AllocsToRemove.empty())
715     return Error::success();
716 
717   return MemMgr.deallocate(std::move(AllocsToRemove));
718 }
719 
720 void ObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
721                                                  ResourceKey SrcKey) {
722   auto I = Allocs.find(SrcKey);
723   if (I != Allocs.end()) {
724     auto &SrcAllocs = I->second;
725     auto &DstAllocs = Allocs[DstKey];
726     DstAllocs.reserve(DstAllocs.size() + SrcAllocs.size());
727     for (auto &Alloc : SrcAllocs)
728       DstAllocs.push_back(std::move(Alloc));
729 
730     // Erase SrcKey entry using value rather than iterator I: I may have been
731     // invalidated when we looked up DstKey.
732     Allocs.erase(SrcKey);
733   }
734 
735   for (auto &P : Plugins)
736     P->notifyTransferringResources(DstKey, SrcKey);
737 }
738 
739 EHFrameRegistrationPlugin::EHFrameRegistrationPlugin(
740     ExecutionSession &ES, std::unique_ptr<EHFrameRegistrar> Registrar)
741     : ES(ES), Registrar(std::move(Registrar)) {}
742 
743 void EHFrameRegistrationPlugin::modifyPassConfig(
744     MaterializationResponsibility &MR, LinkGraph &G,
745     PassConfiguration &PassConfig) {
746 
747   PassConfig.PostFixupPasses.push_back(createEHFrameRecorderPass(
748       G.getTargetTriple(), [this, &MR](JITTargetAddress Addr, size_t Size) {
749         if (Addr) {
750           std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
751           assert(!InProcessLinks.count(&MR) &&
752                  "Link for MR already being tracked?");
753           InProcessLinks[&MR] = {Addr, Size};
754         }
755       }));
756 }
757 
758 Error EHFrameRegistrationPlugin::notifyEmitted(
759     MaterializationResponsibility &MR) {
760 
761   EHFrameRange EmittedRange;
762   {
763     std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
764 
765     auto EHFrameRangeItr = InProcessLinks.find(&MR);
766     if (EHFrameRangeItr == InProcessLinks.end())
767       return Error::success();
768 
769     EmittedRange = EHFrameRangeItr->second;
770     assert(EmittedRange.Addr && "eh-frame addr to register can not be null");
771     InProcessLinks.erase(EHFrameRangeItr);
772   }
773 
774   if (auto Err = MR.withResourceKeyDo(
775           [&](ResourceKey K) { EHFrameRanges[K].push_back(EmittedRange); }))
776     return Err;
777 
778   return Registrar->registerEHFrames(EmittedRange.Addr, EmittedRange.Size);
779 }
780 
781 Error EHFrameRegistrationPlugin::notifyFailed(
782     MaterializationResponsibility &MR) {
783   std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
784   InProcessLinks.erase(&MR);
785   return Error::success();
786 }
787 
788 Error EHFrameRegistrationPlugin::notifyRemovingResources(ResourceKey K) {
789   std::vector<EHFrameRange> RangesToRemove;
790 
791   ES.runSessionLocked([&] {
792     auto I = EHFrameRanges.find(K);
793     if (I != EHFrameRanges.end()) {
794       RangesToRemove = std::move(I->second);
795       EHFrameRanges.erase(I);
796     }
797   });
798 
799   Error Err = Error::success();
800   while (!RangesToRemove.empty()) {
801     auto RangeToRemove = RangesToRemove.back();
802     RangesToRemove.pop_back();
803     assert(RangeToRemove.Addr && "Untracked eh-frame range must not be null");
804     Err = joinErrors(
805         std::move(Err),
806         Registrar->deregisterEHFrames(RangeToRemove.Addr, RangeToRemove.Size));
807   }
808 
809   return Err;
810 }
811 
812 void EHFrameRegistrationPlugin::notifyTransferringResources(
813     ResourceKey DstKey, ResourceKey SrcKey) {
814   auto SI = EHFrameRanges.find(SrcKey);
815   if (SI == EHFrameRanges.end())
816     return;
817 
818   auto DI = EHFrameRanges.find(DstKey);
819   if (DI != EHFrameRanges.end()) {
820     auto &SrcRanges = SI->second;
821     auto &DstRanges = DI->second;
822     DstRanges.reserve(DstRanges.size() + SrcRanges.size());
823     for (auto &SrcRange : SrcRanges)
824       DstRanges.push_back(std::move(SrcRange));
825     EHFrameRanges.erase(SI);
826   } else {
827     // We need to move SrcKey's ranges over without invalidating the SI
828     // iterator.
829     auto Tmp = std::move(SI->second);
830     EHFrameRanges.erase(SI);
831     EHFrameRanges[DstKey] = std::move(Tmp);
832   }
833 }
834 
835 } // End namespace orc.
836 } // End namespace llvm.
837