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