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 SearchOrder;
51     MR.getTargetJITDylib().withSearchOrderDo(
52         [&](const JITDylibSearchOrder &JDs) { SearchOrder = JDs; });
53     ES.lookup(LookupKind::Static, SearchOrder, 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       // Don't include symbols that aren't global.
129       if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
130         if (auto SymName = Sym.getName())
131           InternalSymbols->insert(*SymName);
132         else {
133           ES.reportError(SymName.takeError());
134           R.failMaterialization();
135           return;
136         }
137       }
138     }
139   }
140 
141   auto K = R.getVModuleKey();
142   RuntimeDyld::MemoryManager *MemMgr = nullptr;
143 
144   // Create a record a memory manager for this object.
145   {
146     auto Tmp = GetMemoryManager();
147     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
148     MemMgrs.push_back(std::move(Tmp));
149     MemMgr = MemMgrs.back().get();
150   }
151 
152   JITDylibSearchOrderResolver Resolver(*SharedR);
153 
154   jitLinkForORC(
155       object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
156       *MemMgr, Resolver, ProcessAllSections,
157       [this, K, SharedR, MemMgr, InternalSymbols](
158           const object::ObjectFile &Obj,
159           std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
160           std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
161         return onObjLoad(K, *SharedR, Obj, MemMgr, std::move(LoadedObjInfo),
162                          ResolvedSymbols, *InternalSymbols);
163       },
164       [this, K, SharedR, MemMgr](object::OwningBinary<object::ObjectFile> Obj,
165                                  Error Err) mutable {
166         onObjEmit(K, *SharedR, std::move(Obj), MemMgr, std::move(Err));
167       });
168 }
169 
170 void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
171   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
172   assert(llvm::none_of(EventListeners,
173                        [&](JITEventListener *O) { return O == &L; }) &&
174          "Listener has already been registered");
175   EventListeners.push_back(&L);
176 }
177 
178 void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
179   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
180   auto I = llvm::find(EventListeners, &L);
181   assert(I != EventListeners.end() && "Listener not registered");
182   EventListeners.erase(I);
183 }
184 
185 Error RTDyldObjectLinkingLayer::onObjLoad(
186     VModuleKey K, MaterializationResponsibility &R,
187     const object::ObjectFile &Obj, RuntimeDyld::MemoryManager *MemMgr,
188     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
189     std::map<StringRef, JITEvaluatedSymbol> Resolved,
190     std::set<StringRef> &InternalSymbols) {
191   SymbolFlagsMap ExtraSymbolsToClaim;
192   SymbolMap Symbols;
193 
194   // Hack to support COFF constant pool comdats introduced during compilation:
195   // (See http://llvm.org/PR40074)
196   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
197     auto &ES = getExecutionSession();
198 
199     // For all resolved symbols that are not already in the responsibilty set:
200     // check whether the symbol is in a comdat section and if so mark it as
201     // weak.
202     for (auto &Sym : COFFObj->symbols()) {
203       if (Sym.getFlags() & 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(K, Obj, *LoadedObjInfo);
268 
269   std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
270   assert(!LoadedObjInfos.count(MemMgr) && "Duplicate loaded info for MemMgr");
271   LoadedObjInfos[MemMgr] = std::move(LoadedObjInfo);
272 
273   return Error::success();
274 }
275 
276 void RTDyldObjectLinkingLayer::onObjEmit(
277     VModuleKey K, MaterializationResponsibility &R,
278     object::OwningBinary<object::ObjectFile> O,
279     RuntimeDyld::MemoryManager *MemMgr, Error Err) {
280   if (Err) {
281     getExecutionSession().reportError(std::move(Err));
282     R.failMaterialization();
283     return;
284   }
285 
286   if (auto Err = R.notifyEmitted()) {
287     getExecutionSession().reportError(std::move(Err));
288     R.failMaterialization();
289     return;
290   }
291 
292   std::unique_ptr<object::ObjectFile> Obj;
293   std::unique_ptr<MemoryBuffer> ObjBuffer;
294   std::tie(Obj, ObjBuffer) = O.takeBinary();
295 
296   // Run EventListener notifyLoaded callbacks.
297   {
298     std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
299     auto LOIItr = LoadedObjInfos.find(MemMgr);
300     assert(LOIItr != LoadedObjInfos.end() && "LoadedObjInfo missing");
301     for (auto *L : EventListeners)
302       L->notifyObjectLoaded(
303           static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr)), *Obj,
304           *LOIItr->second);
305     LoadedObjInfos.erase(MemMgr);
306   }
307 
308   if (NotifyEmitted)
309     NotifyEmitted(K, std::move(ObjBuffer));
310 }
311 
312 LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
313     ExecutionSession &ES, ResourcesGetter GetResources,
314     NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
315     NotifyFreedFtor NotifyFreed)
316     : ES(ES), GetResources(std::move(GetResources)),
317       NotifyLoaded(std::move(NotifyLoaded)),
318       NotifyFinalized(std::move(NotifyFinalized)),
319       NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
320 
321 } // End namespace orc.
322 } // End namespace llvm.
323