1 //===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld 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/RTDyldObjectLinkingLayer.h"
10 #include "llvm/Object/COFF.h"
11 
12 namespace {
13 
14 using namespace llvm;
15 using namespace llvm::orc;
16 
17 class JITDylibSearchOrderResolver : public JITSymbolResolver {
18 public:
JITDylibSearchOrderResolver(MaterializationResponsibility & MR)19   JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20 
lookup(const LookupSet & Symbols,OnResolvedFunction OnResolved)21   void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
22     auto &ES = MR.getTargetJITDylib().getExecutionSession();
23     SymbolLookupSet InternedSymbols;
24 
25     // Intern the requested symbols: lookup takes interned strings.
26     for (auto &S : Symbols)
27       InternedSymbols.add(ES.intern(S));
28 
29     // Build an OnResolve callback to unwrap the interned strings and pass them
30     // to the OnResolved callback.
31     auto OnResolvedWithUnwrap =
32         [OnResolved = std::move(OnResolved)](
33             Expected<SymbolMap> InternedResult) mutable {
34           if (!InternedResult) {
35             OnResolved(InternedResult.takeError());
36             return;
37           }
38 
39           LookupResult Result;
40           for (auto &KV : *InternedResult)
41             Result[*KV.first] = std::move(KV.second);
42           OnResolved(Result);
43         };
44 
45     // Register dependencies for all symbols contained in this set.
46     auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
47       MR.addDependenciesForAll(Deps);
48     };
49 
50     JITDylibSearchOrder LinkOrder;
51     MR.getTargetJITDylib().withLinkOrderDo(
52         [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
53     ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
54               SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
55               RegisterDependencies);
56   }
57 
getResponsibilitySet(const LookupSet & Symbols)58   Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
59     LookupSet Result;
60 
61     for (auto &KV : MR.getSymbols()) {
62       if (Symbols.count(*KV.first))
63         Result.insert(*KV.first);
64     }
65 
66     return Result;
67   }
68 
69 private:
70   MaterializationResponsibility &MR;
71 };
72 
73 } // end anonymous namespace
74 
75 namespace llvm {
76 namespace orc {
77 
RTDyldObjectLinkingLayer(ExecutionSession & ES,GetMemoryManagerFunction GetMemoryManager)78 RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
79     ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80     : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {
81   ES.registerResourceManager(*this);
82 }
83 
~RTDyldObjectLinkingLayer()84 RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
85   assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
86 }
87 
emit(std::unique_ptr<MaterializationResponsibility> R,std::unique_ptr<MemoryBuffer> O)88 void RTDyldObjectLinkingLayer::emit(
89     std::unique_ptr<MaterializationResponsibility> R,
90     std::unique_ptr<MemoryBuffer> O) {
91   assert(O && "Object must not be null");
92 
93   auto &ES = getExecutionSession();
94 
95   auto Obj = object::ObjectFile::createObjectFile(*O);
96 
97   if (!Obj) {
98     getExecutionSession().reportError(Obj.takeError());
99     R->failMaterialization();
100     return;
101   }
102 
103   // Collect the internal symbols from the object file: We will need to
104   // filter these later.
105   auto InternalSymbols = std::make_shared<std::set<StringRef>>();
106   {
107     for (auto &Sym : (*Obj)->symbols()) {
108 
109       // Skip file symbols.
110       if (auto SymType = Sym.getType()) {
111         if (*SymType == object::SymbolRef::ST_File)
112           continue;
113       } else {
114         ES.reportError(SymType.takeError());
115         R->failMaterialization();
116         return;
117       }
118 
119       Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
120       if (!SymFlagsOrErr) {
121         // TODO: Test this error.
122         ES.reportError(SymFlagsOrErr.takeError());
123         R->failMaterialization();
124         return;
125       }
126 
127       // Don't include symbols that aren't global.
128       if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
129         if (auto SymName = Sym.getName())
130           InternalSymbols->insert(*SymName);
131         else {
132           ES.reportError(SymName.takeError());
133           R->failMaterialization();
134           return;
135         }
136       }
137     }
138   }
139 
140   auto MemMgr = GetMemoryManager();
141   auto &MemMgrRef = *MemMgr;
142 
143   // Switch to shared ownership of MR so that it can be captured by both
144   // lambdas below.
145   std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
146 
147   JITDylibSearchOrderResolver Resolver(*SharedR);
148 
149   jitLinkForORC(
150       object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
151       MemMgrRef, Resolver, ProcessAllSections,
152       [this, SharedR, &MemMgrRef, InternalSymbols](
153           const object::ObjectFile &Obj,
154           RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
155           std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
156         return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
157                          ResolvedSymbols, *InternalSymbols);
158       },
159       [this, SharedR, MemMgr = std::move(MemMgr)](
160           object::OwningBinary<object::ObjectFile> Obj,
161           std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
162           Error Err) mutable {
163         onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
164                   std::move(LoadedObjInfo), std::move(Err));
165       });
166 }
167 
registerJITEventListener(JITEventListener & L)168 void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
169   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
170   assert(llvm::none_of(EventListeners,
171                        [&](JITEventListener *O) { return O == &L; }) &&
172          "Listener has already been registered");
173   EventListeners.push_back(&L);
174 }
175 
unregisterJITEventListener(JITEventListener & L)176 void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
177   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
178   auto I = llvm::find(EventListeners, &L);
179   assert(I != EventListeners.end() && "Listener not registered");
180   EventListeners.erase(I);
181 }
182 
onObjLoad(MaterializationResponsibility & R,const object::ObjectFile & Obj,RuntimeDyld::MemoryManager & MemMgr,RuntimeDyld::LoadedObjectInfo & LoadedObjInfo,std::map<StringRef,JITEvaluatedSymbol> Resolved,std::set<StringRef> & InternalSymbols)183 Error RTDyldObjectLinkingLayer::onObjLoad(
184     MaterializationResponsibility &R, const object::ObjectFile &Obj,
185     RuntimeDyld::MemoryManager &MemMgr,
186     RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
187     std::map<StringRef, JITEvaluatedSymbol> Resolved,
188     std::set<StringRef> &InternalSymbols) {
189   SymbolFlagsMap ExtraSymbolsToClaim;
190   SymbolMap Symbols;
191 
192   // Hack to support COFF constant pool comdats introduced during compilation:
193   // (See http://llvm.org/PR40074)
194   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
195     auto &ES = getExecutionSession();
196 
197     // For all resolved symbols that are not already in the responsibilty set:
198     // check whether the symbol is in a comdat section and if so mark it as
199     // weak.
200     for (auto &Sym : COFFObj->symbols()) {
201       // getFlags() on COFF symbols can't fail.
202       uint32_t SymFlags = cantFail(Sym.getFlags());
203       if (SymFlags & object::BasicSymbolRef::SF_Undefined)
204         continue;
205       auto Name = Sym.getName();
206       if (!Name)
207         return Name.takeError();
208       auto I = Resolved.find(*Name);
209 
210       // Skip unresolved symbols, internal symbols, and symbols that are
211       // already in the responsibility set.
212       if (I == Resolved.end() || InternalSymbols.count(*Name) ||
213           R.getSymbols().count(ES.intern(*Name)))
214         continue;
215       auto Sec = Sym.getSection();
216       if (!Sec)
217         return Sec.takeError();
218       if (*Sec == COFFObj->section_end())
219         continue;
220       auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
221       if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
222         I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
223     }
224   }
225 
226   for (auto &KV : Resolved) {
227     // Scan the symbols and add them to the Symbols map for resolution.
228 
229     // We never claim internal symbols.
230     if (InternalSymbols.count(KV.first))
231       continue;
232 
233     auto InternedName = getExecutionSession().intern(KV.first);
234     auto Flags = KV.second.getFlags();
235 
236     // Override object flags and claim responsibility for symbols if
237     // requested.
238     if (OverrideObjectFlags || AutoClaimObjectSymbols) {
239       auto I = R.getSymbols().find(InternedName);
240 
241       if (OverrideObjectFlags && I != R.getSymbols().end())
242         Flags = I->second;
243       else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
244         ExtraSymbolsToClaim[InternedName] = Flags;
245     }
246 
247     Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
248   }
249 
250   if (!ExtraSymbolsToClaim.empty()) {
251     if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
252       return Err;
253 
254     // If we claimed responsibility for any weak symbols but were rejected then
255     // we need to remove them from the resolved set.
256     for (auto &KV : ExtraSymbolsToClaim)
257       if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
258         Symbols.erase(KV.first);
259   }
260 
261   if (auto Err = R.notifyResolved(Symbols)) {
262     R.failMaterialization();
263     return Err;
264   }
265 
266   if (NotifyLoaded)
267     NotifyLoaded(R, Obj, LoadedObjInfo);
268 
269   return Error::success();
270 }
271 
onObjEmit(MaterializationResponsibility & R,object::OwningBinary<object::ObjectFile> O,std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,Error Err)272 void RTDyldObjectLinkingLayer::onObjEmit(
273     MaterializationResponsibility &R,
274     object::OwningBinary<object::ObjectFile> O,
275     std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
276     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
277   if (Err) {
278     getExecutionSession().reportError(std::move(Err));
279     R.failMaterialization();
280     return;
281   }
282 
283   if (auto Err = R.notifyEmitted()) {
284     getExecutionSession().reportError(std::move(Err));
285     R.failMaterialization();
286     return;
287   }
288 
289   std::unique_ptr<object::ObjectFile> Obj;
290   std::unique_ptr<MemoryBuffer> ObjBuffer;
291   std::tie(Obj, ObjBuffer) = O.takeBinary();
292 
293   // Run EventListener notifyLoaded callbacks.
294   {
295     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
296     for (auto *L : EventListeners)
297       L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
298                             *LoadedObjInfo);
299   }
300 
301   if (NotifyEmitted)
302     NotifyEmitted(R, std::move(ObjBuffer));
303 
304   if (auto Err = R.withResourceKeyDo(
305           [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
306     getExecutionSession().reportError(std::move(Err));
307     R.failMaterialization();
308   }
309 }
310 
handleRemoveResources(ResourceKey K)311 Error RTDyldObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
312 
313   std::vector<MemoryManagerUP> MemMgrsToRemove;
314 
315   getExecutionSession().runSessionLocked([&] {
316     auto I = MemMgrs.find(K);
317     if (I != MemMgrs.end()) {
318       std::swap(MemMgrsToRemove, I->second);
319       MemMgrs.erase(I);
320     }
321   });
322 
323   {
324     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
325     for (auto &MemMgr : MemMgrsToRemove) {
326       for (auto *L : EventListeners)
327         L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
328       MemMgr->deregisterEHFrames();
329     }
330   }
331 
332   return Error::success();
333 }
334 
handleTransferResources(ResourceKey DstKey,ResourceKey SrcKey)335 void RTDyldObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
336                                                        ResourceKey SrcKey) {
337   auto I = MemMgrs.find(SrcKey);
338   if (I != MemMgrs.end()) {
339     auto &SrcMemMgrs = I->second;
340     auto &DstMemMgrs = MemMgrs[DstKey];
341     DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
342     for (auto &MemMgr : SrcMemMgrs)
343       DstMemMgrs.push_back(std::move(MemMgr));
344 
345     // Erase SrcKey entry using value rather than iterator I: I may have been
346     // invalidated when we looked up DstKey.
347     MemMgrs.erase(SrcKey);
348   }
349 }
350 
351 } // End namespace orc.
352 } // End namespace llvm.
353