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