llvm.org GIT mirror llvm / da62155
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through MCJIT. This patch decouples the two responsibilities of the RTDyldMemoryManager class, memory management and symbol resolution, into two new classes: RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver. The symbol resolution interface is modified slightly, from: uint64_t getSymbolAddress(const std::string &Name); to: RuntimeDyld::SymbolInfo findSymbol(const std::string &Name); The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld and others to reason about non-strong/non-exported symbols. The memory management interface removes the following method: void notifyObjectLoaded(ExecutionEngine *EE, const object::ObjectFile &) {} as it is not related to memory management. (Note: Backwards compatibility *is* maintained for this method in MCJIT and OrcMCJITReplacement, see below). The RTDyldMemoryManager class remains in-tree for backwards compatibility. It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which just subclasses RuntimeDyld::MemoryManager and reintroduces the notifyObjectLoaded method for backwards compatibility). The EngineBuilder class retains the existing method: EngineBuilder& setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm); and includes two new methods: EngineBuilder& setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM); EngineBuilder& setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR); Clients should use EITHER: A single call to setMCJITMemoryManager with an RTDyldMemoryManager. OR (exclusive) One call each to each of setMemoryManager and setSymbolResolver. This patch should be fully compatible with existing uses of RTDyldMemoryManager. If it is not it should be considered a bug, and the patch either fixed or reverted. If clients find the new API to be an improvement the goal will be to deprecate and eventually remove the RTDyldMemoryManager class in favor of the new classes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233509 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 4 years ago
38 changed file(s) with 758 addition(s) and 568 deletion(s). Raw diff Collapse all Expand all
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
3 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
34 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
45 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
56 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
11671168 KaleidoscopeJIT(SessionContext &Session)
11681169 : Session(Session),
11691170 Mang(Session.getTarget().getDataLayout()),
1170 ObjectLayer(
1171 [](){ return llvm::make_unique(); }),
11721171 CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
11731172 LazyEmitLayer(CompileLayer),
1174 CompileCallbacks(LazyEmitLayer, Session.getLLVMContext(),
1173 CompileCallbacks(LazyEmitLayer, CCMgrMemMgr, Session.getLLVMContext(),
11751174 reinterpret_cast(EarthShatteringKaboom),
11761175 64) {}
11771176
11931192 // We need a memory manager to allocate memory and resolve symbols for this
11941193 // new module. Create one that resolves symbols by looking back into the
11951194 // JIT.
1196 auto MM = createLookasideRTDyldMM(
1197 [&](const std::string &Name) {
1198 // First try to find 'Name' within the JIT.
1199 if (auto Symbol = findSymbol(Name))
1200 return Symbol.getAddress();
1201
1202 // If we don't already have a definition of 'Name' then search
1203 // the ASTs.
1204 return searchFunctionASTs(Name);
1205 },
1206 [](const std::string &S) { return 0; } );
1195 auto Resolver = createLambdaResolver(
1196 [&](const std::string &Name) {
1197 // First try to find 'Name' within the JIT.
1198 if (auto Symbol = findSymbol(Name))
1199 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
1200 Symbol.getFlags());
1201
1202 // If we don't already have a definition of 'Name' then search
1203 // the ASTs.
1204 return searchFunctionASTs(Name);
1205 },
1206 [](const std::string &S) { return nullptr; } );
12071207
12081208 return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
1209 std::move(MM));
1209 make_unique(),
1210 std::move(Resolver));
12101211 }
12111212
12121213 void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
12311232
12321233 // This method searches the FunctionDefs map for a definition of 'Name'. If it
12331234 // finds one it generates a stub for it and returns the address of the stub.
1234 TargetAddress searchFunctionASTs(const std::string &Name) {
1235 RuntimeDyld::SymbolInfo searchFunctionASTs(const std::string &Name) {
12351236 auto DefI = FunctionDefs.find(Name);
12361237 if (DefI == FunctionDefs.end())
12371238 return 0;
12431244
12441245 // IRGen the AST, add it to the JIT, and return the address for it.
12451246 auto H = irGenStub(std::move(FnAST));
1246 return findSymbolIn(H, Name).getAddress();
1247 auto Sym = findSymbolIn(H, Name);
1248 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
12471249 }
12481250
12491251 // This method will take the AST for a function definition and IR-gen a stub
12601262 // compile and update actions for the callback, and get a pointer to
12611263 // the jit trampoline that we need to call to trigger those actions.
12621264 auto CallbackInfo =
1263 CompileCallbacks.getCompileCallback(*F->getFunctionType());
1265 CompileCallbacks.getCompileCallback(F->getContext());
12641266
12651267 // Step 3) Create a stub that will indirectly call the body of this
12661268 // function once it is compiled. Initially, set the function
12671269 // pointer for the indirection to point at the trampoline.
12681270 std::string BodyPtrName = (F->getName() + "$address").str();
12691271 GlobalVariable *FunctionBodyPointer =
1270 createImplPointer(*F, BodyPtrName, CallbackInfo.getAddress());
1272 createImplPointer(*F, BodyPtrName,
1273 createIRTypedAddress(*F->getFunctionType(),
1274 CallbackInfo.getAddress()));
12711275 makeStub(*F, *FunctionBodyPointer);
12721276
12731277 // Step 4) Add the module containing the stub to the JIT.
12961300
12971301 SessionContext &Session;
12981302 Mangler Mang;
1303 SectionMemoryManager CCMgrMemMgr;
12991304 ObjLayerT ObjectLayer;
13001305 CompileLayerT CompileLayer;
13011306 LazyEmitLayerT LazyEmitLayer;
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
3 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
34 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
45 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
56 #include "llvm/IR/DataLayout.h"
11741175 // We need a memory manager to allocate memory and resolve symbols for this
11751176 // new module. Create one that resolves symbols by looking back into the
11761177 // JIT.
1177 auto MM = createLookasideRTDyldMM(
1178 [&](const std::string &Name) {
1179 return findSymbol(Name).getAddress();
1180 },
1181 [](const std::string &S) { return 0; } );
1182
1183 return CompileLayer.addModuleSet(singletonSet(std::move(M)), std::move(MM));
1178 auto Resolver = createLambdaResolver(
1179 [&](const std::string &Name) {
1180 if (auto Sym = findSymbol(Name))
1181 return RuntimeDyld::SymbolInfo(Sym.getAddress(),
1182 Sym.getFlags());
1183 return RuntimeDyld::SymbolInfo(nullptr);
1184 },
1185 [](const std::string &S) { return nullptr; }
1186 );
1187 return CompileLayer.addModuleSet(singletonSet(std::move(M)),
1188 make_unique(),
1189 std::move(Resolver));
11841190 }
11851191
11861192 void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); }
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
3 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
34 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
45 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
56 #include "llvm/IR/DataLayout.h"
11771178 // We need a memory manager to allocate memory and resolve symbols for this
11781179 // new module. Create one that resolves symbols by looking back into the
11791180 // JIT.
1180 auto MM = createLookasideRTDyldMM(
1181 [&](const std::string &Name) {
1182 return findSymbol(Name).getAddress();
1183 },
1184 [](const std::string &S) { return 0; } );
1181 auto Resolver = createLambdaResolver(
1182 [&](const std::string &Name) {
1183 if (auto Sym = findSymbol(Name))
1184 return RuntimeDyld::SymbolInfo(Sym.getAddress(),
1185 Sym.getFlags());
1186 return RuntimeDyld::SymbolInfo(nullptr);
1187 },
1188 [](const std::string &S) { return nullptr; } );
11851189
11861190 return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
1187 std::move(MM));
1191 make_unique(),
1192 std::move(Resolver));
11881193 }
11891194
11901195 void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
3 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
34 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
45 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
56 #include "llvm/IR/DataLayout.h"
11821183 // We need a memory manager to allocate memory and resolve symbols for this
11831184 // new module. Create one that resolves symbols by looking back into the
11841185 // JIT.
1185 auto MM = createLookasideRTDyldMM(
1186 [&](const std::string &Name) {
1187 // First try to find 'Name' within the JIT.
1188 if (auto Symbol = findSymbol(Name))
1189 return Symbol.getAddress();
1190
1191 // If we don't already have a definition of 'Name' then search
1192 // the ASTs.
1193 return searchFunctionASTs(Name);
1194 },
1195 [](const std::string &S) { return 0; } );
1186 auto Resolver = createLambdaResolver(
1187 [&](const std::string &Name) {
1188 // First try to find 'Name' within the JIT.
1189 if (auto Symbol = findSymbol(Name))
1190 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
1191 Symbol.getFlags());
1192
1193 // If we don't already have a definition of 'Name' then search
1194 // the ASTs.
1195 return searchFunctionASTs(Name);
1196 },
1197 [](const std::string &S) { return nullptr; } );
11961198
11971199 return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
1198 std::move(MM));
1200 make_unique(),
1201 std::move(Resolver));
11991202 }
12001203
12011204 void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
12161219
12171220 // This method searches the FunctionDefs map for a definition of 'Name'. If it
12181221 // finds one it generates a stub for it and returns the address of the stub.
1219 TargetAddress searchFunctionASTs(const std::string &Name) {
1222 RuntimeDyld::SymbolInfo searchFunctionASTs(const std::string &Name) {
12201223 auto DefI = FunctionDefs.find(Name);
12211224 if (DefI == FunctionDefs.end())
12221225 return 0;
12271230
12281231 // IRGen the AST, add it to the JIT, and return the address for it.
12291232 auto H = addModule(IRGen(Session, *FnAST));
1230 return findSymbolIn(H, Name).getAddress();
1233 auto Sym = findSymbolIn(H, Name);
1234 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
12311235 }
12321236
12331237 SessionContext &Session;
1414 #ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
1515 #define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
1616
17 #include "RuntimeDyld.h"
1718 #include "llvm-c/ExecutionEngine.h"
1819 #include "llvm/ADT/SmallVector.h"
1920 #include "llvm/ADT/StringRef.h"
4142 class GlobalValue;
4243 class JITEventListener;
4344 class MachineCodeInfo;
45 class MCJITMemoryManager;
4446 class MutexGuard;
4547 class ObjectCache;
4648 class RTDyldMemoryManager;
138140 virtual char *getMemoryForGV(const GlobalVariable *GV);
139141
140142 static ExecutionEngine *(*MCJITCtor)(
141 std::unique_ptr M,
142 std::string *ErrorStr,
143 std::unique_ptr MCJMM,
144 std::unique_ptr TM);
143 std::unique_ptr M,
144 std::string *ErrorStr,
145 std::shared_ptr MM,
146 std::shared_ptr SR,
147 std::unique_ptr TM);
145148
146149 static ExecutionEngine *(*OrcMCJITReplacementCtor)(
147 std::string *ErrorStr,
148 std::unique_ptr OrcJMM,
149 std::unique_ptr TM);
150 std::string *ErrorStr,
151 std::shared_ptr MM,
152 std::shared_ptr SR,
153 std::unique_ptr TM);
150154
151155 static ExecutionEngine *(*InterpCtor)(std::unique_ptr M,
152156 std::string *ErrorStr);
499503 EngineKind::Kind WhichEngine;
500504 std::string *ErrorStr;
501505 CodeGenOpt::Level OptLevel;
502 std::unique_ptr MCJMM;
506 std::shared_ptr MemMgr;
507 std::shared_ptr Resolver;
503508 TargetOptions Options;
504509 Reloc::Model RelocModel;
505510 CodeModel::Model CMModel;
534539 /// memory manager. This option defaults to NULL.
535540 EngineBuilder &setMCJITMemoryManager(std::unique_ptr mcjmm);
536541
542 EngineBuilder&
543 setMemoryManager(std::unique_ptr MM);
544
545 EngineBuilder&
546 setSymbolResolver(std::unique_ptr SR);
547
537548 /// setErrorStr - Set the error string to write to on error. This option
538549 /// defaults to NULL.
539550 EngineBuilder &setErrorStr(std::string *e) {
1515 #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
1616
1717 #include "IndirectionUtils.h"
18 #include "LookasideRTDyldMM.h"
18 #include "LambdaResolver.h"
1919 #include "llvm/ADT/STLExtras.h"
2020 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2121 #include
138138 typedef typename ModuleSetInfoListT::iterator ModuleSetHandleT;
139139
140140 // @brief Fallback lookup functor.
141 typedef std::function<uint64_t(const std::string &)> LookupFtor;
141 typedef std::function<RuntimeDyld::SymbolInfo(const std::string &)> LookupFtor;
142142
143143 /// @brief Construct a compile-on-demand layer instance.
144144 CompileOnDemandLayer(BaseLayerT &BaseLayer, CompileCallbackMgrT &CallbackMgr)
152152 // If the user didn't supply a fallback lookup then just use
153153 // getSymbolAddress.
154154 if (!FallbackLookup)
155 FallbackLookup = [=](const std::string &Name) {
156 return findSymbol(Name, true).getAddress();
157 };
155 FallbackLookup =
156 [=](const std::string &Name) -> RuntimeDyld::SymbolInfo {
157 if (auto Symbol = findSymbol(Name, true))
158 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
159 Symbol.getFlags());
160 return nullptr;
161 };
158162
159163 // Create a lookup context and ModuleSetInfo for this module set.
160164 // For the purposes of symbol resolution the set Ms will be treated as if
254258 Function *Proto = StubsModule->getFunction(Name);
255259 assert(Proto && "Failed to clone function decl into stubs module.");
256260 auto CallbackInfo =
257 CompileCallbackMgr.getCompileCallback(*Proto->getFunctionType());
261 CompileCallbackMgr.getCompileCallback(Proto->getContext());
258262 GlobalVariable *FunctionBodyPointer =
259263 createImplPointer(*Proto, Name + AddrSuffix,
260264 createIRTypedAddress(*Proto->getFunctionType(),
313317 MSet.push_back(std::move(M));
314318
315319 auto DylibLookup = MSI.Lookup;
316 auto MM =
317 createLookasideRTDyldMM(
320 auto Resolver =
321 createLambdaResolver(
318322 [=](const std::string &Name) {
319323 if (auto Symbol = DylibLookup->findSymbol(LogicalModule, Name))
320 return Symbol.getAddress();
324 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
325 Symbol.getFlags());
321326 return FallbackLookup(Name);
322327 },
323 [=](const std::string &Name) {
324 return DylibLookup->findSymbol(LogicalModule, Name).getAddress();
328 [=](const std::string &Name) -> RuntimeDyld::SymbolInfo {
329 if (auto Symbol = DylibLookup->findSymbol(LogicalModule, Name))
330 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
331 Symbol.getFlags());
332 return nullptr;
325333 });
326334
327335 BaseLayerModuleSetHandleT H =
328 BaseLayer.addModuleSet(std::move(MSet), std::move(MM));
336 BaseLayer.addModuleSet(std::move(MSet),
337 make_unique(),
338 std::move(Resolver));
329339 // Add this module to the logical module lookup.
330340 DylibLookup->addToLogicalModule(LogicalModule, H);
331341 MSI.BaseLayerModuleSetHandles.push_back(H);
5656 // manager MM.
5757 ///
5858 /// @return A handle for the added modules.
59 template >
59 template , typename MemoryManagerPtrT,
60 typename SymbolResolverPtrT>
6061 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
61 std::unique_ptr MM) {
62 MemoryManagerPtrT MemMgr,
63 SymbolResolverPtrT Resolver) {
6264 OwningObjectVec Objects;
6365 OwningBufferVec Buffers;
6466
8082 }
8183
8284 ModuleSetHandleT H =
83 BaseLayer.addObjectSet(Objects, std::move(MM));
85 BaseLayer.addObjectSet(Objects, std::move(MemMgr), std::move(Resolver));
8486
8587 BaseLayer.takeOwnershipOfBuffers(H, std::move(Buffers));
8688
1515
1616 #include "JITSymbol.h"
1717 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1819 #include "llvm/IR/IRBuilder.h"
1920 #include "llvm/IR/Mangler.h"
2021 #include "llvm/IR/Module.h"
9394 }
9495
9596 /// @brief Get/create a compile callback with the given signature.
96 virtual CompileCallbackInfo getCompileCallback(FunctionType &FT) = 0;
97 virtual CompileCallbackInfo getCompileCallback(LLVMContext &Context) = 0;
9798
9899 protected:
99100
124125 /// there is no existing callback trampoline.
125126 /// (Trampolines are allocated in blocks for
126127 /// efficiency.)
127 JITCompileCallbackManager(JITLayerT &JIT, LLVMContext &Context,
128 JITCompileCallbackManager(JITLayerT &JIT, RuntimeDyld::MemoryManager &MemMgr,
129 LLVMContext &Context,
128130 TargetAddress ErrorHandlerAddress,
129131 unsigned NumTrampolinesPerBlock)
130132 : JITCompileCallbackManagerBase(ErrorHandlerAddress,
131133 NumTrampolinesPerBlock),
132 JIT(JIT) {
134 JIT(JIT), MemMgr(MemMgr) {
133135 emitResolverBlock(Context);
134136 }
135137
136138 /// @brief Get/create a compile callback with the given signature.
137 CompileCallbackInfo getCompileCallback(FunctionType &FT) final {
138 TargetAddress TrampolineAddr = getAvailableTrampolineAddr(FT.getContext());
139 CompileCallbackInfo getCompileCallback(LLVMContext &Context) final {
140 TargetAddress TrampolineAddr = getAvailableTrampolineAddr(Context);
139141 auto &CallbackHandler =
140142 this->ActiveTrampolines[TrampolineAddr];
141143
156158 std::unique_ptr M(new Module("resolver_block_module",
157159 Context));
158160 TargetT::insertResolverBlock(*M, *this);
159 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), nullptr);
161 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
162 static_cast(
163 nullptr));
160164 JIT.emitAndFinalize(H);
161165 auto ResolverBlockSymbol =
162166 JIT.findSymbolIn(H, TargetT::ResolverBlockName, false);
181185 TargetT::insertCompileCallbackTrampolines(*M, ResolverBlockAddr,
182186 this->NumTrampolinesPerBlock,
183187 this->ActiveTrampolines.size());
184 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), nullptr);
188 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
189 static_cast(
190 nullptr));
185191 JIT.emitAndFinalize(H);
186192 for (unsigned I = 0; I < this->NumTrampolinesPerBlock; ++I) {
187193 std::string Name = GetLabelName(I);
192198 }
193199
194200 JITLayerT &JIT;
201 RuntimeDyld::MemoryManager &MemMgr;
195202 TargetAddress ResolverBlockAddr;
196203 };
197204
2626
2727 /// @brief Represents a symbol in the JIT.
2828 class JITSymbol : public JITSymbolBase {
29 public:
29 public:
3030
3131 typedef std::function GetAddressFtor;
3232
0 //===-- LambdaResolverMM - Redirect symbol lookup via a functor -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines a RuntimeDyld::SymbolResolver subclass that uses a user-supplied
10 // functor for symbol resolution.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
16
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
19 #include
20 #include
21
22 namespace llvm {
23 namespace orc {
24
25 template
26 class LambdaResolver : public RuntimeDyld::SymbolResolver {
27 public:
28
29 LambdaResolver(ExternalLookupFtorT ExternalLookupFtor,
30 DylibLookupFtorT DylibLookupFtor)
31 : ExternalLookupFtor(ExternalLookupFtor),
32 DylibLookupFtor(DylibLookupFtor) {}
33
34 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) final {
35 return ExternalLookupFtor(Name);
36 }
37
38 RuntimeDyld::SymbolInfo
39 findSymbolInLogicalDylib(const std::string &Name) final {
40 return DylibLookupFtor(Name);
41 }
42
43 private:
44 ExternalLookupFtorT ExternalLookupFtor;
45 DylibLookupFtorT DylibLookupFtor;
46 };
47
48 template
49 typename DylibLookupFtorT>
50 std::unique_ptr>
51 createLambdaResolver(ExternalLookupFtorT ExternalLookupFtor,
52 DylibLookupFtorT DylibLookupFtor) {
53 typedef LambdaResolver LR;
54 return make_unique(std::move(ExternalLookupFtor),
55 std::move(DylibLookupFtor));
56 }
57
58 } // End namespace orc.
59 } // End namespace llvm.
60
61 #endif // LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
1515
1616 #include "JITSymbol.h"
17 #include "LookasideRTDyldMM.h"
1817 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
1918 #include "llvm/IR/GlobalValue.h"
2019 #include "llvm/IR/Mangler.h"
2120 #include "llvm/IR/Module.h"
21 #include "llvm/ADT/STLExtras.h"
2222 #include "llvm/ADT/StringMap.h"
2323 #include
2424
9393 BaseLayer.emitAndFinalize(Handle);
9494 }
9595
96 template >
96 template , typename MemoryManagerPtrT,
97 typename SymbolResolverPtrT>
9798 static std::unique_ptr
98 create(BaseLayerT &B, ModuleSetT Ms,
99 std::unique_ptr MM);
99 create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
100 SymbolResolverPtrT Resolver);
100101
101102 protected:
102103 virtual const GlobalValue* searchGVs(StringRef Name,
108109 BaseLayerHandleT Handle;
109110 };
110111
111 template >
112 template , typename MemoryManagerPtrT,
113 typename SymbolResolverPtrT>
112114 class EmissionDeferredSetImpl : public EmissionDeferredSet {
113115 public:
114116 EmissionDeferredSetImpl(ModuleSetT Ms,
115 std::unique_ptr MM)
116 : Ms(std::move(Ms)), MM(std::move(MM)) {}
117 MemoryManagerPtrT MemMgr,
118 SymbolResolverPtrT Resolver)
119 : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)),
120 Resolver(std::move(Resolver)) {}
117121
118122 protected:
119123
144148 // We don't need the mangled names set any more: Once we've emitted this
145149 // to the base layer we'll just look for symbols there.
146150 MangledSymbols.reset();
147 return BaseLayer.addModuleSet(std::move(Ms), std::move(MM));
151 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
152 std::move(Resolver));
148153 }
149154
150155 private:
205210 }
206211
207212 ModuleSetT Ms;
208 std::unique_ptr MM;
213 MemoryManagerPtrT MemMgr;
214 SymbolResolverPtrT Resolver;
209215 mutable std::unique_ptr> MangledSymbols;
210216 };
211217
222228 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
223229
224230 /// @brief Add the given set of modules to the lazy emitting layer.
225 template >
231 template , typename MemoryManagerPtrT,
232 typename SymbolResolverPtrT>
226233 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
227 std::unique_ptr MM) {
234 MemoryManagerPtrT MemMgr,
235 SymbolResolverPtrT Resolver) {
228236 return ModuleSetList.insert(
229237 ModuleSetList.end(),
230 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MM)));
238 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MemMgr),
239 std::move(Resolver)));
231240 }
232241
233242 /// @brief Remove the module set represented by the given handle.
276285 };
277286
278287 template
279 template >
288 template , typename MemoryManagerPtrT,
289 typename SymbolResolverPtrT>
280290 std::unique_ptr::EmissionDeferredSet>
281291 LazyEmittingLayer::EmissionDeferredSet::create(
282 BaseLayerT &B, ModuleSetT Ms, std::unique_ptr MM) {
283 return llvm::make_unique>(std::move(Ms),
284 std::move(MM));
292 BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
293 SymbolResolverPtrT Resolver) {
294 typedef EmissionDeferredSetImpl
295 EDS;
296 return llvm::make_unique(std::move(Ms), std::move(MemMgr),
297 std::move(Resolver));
285298 }
286299
287300 } // End namespace orc.
+0
-92
include/llvm/ExecutionEngine/Orc/LookasideRTDyldMM.h less more
None //===- LookasideRTDyldMM - Redirect symbol lookup via a functor -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an adapter for RuntimeDyldMM that allows lookups for external
10 // symbols to go via a functor.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
15 #define LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
16
17 #include "llvm/ADT/STLExtras.h"
18 #include
19 #include
20
21 namespace llvm {
22 namespace orc {
23
24 /// @brief Defines an adapter for RuntimeDyldMM that allows lookups for external
25 /// symbols to go via a functor, before falling back to the lookup logic
26 /// provided by the underlying RuntimeDyldMM instance.
27 ///
28 /// This class is useful for redirecting symbol lookup back to various layers
29 /// of a JIT component stack, e.g. to enable lazy module emission.
30 ///
31 template
32 typename DylibLookupFtor>
33 class LookasideRTDyldMM : public BaseRTDyldMM {
34 public:
35 /// @brief Create a LookasideRTDyldMM intance.
36 LookasideRTDyldMM(ExternalLookupFtor ExternalLookup,
37 DylibLookupFtor DylibLookup)
38 : ExternalLookup(std::move(ExternalLookup)),
39 DylibLookup(std::move(DylibLookup)) {}
40
41 /// @brief Look up the given symbol address, first via the functor this
42 /// instance was created with, then (if the symbol isn't found)
43 /// via the underlying RuntimeDyldMM.
44 uint64_t getSymbolAddress(const std::string &Name) override {
45 if (uint64_t Addr = ExternalLookup(Name))
46 return Addr;
47 return BaseRTDyldMM::getSymbolAddress(Name);
48 }
49
50 uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) override {
51 if (uint64_t Addr = DylibLookup(Name))
52 return Addr;
53 return BaseRTDyldMM::getSymbolAddressInLogicalDylib(Name);
54 };
55
56 /// @brief Get a reference to the ExternalLookup functor.
57 ExternalLookupFtor &getExternalLookup() { return ExternalLookup; }
58
59 /// @brief Get a const-reference to the ExternalLookup functor.
60 const ExternalLookupFtor &getExternalLookup() const { return ExternalLookup; }
61
62 /// @brief Get a reference to the DylibLookup functor.
63 DylibLookupFtor &getDylibLookup() { return DylibLookup; }
64
65 /// @brief Get a const-reference to the DylibLookup functor.
66 const DylibLookupFtor &getDylibLookup() const { return DylibLookup; }
67
68 private:
69 ExternalLookupFtor ExternalLookup;
70 DylibLookupFtor DylibLookup;
71 };
72
73 /// @brief Create a LookasideRTDyldMM from a base memory manager type, an
74 /// external lookup functor, and a dylib lookup functor.
75 template
76 typename DylibLookupFtor>
77 std::unique_ptr<
78 LookasideRTDyldMM>
79 createLookasideRTDyldMM(ExternalLookupFtor &&ExternalLookup,
80 DylibLookupFtor &&DylibLookup) {
81 typedef LookasideRTDyldMM
82 ThisLookasideMM;
83 return llvm::make_unique(
84 std::forward(ExternalLookup),
85 std::forward(DylibLookup));
86 }
87
88 } // End namespace orc.
89 } // End namespace llvm.
90
91 #endif // LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
1414 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
1515
1616 #include "JITSymbol.h"
17 #include "LookasideRTDyldMM.h"
1817 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1918 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2019 #include
3736 LinkedObjectSet(const LinkedObjectSet&) = delete;
3837 void operator=(const LinkedObjectSet&) = delete;
3938 public:
40 LinkedObjectSet(std::unique_ptr MM)
41 : MM(std::move(MM)), RTDyld(llvm::make_unique(&*this->MM)),
39 LinkedObjectSet(RuntimeDyld::MemoryManager &MemMgr,
40 RuntimeDyld::SymbolResolver &Resolver)
41 : RTDyld(llvm::make_unique(MemMgr, Resolver)),
4242 State(Raw) {}
4343
44 // MSVC 2012 cannot infer a move constructor, so write it out longhand.
45 LinkedObjectSet(LinkedObjectSet &&O)
46 : MM(std::move(O.MM)), RTDyld(std::move(O.RTDyld)), State(O.State) {}
44 virtual ~LinkedObjectSet() {}
4745
4846 std::unique_ptr
4947 addObject(const object::ObjectFile &Obj) {
5654
5755 bool NeedsFinalization() const { return (State == Raw); }
5856
59 void Finalize() {
60 State = Finalizing;
61 RTDyld->resolveRelocations();
62 RTDyld->registerEHFrames();
63 MM->finalizeMemory();
64 OwnedBuffers.clear();
65 State = Finalized;
66 }
57 virtual void Finalize() = 0;
6758
6859 void mapSectionAddress(const void *LocalAddress, TargetAddress TargetAddr) {
6960 assert((State != Finalized) &&
7566 OwnedBuffers.push_back(std::move(B));
7667 }
7768
78 private:
79 std::unique_ptr MM;
69 protected:
8070 std::unique_ptr RTDyld;
8171 enum { Raw, Finalizing, Finalized } State;
8272
8676 std::vector> OwnedBuffers;
8777 };
8878
89 typedef std::list<LinkedObjectSet> LinkedObjectSetListT;
79 typedef std::list<std::unique_ptr> LinkedObjectSetListT;
9080
9181 public:
9282 /// @brief Handle to a set of loaded objects.
9888 template
9989 void takeOwnershipOfBuffers(ObjSetHandleT H, OwningMBSet MBs) {
10090 for (auto &MB : MBs)
101 H->takeOwnershipOfBuffer(std::move(MB));
91 (*H)->takeOwnershipOfBuffer(std::move(MB));
10292 }
10393
10494 };
119109 /// symbols.
120110 template
121111 class ObjectLinkingLayer : public ObjectLinkingLayerBase {
112 private:
113
114 template
115 class ConcreteLinkedObjectSet : public LinkedObjectSet {
116 public:
117 ConcreteLinkedObjectSet(MemoryManagerPtrT MemMgr,
118 SymbolResolverPtrT Resolver)
119 : LinkedObjectSet(*MemMgr, *Resolver), MemMgr(std::move(MemMgr)),
120 Resolver(std::move(Resolver)) { }
121
122 void Finalize() override {
123 State = Finalizing;
124 RTDyld->resolveRelocations();
125 RTDyld->registerEHFrames();
126 MemMgr->finalizeMemory();
127 OwnedBuffers.clear();
128 State = Finalized;
129 }
130
131 private:
132 MemoryManagerPtrT MemMgr;
133 SymbolResolverPtrT Resolver;
134 };
135
136 template
137 std::unique_ptr
138 createLinkedObjectSet(MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver) {
139 typedef ConcreteLinkedObjectSet LOS;
140 return llvm::make_unique(std::move(MemMgr), std::move(Resolver));
141 }
142
122143 public:
123144
124145 /// @brief LoadedObjectInfo list. Contains a list of owning pointers to
126147 typedef std::vector>
127148 LoadedObjInfoList;
128149
129 /// @brief Functor to create RTDyldMemoryManager instances.
130 typedef std::function()> CreateRTDyldMMFtor;
131
132150 /// @brief Functor for receiving finalization notifications.
133151 typedef std::function NotifyFinalizedFtor;
134152
135153 /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
136 /// NotifyFinalized and CreateMemoryManager functors.
154 /// and NotifyFinalized functors.
137155 ObjectLinkingLayer(
138 CreateRTDyldMMFtor CreateMemoryManager = CreateRTDyldMMFtor(),
139156 NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(),
140157 NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor())
141158 : NotifyLoaded(std::move(NotifyLoaded)),
142 NotifyFinalized(std::move(NotifyFinalized)),
143 CreateMemoryManager(std::move(CreateMemoryManager)) {}
159 NotifyFinalized(std::move(NotifyFinalized)) {}
144160
145161 /// @brief Add a set of objects (or archives) that will be treated as a unit
146162 /// for the purposes of symbol lookup and memory management.
153169 /// This version of this method allows the client to pass in an
154170 /// RTDyldMemoryManager instance that will be used to allocate memory and look
155171 /// up external symbol addresses for the given objects.
156 template >
172 template ,
173 typename MemoryManagerPtrT,
174 typename SymbolResolverPtrT>
157175 ObjSetHandleT addObjectSet(const ObjSetT &Objects,
158 std::unique_ptr MM) {
159
160 if (!MM) {
161 assert(CreateMemoryManager &&
162 "No memory manager or memory manager creator provided.");
163 MM = CreateMemoryManager();
164 }
165
166 ObjSetHandleT Handle = LinkedObjSetList.insert(
167 LinkedObjSetList.end(), LinkedObjectSet(std::move(MM)));
168 LinkedObjectSet &LOS = *Handle;
176 MemoryManagerPtrT MemMgr,
177 SymbolResolverPtrT Resolver) {
178 ObjSetHandleT Handle =
179 LinkedObjSetList.insert(
180 LinkedObjSetList.end(),
181 createLinkedObjectSet(std::move(MemMgr), std::move(Resolver)));
182
183 LinkedObjectSet &LOS = **Handle;
169184 LoadedObjInfoList LoadedObjInfos;
170185
171186 for (auto &Obj : Objects)
211226 /// given object set.
212227 JITSymbol findSymbolIn(ObjSetHandleT H, StringRef Name,
213228 bool ExportedSymbolsOnly) {
214 if (auto Sym = H->getSymbol(Name)) {
229 if (auto Sym = (*H)->getSymbol(Name)) {
215230 if (Sym.isExported() || !ExportedSymbolsOnly) {
216231 auto Addr = Sym.getAddress();
217232 auto Flags = Sym.getFlags();
218 if (!H->NeedsFinalization()) {
233 if (!(*H)->NeedsFinalization()) {
219234 // If this instance has already been finalized then we can just return
220235 // the address.
221236 return JITSymbol(Addr, Flags);
224239 // it. The functor still needs to double-check whether finalization is
225240 // required, in case someone else finalizes this set before the
226241 // functor is called.
227 auto GetAddress =
242 auto GetAddress =
228243 [this, Addr, H]() {
229 if (H->NeedsFinalization()) {
230 H->Finalize();
244 if ((*H)->NeedsFinalization()) {
245 (*H)->Finalize();
231246 if (NotifyFinalized)
232247 NotifyFinalized(H);
233248 }
243258 /// @brief Map section addresses for the objects associated with the handle H.
244259 void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
245260 TargetAddress TargetAddr) {
246 H->mapSectionAddress(LocalAddress, TargetAddr);
261 (*H)->mapSectionAddress(LocalAddress, TargetAddr);
247262 }
248263
249264 /// @brief Immediately emit and finalize the object set represented by the
250265 /// given handle.
251266 /// @param H Handle for object set to emit/finalize.
252267 void emitAndFinalize(ObjSetHandleT H) {
253 H->Finalize();
268 (*H)->Finalize();
254269 if (NotifyFinalized)
255270 NotifyFinalized(H);
256271 }
259274 LinkedObjectSetListT LinkedObjSetList;
260275 NotifyLoadedFtor NotifyLoaded;
261276 NotifyFinalizedFtor NotifyFinalized;
262 CreateRTDyldMMFtor CreateMemoryManager;
263277 };
264278
265279 } // End namespace orc.
1313 #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
1414 #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
1515
16 #include "RuntimeDyld.h"
1617 #include "llvm-c/ExecutionEngine.h"
1718 #include "llvm/ADT/StringRef.h"
1819 #include "llvm/Support/CBindingWrapping.h"
2627 class ObjectFile;
2728 }
2829
30 class MCJITMemoryManager : public RuntimeDyld::MemoryManager {
31 public:
32 /// This method is called after an object has been loaded into memory but
33 /// before relocations are applied to the loaded sections. The object load
34 /// may have been initiated by MCJIT to resolve an external symbol for another
35 /// object that is being finalized. In that case, the object about which
36 /// the memory manager is being notified will be finalized immediately after
37 /// the memory manager returns from this call.
38 ///
39 /// Memory managers which are preparing code for execution in an external
40 /// address space can use this call to remap the section addresses for the
41 /// newly loaded object.
42 virtual void notifyObjectLoaded(ExecutionEngine *EE,
43 const object::ObjectFile &) {}
44 };
45
2946 // RuntimeDyld clients often want to handle the memory management of
3047 // what gets placed where. For JIT clients, this is the subset of
3148 // JITMemoryManager required for dynamic loading of binaries.
3249 //
3350 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
3451 // for the varying types of objects to be allocated.
35 class RTDyldMemoryManager {
52 class RTDyldMemoryManager : public MCJITMemoryManager,
53 public RuntimeDyld::SymbolResolver {
3654 RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
3755 void operator=(const RTDyldMemoryManager&) = delete;
3856 public:
3957 RTDyldMemoryManager() {}
4058 virtual ~RTDyldMemoryManager();
4159
42 /// Allocate a memory block of (at least) the given size suitable for
43 /// executable code. The SectionID is a unique identifier assigned by the JIT
44 /// engine, and optionally recorded by the memory manager to access a loaded
45 /// section.
46 virtual uint8_t *allocateCodeSection(
47 uintptr_t Size, unsigned Alignment, unsigned SectionID,
48 StringRef SectionName) = 0;
49
50 /// Allocate a memory block of (at least) the given size suitable for data.
51 /// The SectionID is a unique identifier assigned by the JIT engine, and
52 /// optionally recorded by the memory manager to access a loaded section.
53 virtual uint8_t *allocateDataSection(
54 uintptr_t Size, unsigned Alignment, unsigned SectionID,
55 StringRef SectionName, bool IsReadOnly) = 0;
56
57 /// Inform the memory manager about the total amount of memory required to
58 /// allocate all sections to be loaded:
59 /// \p CodeSize - the total size of all code sections
60 /// \p DataSizeRO - the total size of all read-only data sections
61 /// \p DataSizeRW - the total size of all read-write data sections
62 ///
63 /// Note that by default the callback is disabled. To enable it
64 /// redefine the method needsToReserveAllocationSpace to return true.
65 virtual void reserveAllocationSpace(
66 uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { }
67
68 /// Override to return true to enable the reserveAllocationSpace callback.
69 virtual bool needsToReserveAllocationSpace() { return false; }
70
71 /// Register the EH frames with the runtime so that c++ exceptions work.
72 ///
73 /// \p Addr parameter provides the local address of the EH frame section
74 /// data, while \p LoadAddr provides the address of the data in the target
75 /// address space. If the section has not been remapped (which will usually
76 /// be the case for local execution) these two values will be the same.
77 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
78
79 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
60 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
61 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
8062
8163 /// This method returns the address of the specified function or variable in
8264 /// the current process.
8365 static uint64_t getSymbolAddressInProcess(const std::string &Name);
8466
67 /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
68 ///
8569 /// This method returns the address of the specified function or variable.
8670 /// It is used to resolve symbols during module linking.
8771 virtual uint64_t getSymbolAddress(const std::string &Name) {
8872 return getSymbolAddressInProcess(Name);
8973 }
9074
91 /// This method returns the address of the specified symbol if it exists
92 /// within the logical dynamic library represented by this
93 /// RTDyldMemoryManager. Unlike getSymbolAddress, queries through this
94 /// interface should return addresses for hidden symbols.
75 /// This method returns a RuntimeDyld::SymbolInfo for the specified function
76 /// or variable. It is used to resolve symbols during module linking.
9577 ///
96 /// This is of particular importance for the Orc JIT APIs, which support lazy
97 /// compilation by breaking up modules: Each of those broken out modules
98 /// must be able to resolve hidden symbols provided by the others. Clients
99 /// writing memory managers for MCJIT can usually ignore this method.
78 /// By default this falls back on the legacy lookup method:
79 /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
80 /// a strong, exported symbol, consistent with historical treatment by
81 /// RuntimeDyld.
10082 ///
101 /// This method will be queried by RuntimeDyld when checking for previous
102 /// definitions of common symbols. It will *not* be queried by default when
103 /// resolving external symbols (this minimises the link-time overhead for
104 /// MCJIT clients who don't care about Orc features). If you are writing a
105 /// RTDyldMemoryManager for Orc and want "external" symbol resolution to
106 /// search the logical dylib, you should override your getSymbolAddress
107 /// method call this method directly.
83 /// Clients writing custom RTDyldMemoryManagers are encouraged to override
84 /// this method and return a SymbolInfo with the flags set correctly. This is
85 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
86 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
87 return RuntimeDyld::SymbolInfo(getSymbolAddress(Name),
88 JITSymbolFlags::Exported);
89 }
90
91 /// Legacy symbol lookup -- DEPRECATED! Please override
92 /// findSymbolInLogicalDylib instead.
93 ///
94 /// Default to treating all modules as separate.
10895 virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
10996 return 0;
97 }
98
99 /// Default to treating all modules as separate.
100 ///
101 /// By default this falls back on the legacy lookup method:
102 /// 'getSymbolAddressInLogicalDylib'. The address returned by
103 /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
104 /// consistent with historical treatment by RuntimeDyld.
105 ///
106 /// Clients writing custom RTDyldMemoryManagers are encouraged to override
107 /// this method and return a SymbolInfo with the flags set correctly. This is
108 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
109 RuntimeDyld::SymbolInfo
110 findSymbolInLogicalDylib(const std::string &Name) override {
111 return RuntimeDyld::SymbolInfo(getSymbolAddressInLogicalDylib(Name),
112 JITSymbolFlags::Exported);
110113 }
111114
112115 /// This method returns the address of the specified function. As such it is
120123 /// MCJIT or RuntimeDyld. Use getSymbolAddress instead.
121124 virtual void *getPointerToNamedFunction(const std::string &Name,
122125 bool AbortOnFailure = true);
123
124 /// This method is called after an object has been loaded into memory but
125 /// before relocations are applied to the loaded sections. The object load
126 /// may have been initiated by MCJIT to resolve an external symbol for another
127 /// object that is being finalized. In that case, the object about which
128 /// the memory manager is being notified will be finalized immediately after
129 /// the memory manager returns from this call.
130 ///
131 /// Memory managers which are preparing code for execution in an external
132 /// address space can use this call to remap the section addresses for the
133 /// newly loaded object.
134 virtual void notifyObjectLoaded(ExecutionEngine *EE,
135 const object::ObjectFile &) {}
136
137 /// This method is called when object loading is complete and section page
138 /// permissions can be applied. It is up to the memory manager implementation
139 /// to decide whether or not to act on this method. The memory manager will
140 /// typically allocate all sections as read-write and then apply specific
141 /// permissions when this method is called. Code sections cannot be executed
142 /// until this function has been called. In addition, any cache coherency
143 /// operations needed to reliably use the memory are also performed.
144 ///
145 /// Returns true if an error occurred, false otherwise.
146 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
147126 };
148127
149128 // Create wrappers for C Binding types (see CBindingWrapping.h).
152131
153132 } // namespace llvm
154133
134
155135 #endif
1515
1616 #include "JITSymbolFlags.h"
1717 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
1918 #include "llvm/Support/Memory.h"
2019 #include
2120
2827
2928 class RuntimeDyldImpl;
3029 class RuntimeDyldCheckerImpl;
31
30
3231 class RuntimeDyld {
3332 friend class RuntimeDyldCheckerImpl;
3433
3534 RuntimeDyld(const RuntimeDyld &) = delete;
3635 void operator=(const RuntimeDyld &) = delete;
3736
38 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
39 // interface.
40 std::unique_ptr Dyld;
41 RTDyldMemoryManager *MM;
42 bool ProcessAllSections;
43 RuntimeDyldCheckerImpl *Checker;
4437 protected:
4538 // Change the address associated with a section when resolving relocations.
4639 // Any relocations already associated with the symbol will be re-resolved.
8174 unsigned BeginIdx, EndIdx;
8275 };
8376
84 RuntimeDyld(RTDyldMemoryManager *);
77 /// \brief Memory Management.
78 class MemoryManager {
79 public:
80 virtual ~MemoryManager() {};
81
82 /// Allocate a memory block of (at least) the given size suitable for
83 /// executable code. The SectionID is a unique identifier assigned by the
84 /// RuntimeDyld instance, and optionally recorded by the memory manager to
85 /// access a loaded section.
86 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
87 unsigned SectionID,
88 StringRef SectionName) = 0;
89
90 /// Allocate a memory block of (at least) the given size suitable for data.
91 /// The SectionID is a unique identifier assigned by the JIT engine, and
92 /// optionally recorded by the memory manager to access a loaded section.
93 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
94 unsigned SectionID,
95 StringRef SectionName,
96 bool IsReadOnly) = 0;
97
98 /// Inform the memory manager about the total amount of memory required to
99 /// allocate all sections to be loaded:
100 /// \p CodeSize - the total size of all code sections
101 /// \p DataSizeRO - the total size of all read-only data sections
102 /// \p DataSizeRW - the total size of all read-write data sections
103 ///
104 /// Note that by default the callback is disabled. To enable it
105 /// redefine the method needsToReserveAllocationSpace to return true.
106 virtual void reserveAllocationSpace(uintptr_t CodeSize,
107 uintptr_t DataSizeRO,
108 uintptr_t DataSizeRW) {}
109
110 /// Override to return true to enable the reserveAllocationSpace callback.
111 virtual bool needsToReserveAllocationSpace() { return false; }
112
113 /// Register the EH frames with the runtime so that c++ exceptions work.
114 ///
115 /// \p Addr parameter provides the local address of the EH frame section
116 /// data, while \p LoadAddr provides the address of the data in the target
117 /// address space. If the section has not been remapped (which will usually
118 /// be the case for local execution) these two values will be the same.
119 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
120 size_t Size) = 0;
121 virtual void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
122 size_t Size) = 0;
123
124 /// This method is called when object loading is complete and section page
125 /// permissions can be applied. It is up to the memory manager implementation
126 /// to decide whether or not to act on this method. The memory manager will
127 /// typically allocate all sections as read-write and then apply specific
128 /// permissions when this method is called. Code sections cannot be executed
129 /// until this function has been called. In addition, any cache coherency
130 /// operations needed to reliably use the memory are also performed.
131 ///
132 /// Returns true if an error occurred, false otherwise.
133 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
134
135 private:
136 virtual void anchor();
137 };
138
139 /// \brief Symbol resolution.
140 class SymbolResolver {
141 public:
142 virtual ~SymbolResolver() {};
143
144 /// This method returns the address of the specified function or variable.
145 /// It is used to resolve symbols during module linking.
146 virtual SymbolInfo findSymbol(const std::string &Name) = 0;
147
148 /// This method returns the address of the specified symbol if it exists
149 /// within the logical dynamic library represented by this
150 /// RTDyldMemoryManager. Unlike getSymbolAddress, queries through this
151 /// interface should return addresses for hidden symbols.
152 ///
153 /// This is of particular importance for the Orc JIT APIs, which support lazy
154 /// compilation by breaking up modules: Each of those broken out modules
155 /// must be able to resolve hidden symbols provided by the others. Clients
156 /// writing memory managers for MCJIT can usually ignore this method.
157 ///
158 /// This method will be queried by RuntimeDyld when checking for previous
159 /// definitions of common symbols. It will *not* be queried by default when
160 /// resolving external symbols (this minimises the link-time overhead for
161 /// MCJIT clients who don't care about Orc features). If you are writing a
162 /// RTDyldMemoryManager for Orc and want "external" symbol resolution to
163 /// search the logical dylib, you should override your getSymbolAddress
164 /// method call this method directly.
165 virtual SymbolInfo findSymbolInLogicalDylib(const std::string &Name) = 0;
166 private:
167 virtual void anchor();
168 };
169
170 /// \brief Construct a RuntimeDyld instance.
171 RuntimeDyld(MemoryManager &MemMgr, SymbolResolver &Resolver);
85172 ~RuntimeDyld();
86173
87174 /// Add the referenced object file to the list of objects to be loaded and
130217 assert(!Dyld && "setProcessAllSections must be called before loadObject.");
131218 this->ProcessAllSections = ProcessAllSections;
132219 }
220
221 private:
222 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
223 // interface.
224 std::unique_ptr Dyld;
225 MemoryManager &MemMgr;
226 SymbolResolver &Resolver;
227 bool ProcessAllSections;
228 RuntimeDyldCheckerImpl *Checker;
133229 };
134230
135231 } // end namespace llvm
1515 #define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
1616
1717 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
1919 #include "llvm/Support/ErrorHandling.h"
2020 #include "llvm/Support/Memory.h"
2121
1717 #include "llvm/ADT/Statistic.h"
1818 #include "llvm/ExecutionEngine/GenericValue.h"
1919 #include "llvm/ExecutionEngine/JITEventListener.h"
20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2021 #include "llvm/IR/Constants.h"
2122 #include "llvm/IR/DataLayout.h"
2223 #include "llvm/IR/DerivedTypes.h"
4445
4546 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
4647 std::unique_ptr M, std::string *ErrorStr,
47 std::unique_ptr MCJMM,
48 std::shared_ptr MemMgr,
49 std::shared_ptr Resolver,
4850 std::unique_ptr TM) = nullptr;
4951
5052 ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
51 std::string *ErrorStr, std::unique_ptr OrcJMM,
53 std::string *ErrorStr, std::shared_ptr MemMgr,
54 std::shared_ptr Resolver,
5255 std::unique_ptr TM) = nullptr;
5356
5457 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr M,
403406
404407 EngineBuilder::EngineBuilder(std::unique_ptr M)
405408 : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
406 OptLevel(CodeGenOpt::Default), MCJMM(nullptr), RelocModel(Reloc::Default),
407 CMModel(CodeModel::JITDefault), UseOrcMCJITReplacement(false) {
409 OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
410 RelocModel(Reloc::Default), CMModel(CodeModel::JITDefault),
411 UseOrcMCJITReplacement(false) {
408412 // IR module verification is enabled by default in debug builds, and disabled
409413 // by default in release builds.
410414 #ifndef NDEBUG
418422
419423 EngineBuilder &EngineBuilder::setMCJITMemoryManager(
420424 std::unique_ptr mcjmm) {
421 MCJMM = std::move(mcjmm);
425 auto SharedMM = std::shared_ptr(std::move(mcjmm));
426 MemMgr = SharedMM;
427 Resolver = SharedMM;
428 return *this;
429 }
430
431 EngineBuilder&
432 EngineBuilder::setMemoryManager(std::unique_ptr MM) {
433 MemMgr = std::shared_ptr(std::move(MM));
434 return *this;
435 }
436
437 EngineBuilder&
438 EngineBuilder::setSymbolResolver(std::unique_ptr SR) {
439 Resolver = std::shared_ptr(std::move(SR));
422440 return *this;
423441 }
424442
433451 // If the user specified a memory manager but didn't specify which engine to
434452 // create, we assume they only want the JIT, and we fail if they only want
435453 // the interpreter.
436 if (MCJMM) {
454 if (MemMgr) {
437455 if (WhichEngine & EngineKind::JIT)
438456 WhichEngine = EngineKind::JIT;
439457 else {
455473
456474 ExecutionEngine *EE = nullptr;
457475 if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
458 EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MCJMM),
476 EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
477 std::move(Resolver),
459478 std::move(TheTM));
460479 EE->addModule(std::move(M));
461480 } else if (ExecutionEngine::MCJITCtor)
462 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MCJMM),
463 std::move(TheTM));
481 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
482 std::move(Resolver), std::move(TheTM));
464483
465484 if (EE) {
466485 EE->setVerifyModules(VerifyModules);
77 //===----------------------------------------------------------------------===//
88
99 #include "MCJIT.h"
10 #include "llvm/ADT/STLExtras.h"
1011 #include "llvm/ExecutionEngine/GenericValue.h"
1112 #include "llvm/ExecutionEngine/JITEventListener.h"
1213 #include "llvm/ExecutionEngine/MCJIT.h"
4041 extern "C" void LLVMLinkInMCJIT() {
4142 }
4243
43 ExecutionEngine *MCJIT::createJIT(std::unique_ptr M,
44 std::string *ErrorStr,
45 std::unique_ptr MemMgr,
46 std::unique_ptr TM) {
44 ExecutionEngine*
45 MCJIT::createJIT(std::unique_ptr M,
46 std::string *ErrorStr,
47 std::shared_ptr MemMgr,
48 std::shared_ptr Resolver,
49 std::unique_ptr TM) {
4750 // Try to register the program as a source of symbols to resolve against.
4851 //
4952 // FIXME: Don't do this here.
5053 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
5154
52 std::unique_ptr MM = std::move(MemMgr);
53 if (!MM)
54 MM = std::unique_ptr(new SectionMemoryManager());
55
56 return new MCJIT(std::move(M), std::move(TM), std::move(MM));
55 if (!MemMgr || !Resolver) {
56 auto RTDyldMM = std::make_shared();
57 if (!MemMgr)
58 MemMgr = RTDyldMM;
59 if (!Resolver)
60 Resolver = RTDyldMM;
61 }
62
63 return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
64 std::move(Resolver));
5765 }
5866
5967 MCJIT::MCJIT(std::unique_ptr M, std::unique_ptr tm,
60 std::unique_ptr MM)
68 std::shared_ptr MemMgr,
69 std::shared_ptr Resolver)
6170 : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
62 MemMgr(this, std::move(MM)), Dyld(&MemMgr), ObjCache(nullptr) {
71 MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
72 Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
6373 // FIXME: We are managing our modules, so we do not want the base class
6474 // ExecutionEngine to manage them as well. To avoid double destruction
6575 // of the first (and only) module added in ExecutionEngine constructor
220230 Dyld.registerEHFrames();
221231
222232 // Set page permissions.
223 MemMgr.finalizeMemory();
233 MemMgr->finalizeMemory();
224234 }
225235
226236 // FIXME: Rename this.
252262 finalizeLoadedModules();
253263 }
254264
255 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
265 RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
256266 Mangler Mang(TM->getDataLayout());
257267 SmallString<128> FullName;
258268 Mang.getNameWithPrefix(FullName, Name);
259 return Dyld.getSymbol(FullName).getAddress();
269 return Dyld.getSymbol(FullName);
260270 }
261271
262272 Module *MCJIT::findModuleForSymbol(const std::string &Name,
283293 }
284294
285295 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
286 bool CheckFunctionsOnly)
287 {
296 bool CheckFunctionsOnly) {
297 return findSymbol(Name, CheckFunctionsOnly).getAddress();
298 }
299
300 RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name,
301 bool CheckFunctionsOnly) {
288302 MutexGuard locked(lock);
289303
290304 // First, check to see if we already have this symbol.
291 uint64_t Addr = getExistingSymbolAddress(Name);
292 if (Addr)
293 return Addr;
305 if (auto Sym = findExistingSymbol(Name))
306 return Sym;
294307
295308 for (object::OwningBinary &OB : Archives) {
296309 object::Archive *A = OB.getBinary();
309322 // This causes the object file to be loaded.
310323 addObjectFile(std::move(OF));
311324 // The address should be here now.
312 Addr = getExistingSymbolAddress(Name);
313 if (Addr)
314 return Addr;
325 if (auto Sym = findExistingSymbol(Name))
326 return Sym;
315327 }
316328 }
317329 }
322334 generateCodeForModule(M);
323335
324336 // Check the RuntimeDyld table again, it should be there now.
325 return getExistingSymbolAddress(Name);
337 return findExistingSymbol(Name);
326338 }
327339
328340 // If a LazyFunctionCreator is installed, use it to get/create the function.
329341 // FIXME: Should we instead have a LazySymbolCreator callback?
330 if (LazyFunctionCreator)
331 Addr = (uint64_t)LazyFunctionCreator(Name);
332
333 return Addr;
342 if (LazyFunctionCreator) {
343 auto Addr = static_cast(
344 reinterpret_cast(LazyFunctionCreator(Name)));
345 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
346 }
347
348 return nullptr;
334349 }
335350
336351 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
527542
528543 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
529544 if (!isSymbolSearchingDisabled()) {
530 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
545 void *ptr =
546 reinterpret_cast(
547 static_cast(Resolver.findSymbol(Name).getAddress()));
531548 if (ptr)
532549 return ptr;
533550 }
565582 void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj,
566583 const RuntimeDyld::LoadedObjectInfo &L) {
567584 MutexGuard locked(lock);
568 MemMgr.notifyObjectLoaded(this, Obj);
585 MemMgr->notifyObjectLoaded(this, Obj);
569586 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
570587 EventListeners[I]->NotifyObjectEmitted(Obj, L);
571588 }
577594 L->NotifyFreeingObject(Obj);
578595 }
579596
580 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
581 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
597 RuntimeDyld::SymbolInfo
598 LinkingSymbolResolver::findSymbol(const std::string &Name) {
599 auto Result = ParentEngine.findSymbol(Name, false);
582600 // If the symbols wasn't found and it begins with an underscore, try again
583601 // without the underscore.
584602 if (!Result && Name[0] == '_')
585 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
603 Result = ParentEngine.findSymbol(Name.substr(1), false);
586604 if (Result)
587605 return Result;
588 if (ParentEngine->isSymbolSearchingDisabled())
589 return 0;
590 return ClientMM->getSymbolAddress(Name);
591 }
606 if (ParentEngine.isSymbolSearchingDisabled())
607 return nullptr;
608 return ClientResolver->findSymbol(Name);
609 }
1515 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1616 #include "llvm/ExecutionEngine/ObjectCache.h"
1717 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
1819 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1920 #include "llvm/IR/Module.h"
2021
2526 // functions across modules that it owns. It aggregates the memory manager
2627 // that is passed in to the MCJIT constructor and defers most functionality
2728 // to that object.
28 class LinkingMemoryManager : public RTDyldMemoryManager {
29 class LinkingSymbolResolver : public RuntimeDyld::SymbolResolver {
2930 public:
30 LinkingMemoryManager(MCJIT *Parent,
31 std::unique_ptr MM)
32 : ParentEngine(Parent), ClientMM(std::move(MM)) {}
33
34 uint64_t getSymbolAddress(const std::string &Name) override;
35
36 // Functions deferred to client memory manager
37 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
38 unsigned SectionID,
39 StringRef SectionName) override {
40 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
31 LinkingSymbolResolver(MCJIT &Parent,
32 std::shared_ptr Resolver)
33 : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {}
34
35 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override;
36
37 // MCJIT doesn't support logical dylibs.
38 RuntimeDyld::SymbolInfo
39 findSymbolInLogicalDylib(const std::string &Name) override {
40 return nullptr;
4141 }
4242
43 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
44 unsigned SectionID, StringRef SectionName,
45 bool IsReadOnly) override {
46 return ClientMM->allocateDataSection(Size, Alignment,
47 SectionID, SectionName, IsReadOnly);
48 }
49
50 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
51 uintptr_t DataSizeRW) override {
52 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
53 }
54
55 bool needsToReserveAllocationSpace() override {
56 return ClientMM->needsToReserveAllocationSpace();
57 }
58
59 void notifyObjectLoaded(ExecutionEngine *EE,
60 const object::ObjectFile &Obj) override {
61 ClientMM->notifyObjectLoaded(EE, Obj);
62 }
63
64 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
65 size_t Size) override {
66 ClientMM->registerEHFrames(Addr, LoadAddr, Size);
67 }
68
69 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
70 size_t Size) override {
71 ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
72 }
73
74 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
75 return ClientMM->finalizeMemory(ErrMsg);
76 }
77
7843 private:
79 MCJIT *ParentEngine;
80 std::unique_ptr ClientMM;
44 MCJIT &ParentEngine;
45 std::shared_ptr ClientResolver;
8146 };
8247
8348 // About Module states: added->loaded->finalized.
10267
10368 class MCJIT : public ExecutionEngine {
10469 MCJIT(std::unique_ptr M, std::unique_ptr tm,
105 std::unique_ptr MemMgr);
70 std::shared_ptr MemMgr,
71 std::shared_ptr Resolver);
10672
10773 typedef llvm::SmallPtrSet ModulePtrSet;
10874
213179
214180 std::unique_ptr TM;
215181 MCContext *Ctx;
216 LinkingMemoryManager MemMgr;
182 std::shared_ptr MemMgr;
183 LinkingSymbolResolver Resolver;
217184 RuntimeDyld Dyld;
218185 std::vector EventListeners;
219186
323290 MCJITCtor = createJIT;
324291 }
325292
326 static ExecutionEngine *createJIT(std::unique_ptr M,
327 std::string *ErrorStr,
328 std::unique_ptr MemMgr,
329 std::unique_ptr TM);
293 static ExecutionEngine*
294 createJIT(std::unique_ptr M,
295 std::string *ErrorStr,
296 std::shared_ptr MemMgr,
297 std::shared_ptr Resolver,
298 std::unique_ptr TM);
330299
331300 // @}
332301
302 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name,
303 bool CheckFunctionsOnly);
304 // DEPRECATED - Please use findSymbol instead.
333305 // This is not directly exposed via the ExecutionEngine API, but it is
334306 // used by the LinkingMemoryManager.
335307 uint64_t getSymbolAddress(const std::string &Name,
336 bool CheckFunctionsOnly);
308 bool CheckFunctionsOnly);
337309
338310 protected:
339311 /// emitObject -- Generate a JITed object in memory from the specified module
347319 const RuntimeDyld::LoadedObjectInfo &L);
348320 void NotifyFreeingObject(const object::ObjectFile& Obj);
349321
350 uint64_t getExistingSymbolAddress(const std::string &Name);
322 RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name);
351323 Module *findModuleForSymbol(const std::string &Name,
352324 bool CheckFunctionsOnly);
353325 };
2525
2626 class OrcMCJITReplacement : public ExecutionEngine {
2727
28 class ForwardingRTDyldMM : public RTDyldMemoryManager {
28 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
29 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
30 // expecting - see finalizeMemory.
31 class MCJITReplacementMemMgr : public MCJITMemoryManager {
2932 public:
30 ForwardingRTDyldMM(OrcMCJITReplacement &M) : M(M) {}
33 MCJITReplacementMemMgr(OrcMCJITReplacement &M,
34 std::shared_ptr ClientMM)
35 : M(M), ClientMM(std::move(ClientMM)) {}
3136
3237 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
3338 unsigned SectionID,
3439 StringRef SectionName) override {
3540 uint8_t *Addr =
36 M.MM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
41 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
42 SectionName);
3743 M.SectionsAllocatedSinceLastLoad.insert(Addr);
3844 return Addr;
3945 }
4147 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
4248 unsigned SectionID, StringRef SectionName,
4349 bool IsReadOnly) override {
44 uint8_t *Addr = M.MM->allocateDataSection(Size, Alignment, SectionID,
45 SectionName, IsReadOnly);
50 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
51 SectionName, IsReadOnly);
4652 M.SectionsAllocatedSinceLastLoad.insert(Addr);
4753 return Addr;
4854 }
4955
5056 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
5157 uintptr_t DataSizeRW) override {
52 return M.MM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
58 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO,
59 DataSizeRW);
5360 }
5461
5562 bool needsToReserveAllocationSpace() override {
56 return M.MM->needsToReserveAllocationSpace();
63 return ClientMM->needsToReserveAllocationSpace();
5764 }
5865
5966 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
6067 size_t Size) override {
61 return M.MM->registerEHFrames(Addr, LoadAddr, Size);
68 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
6269 }
6370
6471 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
6572 size_t Size) override {
66 return M.MM->deregisterEHFrames(Addr, LoadAddr, Size);
67 }
68
69 uint64_t getSymbolAddress(const std::string &Name) override {
70 return M.getSymbolAddressWithoutMangling(Name);
71 }
72
73 void *getPointerToNamedFunction(const std::string &Name,
74 bool AbortOnFailure = true) override {
75 return M.MM->getPointerToNamedFunction(Name, AbortOnFailure);
73 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
7674 }
7775
7876 void notifyObjectLoaded(ExecutionEngine *EE,
7977 const object::ObjectFile &O) override {
80 return M.MM->notifyObjectLoaded(EE, O);
78 return ClientMM->notifyObjectLoaded(EE, O);
8179 }
8280
8381 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
9593 // get more than one set of objects loaded but not yet finalized is if
9694 // they were loaded during relocation of another set.
9795 if (M.UnfinalizedSections.size() == 1)
98 return M.MM->finalizeMemory(ErrMsg);
96 return ClientMM->finalizeMemory(ErrMsg);
9997 return false;
10098 }
10199
102100 private:
103101 OrcMCJITReplacement &M;
102 std::shared_ptr ClientMM;
103 };
104
105 class LinkingResolver : public RuntimeDyld::SymbolResolver {
106 public:
107 LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
108
109 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) {
110 return M.findMangledSymbol(Name);
111 }
112
113 RuntimeDyld::SymbolInfo findSymbolInLogicalDylib(const std::string &Name) {
114 return M.ClientResolver->findSymbolInLogicalDylib(Name);
115 }
116
117 private:
118 OrcMCJITReplacement &M;
104119 };
105120
106121 private:
107122
108123 static ExecutionEngine *
109124 createOrcMCJITReplacement(std::string *ErrorMsg,
110 std::unique_ptr OrcJMM,
125 std::shared_ptr MemMgr,
126 std::shared_ptr Resolver,
111127 std::unique_ptr TM) {
112 return new OrcMCJITReplacement(std::move(OrcJMM), std::move(TM));
128 return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver),
129 std::move(TM));
113130 }
114131
115132 public:
117134 OrcMCJITReplacementCtor = createOrcMCJITReplacement;
118135 }
119136
120 OrcMCJITReplacement(std::unique_ptr MM,
121 std::unique_ptr TM)
122 : TM(std::move(TM)), MM(std::move(MM)), Mang(this->TM->getDataLayout()),
137 OrcMCJITReplacement(
138 std::shared_ptr MemMgr,
139 std::shared_ptr ClientResolver,
140 std::unique_ptr TM)
141 : TM(std::move(TM)), MemMgr(*this, std::move(MemMgr)),
142 Resolver(*this), ClientResolver(std::move(ClientResolver)),
143 Mang(this->TM->getDataLayout()),
123144 NotifyObjectLoaded(*this), NotifyFinalized(*this),
124 ObjectLayer(ObjectLayerT::CreateRTDyldMMFtor(), NotifyObjectLoaded,
125 NotifyFinalized),
145 ObjectLayer(NotifyObjectLoaded, NotifyFinalized),
126146 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
127147 LazyEmitLayer(CompileLayer) {
128148 setDataLayout(this->TM->getDataLayout());
138158 Modules.push_back(std::move(M));
139159 std::vector Ms;
140160 Ms.push_back(&*Modules.back());
141 LazyEmitLayer.addModuleSet(std::move(Ms),
142 llvm::make_unique(*this));
161 LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver);
143162 }
144163
145164 void addObjectFile(std::unique_ptr O) override {
146165 std::vector> Objs;
147166 Objs.push_back(std::move(O));
148 ObjectLayer.addObjectSet(std::move(Objs),
149 llvm::make_unique(*this));
167 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
150168 }
151169
152170 void addObjectFile(object::OwningBinary O) override {
156174 std::vector> Objs;
157175 Objs.push_back(std::move(Obj));
158176 auto H =
159 ObjectLayer.addObjectSet(std::move(Objs),
160 llvm::make_unique(*this));
177 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
161178
162179 std::vector> Bufs;
163180 Bufs.push_back(std::move(Buf));
169186 }
170187
171188 uint64_t getSymbolAddress(StringRef Name) {
172 return getSymbolAddressWithoutMangling(Mangle(Name));
189 return findSymbol(Name).getAddress();
190 }
191
192 RuntimeDyld::SymbolInfo findSymbol(StringRef Name) {
193 return findMangledSymbol(Mangle(Name));
173194 }
174195
175196 void finalizeObject() override {
213234 }
214235
215236 private:
216 uint64_t getSymbolAddressWithoutMangling(StringRef Name) {
217 if (uint64_t Addr = LazyEmitLayer.findSymbol(Name, false).getAddress())
218 return Addr;
219 if (uint64_t Addr = MM->getSymbolAddress(Name))
220 return Addr;
221 if (uint64_t Addr = scanArchives(Name))
222 return Addr;
223
224 return 0;
225 }
226
227 uint64_t scanArchives(StringRef Name) {
237
238 RuntimeDyld::SymbolInfo findMangledSymbol(StringRef Name) {
239 if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
240 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
241 if (auto Sym = ClientResolver->findSymbol(Name))
242 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
243 if (auto Sym = scanArchives(Name))
244 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
245
246 return nullptr;
247 }
248
249 JITSymbol scanArchives(StringRef Name) {
228250 for (object::OwningBinary &OB : Archives) {
229251 object::Archive *A = OB.getBinary();
230252 // Look for our symbols in each Archive
240262 std::vector> ObjSet;
241263 ObjSet.push_back(std::unique_ptr(
242264 static_cast(ChildBin.release())));
243 ObjectLayer.addObjectSet(
244 std::move(ObjSet), llvm::make_unique(*this));
245 if (uint64_t Addr = ObjectLayer.findSymbol(Name, true).getAddress())
246 return Addr;
265 ObjectLayer.addObjectSet(std::move(ObjSet), &MemMgr, &Resolver);
266 if (auto Sym = ObjectLayer.findSymbol(Name, true))
267 return Sym;
247268 }
248269 }
249270 }
250 return 0;
271 return nullptr;
251272 }
252273
253274 class NotifyObjectLoadedT {
266287 assert(Objects.size() == Infos.size() &&
267288 "Incorrect number of Infos for Objects.");
268289 for (unsigned I = 0; I < Objects.size(); ++I)
269 M.MM->notifyObjectLoaded(&M, *Objects[I]);
290 M.MemMgr.notifyObjectLoaded(&M, *Objects[I]);
270291 };
271292
272293 private:
298319 typedef LazyEmittingLayer LazyEmitLayerT;
299320
300321 std::unique_ptr TM;
301 std::unique_ptr MM;
322 MCJITReplacementMemMgr MemMgr;
323 LinkingResolver Resolver;
324 std::shared_ptr ClientResolver;
302325 Mangler Mang;
303326
304327 NotifyObjectLoadedT NotifyObjectLoaded;
150150
151151 // Compute the memory size required to load all sections to be loaded
152152 // and pass this information to the memory manager
153 if (MemMgr->needsToReserveAllocationSpace()) {
153 if (MemMgr.needsToReserveAllocationSpace()) {
154154 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
155155 computeTotalAllocSize(Obj, CodeSize, DataSizeRO, DataSizeRW);
156 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
156 MemMgr.reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
157157 }
158158
159159 // Used sections from the object file
484484
485485 // Skip common symbols already elsewhere.
486486 if (GlobalSymbolTable.count(Name) ||
487 MemMgr->getSymbolAddressInLogicalDylib(Name)) {
487 Resolver.findSymbolInLogicalDylib(Name)) {
488488 DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
489489 << "'\n");
490490 continue;
501501
502502 // Allocate memory for the section
503503 unsigned SectionID = Sections.size();
504 uint8_t *Addr = MemMgr->allocateDataSection(CommonSize, sizeof(void *),
505 SectionID, StringRef(), false);
504 uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, sizeof(void *),
505 SectionID, StringRef(), false);
506506 if (!Addr)
507507 report_fatal_error("Unable to allocate memory for common symbols!");
508508 uint64_t Offset = 0;
576576 if (IsRequired) {
577577 Check(Section.getContents(data));
578578 Allocate = DataSize + PaddingSize + StubBufSize;
579 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
580 Name)
581 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
582 Name, IsReadOnly);
579 Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
580 Name)
581 : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
582 Name, IsReadOnly);
583583 if (!Addr)
584584 report_fatal_error("Unable to allocate section memory!");
585585
786786 uint64_t Addr = 0;
787787 RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
788788 if (Loc == GlobalSymbolTable.end()) {
789 // This is an external symbol, try to get its address from
790 // MemoryManager.
791 Addr = MemMgr->getSymbolAddress(Name.data());
789 // This is an external symbol, try to get its address from the symbol
790 // resolver.
791 Addr = Resolver.findSymbol(Name.data()).getAddress();
792792 // The call to getSymbolAddress may have caused additional modules to
793793 // be loaded, which may have added new entries to the
794794 // ExternalSymbolRelocations map. Consquently, we need to update our
834834 return 0;
835835 }
836836
837 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
837 void RuntimeDyld::MemoryManager::anchor() {}
838 void RuntimeDyld::SymbolResolver::anchor() {}
839
840 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
841 RuntimeDyld::SymbolResolver &Resolver)
842 : MemMgr(MemMgr), Resolver(Resolver) {
838843 // FIXME: There's a potential issue lurking here if a single instance of
839844 // RuntimeDyld is used to load multiple objects. The current implementation
840845 // associates a single memory manager with a RuntimeDyld instance. Even
842847 // they share a single memory manager. This can become a problem when page
843848 // permissions are applied.
844849 Dyld = nullptr;
845 MM = mm;
846850 ProcessAllSections = false;
847851 Checker = nullptr;
848852 }
850854 RuntimeDyld::~RuntimeDyld() {}
851855
852856 static std::unique_ptr
853 createRuntimeDyldCOFF(Triple::ArchType Arch, RTDyldMemoryManager *MM,
857 createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
858 RuntimeDyld::SymbolResolver &Resolver,
854859 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
855 std::unique_ptr Dyld(RuntimeDyldCOFF::create(Arch, MM));
860 std::unique_ptr Dyld =
861 RuntimeDyldCOFF::create(Arch, MM, Resolver);
856862 Dyld->setProcessAllSections(ProcessAllSections);
857863 Dyld->setRuntimeDyldChecker(Checker);
858864 return Dyld;
859865 }
860866
861867 static std::unique_ptr
862 createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections,
863 RuntimeDyldCheckerImpl *Checker) {
864 std::unique_ptr Dyld(new RuntimeDyldELF(MM));
868 createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
869 RuntimeDyld::SymbolResolver &Resolver,
870 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
871 std::unique_ptr Dyld(new RuntimeDyldELF(MM, Resolver));
865872 Dyld->setProcessAllSections(ProcessAllSections);
866873 Dyld->setRuntimeDyldChecker(Checker);
867874 return Dyld;
868875 }
869876
870877 static std::unique_ptr
871 createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
872 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
873 std::unique_ptr Dyld(RuntimeDyldMachO::create(Arch, MM));
878 createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
879 RuntimeDyld::SymbolResolver &Resolver,
880 bool ProcessAllSections,
881 RuntimeDyldCheckerImpl *Checker) {
882 std::unique_ptr Dyld =
883 RuntimeDyldMachO::create(Arch, MM, Resolver);
874884 Dyld->setProcessAllSections(ProcessAllSections);
875885 Dyld->setRuntimeDyldChecker(Checker);
876886 return Dyld;
880890 RuntimeDyld::loadObject(const ObjectFile &Obj) {
881891 if (!Dyld) {
882892 if (Obj.isELF())
883 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker);
893 Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
884894 else if (Obj.isMachO())
885895 Dyld = createRuntimeDyldMachO(
886 static_cast(Obj.getArch()), MM,
896 static_cast(Obj.getArch()), MemMgr, Resolver,
887897 ProcessAllSections, Checker);
888898 else if (Obj.isCOFF())
889899 Dyld = createRuntimeDyldCOFF(
890 static_cast(Obj.getArch()), MM,
900 static_cast(Obj.getArch()), MemMgr, Resolver,
891901 ProcessAllSections, Checker);
892902 else
893903 report_fatal_error("Incompatible object format!");
3939 namespace llvm {
4040
4141 std::unique_ptr
42 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
42 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch,
43 RuntimeDyld::MemoryManager &MemMgr,
44 RuntimeDyld::SymbolResolver &Resolver) {
4345 switch (Arch) {
4446 default:
4547 llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
4648 break;
4749 case Triple::x86_64:
48 return make_unique(MM);
50 return make_unique(MemMgr, Resolver);
4951 }
5052 }
5153
3030 std::unique_ptr
3131 loadObject(const object::ObjectFile &Obj) override;
3232 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
33 static std::unique_ptr create(Triple::ArchType Arch,
34 RTDyldMemoryManager *MM);
33
34 static std::unique_ptr
35 create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
36 RuntimeDyld::SymbolResolver &Resolver);
3537
3638 protected:
37 RuntimeDyldCOFF(RTDyldMemoryManager *MM) : RuntimeDyldImpl(MM) {}
39 RuntimeDyldCOFF(RuntimeDyld::MemoryManager &MemMgr,
40 RuntimeDyld::SymbolResolver &Resolver)
41 : RuntimeDyldImpl(MemMgr, Resolver) {}
3842 uint64_t getSymbolOffset(const SymbolRef &Sym);
3943 };
4044
737737 uint64_t RuntimeDyldCheckerImpl::getSymbolRemoteAddr(StringRef Symbol) const {
738738 if (auto InternalSymbol = getRTDyld().getSymbol(Symbol))
739739 return InternalSymbol.getAddress();
740 return getRTDyld().MemMgr->getSymbolAddress(Symbol);
740 return getRTDyld().Resolver.findSymbol(Symbol).getAddress();
741741 }
742742
743743 uint64_t RuntimeDyldCheckerImpl::readMemoryAtAddr(uint64_t SrcAddr,
182182
183183 namespace llvm {
184184
185 RuntimeDyldELF::RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
185 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
186 RuntimeDyld::SymbolResolver &Resolver)
187 : RuntimeDyldImpl(MemMgr, Resolver) {}
186188 RuntimeDyldELF::~RuntimeDyldELF() {}
187189
188190 void RuntimeDyldELF::registerEHFrames() {
189 if (!MemMgr)
190 return;
191191 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
192192 SID EHFrameSID = UnregisteredEHFrameSections[i];
193193 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
194194 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
195195 size_t EHFrameSize = Sections[EHFrameSID].Size;
196 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
196 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
197197 RegisteredEHFrameSections.push_back(EHFrameSID);
198198 }
199199 UnregisteredEHFrameSections.clear();
200200 }
201201
202202 void RuntimeDyldELF::deregisterEHFrames() {
203 if (!MemMgr)
204 return;
205203 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
206204 SID EHFrameSID = RegisteredEHFrameSections[i];
207205 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
208206 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
209207 size_t EHFrameSize = Sections[EHFrameSID].Size;
210 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
208 MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
211209 }
212210 RegisteredEHFrameSections.clear();
213211 }
14561454 void RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
14571455 ObjSectionToIDMap &SectionMap) {
14581456 // If necessary, allocate the global offset table
1459 if (MemMgr) {
1460 // Allocate the GOT if necessary
1461 size_t numGOTEntries = GOTEntries.size();
1462 if (numGOTEntries != 0) {
1463 // Allocate memory for the section
1464 unsigned SectionID = Sections.size();
1465 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1466 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1467 SectionID, ".got", false);
1468 if (!Addr)
1469 report_fatal_error("Unable to allocate memory for GOT!");
1470
1471 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1472 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1473 // For now, initialize all GOT entries to zero. We'll fill them in as
1474 // needed when GOT-based relocations are applied.
1475 memset(Addr, 0, TotalSize);
1476 }
1477 } else {
1478 report_fatal_error("Unable to allocate memory for GOT!");
1457 size_t numGOTEntries = GOTEntries.size();
1458 if (numGOTEntries != 0) {
1459 // Allocate memory for the section
1460 unsigned SectionID = Sections.size();
1461 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1462 uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
1463 SectionID, ".got", false);
1464 if (!Addr)
1465 report_fatal_error("Unable to allocate memory for GOT!");
1466
1467 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1468 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1469 // For now, initialize all GOT entries to zero. We'll fill them in as
1470 // needed when GOT-based relocations are applied.
1471 memset(Addr, 0, TotalSize);
14791472 }
14801473
14811474 // Look for and record the EH frame section.
9797 SmallVector RegisteredEHFrameSections;
9898
9999 public:
100 RuntimeDyldELF(RTDyldMemoryManager *mm);
100 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
101 RuntimeDyld::SymbolResolver &Resolver);
101102 virtual ~RuntimeDyldELF();
102103
103104 std::unique_ptr
1717 #include "llvm/ADT/SmallVector.h"
1818 #include "llvm/ADT/StringMap.h"
1919 #include "llvm/ADT/Triple.h"
20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2021 #include "llvm/ExecutionEngine/RuntimeDyld.h"
2122 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
2223 #include "llvm/Object/ObjectFile.h"
187188 friend class RuntimeDyldCheckerImpl;
188189 protected:
189190 // The MemoryManager to load objects into.
190 RTDyldMemoryManager *MemMgr;
191 RuntimeDyld::MemoryManager &MemMgr;
192
193 // The symbol resolver to use for external symbols.
194 RuntimeDyld::SymbolResolver &Resolver;
191195
192196 // Attached RuntimeDyldChecker instance. Null if no instance attached.
193197 RuntimeDyldCheckerImpl *Checker;
373377 std::pair loadObjectImpl(const object::ObjectFile &Obj);
374378
375379 public:
376 RuntimeDyldImpl(RTDyldMemoryManager *mm)
377 : MemMgr(mm), Checker(nullptr), ProcessAllSections(false), HasError(false) {
380 RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
381 RuntimeDyld::SymbolResolver &Resolver)
382 : MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr),
383 ProcessAllSections(false), HasError(false) {
378384 }
379385
380386 virtual ~RuntimeDyldImpl();
246246 template
247247 void RuntimeDyldMachOCRTPBase::registerEHFrames() {
248248
249 if (!MemMgr)
250 return;
251249 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
252250 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
253251 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
270268 P = processFDE(P, DeltaForText, DeltaForEH);
271269 } while (P != End);
272270
273 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
274 EHFrame->Size);
271 MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
272 EHFrame->Size);
275273 }
276274 UnregisteredEHFrameSections.clear();
277275 }
278276
279277 std::unique_ptr
280 RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
278 RuntimeDyldMachO::create(Triple::ArchType Arch,
279 RuntimeDyld::MemoryManager &MemMgr,
280 RuntimeDyld::SymbolResolver &Resolver) {
281281 switch (Arch) {
282282 default:
283283 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
284284 break;
285 case Triple::arm: return make_unique(MM);
286 case Triple::aarch64: return make_unique(MM);
287 case Triple::x86: return make_unique(MM);
288 case Triple::x86_64: return make_unique(MM);
285 case Triple::arm:
286 return make_unique(MemMgr, Resolver);
287 case Triple::aarch64:
288 return make_unique(MemMgr, Resolver);
289 case Triple::x86:
290 return make_unique(MemMgr, Resolver);
291 case Triple::x86_64:
292 return make_unique(MemMgr, Resolver);
289293 }
290294 }
291295
4848 // EH frame sections with the memory manager.
4949 SmallVector UnregisteredEHFrameSections;
5050
51 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
51 RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr,
52 RuntimeDyld::SymbolResolver &Resolver)
53 : RuntimeDyldImpl(MemMgr, Resolver) {}
5254
5355 /// This convenience method uses memcpy to extract a contiguous addend (the
5456 /// addend size and offset are taken from the corresponding fields of the RE).
113115 public:
114116
115117 /// Create a RuntimeDyldMachO instance for the given target architecture.
116 static std::unique_ptr create(Triple::ArchType Arch,
117 RTDyldMemoryManager *mm);
118 static std::unique_ptr
119 create(Triple::ArchType Arch,
120 RuntimeDyld::MemoryManager &MemMgr,
121 RuntimeDyld::SymbolResolver &Resolver);
118122
119123 std::unique_ptr
120124 loadObject(const object::ObjectFile &O) override;
141145 int64_t DeltaForEH);
142146
143147 public:
144 RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
148 RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,
149 RuntimeDyld::SymbolResolver &Resolver)
150 : RuntimeDyldMachO(MemMgr, Resolver) {}
145151
146152 void finalizeLoad(const ObjectFile &Obj,
147153 ObjSectionToIDMap &SectionMap) override;
3131 SmallVector RegisteredEHFrameSections;
3232
3333 public:
34 RuntimeDyldCOFFX86_64(RTDyldMemoryManager *MM) : RuntimeDyldCOFF(MM) {}
34 RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM,
35 RuntimeDyld::SymbolResolver &Resolver)
36 : RuntimeDyldCOFF(MM, Resolver) {}
3537
3638 unsigned getMaxStubSize() override {
3739 return 6; // 2-byte jmp instruction + 32-bit relative address
176178
177179 unsigned getStubAlignment() override { return 1; }
178180 void registerEHFrames() override {
179 if (!MemMgr)
180 return;
181181 for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
182182 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
183183 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
184184 size_t EHFrameSize = Sections[EHFrameSID].Size;
185 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
185 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
186186 RegisteredEHFrameSections.push_back(EHFrameSID);
187187 }
188188 UnregisteredEHFrameSections.clear();
2222
2323 typedef uint64_t TargetPtrT;
2424
25 RuntimeDyldMachOAArch64(RTDyldMemoryManager *MM)
26 : RuntimeDyldMachOCRTPBase(MM) {}
25 RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
27 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2728
2829 unsigned getMaxStubSize() override { return 8; }
2930
2424
2525 typedef uint32_t TargetPtrT;
2626
27 RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
27 RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
28 RuntimeDyld::SymbolResolver &Resolver)
29 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2830
2931 unsigned getMaxStubSize() override { return 8; }
3032
2121
2222 typedef uint32_t TargetPtrT;
2323
24 RuntimeDyldMachOI386(RTDyldMemoryManager *MM)
25 : RuntimeDyldMachOCRTPBase(MM) {}
24 RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM,
25 RuntimeDyld::SymbolResolver &Resolver)
26 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2627
2728 unsigned getMaxStubSize() override { return 0; }
2829
2121
2222 typedef uint64_t TargetPtrT;
2323
24 RuntimeDyldMachOX86_64(RTDyldMemoryManager *MM)
25 : RuntimeDyldMachOCRTPBase(MM) {}
24 RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,
25 RuntimeDyld::SymbolResolver &Resolver)
26 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2627
2728 unsigned getMaxStubSize() override { return 8; }
2829
2222 case Triple::x86_64: {
2323 typedef orc::JITCompileCallbackManager
2424 orc::OrcX86_64> CCMgrT;
25 return make_unique(CompileLayer, Context, 0, 64);
25 return make_unique(CompileLayer, CCMgrMemMgr, Context, 0, 64);
2626 }
2727 }
2828 }
3838 OrcLazyJIT(std::unique_ptr TM, LLVMContext &Context)
3939 : Error(false), TM(std::move(TM)),
4040 Mang(this->TM->getDataLayout()),
41 ObjectLayer([](){ return llvm::make_unique(); }),
41 ObjectLayer(),
4242 CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
4343 LazyEmitLayer(CompileLayer),
4444 CCMgr(createCallbackMgr(Triple(this->TM->getTargetTriple()), Context)),
8181 bool Error;
8282 std::unique_ptr TM;
8383 Mangler Mang;
84 SectionMemoryManager CCMgrMemMgr;
8485
8586 ObjLayerT ObjectLayer;
8687 CompileLayerT CompileLayer;
1212
1313 #include "llvm/ADT/StringMap.h"
1414 #include "llvm/DebugInfo/DWARF/DIContext.h"
15 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
1516 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1617 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
1718 #include "llvm/MC/MCAsmInfo.h"
195196 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
196197 // Instantiate a dynamic linker.
197198 TrivialMemoryManager MemMgr;
198 RuntimeDyld Dyld(&MemMgr);
199 RuntimeDyld Dyld(MemMgr, MemMgr);
199200
200201 // Load the input memory buffer.
201202
263264
264265 // Instantiate a dynamic linker.
265266 TrivialMemoryManager MemMgr;
266 RuntimeDyld Dyld(&MemMgr);
267 RuntimeDyld Dyld(MemMgr, MemMgr);
267268
268269 // If we don't have any input files, read from stdin.
269270 if (!InputFileList.size())
512513
513514 // Instantiate a dynamic linker.
514515 TrivialMemoryManager MemMgr;
515 RuntimeDyld Dyld(&MemMgr);
516 RuntimeDyld Dyld(MemMgr, MemMgr);
516517 Dyld.setProcessAllSections(true);
517518 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
518519 llvm::dbgs());
1313
1414 struct MockBaseLayer {
1515 typedef int ModuleSetHandleT;
16 ModuleSetHandleT addModuleSet(std::list>,
17 std::unique_ptr x) {
18 EXPECT_FALSE(x);
16 ModuleSetHandleT addModuleSet(
17 std::list>,
18 std::unique_ptr MemMgr,
19 std::unique_ptr Resolver) {
20 EXPECT_FALSE(MemMgr);
1921 return 42;
2022 }
2123 };
2325 TEST(LazyEmittingLayerTest, Empty) {
2426 MockBaseLayer M;
2527 llvm::orc::LazyEmittingLayer L(M);
26 L.addModuleSet(std::list>(), nullptr);
28 L.addModuleSet(std::list>(), nullptr, nullptr);
2729 }
2830
2931 }