blob: 3344bd4d53f98f61f56be5d2ea0a5721eb9ae557 [file] [log] [blame]
Lang Hames373f4622018-05-21 23:45:40 +00001//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Lang Hames373f4622018-05-21 23:45:40 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
10
Lang Hamesfd0c1e712018-07-20 18:31:50 +000011namespace {
12
13using namespace llvm;
14using namespace llvm::orc;
15
Lang Hamesd5f56c52018-08-17 21:18:18 +000016class JITDylibSearchOrderResolver : public JITSymbolResolver {
Lang Hamesfd0c1e712018-07-20 18:31:50 +000017public:
Lang Hamesd5f56c52018-08-17 21:18:18 +000018 JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
Lang Hamesfd0c1e712018-07-20 18:31:50 +000019
Lang Hamesadde5ba2018-09-25 19:48:46 +000020 void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) {
Lang Hamesd5f56c52018-08-17 21:18:18 +000021 auto &ES = MR.getTargetJITDylib().getExecutionSession();
Lang Hames674df132019-11-25 21:57:27 -080022 SymbolLookupSet InternedSymbols;
Lang Hamesfd0c1e712018-07-20 18:31:50 +000023
Lang Hamesadde5ba2018-09-25 19:48:46 +000024 // Intern the requested symbols: lookup takes interned strings.
Lang Hamesfd0c1e712018-07-20 18:31:50 +000025 for (auto &S : Symbols)
Lang Hames674df132019-11-25 21:57:27 -080026 InternedSymbols.add(ES.intern(S));
Lang Hamesfd0c1e712018-07-20 18:31:50 +000027
Lang Hamesadde5ba2018-09-25 19:48:46 +000028 // Build an OnResolve callback to unwrap the interned strings and pass them
29 // to the OnResolved callback.
Lang Hamesadde5ba2018-09-25 19:48:46 +000030 auto OnResolvedWithUnwrap =
Benjamin Kramerce74c3b2019-09-13 11:35:33 +000031 [OnResolved = std::move(OnResolved)](
32 Expected<SymbolMap> InternedResult) mutable {
Lang Hamesadde5ba2018-09-25 19:48:46 +000033 if (!InternedResult) {
34 OnResolved(InternedResult.takeError());
35 return;
36 }
37
38 LookupResult Result;
39 for (auto &KV : *InternedResult)
40 Result[*KV.first] = std::move(KV.second);
41 OnResolved(Result);
42 };
43
Lang Hamesadde5ba2018-09-25 19:48:46 +000044 // Register dependencies for all symbols contained in this set.
Lang Hamesa48d1082018-07-20 22:22:19 +000045 auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
Lang Hames960246d2018-07-21 00:12:05 +000046 MR.addDependenciesForAll(Deps);
Lang Hamesfd0c1e712018-07-20 18:31:50 +000047 };
48
Lang Hames674df132019-11-25 21:57:27 -080049 JITDylibSearchOrder SearchOrder;
Lang Hames23cb2e72018-10-23 23:01:39 +000050 MR.getTargetJITDylib().withSearchOrderDo(
Lang Hames674df132019-11-25 21:57:27 -080051 [&](const JITDylibSearchOrder &JDs) { SearchOrder = JDs; });
52 ES.lookup(LookupKind::Static, SearchOrder, InternedSymbols,
53 SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
54 RegisterDependencies);
Lang Hamesfd0c1e712018-07-20 18:31:50 +000055 }
56
Lang Hames6cadc7c2018-08-28 21:18:05 +000057 Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
58 LookupSet Result;
Lang Hamesa48d1082018-07-20 22:22:19 +000059
Lang Hames6cadc7c2018-08-28 21:18:05 +000060 for (auto &KV : MR.getSymbols()) {
61 if (Symbols.count(*KV.first))
62 Result.insert(*KV.first);
63 }
Lang Hamesfd0c1e712018-07-20 18:31:50 +000064
65 return Result;
66 }
67
68private:
Lang Hamesfd0c1e712018-07-20 18:31:50 +000069 MaterializationResponsibility &MR;
70};
71
72} // end anonymous namespace
73
Lang Hames373f4622018-05-21 23:45:40 +000074namespace llvm {
75namespace orc {
76
Lang Hames079df9a2018-10-15 22:56:10 +000077RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
Lang Hames42a3b4f2019-05-01 22:40:23 +000078 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
79 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
Lang Hames373f4622018-05-21 23:45:40 +000080
Lang Hames079df9a2018-10-15 22:56:10 +000081void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R,
Lang Hames8b942742018-10-16 20:13:06 +000082 std::unique_ptr<MemoryBuffer> O) {
Lang Hames4caa2f72018-05-23 21:27:01 +000083 assert(O && "Object must not be null");
Lang Hames373f4622018-05-21 23:45:40 +000084
Lang Hamesabeedf12018-09-25 22:57:44 +000085 // This method launches an asynchronous link step that will fulfill our
86 // materialization responsibility. We need to switch R to be heap
87 // allocated before that happens so it can live as long as the asynchronous
88 // link needs it to (i.e. it must be able to outlive this method).
89 auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R));
90
Lang Hames373f4622018-05-21 23:45:40 +000091 auto &ES = getExecutionSession();
92
Lang Hames42a3b4f2019-05-01 22:40:23 +000093 // Create a MemoryBufferRef backed MemoryBuffer (i.e. shallow) copy of the
94 // the underlying buffer to pass into RuntimeDyld. This allows us to hold
95 // ownership of the real underlying buffer and return it to the user once
96 // the object has been emitted.
97 auto ObjBuffer = MemoryBuffer::getMemBuffer(O->getMemBufferRef(), false);
98
99 auto Obj = object::ObjectFile::createObjectFile(*ObjBuffer);
Lang Hamesabeedf12018-09-25 22:57:44 +0000100
101 if (!Obj) {
102 getExecutionSession().reportError(Obj.takeError());
103 SharedR->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 if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
113 if (auto SymName = Sym.getName())
114 InternalSymbols->insert(*SymName);
115 else {
116 ES.reportError(SymName.takeError());
117 R.failMaterialization();
118 return;
119 }
120 }
121 }
Lang Hames373f4622018-05-21 23:45:40 +0000122 }
123
Lang Hames8b942742018-10-16 20:13:06 +0000124 auto K = R.getVModuleKey();
Lang Hames95abade2018-10-22 21:17:56 +0000125 RuntimeDyld::MemoryManager *MemMgr = nullptr;
126
127 // Create a record a memory manager for this object.
128 {
129 auto Tmp = GetMemoryManager();
130 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
131 MemMgrs.push_back(std::move(Tmp));
132 MemMgr = MemMgrs.back().get();
133 }
Lang Hames373f4622018-05-21 23:45:40 +0000134
Lang Hamesabeedf12018-09-25 22:57:44 +0000135 JITDylibSearchOrderResolver Resolver(*SharedR);
Lang Hames373f4622018-05-21 23:45:40 +0000136
Lang Hamesabeedf12018-09-25 22:57:44 +0000137 jitLinkForORC(
Lang Hames95abade2018-10-22 21:17:56 +0000138 **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections,
Lang Hamesabeedf12018-09-25 22:57:44 +0000139 [this, K, SharedR, &Obj, InternalSymbols](
140 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
141 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
142 return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo),
143 ResolvedSymbols, *InternalSymbols);
144 },
Benjamin Kramerce74c3b2019-09-13 11:35:33 +0000145 [this, K, SharedR, O = std::move(O)](Error Err) mutable {
146 onObjEmit(K, std::move(O), *SharedR, std::move(Err));
Lang Hamesabeedf12018-09-25 22:57:44 +0000147 });
148}
149
Lang Hames079df9a2018-10-15 22:56:10 +0000150Error RTDyldObjectLinkingLayer::onObjLoad(
Lang Hamesabeedf12018-09-25 22:57:44 +0000151 VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj,
152 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
153 std::map<StringRef, JITEvaluatedSymbol> Resolved,
154 std::set<StringRef> &InternalSymbols) {
155 SymbolFlagsMap ExtraSymbolsToClaim;
156 SymbolMap Symbols;
157 for (auto &KV : Resolved) {
158 // Scan the symbols and add them to the Symbols map for resolution.
159
160 // We never claim internal symbols.
161 if (InternalSymbols.count(KV.first))
162 continue;
163
Lang Hames71d781c2018-09-30 23:18:24 +0000164 auto InternedName = getExecutionSession().intern(KV.first);
Lang Hamesabeedf12018-09-25 22:57:44 +0000165 auto Flags = KV.second.getFlags();
166
167 // Override object flags and claim responsibility for symbols if
168 // requested.
169 if (OverrideObjectFlags || AutoClaimObjectSymbols) {
170 auto I = R.getSymbols().find(InternedName);
171
172 if (OverrideObjectFlags && I != R.getSymbols().end())
Lang Hameseb5ee302019-05-28 23:35:44 +0000173 Flags = I->second;
Lang Hamesabeedf12018-09-25 22:57:44 +0000174 else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
175 ExtraSymbolsToClaim[InternedName] = Flags;
Lang Hames68c9b8d2018-06-18 18:01:43 +0000176 }
177
Lang Hamesabeedf12018-09-25 22:57:44 +0000178 Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
Lang Hames373f4622018-05-21 23:45:40 +0000179 }
180
Lang Hamesabeedf12018-09-25 22:57:44 +0000181 if (!ExtraSymbolsToClaim.empty())
182 if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
183 return Err;
184
Lang Hamese00585c2019-08-23 20:37:31 +0000185 if (auto Err = R.notifyResolved(Symbols)) {
186 R.failMaterialization();
187 return Err;
188 }
Lang Hamesabeedf12018-09-25 22:57:44 +0000189
Lang Hames373f4622018-05-21 23:45:40 +0000190 if (NotifyLoaded)
Lang Hamesabeedf12018-09-25 22:57:44 +0000191 NotifyLoaded(K, Obj, *LoadedObjInfo);
Lang Hames373f4622018-05-21 23:45:40 +0000192
Lang Hamesabeedf12018-09-25 22:57:44 +0000193 return Error::success();
194}
Lang Hames373f4622018-05-21 23:45:40 +0000195
Lang Hames42a3b4f2019-05-01 22:40:23 +0000196void RTDyldObjectLinkingLayer::onObjEmit(
197 VModuleKey K, std::unique_ptr<MemoryBuffer> ObjBuffer,
198 MaterializationResponsibility &R, Error Err) {
Lang Hamesabeedf12018-09-25 22:57:44 +0000199 if (Err) {
200 getExecutionSession().reportError(std::move(Err));
Lang Hames373f4622018-05-21 23:45:40 +0000201 R.failMaterialization();
Lang Hames68c9b8d2018-06-18 18:01:43 +0000202 return;
Lang Hames373f4622018-05-21 23:45:40 +0000203 }
204
Lang Hamese00585c2019-08-23 20:37:31 +0000205 if (auto Err = R.notifyEmitted()) {
206 getExecutionSession().reportError(std::move(Err));
207 R.failMaterialization();
208 return;
209 }
Lang Hames373f4622018-05-21 23:45:40 +0000210
Lang Hames76e21c92018-08-18 02:06:18 +0000211 if (NotifyEmitted)
Lang Hames42a3b4f2019-05-01 22:40:23 +0000212 NotifyEmitted(K, std::move(ObjBuffer));
Lang Hames373f4622018-05-21 23:45:40 +0000213}
214
Lang Hames17164542019-07-17 16:40:52 +0000215LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
216 ExecutionSession &ES, ResourcesGetter GetResources,
217 NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
218 NotifyFreedFtor NotifyFreed)
219 : ES(ES), GetResources(std::move(GetResources)),
220 NotifyLoaded(std::move(NotifyLoaded)),
221 NotifyFinalized(std::move(NotifyFinalized)),
222 NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
223
Lang Hames373f4622018-05-21 23:45:40 +0000224} // End namespace orc.
225} // End namespace llvm.