xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
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 RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
79     ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80     : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {
81   ES.registerResourceManager(*this);
82 }
83 
84 RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
85   assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
86 }
87 
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 
168 void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
169   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
170   assert(!llvm::is_contained(EventListeners, &L) &&
171          "Listener has already been registered");
172   EventListeners.push_back(&L);
173 }
174 
175 void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
176   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
177   auto I = llvm::find(EventListeners, &L);
178   assert(I != EventListeners.end() && "Listener not registered");
179   EventListeners.erase(I);
180 }
181 
182 Error RTDyldObjectLinkingLayer::onObjLoad(
183     MaterializationResponsibility &R, const object::ObjectFile &Obj,
184     RuntimeDyld::MemoryManager &MemMgr,
185     RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
186     std::map<StringRef, JITEvaluatedSymbol> Resolved,
187     std::set<StringRef> &InternalSymbols) {
188   SymbolFlagsMap ExtraSymbolsToClaim;
189   SymbolMap Symbols;
190 
191   // Hack to support COFF constant pool comdats introduced during compilation:
192   // (See http://llvm.org/PR40074)
193   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
194     auto &ES = getExecutionSession();
195 
196     // For all resolved symbols that are not already in the responsibilty set:
197     // check whether the symbol is in a comdat section and if so mark it as
198     // weak.
199     for (auto &Sym : COFFObj->symbols()) {
200       // getFlags() on COFF symbols can't fail.
201       uint32_t SymFlags = cantFail(Sym.getFlags());
202       if (SymFlags & object::BasicSymbolRef::SF_Undefined)
203         continue;
204       auto Name = Sym.getName();
205       if (!Name)
206         return Name.takeError();
207       auto I = Resolved.find(*Name);
208 
209       // Skip unresolved symbols, internal symbols, and symbols that are
210       // already in the responsibility set.
211       if (I == Resolved.end() || InternalSymbols.count(*Name) ||
212           R.getSymbols().count(ES.intern(*Name)))
213         continue;
214       auto Sec = Sym.getSection();
215       if (!Sec)
216         return Sec.takeError();
217       if (*Sec == COFFObj->section_end())
218         continue;
219       auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
220       if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
221         I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
222     }
223   }
224 
225   for (auto &KV : Resolved) {
226     // Scan the symbols and add them to the Symbols map for resolution.
227 
228     // We never claim internal symbols.
229     if (InternalSymbols.count(KV.first))
230       continue;
231 
232     auto InternedName = getExecutionSession().intern(KV.first);
233     auto Flags = KV.second.getFlags();
234 
235     // Override object flags and claim responsibility for symbols if
236     // requested.
237     if (OverrideObjectFlags || AutoClaimObjectSymbols) {
238       auto I = R.getSymbols().find(InternedName);
239 
240       if (OverrideObjectFlags && I != R.getSymbols().end())
241         Flags = I->second;
242       else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
243         ExtraSymbolsToClaim[InternedName] = Flags;
244     }
245 
246     Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
247   }
248 
249   if (!ExtraSymbolsToClaim.empty()) {
250     if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
251       return Err;
252 
253     // If we claimed responsibility for any weak symbols but were rejected then
254     // we need to remove them from the resolved set.
255     for (auto &KV : ExtraSymbolsToClaim)
256       if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
257         Symbols.erase(KV.first);
258   }
259 
260   if (auto Err = R.notifyResolved(Symbols)) {
261     R.failMaterialization();
262     return Err;
263   }
264 
265   if (NotifyLoaded)
266     NotifyLoaded(R, Obj, LoadedObjInfo);
267 
268   return Error::success();
269 }
270 
271 void RTDyldObjectLinkingLayer::onObjEmit(
272     MaterializationResponsibility &R,
273     object::OwningBinary<object::ObjectFile> O,
274     std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
275     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
276   if (Err) {
277     getExecutionSession().reportError(std::move(Err));
278     R.failMaterialization();
279     return;
280   }
281 
282   if (auto Err = R.notifyEmitted()) {
283     getExecutionSession().reportError(std::move(Err));
284     R.failMaterialization();
285     return;
286   }
287 
288   std::unique_ptr<object::ObjectFile> Obj;
289   std::unique_ptr<MemoryBuffer> ObjBuffer;
290   std::tie(Obj, ObjBuffer) = O.takeBinary();
291 
292   // Run EventListener notifyLoaded callbacks.
293   {
294     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
295     for (auto *L : EventListeners)
296       L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
297                             *LoadedObjInfo);
298   }
299 
300   if (NotifyEmitted)
301     NotifyEmitted(R, std::move(ObjBuffer));
302 
303   if (auto Err = R.withResourceKeyDo(
304           [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
305     getExecutionSession().reportError(std::move(Err));
306     R.failMaterialization();
307   }
308 }
309 
310 Error RTDyldObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
311 
312   std::vector<MemoryManagerUP> MemMgrsToRemove;
313 
314   getExecutionSession().runSessionLocked([&] {
315     auto I = MemMgrs.find(K);
316     if (I != MemMgrs.end()) {
317       std::swap(MemMgrsToRemove, I->second);
318       MemMgrs.erase(I);
319     }
320   });
321 
322   {
323     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
324     for (auto &MemMgr : MemMgrsToRemove) {
325       for (auto *L : EventListeners)
326         L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
327       MemMgr->deregisterEHFrames();
328     }
329   }
330 
331   return Error::success();
332 }
333 
334 void RTDyldObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
335                                                        ResourceKey SrcKey) {
336   auto I = MemMgrs.find(SrcKey);
337   if (I != MemMgrs.end()) {
338     auto &SrcMemMgrs = I->second;
339     auto &DstMemMgrs = MemMgrs[DstKey];
340     DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
341     for (auto &MemMgr : SrcMemMgrs)
342       DstMemMgrs.push_back(std::move(MemMgr));
343 
344     // Erase SrcKey entry using value rather than iterator I: I may have been
345     // invalidated when we looked up DstKey.
346     MemMgrs.erase(SrcKey);
347   }
348 }
349 
350 } // End namespace orc.
351 } // End namespace llvm.
352