xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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:
19   JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20 
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 
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 
78 char RTDyldObjectLinkingLayer::ID;
79 
80 using BaseT = RTTIExtends<RTDyldObjectLinkingLayer, ObjectLayer>;
81 
82 RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
83     ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
84     : BaseT(ES), GetMemoryManager(std::move(GetMemoryManager)) {
85   ES.registerResourceManager(*this);
86 }
87 
88 RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
89   assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
90 }
91 
92 void RTDyldObjectLinkingLayer::emit(
93     std::unique_ptr<MaterializationResponsibility> R,
94     std::unique_ptr<MemoryBuffer> O) {
95   assert(O && "Object must not be null");
96 
97   auto &ES = getExecutionSession();
98 
99   auto Obj = object::ObjectFile::createObjectFile(*O);
100 
101   if (!Obj) {
102     getExecutionSession().reportError(Obj.takeError());
103     R->failMaterialization();
104     return;
105   }
106 
107   // Collect the internal symbols from the object file: We will need to
108   // filter these later.
109   auto InternalSymbols = std::make_shared<std::set<StringRef>>();
110   {
111     SymbolFlagsMap ExtraSymbolsToClaim;
112     for (auto &Sym : (*Obj)->symbols()) {
113 
114       // Skip file symbols.
115       if (auto SymType = Sym.getType()) {
116         if (*SymType == object::SymbolRef::ST_File)
117           continue;
118       } else {
119         ES.reportError(SymType.takeError());
120         R->failMaterialization();
121         return;
122       }
123 
124       Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
125       if (!SymFlagsOrErr) {
126         // TODO: Test this error.
127         ES.reportError(SymFlagsOrErr.takeError());
128         R->failMaterialization();
129         return;
130       }
131 
132       // Try to claim responsibility of weak symbols
133       // if AutoClaimObjectSymbols flag is set.
134       if (AutoClaimObjectSymbols &&
135           (*SymFlagsOrErr & object::BasicSymbolRef::SF_Weak)) {
136         auto SymName = Sym.getName();
137         if (!SymName) {
138           ES.reportError(SymName.takeError());
139           R->failMaterialization();
140           return;
141         }
142 
143         // Already included in responsibility set, skip it
144         SymbolStringPtr SymbolName = ES.intern(*SymName);
145         if (R->getSymbols().count(SymbolName))
146           continue;
147 
148         auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
149         if (!SymFlags) {
150           ES.reportError(SymFlags.takeError());
151           R->failMaterialization();
152           return;
153         }
154 
155         ExtraSymbolsToClaim[SymbolName] = *SymFlags;
156         continue;
157       }
158 
159       // Don't include symbols that aren't global.
160       if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
161         if (auto SymName = Sym.getName())
162           InternalSymbols->insert(*SymName);
163         else {
164           ES.reportError(SymName.takeError());
165           R->failMaterialization();
166           return;
167         }
168       }
169     }
170 
171     if (!ExtraSymbolsToClaim.empty()) {
172       if (auto Err = R->defineMaterializing(ExtraSymbolsToClaim)) {
173         ES.reportError(std::move(Err));
174         R->failMaterialization();
175       }
176     }
177   }
178 
179   auto MemMgr = GetMemoryManager();
180   auto &MemMgrRef = *MemMgr;
181 
182   // Switch to shared ownership of MR so that it can be captured by both
183   // lambdas below.
184   std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
185 
186   JITDylibSearchOrderResolver Resolver(*SharedR);
187 
188   jitLinkForORC(
189       object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
190       MemMgrRef, Resolver, ProcessAllSections,
191       [this, SharedR, &MemMgrRef, InternalSymbols](
192           const object::ObjectFile &Obj,
193           RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
194           std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
195         return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
196                          ResolvedSymbols, *InternalSymbols);
197       },
198       [this, SharedR, MemMgr = std::move(MemMgr)](
199           object::OwningBinary<object::ObjectFile> Obj,
200           std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
201           Error Err) mutable {
202         onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
203                   std::move(LoadedObjInfo), std::move(Err));
204       });
205 }
206 
207 void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
208   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
209   assert(!llvm::is_contained(EventListeners, &L) &&
210          "Listener has already been registered");
211   EventListeners.push_back(&L);
212 }
213 
214 void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
215   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
216   auto I = llvm::find(EventListeners, &L);
217   assert(I != EventListeners.end() && "Listener not registered");
218   EventListeners.erase(I);
219 }
220 
221 Error RTDyldObjectLinkingLayer::onObjLoad(
222     MaterializationResponsibility &R, const object::ObjectFile &Obj,
223     RuntimeDyld::MemoryManager &MemMgr,
224     RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
225     std::map<StringRef, JITEvaluatedSymbol> Resolved,
226     std::set<StringRef> &InternalSymbols) {
227   SymbolFlagsMap ExtraSymbolsToClaim;
228   SymbolMap Symbols;
229 
230   // Hack to support COFF constant pool comdats introduced during compilation:
231   // (See http://llvm.org/PR40074)
232   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
233     auto &ES = getExecutionSession();
234 
235     // For all resolved symbols that are not already in the responsibilty set:
236     // check whether the symbol is in a comdat section and if so mark it as
237     // weak.
238     for (auto &Sym : COFFObj->symbols()) {
239       // getFlags() on COFF symbols can't fail.
240       uint32_t SymFlags = cantFail(Sym.getFlags());
241       if (SymFlags & object::BasicSymbolRef::SF_Undefined)
242         continue;
243       auto Name = Sym.getName();
244       if (!Name)
245         return Name.takeError();
246       auto I = Resolved.find(*Name);
247 
248       // Skip unresolved symbols, internal symbols, and symbols that are
249       // already in the responsibility set.
250       if (I == Resolved.end() || InternalSymbols.count(*Name) ||
251           R.getSymbols().count(ES.intern(*Name)))
252         continue;
253       auto Sec = Sym.getSection();
254       if (!Sec)
255         return Sec.takeError();
256       if (*Sec == COFFObj->section_end())
257         continue;
258       auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
259       if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
260         I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
261     }
262 
263     // Handle any aliases.
264     for (auto &Sym : COFFObj->symbols()) {
265       uint32_t SymFlags = cantFail(Sym.getFlags());
266       if (SymFlags & object::BasicSymbolRef::SF_Undefined)
267         continue;
268       auto Name = Sym.getName();
269       if (!Name)
270         return Name.takeError();
271       auto I = Resolved.find(*Name);
272 
273       // Skip already-resolved symbols, and symbols that we're not responsible
274       // for.
275       if (I != Resolved.end() || !R.getSymbols().count(ES.intern(*Name)))
276         continue;
277 
278       // Skip anything other than weak externals.
279       auto COFFSym = COFFObj->getCOFFSymbol(Sym);
280       if (!COFFSym.isWeakExternal())
281         continue;
282       auto *WeakExternal = COFFSym.getAux<object::coff_aux_weak_external>();
283       if (WeakExternal->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS)
284         continue;
285 
286       // We found an alias. Reuse the resolution of the alias target for the
287       // alias itself.
288       Expected<object::COFFSymbolRef> TargetSymbol =
289           COFFObj->getSymbol(WeakExternal->TagIndex);
290       if (!TargetSymbol)
291         return TargetSymbol.takeError();
292       Expected<StringRef> TargetName = COFFObj->getSymbolName(*TargetSymbol);
293       if (!TargetName)
294         return TargetName.takeError();
295       auto J = Resolved.find(*TargetName);
296       if (J == Resolved.end())
297         return make_error<StringError>("Could alias target " + *TargetName +
298                                            " not resolved",
299                                        inconvertibleErrorCode());
300       Resolved[*Name] = J->second;
301     }
302   }
303 
304   for (auto &KV : Resolved) {
305     // Scan the symbols and add them to the Symbols map for resolution.
306 
307     // We never claim internal symbols.
308     if (InternalSymbols.count(KV.first))
309       continue;
310 
311     auto InternedName = getExecutionSession().intern(KV.first);
312     auto Flags = KV.second.getFlags();
313     auto I = R.getSymbols().find(InternedName);
314     if (I != R.getSymbols().end()) {
315       // Override object flags and claim responsibility for symbols if
316       // requested.
317       if (OverrideObjectFlags)
318         Flags = I->second;
319       else {
320         // RuntimeDyld/MCJIT's weak tracking isn't compatible with ORC's. Even
321         // if we're not overriding flags in general we should set the weak flag
322         // according to the MaterializationResponsibility object symbol table.
323         if (I->second.isWeak())
324           Flags |= JITSymbolFlags::Weak;
325       }
326     } else if (AutoClaimObjectSymbols)
327       ExtraSymbolsToClaim[InternedName] = Flags;
328 
329     Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
330   }
331 
332   if (!ExtraSymbolsToClaim.empty()) {
333     if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
334       return Err;
335 
336     // If we claimed responsibility for any weak symbols but were rejected then
337     // we need to remove them from the resolved set.
338     for (auto &KV : ExtraSymbolsToClaim)
339       if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
340         Symbols.erase(KV.first);
341   }
342 
343   if (auto Err = R.notifyResolved(Symbols)) {
344     R.failMaterialization();
345     return Err;
346   }
347 
348   if (NotifyLoaded)
349     NotifyLoaded(R, Obj, LoadedObjInfo);
350 
351   return Error::success();
352 }
353 
354 void RTDyldObjectLinkingLayer::onObjEmit(
355     MaterializationResponsibility &R,
356     object::OwningBinary<object::ObjectFile> O,
357     std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
358     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
359   if (Err) {
360     getExecutionSession().reportError(std::move(Err));
361     R.failMaterialization();
362     return;
363   }
364 
365   if (auto Err = R.notifyEmitted()) {
366     getExecutionSession().reportError(std::move(Err));
367     R.failMaterialization();
368     return;
369   }
370 
371   std::unique_ptr<object::ObjectFile> Obj;
372   std::unique_ptr<MemoryBuffer> ObjBuffer;
373   std::tie(Obj, ObjBuffer) = O.takeBinary();
374 
375   // Run EventListener notifyLoaded callbacks.
376   {
377     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
378     for (auto *L : EventListeners)
379       L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
380                             *LoadedObjInfo);
381   }
382 
383   if (NotifyEmitted)
384     NotifyEmitted(R, std::move(ObjBuffer));
385 
386   if (auto Err = R.withResourceKeyDo(
387           [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
388     getExecutionSession().reportError(std::move(Err));
389     R.failMaterialization();
390   }
391 }
392 
393 Error RTDyldObjectLinkingLayer::handleRemoveResources(JITDylib &JD,
394                                                       ResourceKey K) {
395 
396   std::vector<MemoryManagerUP> MemMgrsToRemove;
397 
398   getExecutionSession().runSessionLocked([&] {
399     auto I = MemMgrs.find(K);
400     if (I != MemMgrs.end()) {
401       std::swap(MemMgrsToRemove, I->second);
402       MemMgrs.erase(I);
403     }
404   });
405 
406   {
407     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
408     for (auto &MemMgr : MemMgrsToRemove) {
409       for (auto *L : EventListeners)
410         L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
411       MemMgr->deregisterEHFrames();
412     }
413   }
414 
415   return Error::success();
416 }
417 
418 void RTDyldObjectLinkingLayer::handleTransferResources(JITDylib &JD,
419                                                        ResourceKey DstKey,
420                                                        ResourceKey SrcKey) {
421   auto I = MemMgrs.find(SrcKey);
422   if (I != MemMgrs.end()) {
423     auto &SrcMemMgrs = I->second;
424     auto &DstMemMgrs = MemMgrs[DstKey];
425     DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
426     for (auto &MemMgr : SrcMemMgrs)
427       DstMemMgrs.push_back(std::move(MemMgr));
428 
429     // Erase SrcKey entry using value rather than iterator I: I may have been
430     // invalidated when we looked up DstKey.
431     MemMgrs.erase(SrcKey);
432   }
433 }
434 
435 } // End namespace orc.
436 } // End namespace llvm.
437