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 
82 RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
83   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
84   for (auto &MemMgr : MemMgrs) {
85     for (auto *L : EventListeners)
86       L->notifyFreeingObject(
87           static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr.get())));
88     MemMgr->deregisterEHFrames();
89   }
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     for (auto &Sym : (*Obj)->symbols()) {
112 
113       // Skip file symbols.
114       if (auto SymType = Sym.getType()) {
115         if (*SymType == object::SymbolRef::ST_File)
116           continue;
117       } else {
118         ES.reportError(SymType.takeError());
119         R->failMaterialization();
120         return;
121       }
122 
123       Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
124       if (!SymFlagsOrErr) {
125         // TODO: Test this error.
126         ES.reportError(SymFlagsOrErr.takeError());
127         R->failMaterialization();
128         return;
129       }
130 
131       // Don't include symbols that aren't global.
132       if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
133         if (auto SymName = Sym.getName())
134           InternalSymbols->insert(*SymName);
135         else {
136           ES.reportError(SymName.takeError());
137           R->failMaterialization();
138           return;
139         }
140       }
141     }
142   }
143 
144   auto K = R->getVModuleKey();
145   RuntimeDyld::MemoryManager *MemMgr = nullptr;
146 
147   // Create a record a memory manager for this object.
148   {
149     auto Tmp = GetMemoryManager();
150     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
151     MemMgrs.push_back(std::move(Tmp));
152     MemMgr = MemMgrs.back().get();
153   }
154 
155   // Switch to shared ownership of MR so that it can be captured by both
156   // lambdas below.
157   std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
158 
159   JITDylibSearchOrderResolver Resolver(*SharedR);
160 
161   jitLinkForORC(
162       object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
163       *MemMgr, Resolver, ProcessAllSections,
164       [this, K, SharedR, MemMgr, InternalSymbols](
165           const object::ObjectFile &Obj,
166           std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
167           std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
168         return onObjLoad(K, *SharedR, Obj, MemMgr, std::move(LoadedObjInfo),
169                          ResolvedSymbols, *InternalSymbols);
170       },
171       [this, K, SharedR, MemMgr](object::OwningBinary<object::ObjectFile> Obj,
172                                  Error Err) mutable {
173         onObjEmit(K, *SharedR, std::move(Obj), MemMgr, std::move(Err));
174       });
175 }
176 
177 void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
178   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
179   assert(llvm::none_of(EventListeners,
180                        [&](JITEventListener *O) { return O == &L; }) &&
181          "Listener has already been registered");
182   EventListeners.push_back(&L);
183 }
184 
185 void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
186   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
187   auto I = llvm::find(EventListeners, &L);
188   assert(I != EventListeners.end() && "Listener not registered");
189   EventListeners.erase(I);
190 }
191 
192 Error RTDyldObjectLinkingLayer::onObjLoad(
193     VModuleKey K, MaterializationResponsibility &R,
194     const object::ObjectFile &Obj, RuntimeDyld::MemoryManager *MemMgr,
195     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
196     std::map<StringRef, JITEvaluatedSymbol> Resolved,
197     std::set<StringRef> &InternalSymbols) {
198   SymbolFlagsMap ExtraSymbolsToClaim;
199   SymbolMap Symbols;
200 
201   // Hack to support COFF constant pool comdats introduced during compilation:
202   // (See http://llvm.org/PR40074)
203   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
204     auto &ES = getExecutionSession();
205 
206     // For all resolved symbols that are not already in the responsibilty set:
207     // check whether the symbol is in a comdat section and if so mark it as
208     // weak.
209     for (auto &Sym : COFFObj->symbols()) {
210       // getFlags() on COFF symbols can't fail.
211       uint32_t SymFlags = cantFail(Sym.getFlags());
212       if (SymFlags & object::BasicSymbolRef::SF_Undefined)
213         continue;
214       auto Name = Sym.getName();
215       if (!Name)
216         return Name.takeError();
217       auto I = Resolved.find(*Name);
218 
219       // Skip unresolved symbols, internal symbols, and symbols that are
220       // already in the responsibility set.
221       if (I == Resolved.end() || InternalSymbols.count(*Name) ||
222           R.getSymbols().count(ES.intern(*Name)))
223         continue;
224       auto Sec = Sym.getSection();
225       if (!Sec)
226         return Sec.takeError();
227       if (*Sec == COFFObj->section_end())
228         continue;
229       auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
230       if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
231         I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
232     }
233   }
234 
235   for (auto &KV : Resolved) {
236     // Scan the symbols and add them to the Symbols map for resolution.
237 
238     // We never claim internal symbols.
239     if (InternalSymbols.count(KV.first))
240       continue;
241 
242     auto InternedName = getExecutionSession().intern(KV.first);
243     auto Flags = KV.second.getFlags();
244 
245     // Override object flags and claim responsibility for symbols if
246     // requested.
247     if (OverrideObjectFlags || AutoClaimObjectSymbols) {
248       auto I = R.getSymbols().find(InternedName);
249 
250       if (OverrideObjectFlags && I != R.getSymbols().end())
251         Flags = I->second;
252       else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
253         ExtraSymbolsToClaim[InternedName] = Flags;
254     }
255 
256     Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
257   }
258 
259   if (!ExtraSymbolsToClaim.empty()) {
260     if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
261       return Err;
262 
263     // If we claimed responsibility for any weak symbols but were rejected then
264     // we need to remove them from the resolved set.
265     for (auto &KV : ExtraSymbolsToClaim)
266       if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
267         Symbols.erase(KV.first);
268   }
269 
270   if (auto Err = R.notifyResolved(Symbols)) {
271     R.failMaterialization();
272     return Err;
273   }
274 
275   if (NotifyLoaded)
276     NotifyLoaded(K, Obj, *LoadedObjInfo);
277 
278   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
279   assert(!LoadedObjInfos.count(MemMgr) && "Duplicate loaded info for MemMgr");
280   LoadedObjInfos[MemMgr] = std::move(LoadedObjInfo);
281 
282   return Error::success();
283 }
284 
285 void RTDyldObjectLinkingLayer::onObjEmit(
286     VModuleKey K, MaterializationResponsibility &R,
287     object::OwningBinary<object::ObjectFile> O,
288     RuntimeDyld::MemoryManager *MemMgr, Error Err) {
289   if (Err) {
290     getExecutionSession().reportError(std::move(Err));
291     R.failMaterialization();
292     return;
293   }
294 
295   if (auto Err = R.notifyEmitted()) {
296     getExecutionSession().reportError(std::move(Err));
297     R.failMaterialization();
298     return;
299   }
300 
301   std::unique_ptr<object::ObjectFile> Obj;
302   std::unique_ptr<MemoryBuffer> ObjBuffer;
303   std::tie(Obj, ObjBuffer) = O.takeBinary();
304 
305   // Run EventListener notifyLoaded callbacks.
306   {
307     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
308     auto LOIItr = LoadedObjInfos.find(MemMgr);
309     assert(LOIItr != LoadedObjInfos.end() && "LoadedObjInfo missing");
310     for (auto *L : EventListeners)
311       L->notifyObjectLoaded(
312           static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr)), *Obj,
313           *LOIItr->second);
314     LoadedObjInfos.erase(MemMgr);
315   }
316 
317   if (NotifyEmitted)
318     NotifyEmitted(K, std::move(ObjBuffer));
319 }
320 
321 LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
322     ExecutionSession &ES, ResourcesGetter GetResources,
323     NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
324     NotifyFreedFtor NotifyFreed)
325     : ES(ES), GetResources(std::move(GetResources)),
326       NotifyLoaded(std::move(NotifyLoaded)),
327       NotifyFinalized(std::move(NotifyFinalized)),
328       NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
329 
330 } // End namespace orc.
331 } // End namespace llvm.
332