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