blob: b22ecd5f80a13e5556e024c14cc4f3943d956d42 [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 Hamesfd0c1e712018-07-20 18:31:50 +000022 SymbolNameSet InternedSymbols;
23
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 Hames71d781c2018-09-30 23:18:24 +000026 InternedSymbols.insert(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.
30 // FIXME: Switch to move capture of OnResolved once we have c++14.
31 auto OnResolvedWithUnwrap =
32 [OnResolved](Expected<SymbolMap> InternedResult) {
33 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 Hames23cb2e72018-10-23 23:01:39 +000049 JITDylibSearchList SearchOrder;
50 MR.getTargetJITDylib().withSearchOrderDo(
51 [&](const JITDylibSearchList &JDs) { SearchOrder = JDs; });
Lang Hamesd4a80892019-06-07 19:33:51 +000052 ES.lookup(SearchOrder, InternedSymbols, SymbolState::Resolved,
53 OnResolvedWithUnwrap, RegisterDependencies);
Lang Hamesfd0c1e712018-07-20 18:31:50 +000054 }
55
Lang Hames6cadc7c2018-08-28 21:18:05 +000056 Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
57 LookupSet Result;
Lang Hamesa48d1082018-07-20 22:22:19 +000058
Lang Hames6cadc7c2018-08-28 21:18:05 +000059 for (auto &KV : MR.getSymbols()) {
60 if (Symbols.count(*KV.first))
61 Result.insert(*KV.first);
62 }
Lang Hamesfd0c1e712018-07-20 18:31:50 +000063
64 return Result;
65 }
66
67private:
Lang Hamesfd0c1e712018-07-20 18:31:50 +000068 MaterializationResponsibility &MR;
69};
70
71} // end anonymous namespace
72
Lang Hames373f4622018-05-21 23:45:40 +000073namespace llvm {
74namespace orc {
75
Lang Hames079df9a2018-10-15 22:56:10 +000076RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
Lang Hames42a3b4f2019-05-01 22:40:23 +000077 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
78 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
Lang Hames373f4622018-05-21 23:45:40 +000079
Lang Hames079df9a2018-10-15 22:56:10 +000080void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R,
Lang Hames8b942742018-10-16 20:13:06 +000081 std::unique_ptr<MemoryBuffer> O) {
Lang Hames4caa2f72018-05-23 21:27:01 +000082 assert(O && "Object must not be null");
Lang Hames373f4622018-05-21 23:45:40 +000083
Lang Hamesabeedf12018-09-25 22:57:44 +000084 // This method launches an asynchronous link step that will fulfill our
85 // materialization responsibility. We need to switch R to be heap
86 // allocated before that happens so it can live as long as the asynchronous
87 // link needs it to (i.e. it must be able to outlive this method).
88 auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R));
89
Lang Hames373f4622018-05-21 23:45:40 +000090 auto &ES = getExecutionSession();
91
Lang Hames42a3b4f2019-05-01 22:40:23 +000092 // Create a MemoryBufferRef backed MemoryBuffer (i.e. shallow) copy of the
93 // the underlying buffer to pass into RuntimeDyld. This allows us to hold
94 // ownership of the real underlying buffer and return it to the user once
95 // the object has been emitted.
96 auto ObjBuffer = MemoryBuffer::getMemBuffer(O->getMemBufferRef(), false);
97
98 auto Obj = object::ObjectFile::createObjectFile(*ObjBuffer);
Lang Hamesabeedf12018-09-25 22:57:44 +000099
100 if (!Obj) {
101 getExecutionSession().reportError(Obj.takeError());
102 SharedR->failMaterialization();
103 return;
104 }
105
106 // Collect the internal symbols from the object file: We will need to
107 // filter these later.
108 auto InternalSymbols = std::make_shared<std::set<StringRef>>();
109 {
110 for (auto &Sym : (*Obj)->symbols()) {
111 if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
112 if (auto SymName = Sym.getName())
113 InternalSymbols->insert(*SymName);
114 else {
115 ES.reportError(SymName.takeError());
116 R.failMaterialization();
117 return;
118 }
119 }
120 }
Lang Hames373f4622018-05-21 23:45:40 +0000121 }
122
Lang Hames8b942742018-10-16 20:13:06 +0000123 auto K = R.getVModuleKey();
Lang Hames95abade2018-10-22 21:17:56 +0000124 RuntimeDyld::MemoryManager *MemMgr = nullptr;
125
126 // Create a record a memory manager for this object.
127 {
128 auto Tmp = GetMemoryManager();
129 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
130 MemMgrs.push_back(std::move(Tmp));
131 MemMgr = MemMgrs.back().get();
132 }
Lang Hames373f4622018-05-21 23:45:40 +0000133
Lang Hamesabeedf12018-09-25 22:57:44 +0000134 JITDylibSearchOrderResolver Resolver(*SharedR);
Lang Hames373f4622018-05-21 23:45:40 +0000135
Lang Hames42a3b4f2019-05-01 22:40:23 +0000136 // FIXME: Switch to move-capture for the 'O' buffer once we have c++14.
137 MemoryBuffer *UnownedObjBuffer = O.release();
Lang Hamesabeedf12018-09-25 22:57:44 +0000138 jitLinkForORC(
Lang Hames95abade2018-10-22 21:17:56 +0000139 **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections,
Lang Hamesabeedf12018-09-25 22:57:44 +0000140 [this, K, SharedR, &Obj, InternalSymbols](
141 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
142 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
143 return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo),
144 ResolvedSymbols, *InternalSymbols);
145 },
Lang Hames42a3b4f2019-05-01 22:40:23 +0000146 [this, K, SharedR, UnownedObjBuffer](Error Err) {
147 std::unique_ptr<MemoryBuffer> ObjBuffer(UnownedObjBuffer);
148 onObjEmit(K, std::move(ObjBuffer), *SharedR, std::move(Err));
Lang Hamesabeedf12018-09-25 22:57:44 +0000149 });
150}
151
Lang Hames079df9a2018-10-15 22:56:10 +0000152Error RTDyldObjectLinkingLayer::onObjLoad(
Lang Hamesabeedf12018-09-25 22:57:44 +0000153 VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj,
154 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
155 std::map<StringRef, JITEvaluatedSymbol> Resolved,
156 std::set<StringRef> &InternalSymbols) {
157 SymbolFlagsMap ExtraSymbolsToClaim;
158 SymbolMap Symbols;
159 for (auto &KV : Resolved) {
160 // Scan the symbols and add them to the Symbols map for resolution.
161
162 // We never claim internal symbols.
163 if (InternalSymbols.count(KV.first))
164 continue;
165
Lang Hames71d781c2018-09-30 23:18:24 +0000166 auto InternedName = getExecutionSession().intern(KV.first);
Lang Hamesabeedf12018-09-25 22:57:44 +0000167 auto Flags = KV.second.getFlags();
168
169 // Override object flags and claim responsibility for symbols if
170 // requested.
171 if (OverrideObjectFlags || AutoClaimObjectSymbols) {
172 auto I = R.getSymbols().find(InternedName);
173
174 if (OverrideObjectFlags && I != R.getSymbols().end())
Lang Hameseb5ee302019-05-28 23:35:44 +0000175 Flags = I->second;
Lang Hamesabeedf12018-09-25 22:57:44 +0000176 else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
177 ExtraSymbolsToClaim[InternedName] = Flags;
Lang Hames68c9b8d2018-06-18 18:01:43 +0000178 }
179
Lang Hamesabeedf12018-09-25 22:57:44 +0000180 Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
Lang Hames373f4622018-05-21 23:45:40 +0000181 }
182
Lang Hamesabeedf12018-09-25 22:57:44 +0000183 if (!ExtraSymbolsToClaim.empty())
184 if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
185 return Err;
186
Lang Hames2f8c6f92019-06-13 20:11:23 +0000187 R.notifyResolved(Symbols);
Lang Hamesabeedf12018-09-25 22:57:44 +0000188
Lang Hames373f4622018-05-21 23:45:40 +0000189 if (NotifyLoaded)
Lang Hamesabeedf12018-09-25 22:57:44 +0000190 NotifyLoaded(K, Obj, *LoadedObjInfo);
Lang Hames373f4622018-05-21 23:45:40 +0000191
Lang Hamesabeedf12018-09-25 22:57:44 +0000192 return Error::success();
193}
Lang Hames373f4622018-05-21 23:45:40 +0000194
Lang Hames42a3b4f2019-05-01 22:40:23 +0000195void RTDyldObjectLinkingLayer::onObjEmit(
196 VModuleKey K, std::unique_ptr<MemoryBuffer> ObjBuffer,
197 MaterializationResponsibility &R, Error Err) {
Lang Hamesabeedf12018-09-25 22:57:44 +0000198 if (Err) {
199 getExecutionSession().reportError(std::move(Err));
Lang Hames373f4622018-05-21 23:45:40 +0000200 R.failMaterialization();
Lang Hames68c9b8d2018-06-18 18:01:43 +0000201 return;
Lang Hames373f4622018-05-21 23:45:40 +0000202 }
203
Lang Hames2f8c6f92019-06-13 20:11:23 +0000204 R.notifyEmitted();
Lang Hames373f4622018-05-21 23:45:40 +0000205
Lang Hames76e21c92018-08-18 02:06:18 +0000206 if (NotifyEmitted)
Lang Hames42a3b4f2019-05-01 22:40:23 +0000207 NotifyEmitted(K, std::move(ObjBuffer));
Lang Hames373f4622018-05-21 23:45:40 +0000208}
209
Lang Hames17164542019-07-17 16:40:52 +0000210LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
211 ExecutionSession &ES, ResourcesGetter GetResources,
212 NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
213 NotifyFreedFtor NotifyFreed)
214 : ES(ES), GetResources(std::move(GetResources)),
215 NotifyLoaded(std::move(NotifyLoaded)),
216 NotifyFinalized(std::move(NotifyFinalized)),
217 NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
218
Lang Hames373f4622018-05-21 23:45:40 +0000219} // End namespace orc.
220} // End namespace llvm.