llvm.org GIT mirror llvm / 075c1e2
[ExecutionEngine][MCJIT][Orc] Replace RuntimeDyld::SymbolInfo with JITSymbol. This patch replaces RuntimeDyld::SymbolInfo with JITSymbol: A symbol class that is capable of lazy materialization (i.e. the symbol definition needn't be emitted until the address is requested). This can be used to support common and weak symbols in the JIT (though this is not implemented in this patch). For consistency, RuntimeDyld::SymbolResolver is renamed to JITSymbolResolver. For space efficiency a new class, JITEvaluatedSymbol, is introduced that behaves like the old RuntimeDyld::SymbolInfo - i.e. it is just a pair of an address and symbol flags. Instances of JITEvaluatedSymbol can be used in symbol-tables to avoid paying the space cost of the materializer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277386 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 3 years ago
63 changed file(s) with 512 addition(s) and 507 deletion(s). Raw diff Collapse all Expand all
189189 auto Resolver = createLambdaResolver(
190190 [&](const std::string &Name) {
191191 if (auto Sym = CompileLayer.findSymbol(Name, false))
192 return Sym.toRuntimeDyldSymbol();
193 return RuntimeDyld::SymbolInfo(nullptr);
192 return Sym;
193 return JITSymbol(nullptr);
194194 },
195195 [](const std::string &S) {
196196 if (auto SymAddr =
197197 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
198 return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
199 return RuntimeDyld::SymbolInfo(nullptr);
198 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
199 return JITSymbol(nullptr);
200200 });
201201
202202 // Build a singlton module set to hold our module.
241241 whatever makes the most sense for any given use case.
242242
243243 Building a symbol resolver is made especially easy by the *createLambdaResolver*
244 function. This function takes two lambdas [3]_ and returns a
245 RuntimeDyld::SymbolResolver instance. The first lambda is used as the
246 implementation of the resolver's findSymbolInLogicalDylib method, which searches
247 for symbol definitions that should be thought of as being part of the same
248 "logical" dynamic library as this Module. If you are familiar with static
249 linking: this means that findSymbolInLogicalDylib should expose symbols with
250 common linkage and hidden visibility. If all this sounds foreign you can ignore
251 the details and just remember that this is the first method that the linker will
252 use to try to find a symbol definition. If the findSymbolInLogicalDylib method
253 returns a null result then the linker will call the second symbol resolver
254 method, called findSymbol, which searches for symbols that should be thought of
255 as external to (but visibile from) the module and its logical dylib. In this
256 tutorial we will adopt the following simple scheme: All modules added to the JIT
257 will behave as if they were linked into a single, ever-growing logical dylib. To
258 implement this our first lambda (the one defining findSymbolInLogicalDylib) will
259 just search for JIT'd code by calling the CompileLayer's findSymbol method. If
260 we don't find a symbol in the JIT itself we'll fall back to our second lambda,
261 which implements findSymbol. This will use the
262 RTDyldMemoyrManager::getSymbolAddressInProcess method to search for the symbol
263 within the program itself. If we can't find a symbol definition via either of
264 these paths the JIT will refuse to accept our module, returning a "symbol not
265 found" error.
244 function. This function takes two lambdas [3]_ and returns a JITSymbolResolver
245 instance. The first lambda is used as the implementation of the resolver's
246 findSymbolInLogicalDylib method, which searches for symbol definitions that
247 should be thought of as being part of the same "logical" dynamic library as this
248 Module. If you are familiar with static linking: this means that
249 findSymbolInLogicalDylib should expose symbols with common linkage and hidden
250 visibility. If all this sounds foreign you can ignore the details and just
251 remember that this is the first method that the linker will use to try to find a
252 symbol definition. If the findSymbolInLogicalDylib method returns a null result
253 then the linker will call the second symbol resolver method, called findSymbol,
254 which searches for symbols that should be thought of as external to (but
255 visibile from) the module and its logical dylib. In this tutorial we will adopt
256 the following simple scheme: All modules added to the JIT will behave as if they
257 were linked into a single, ever-growing logical dylib. To implement this our
258 first lambda (the one defining findSymbolInLogicalDylib) will just search for
259 JIT'd code by calling the CompileLayer's findSymbol method. If we don't find a
260 symbol in the JIT itself we'll fall back to our second lambda, which implements
261 findSymbol. This will use the RTDyldMemoyrManager::getSymbolAddressInProcess
262 method to search for the symbol within the program itself. If we can't find a
263 symbol definition via either of these paths the JIT will refuse to accept our
264 module, returning a "symbol not found" error.
266265
267266 Now that we've built our symbol resolver we're ready to add our module to the
268267 JIT. We do this by calling the CompileLayer's addModuleSet method [4]_. Since
9292 auto Resolver = createLambdaResolver(
9393 [&](const std::string &Name) {
9494 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
95 return Sym.toRuntimeDyldSymbol();
96 return RuntimeDyld::SymbolInfo(nullptr);
95 return Sym;
96 return JITSymbol(nullptr);
9797 },
9898 // ...
9999
112112 Next we have to update our constructor to initialize the new members. To create
113113 an appropriate compile callback manager we use the
114114 createLocalCompileCallbackManager function, which takes a TargetMachine and a
115 TargetAddress to call if it receives a request to compile an unknown function.
116 In our simple JIT this situation is unlikely to come up, so we'll cheat and
117 just pass '0' here. In a production quality JIT you could give the address of a
118 function that throws an exception in order to unwind the JIT'd code's stack.
115 JITTargetAddress to call if it receives a request to compile an unknown
116 function. In our simple JIT this situation is unlikely to come up, so we'll
117 cheat and just pass '0' here. In a production quality JIT you could give the
118 address of a function that throws an exception in order to unwind the JIT'd
119 code's stack.
119120
120121 Now we can construct our CompileOnDemandLayer. Following the pattern from
121122 previous layers we start by passing a reference to the next layer down in our
1515
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1819 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1920 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2021 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
21 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2222 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2323 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2424 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
6161 auto Resolver = createLambdaResolver(
6262 [&](const std::string &Name) {
6363 if (auto Sym = CompileLayer.findSymbol(Name, false))
64 return Sym.toRuntimeDyldSymbol();
65 return RuntimeDyld::SymbolInfo(nullptr);
64 return Sym;
65 return JITSymbol(nullptr);
6666 },
6767 [](const std::string &Name) {
6868 if (auto SymAddr =
6969 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
70 return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
71 return RuntimeDyld::SymbolInfo(nullptr);
70 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
71 return JITSymbol(nullptr);
7272 });
7373
7474 // Build a singlton module set to hold our module.
1515
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1819 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1920 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2021 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
21 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2222 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2323 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2424 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
7171 auto Resolver = createLambdaResolver(
7272 [&](const std::string &Name) {
7373 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
74 return Sym.toRuntimeDyldSymbol();
75 return RuntimeDyld::SymbolInfo(nullptr);
74 return Sym;
75 return JITSymbol(nullptr);
7676 },
7777 [](const std::string &Name) {
7878 if (auto SymAddr =
7979 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
80 return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
81 return RuntimeDyld::SymbolInfo(nullptr);
80 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
81 return JITSymbol(nullptr);
8282 });
8383
8484 // Build a singlton module set to hold our module.
1515
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1819 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1920 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2021 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
2122 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2323 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2424 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2525 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
8282 auto Resolver = createLambdaResolver(
8383 [&](const std::string &Name) {
8484 if (auto Sym = CODLayer.findSymbol(Name, false))
85 return Sym.toRuntimeDyldSymbol();
86 return RuntimeDyld::SymbolInfo(nullptr);
85 return Sym;
86 return JITSymbol(nullptr);
8787 },
8888 [](const std::string &Name) {
8989 if (auto SymAddr =
9090 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
91 return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
92 return RuntimeDyld::SymbolInfo(nullptr);
91 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
92 return JITSymbol(nullptr);
9393 });
9494
9595 // Build a singlton module set to hold our module.
1515
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1819 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1920 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2021 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
2122 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2323 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2424 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2525 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
106106 auto Resolver = createLambdaResolver(
107107 [&](const std::string &Name) {
108108 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
109 return Sym.toRuntimeDyldSymbol();
109 return Sym;
110110 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
111 return Sym.toRuntimeDyldSymbol();
112 return RuntimeDyld::SymbolInfo(nullptr);
111 return Sym;
112 return JITSymbol(nullptr);
113113 },
114114 [](const std::string &Name) {
115115 if (auto SymAddr =
116116 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
117 return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
118 return RuntimeDyld::SymbolInfo(nullptr);
117 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
118 return JITSymbol(nullptr);
119119 });
120120
121121 // Build a singlton module set to hold our module.
172172 addModule(std::move(M));
173173 auto Sym = findSymbol(SharedFnAST->getName() + "$impl");
174174 assert(Sym && "Couldn't find compiled function?");
175 TargetAddress SymAddr = Sym.getAddress();
175 JITTargetAddress SymAddr = Sym.getAddress();
176176 if (auto Err =
177177 IndirectStubsMgr->updatePointer(mangle(SharedFnAST->getName()),
178178 SymAddr)) {
1616 #include "RemoteJITUtils.h"
1717 #include "llvm/ADT/STLExtras.h"
1818 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/JITSymbol.h"
1920 #include "llvm/ExecutionEngine/RuntimeDyld.h"
2021 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2122 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
2223 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
23 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2424 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2525 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2626 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
122122 auto Resolver = createLambdaResolver(
123123 [&](const std::string &Name) {
124124 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
125 return Sym.toRuntimeDyldSymbol();
125 return Sym;
126126 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
127 return Sym.toRuntimeDyldSymbol();
128 return RuntimeDyld::SymbolInfo(nullptr);
127 return Sym;
128 return JITSymbol(nullptr);
129129 },
130130 [&](const std::string &Name) {
131131 if (auto AddrOrErr = Remote.getSymbolAddress(Name))
132 return RuntimeDyld::SymbolInfo(*AddrOrErr,
133 JITSymbolFlags::Exported);
132 return JITSymbol(*AddrOrErr, JITSymbolFlags::Exported);
134133 else {
135134 logAllUnhandledErrors(AddrOrErr.takeError(), errs(),
136135 "Error resolving remote symbol:");
137136 exit(1);
138137 }
139 return RuntimeDyld::SymbolInfo(nullptr);
138 return JITSymbol(nullptr);
140139 });
141140
142141 std::unique_ptr MemMgr;
200199 addModule(std::move(M));
201200 auto Sym = findSymbol(SharedFnAST->getName() + "$impl");
202201 assert(Sym && "Couldn't find compiled function?");
203 TargetAddress SymAddr = Sym.getAddress();
202 JITTargetAddress SymAddr = Sym.getAddress();
204203 if (auto Err =
205204 IndirectStubsMgr->updatePointer(mangle(SharedFnAST->getName()),
206205 SymAddr)) {
215214 return Error::success();
216215 }
217216
218 Error executeRemoteExpr(TargetAddress ExprAddr) {
217 Error executeRemoteExpr(JITTargetAddress ExprAddr) {
219218 return Remote.callVoidVoid(ExprAddr);
220219 }
221220
1616 #include "llvm/ADT/iterator_range.h"
1717 #include "llvm/ADT/STLExtras.h"
1818 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/JITSymbolFlags.h"
19 #include "llvm/ExecutionEngine/JITSymbol.h"
2020 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2121 #include "llvm/ExecutionEngine/RuntimeDyld.h"
2222 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2323 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
24 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
2524 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2625 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2726 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
5958 auto Resolver = createLambdaResolver(
6059 [&](const std::string &Name) {
6160 if (auto Sym = findMangledSymbol(Name))
62 return Sym.toRuntimeDyldSymbol();
63 return RuntimeDyld::SymbolInfo(nullptr);
61 return Sym;
62 return JITSymbol(nullptr);
6463 },
6564 [](const std::string &S) { return nullptr; });
6665 auto H = CompileLayer.addModuleSet(singletonSet(std::move(M)),
136136 std::unique_ptr M,
137137 std::string *ErrorStr,
138138 std::shared_ptr MM,
139 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
139 std::shared_ptr<JITSymbolResolver> SR,
140140 std::unique_ptr TM);
141141
142142 static ExecutionEngine *(*OrcMCJITReplacementCtor)(
143143 std::string *ErrorStr,
144144 std::shared_ptr MM,
145 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
145 std::shared_ptr<JITSymbolResolver> SR,
146146 std::unique_ptr TM);
147147
148148 static ExecutionEngine *(*InterpCtor)(std::unique_ptr M,
515515 std::string *ErrorStr;
516516 CodeGenOpt::Level OptLevel;
517517 std::shared_ptr MemMgr;
518 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver;
518 std::shared_ptr<JITSymbolResolver> Resolver;
519519 TargetOptions Options;
520520 Optional RelocModel;
521521 CodeModel::Model CMModel;
554554 setMemoryManager(std::unique_ptr MM);
555555
556556 EngineBuilder&
557 setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
557 setSymbolResolver(std::unique_ptr<JITSymbolResolver> SR);
558558
559559 /// setErrorStr - Set the error string to write to on error. This option
560560 /// defaults to NULL.
0 //===----------- JITSymbol.h - JIT symbol abstraction -----------*- 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 // Abstraction for target process addresses.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
14 #define LLVM_EXECUTIONENGINE_JITSYMBOL_H
15
16 #include "llvm/ExecutionEngine/JITSymbolFlags.h"
17 #include "llvm/Support/DataTypes.h"
18 #include
19 #include
20
21 namespace llvm {
22
23 /// @brief Represents an address in the target process's address space.
24 typedef uint64_t JITTargetAddress;
25
26 /// @brief Represents a symbol that has been evaluated to an address already.
27 class JITEvaluatedSymbol : public JITSymbolBase {
28 public:
29
30 /// @brief Create a 'null' symbol.
31 JITEvaluatedSymbol(std::nullptr_t)
32 : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
33
34 /// @brief Create a symbol for the given address and flags.
35 JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags)
36 : JITSymbolBase(Flags), Address(Address) {}
37
38 /// @brief An evaluated symbol converts to 'true' if its address is non-zero.
39 explicit operator bool() const { return Address != 0; }
40
41 /// @brief Return the address of this symbol.
42 JITTargetAddress getAddress() const { return Address; }
43
44 private:
45 JITTargetAddress Address;
46 };
47
48 /// @brief Represents a symbol in the JIT.
49 class JITSymbol : public JITSymbolBase {
50 public:
51
52 typedef std::function GetAddressFtor;
53
54 /// @brief Create a 'null' symbol that represents failure to find a symbol
55 /// definition.
56 JITSymbol(std::nullptr_t)
57 : JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
58
59 /// @brief Create a symbol for a definition with a known address.
60 JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
61 : JITSymbolBase(Flags), CachedAddr(Addr) {}
62
63 /// @brief Construct a JITSymbol from a JITEvaluatedSymbol.
64 JITSymbol(JITEvaluatedSymbol Sym)
65 : JITSymbolBase(Sym.getFlags()), CachedAddr(Sym.getAddress()) {}
66
67 /// @brief Create a symbol for a definition that doesn't have a known address
68 /// yet.
69 /// @param GetAddress A functor to materialize a definition (fixing the
70 /// address) on demand.
71 ///
72 /// This constructor allows a JIT layer to provide a reference to a symbol
73 /// definition without actually materializing the definition up front. The
74 /// user can materialize the definition at any time by calling the getAddress
75 /// method.
76 JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
77 : JITSymbolBase(Flags), GetAddress(std::move(GetAddress)), CachedAddr(0) {}
78
79 /// @brief Returns true if the symbol exists, false otherwise.
80 explicit operator bool() const { return CachedAddr || GetAddress; }
81
82 /// @brief Get the address of the symbol in the target address space. Returns
83 /// '0' if the symbol does not exist.
84 JITTargetAddress getAddress() {
85 if (GetAddress) {
86 CachedAddr = GetAddress();
87 assert(CachedAddr && "Symbol could not be materialized.");
88 GetAddress = nullptr;
89 }
90 return CachedAddr;
91 }
92
93 private:
94 GetAddressFtor GetAddress;
95 JITTargetAddress CachedAddr;
96 };
97
98 /// \brief Symbol resolution.
99 class JITSymbolResolver {
100 public:
101 virtual ~JITSymbolResolver() {}
102
103 /// This method returns the address of the specified symbol if it exists
104 /// within the logical dynamic library represented by this JITSymbolResolver.
105 /// Unlike findSymbol, queries through this interface should return addresses
106 /// for hidden symbols.
107 ///
108 /// This is of particular importance for the Orc JIT APIs, which support lazy
109 /// compilation by breaking up modules: Each of those broken out modules
110 /// must be able to resolve hidden symbols provided by the others. Clients
111 /// writing memory managers for MCJIT can usually ignore this method.
112 ///
113 /// This method will be queried by RuntimeDyld when checking for previous
114 /// definitions of common symbols.
115 virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name) = 0;
116
117 /// This method returns the address of the specified function or variable.
118 /// It is used to resolve symbols during module linking.
119 ///
120 /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
121 /// skip all relocations for that symbol, and the client will be responsible
122 /// for handling them manually.
123 virtual JITSymbol findSymbol(const std::string &Name) = 0;
124
125 private:
126 virtual void anchor();
127 };
128
129 } // End namespace llvm.
130
131 #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H
119119 };
120120
121121 struct LogicalDylibResources {
122 typedef std::function
123 SymbolResolverFtor;
122 typedef std::function SymbolResolverFtor;
124123
125124 typedef std::function
126125 BaseLayerT&,
127126 std::unique_ptr,
128 std::unique_ptr<RuntimeDyld::SymbolResolver>)>
127 std::unique_ptr<JITSymbolResolver>)>
129128 ModuleAdderFtor;
130129
131130 LogicalDylibResources() = default;
144143 return *this;
145144 }
146145
147 std::unique_ptr<RuntimeDyld::SymbolResolver> ExternalSymbolResolver;
146 std::unique_ptr<JITSymbolResolver> ExternalSymbolResolver;
148147 std::unique_ptr> MemMgr;
149148 ModuleAdderFtor ModuleAdder;
150149 };
195194
196195 LDResources.ModuleAdder =
197196 [&MemMgrRef](BaseLayerT &B, std::unique_ptr M,
198 std::unique_ptr<RuntimeDyld::SymbolResolver> R) {
197 std::unique_ptr<JITSymbolResolver> R) {
199198 std::vector> Ms;
200199 Ms.push_back(std::move(M));
201200 return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R));
244243 // callbacks, uncompiled IR, and no-longer-needed/reachable function
245244 // implementations).
246245 // FIXME: Return Error once the JIT APIs are Errorized.
247 bool updatePointer(std::string FuncName, TargetAddress FnBodyAddr) {
246 bool updatePointer(std::string FuncName, JITTargetAddress FnBodyAddr) {
248247 //Find out which logical dylib contains our symbol
249248 auto LDI = LogicalDylibs.begin();
250249 for (auto LDE = LogicalDylibs.end(); LDI != LDE; ++LDI) {
385384 [&LD, LMH](const std::string &Name) {
386385 auto &LMResources = LD.getLogicalModuleResources(LMH);
387386 if (auto Sym = LMResources.StubsMgr->findStub(Name, false))
388 return Sym.toRuntimeDyldSymbol();
387 return Sym;
389388 auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
390389 return LDResolver->findSymbolInLogicalDylib(Name);
391390 },
408407 return MangledName;
409408 }
410409
411 TargetAddress extractAndCompile(CODLogicalDylib &LD,
412 LogicalModuleHandle LMH,
413 Function &F) {
410 JITTargetAddress extractAndCompile(CODLogicalDylib &LD,
411 LogicalModuleHandle LMH,
412 Function &F) {
414413 auto &LMResources = LD.getLogicalModuleResources(LMH);
415414 Module &SrcM = LMResources.SourceModule->getResource();
416415
424423 auto Part = Partition(F);
425424 auto PartH = emitPartition(LD, LMH, Part);
426425
427 TargetAddress CalledAddr = 0;
426 JITTargetAddress CalledAddr = 0;
428427 for (auto *SubF : Part) {
429428 std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
430429 auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false);
431430 assert(FnBodySym && "Couldn't find function body.");
432431
433 TargetAddress FnBodyAddr = FnBodySym.getAddress();
432 JITTargetAddress FnBodyAddr = FnBodySym.getAddress();
434433
435434 // If this is the function we're calling record the address so we can
436435 // return it from this function.
512511 auto Resolver = createLambdaResolver(
513512 [this, &LD, LMH](const std::string &Name) {
514513 if (auto Sym = LD.findSymbolInternally(LMH, Name))
515 return Sym.toRuntimeDyldSymbol();
514 return Sym;
516515 auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
517516 return LDResolver->findSymbolInLogicalDylib(Name);
518517 },
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
1414 #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
1515
16 #include "JITSymbol.h"
1716 #include "llvm/ADT/iterator_range.h"
1817 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1919 #include "llvm/ExecutionEngine/RuntimeDyld.h"
2020 #include
2121
143143 }
144144
145145 /// Search overrided symbols.
146 RuntimeDyld::SymbolInfo searchOverrides(const std::string &Name) {
146 JITEvaluatedSymbol searchOverrides(const std::string &Name) {
147147 auto I = CXXRuntimeOverrides.find(Name);
148148 if (I != CXXRuntimeOverrides.end())
149 return RuntimeDyld::SymbolInfo(I->second, JITSymbolFlags::Exported);
149 return JITEvaluatedSymbol(I->second, JITSymbolFlags::Exported);
150150 return nullptr;
151151 }
152152
157157 private:
158158
159159 template
160 TargetAddress toTargetAddress(PtrTy* P) {
161 return static_cast(reinterpret_cast(P));
160 JITTargetAddress toTargetAddress(PtrTy* P) {
161 return static_cast(reinterpret_cast(P));
162162 }
163163
164 void addOverride(const std::string &Name, TargetAddress Addr) {
164 void addOverride(const std::string &Name, JITTargetAddress Addr) {
165165 CXXRuntimeOverrides.insert(std::make_pair(Name, Addr));
166166 }
167167
168 StringMap<TargetAddress> CXXRuntimeOverrides;
168 StringMap<JITTargetAddress> CXXRuntimeOverrides;
169169
170170 typedef void (*DestructorPtr)(void*);
171171 typedef std::pair CXXDestructorDataPair;
1414 #ifndef LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
1515 #define LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
1616
17 #include "JITSymbol.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
1818 #include
1919
2020 namespace llvm {
5151 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
5252
5353 /// @brief Manually set the address to return for the given symbol.
54 void setGlobalMapping(const std::string &Name, TargetAddress Addr) {
54 void setGlobalMapping(const std::string &Name, JITTargetAddress Addr) {
5555 SymbolTable[Name] = Addr;
5656 }
5757
9898
9999 private:
100100 BaseLayerT &BaseLayer;
101 std::mapTargetAddress> SymbolTable;
101 std::mapJITTargetAddress> SymbolTable;
102102 };
103103
104104 } // End namespace orc.
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
1515
16 #include "JITSymbol.h"
1716 #include "llvm/ExecutionEngine/ObjectCache.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
1918 #include "llvm/Object/ObjectFile.h"
2019 #include
2120
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
1515
16 #include "JITSymbol.h"
16 #include "llvm/ExecutionEngine/JITSymbol.h"
1717
1818 namespace llvm {
1919 namespace orc {
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H
1414 #define LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H
1515
16 #include "JITSymbol.h"
1716 #include "LambdaResolver.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
1818 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1919 #include "llvm/IR/IRBuilder.h"
2020 #include "llvm/IR/Mangler.h"
2828 /// @brief Target-independent base class for compile callback management.
2929 class JITCompileCallbackManager {
3030 public:
31 typedef std::function<TargetAddress()> CompileFtor;
31 typedef std::function<JITTargetAddress()> CompileFtor;
3232
3333 /// @brief Handle to a newly created compile callback. Can be used to get an
3434 /// IR constant representing the address of the trampoline, and to set
3535 /// the compile action for the callback.
3636 class CompileCallbackInfo {
3737 public:
38 CompileCallbackInfo(TargetAddress Addr, CompileFtor &Compile)
38 CompileCallbackInfo(JITTargetAddress Addr, CompileFtor &Compile)
3939 : Addr(Addr), Compile(Compile) {}
4040
41 TargetAddress getAddress() const { return Addr; }
41 JITTargetAddress getAddress() const { return Addr; }
4242 void setCompileAction(CompileFtor Compile) {
4343 this->Compile = std::move(Compile);
4444 }
4545
4646 private:
47 TargetAddress Addr;
47 JITTargetAddress Addr;
4848 CompileFtor &Compile;
4949 };
5050
5151 /// @brief Construct a JITCompileCallbackManager.
5252 /// @param ErrorHandlerAddress The address of an error handler in the target
5353 /// process to be used if a compile callback fails.
54 JITCompileCallbackManager(TargetAddress ErrorHandlerAddress)
54 JITCompileCallbackManager(JITTargetAddress ErrorHandlerAddress)
5555 : ErrorHandlerAddress(ErrorHandlerAddress) {}
5656
5757 virtual ~JITCompileCallbackManager() {}
5858
5959 /// @brief Execute the callback for the given trampoline id. Called by the JIT
6060 /// to compile functions on demand.
61 TargetAddress executeCompileCallback(TargetAddress TrampolineAddr) {
61 JITTargetAddress executeCompileCallback(JITTargetAddress TrampolineAddr) {
6262 auto I = ActiveTrampolines.find(TrampolineAddr);
6363 // FIXME: Also raise an error in the Orc error-handler when we finally have
6464 // one.
8585
8686 /// @brief Reserve a compile callback.
8787 CompileCallbackInfo getCompileCallback() {
88 TargetAddress TrampolineAddr = getAvailableTrampolineAddr();
88 JITTargetAddress TrampolineAddr = getAvailableTrampolineAddr();
8989 auto &Compile = this->ActiveTrampolines[TrampolineAddr];
9090 return CompileCallbackInfo(TrampolineAddr, Compile);
9191 }
9292
9393 /// @brief Get a CompileCallbackInfo for an existing callback.
94 CompileCallbackInfo getCompileCallbackInfo(TargetAddress TrampolineAddr) {
94 CompileCallbackInfo getCompileCallbackInfo(JITTargetAddress TrampolineAddr) {
9595 auto I = ActiveTrampolines.find(TrampolineAddr);
9696 assert(I != ActiveTrampolines.end() && "Not an active trampoline.");
9797 return CompileCallbackInfo(I->first, I->second);
102102 /// Note: Callbacks are auto-released after they execute. This method should
103103 /// only be called to manually release a callback that is not going to
104104 /// execute.
105 void releaseCompileCallback(TargetAddress TrampolineAddr) {
105 void releaseCompileCallback(JITTargetAddress TrampolineAddr) {
106106 auto I = ActiveTrampolines.find(TrampolineAddr);
107107 assert(I != ActiveTrampolines.end() && "Not an active trampoline.");
108108 ActiveTrampolines.erase(I);
110110 }
111111
112112 protected:
113 TargetAddress ErrorHandlerAddress;
114
115 typedef std::map TrampolineMapT;
113 JITTargetAddress ErrorHandlerAddress;
114
115 typedef std::map TrampolineMapT;
116116 TrampolineMapT ActiveTrampolines;
117 std::vector<TargetAddress> AvailableTrampolines;
117 std::vector<JITTargetAddress> AvailableTrampolines;
118118
119119 private:
120 TargetAddress getAvailableTrampolineAddr() {
120 JITTargetAddress getAvailableTrampolineAddr() {
121121 if (this->AvailableTrampolines.empty())
122122 grow();
123123 assert(!this->AvailableTrampolines.empty() &&
124124 "Failed to grow available trampolines.");
125 TargetAddress TrampolineAddr = this->AvailableTrampolines.back();
125 JITTargetAddress TrampolineAddr = this->AvailableTrampolines.back();
126126 this->AvailableTrampolines.pop_back();
127127 return TrampolineAddr;
128128 }
140140 /// @brief Construct a InProcessJITCompileCallbackManager.
141141 /// @param ErrorHandlerAddress The address of an error handler in the target
142142 /// process to be used if a compile callback fails.
143 LocalJITCompileCallbackManager(TargetAddress ErrorHandlerAddress)
143 LocalJITCompileCallbackManager(JITTargetAddress ErrorHandlerAddress)
144144 : JITCompileCallbackManager(ErrorHandlerAddress) {
145145
146146 /// Set up the resolver block.
160160 }
161161
162162 private:
163 static TargetAddress reenter(void *CCMgr, void *TrampolineId) {
163 static JITTargetAddress reenter(void *CCMgr, void *TrampolineId) {
164164 JITCompileCallbackManager *Mgr =
165165 static_cast(CCMgr);
166166 return Mgr->executeCompileCallback(
167 static_cast<TargetAddress>(reinterpret_cast(TrampolineId)));
167 static_cast<JITTargetAddress>(
168 reinterpret_cast(TrampolineId)));
168169 }
169170
170171 void grow() override {
187188
188189 for (unsigned I = 0; I < NumTrampolines; ++I)
189190 this->AvailableTrampolines.push_back(
190 static_cast<TargetAddress>(reinterpret_cast(
191 static_cast<JITTargetAddress>(reinterpret_cast(
191192 TrampolineMem + (I * TargetT::TrampolineSize))));
192193
193194 EC = sys::Memory::protectMappedMemory(TrampolineBlock.getMemoryBlock(),
206207 class IndirectStubsManager {
207208 public:
208209 /// @brief Map type for initializing the manager. See init.
209 typedef StringMapTargetAddress, JITSymbolFlags>> StubInitsMap;
210 typedef StringMapJITTargetAddress, JITSymbolFlags>> StubInitsMap;
210211
211212 virtual ~IndirectStubsManager() {}
212213
213214 /// @brief Create a single stub with the given name, target address and flags.
214 virtual Error createStub(StringRef StubName, TargetAddress StubAddr,
215 virtual Error createStub(StringRef StubName, JITTargetAddress StubAddr,
215216 JITSymbolFlags StubFlags) = 0;
216217
217218 /// @brief Create StubInits.size() stubs with the given names, target
227228 virtual JITSymbol findPointer(StringRef Name) = 0;
228229
229230 /// @brief Change the value of the implementation pointer for the stub.
230 virtual Error updatePointer(StringRef Name, TargetAddress NewAddr) = 0;
231 virtual Error updatePointer(StringRef Name, JITTargetAddress NewAddr) = 0;
231232
232233 private:
233234 virtual void anchor();
238239 template
239240 class LocalIndirectStubsManager : public IndirectStubsManager {
240241 public:
241 Error createStub(StringRef StubName, TargetAddress StubAddr,
242 Error createStub(StringRef StubName, JITTargetAddress StubAddr,
242243 JITSymbolFlags StubFlags) override {
243244 if (auto Err = reserveStubs(1))
244245 return Err;
267268 void *StubAddr = IndirectStubsInfos[Key.first].getStub(Key.second);
268269 assert(StubAddr && "Missing stub address");
269270 auto StubTargetAddr =
270 static_cast<TargetAddress>(reinterpret_cast(StubAddr));
271 static_cast<JITTargetAddress>(reinterpret_cast(StubAddr));
271272 auto StubSymbol = JITSymbol(StubTargetAddr, I->second.second);
272273 if (ExportedStubsOnly && !StubSymbol.isExported())
273274 return nullptr;
282283 void *PtrAddr = IndirectStubsInfos[Key.first].getPtr(Key.second);
283284 assert(PtrAddr && "Missing pointer address");
284285 auto PtrTargetAddr =
285 static_cast<TargetAddress>(reinterpret_cast(PtrAddr));
286 static_cast<JITTargetAddress>(reinterpret_cast(PtrAddr));
286287 return JITSymbol(PtrTargetAddr, I->second.second);
287288 }
288289
289 Error updatePointer(StringRef Name, TargetAddress NewAddr) override {
290 Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override {
290291 auto I = StubIndexes.find(Name);
291292 assert(I != StubIndexes.end() && "No stub pointer for symbol");
292293 auto Key = I->second.first;
312313 return Error::success();
313314 }
314315
315 void createStubInternal(StringRef StubName, TargetAddress InitAddr,
316 void createStubInternal(StringRef StubName, JITTargetAddress InitAddr,
316317 JITSymbolFlags StubFlags) {
317318 auto Key = FreeStubs.back();
318319 FreeStubs.pop_back();
334335 /// manager if a compile callback fails.
335336 std::unique_ptr
336337 createLocalCompileCallbackManager(const Triple &T,
337 TargetAddress ErrorHandlerAddress);
338 JITTargetAddress ErrorHandlerAddress);
338339
339340 /// @brief Create a local indriect stubs manager builder.
340341 ///
347348 ///
348349 /// Usage example: Turn a trampoline address into a function pointer constant
349350 /// for use in a stub.
350 Constant *createIRTypedAddress(FunctionType &FT, TargetAddress Addr);
351 Constant *createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr);
351352
352353 /// @brief Create a function pointer with the given type, name, and initializer
353354 /// in the given Module.
+0
-87
include/llvm/ExecutionEngine/Orc/JITSymbol.h less more
None //===----------- JITSymbol.h - JIT symbol abstraction -----------*- 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 // Abstraction for target process addresses.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
14 #define LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
15
16 #include "llvm/ExecutionEngine/JITSymbolFlags.h"
17 #include "llvm/ExecutionEngine/RuntimeDyld.h"
18 #include "llvm/Support/DataTypes.h"
19 #include
20 #include
21
22 namespace llvm {
23 namespace orc {
24
25 /// @brief Represents an address in the target process's address space.
26 typedef uint64_t TargetAddress;
27
28 /// @brief Represents a symbol in the JIT.
29 class JITSymbol : public JITSymbolBase {
30 public:
31
32 typedef std::function GetAddressFtor;
33
34 /// @brief Create a 'null' symbol that represents failure to find a symbol
35 /// definition.
36 JITSymbol(std::nullptr_t)
37 : JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
38
39 /// @brief Create a symbol for a definition with a known address.
40 JITSymbol(TargetAddress Addr, JITSymbolFlags Flags)
41 : JITSymbolBase(Flags), CachedAddr(Addr) {}
42
43 /// @brief Create a symbol for a definition that doesn't have a known address
44 /// yet.
45 /// @param GetAddress A functor to materialize a definition (fixing the
46 /// address) on demand.
47 ///
48 /// This constructor allows a JIT layer to provide a reference to a symbol
49 /// definition without actually materializing the definition up front. The
50 /// user can materialize the definition at any time by calling the getAddress
51 /// method.
52 JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
53 : JITSymbolBase(Flags), GetAddress(std::move(GetAddress)), CachedAddr(0) {}
54
55 /// @brief Create a JITSymbol from a RuntimeDyld::SymbolInfo.
56 JITSymbol(const RuntimeDyld::SymbolInfo &Sym)
57 : JITSymbolBase(Sym.getFlags()), CachedAddr(Sym.getAddress()) {}
58
59 /// @brief Returns true if the symbol exists, false otherwise.
60 explicit operator bool() const { return CachedAddr || GetAddress; }
61
62 /// @brief Get the address of the symbol in the target address space. Returns
63 /// '0' if the symbol does not exist.
64 TargetAddress getAddress() {
65 if (GetAddress) {
66 CachedAddr = GetAddress();
67 assert(CachedAddr && "Symbol could not be materialized.");
68 GetAddress = nullptr;
69 }
70 return CachedAddr;
71 }
72
73 /// @brief Convert this JITSymbol to a RuntimeDyld::SymbolInfo.
74 RuntimeDyld::SymbolInfo toRuntimeDyldSymbol() {
75 return RuntimeDyld::SymbolInfo(getAddress(), getFlags());
76 }
77
78 private:
79 GetAddressFtor GetAddress;
80 TargetAddress CachedAddr;
81 };
82
83 } // End namespace orc.
84 } // End namespace llvm.
85
86 #endif // LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
2222 namespace orc {
2323
2424 template
25 class LambdaResolver : public RuntimeDyld::SymbolResolver {
25 class LambdaResolver : public JITSymbolResolver {
2626 public:
2727
2828 LambdaResolver(DylibLookupFtorT DylibLookupFtor,
3030 : DylibLookupFtor(DylibLookupFtor),
3131 ExternalLookupFtor(ExternalLookupFtor) {}
3232
33 RuntimeDyld::SymbolInfo
34 findSymbolInLogicalDylib(const std::string &Name) final {
33 JITSymbol findSymbolInLogicalDylib(const std::string &Name) final {
3534 return DylibLookupFtor(Name);
3635 }
3736
38 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) final {
37 JITSymbol findSymbol(const std::string &Name) final {
3938 return ExternalLookupFtor(Name);
4039 }
4140
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
1515
16 #include "JITSymbol.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
16 #include "llvm/ExecutionEngine/JITSymbol.h"
1817 #include "llvm/IR/GlobalValue.h"
1918 #include "llvm/IR/Mangler.h"
2019 #include "llvm/IR/Module.h"
5150 std::string PName = Name;
5251 JITSymbolFlags Flags = JITSymbolBase::flagsFromGlobalValue(*GV);
5352 auto GetAddress =
54 [this, ExportedSymbolsOnly, PName, &B]() -> TargetAddress {
53 [this, ExportedSymbolsOnly, PName, &B]() -> JITTargetAddress {
5554 if (this->EmitState == Emitting)
5655 return 0;
5756 else if (this->EmitState == NotEmitted) {
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
1414 #define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
1515
16 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
16 #include "llvm/ExecutionEngine/JITSymbol.h"
1717 #include
1818 #include
1919
2121
2222 /// SymbolResolver impliementation that rejects all resolution requests.
2323 /// Useful for clients that have no cross-object fixups.
24 class NullResolver : public RuntimeDyld::SymbolResolver {
24 class NullResolver : public JITSymbolResolver {
2525 public:
26 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) final;
26 JITSymbol findSymbol(const std::string &Name) final;
2727
28 RuntimeDyld::SymbolInfo
29 findSymbolInLogicalDylib(const std::string &Name) final;
28 JITSymbol findSymbolInLogicalDylib(const std::string &Name) final;
3029 };
3130
3231 } // End namespace orc.
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
1515
16 #include "JITSymbol.h"
1716 #include "llvm/ADT/STLExtras.h"
1817 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
1919 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2020 #include
2121 #include
4545 getSymbolMaterializer(std::string Name) = 0;
4646
4747 virtual void mapSectionAddress(const void *LocalAddress,
48 TargetAddress TargetAddr) const = 0;
48 JITTargetAddress TargetAddr) const = 0;
4949
5050 JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly) {
5151 auto SymEntry = SymbolTable.find(Name);
5959 return JITSymbol(SymEntry->second);
6060 }
6161 protected:
62 StringMap<RuntimeDyld::SymbolInfo> SymbolTable;
62 StringMap<JITEvaluatedSymbol> SymbolTable;
6363 bool Finalized = false;
6464 };
6565
143143 }
144144
145145 void mapSectionAddress(const void *LocalAddress,
146 TargetAddress TargetAddr) const override {
146 JITTargetAddress TargetAddr) const override {
147147 assert(PFC && "mapSectionAddress called on finalized LinkedObjectSet");
148148 assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObjectSet");
149149 PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr);
164164 }
165165 auto Flags = JITSymbol::flagsFromObjectSymbol(Symbol);
166166 SymbolTable.insert(
167 std::make_pair(*SymbolName, RuntimeDyld::SymbolInfo(0, Flags)));
167 std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags)));
168168 }
169169 }
170170
321321
322322 /// @brief Map section addresses for the objects associated with the handle H.
323323 void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
324 TargetAddress TargetAddr) {
324 JITTargetAddress TargetAddr) {
325325 (*H)->mapSectionAddress(LocalAddress, TargetAddr);
326326 }
327327
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
1414 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
1515
16 #include "JITSymbol.h"
16 #include "llvm/ExecutionEngine/JITSymbol.h"
1717
1818 namespace llvm {
1919 namespace orc {
8282
8383 /// @brief Map section addresses for the objects associated with the handle H.
8484 void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
85 TargetAddress TargetAddr) {
85 JITTargetAddress TargetAddr) {
8686 BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr);
8787 }
8888
3636 static const unsigned TrampolineSize = 1;
3737 static const unsigned ResolverCodeSize = 1;
3838
39 typedef TargetAddress (*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
39 typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr,
40 void *TrampolineId);
4041
4142 static void writeResolverCode(uint8_t *ResolveMem, JITReentryFn Reentry,
4243 void *CallbackMgr) {
114115
115116 typedef GenericIndirectStubsInfo<8> IndirectStubsInfo;
116117
117 typedef TargetAddress (*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
118 typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr,
119 void *TrampolineId);
118120
119121 /// @brief Write the resolver code into the given memory. The user is be
120122 /// responsible for allocating the memory and setting permissions.
169171 class OrcX86_64_SysV : public OrcX86_64_Base {
170172 public:
171173 static const unsigned ResolverCodeSize = 0x6C;
172 typedef TargetAddress(*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
174 typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr,
175 void *TrampolineId);
173176
174177 /// @brief Write the resolver code into the given memory. The user is be
175178 /// responsible for allocating the memory and setting permissions.
183186 class OrcX86_64_Win32 : public OrcX86_64_Base {
184187 public:
185188 static const unsigned ResolverCodeSize = 0x74;
186 typedef TargetAddress(*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
189 typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr,
190 void *TrampolineId);
187191
188192 /// @brief Write the resolver code into the given memory. The user is be
189193 /// responsible for allocating the memory and setting permissions.
202206
203207 typedef GenericIndirectStubsInfo<8> IndirectStubsInfo;
204208
205 typedef TargetAddress (*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
209 typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr,
210 void *TrampolineId);
206211
207212 /// @brief Write the resolver code into the given memory. The user is be
208213 /// responsible for allocating the memory and setting permissions.
184184 DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
185185 for (auto &ObjAllocs : Unmapped) {
186186 {
187 TargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;
187 JITTargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;
188188 for (auto &Alloc : ObjAllocs.CodeAllocs) {
189189 NextCodeAddr = alignTo(NextCodeAddr, Alloc.getAlign());
190190 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextCodeAddr);
196196 }
197197 }
198198 {
199 TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
199 JITTargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
200200 for (auto &Alloc : ObjAllocs.RODataAllocs) {
201201 NextRODataAddr = alignTo(NextRODataAddr, Alloc.getAlign());
202202 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRODataAddr);
209209 }
210210 }
211211 {
212 TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
212 JITTargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
213213 for (auto &Alloc : ObjAllocs.RWDataAllocs) {
214214 NextRWDataAddr = alignTo(NextRWDataAddr, Alloc.getAlign());
215215 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRWDataAddr);
388388 return reinterpret_cast(LocalAddr);
389389 }
390390
391 void setRemoteAddress(TargetAddress RemoteAddr) {
391 void setRemoteAddress(JITTargetAddress RemoteAddr) {
392392 this->RemoteAddr = RemoteAddr;
393393 }
394394
395 TargetAddress getRemoteAddress() const { return RemoteAddr; }
395 JITTargetAddress getRemoteAddress() const { return RemoteAddr; }
396396
397397 private:
398398 uint64_t Size;
399399 unsigned Align;
400400 std::unique_ptr Contents;
401 TargetAddress RemoteAddr = 0;
401 JITTargetAddress RemoteAddr = 0;
402402 };
403403
404404 struct ObjectAllocs {
422422 return *this;
423423 }
424424
425 TargetAddress RemoteCodeAddr = 0;
426 TargetAddress RemoteRODataAddr = 0;
427 TargetAddress RemoteRWDataAddr = 0;
425 JITTargetAddress RemoteCodeAddr = 0;
426 JITTargetAddress RemoteRODataAddr = 0;
427 JITTargetAddress RemoteRWDataAddr = 0;
428428 std::vector CodeAllocs, RODataAllocs, RWDataAllocs;
429429 };
430430
449449 }
450450 }
451451
452 Error createStub(StringRef StubName, TargetAddress StubAddr,
452 Error createStub(StringRef StubName, JITTargetAddress StubAddr,
453453 JITSymbolFlags StubFlags) override {
454454 if (auto Err = reserveStubs(1))
455455 return Err;
490490 return JITSymbol(getPtrAddr(Key), Flags);
491491 }
492492
493 Error updatePointer(StringRef Name, TargetAddress NewAddr) override {
493 Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override {
494494 auto I = StubIndexes.find(Name);
495495 assert(I != StubIndexes.end() && "No stub pointer for symbol");
496496 auto Key = I->second.first;
499499
500500 private:
501501 struct RemoteIndirectStubsInfo {
502 TargetAddress StubBase;
503 TargetAddress PtrBase;
502 JITTargetAddress StubBase;
503 JITTargetAddress PtrBase;
504504 unsigned NumStubs;
505505 };
506506
516516 return Error::success();
517517
518518 unsigned NewStubsRequired = NumStubs - FreeStubs.size();
519 TargetAddress StubBase;
520 TargetAddress PtrBase;
519 JITTargetAddress StubBase;
520 JITTargetAddress PtrBase;
521521 unsigned NumStubsEmitted;
522522
523523 if (auto StubInfoOrErr = Remote.emitIndirectStubs(Id, NewStubsRequired))
534534 return Error::success();
535535 }
536536
537 Error createStubInternal(StringRef StubName, TargetAddress InitAddr,
537 Error createStubInternal(StringRef StubName, JITTargetAddress InitAddr,
538538 JITSymbolFlags StubFlags) {
539539 auto Key = FreeStubs.back();
540540 FreeStubs.pop_back();
542542 return Remote.writePointer(getPtrAddr(Key), InitAddr);
543543 }
544544
545 TargetAddress getStubAddr(StubKey K) {
545 JITTargetAddress getStubAddr(StubKey K) {
546546 assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
547547 "Missing stub address");
548548 return RemoteIndirectStubsInfos[K.first].StubBase +
549549 K.second * Remote.getIndirectStubSize();
550550 }
551551
552 TargetAddress getPtrAddr(StubKey K) {
552 JITTargetAddress getPtrAddr(StubKey K) {
553553 assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
554554 "Missing pointer address");
555555 return RemoteIndirectStubsInfos[K.first].PtrBase +
560560 /// Remote compile callback manager.
561561 class RCCompileCallbackManager : public JITCompileCallbackManager {
562562 public:
563 RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,
563 RCCompileCallbackManager(JITTargetAddress ErrorHandlerAddress,
564564 OrcRemoteTargetClient &Remote)
565565 : JITCompileCallbackManager(ErrorHandlerAddress), Remote(Remote) {}
566566
567567 private:
568568 void grow() override {
569 TargetAddress BlockAddr = 0;
569 JITTargetAddress BlockAddr = 0;
570570 uint32_t NumTrampolines = 0;
571571 if (auto TrampolineInfoOrErr = Remote.emitTrampolineBlock())
572572 std::tie(BlockAddr, NumTrampolines) = *TrampolineInfoOrErr;
596596
597597 /// Call the int(void) function at the given address in the target and return
598598 /// its result.
599 Expected callIntVoid(TargetAddress Addr) {
599 Expected callIntVoid(JITTargetAddress Addr) {
600600 DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
601601
602602 auto Listen = [&](RPCChannel &C, uint32_t Id) {
607607
608608 /// Call the int(int, char*[]) function at the given address in the target and
609609 /// return its result.
610 Expected callMain(TargetAddress Addr,
610 Expected callMain(JITTargetAddress Addr,
611611 const std::vector &Args) {
612612 DEBUG(dbgs() << "Calling int(*)(int, char*[]) " << format("0x%016x", Addr)
613613 << "\n");
620620
621621 /// Call the void() function at the given address in the target and wait for
622622 /// it to finish.
623 Error callVoidVoid(TargetAddress Addr) {
623 Error callVoidVoid(JITTargetAddress Addr) {
624624 DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
625625 << "\n");
626626
654654 }
655655
656656 Expected
657 enableCompileCallbacks(TargetAddress ErrorHandlerAddress) {
657 enableCompileCallbacks(JITTargetAddress ErrorHandlerAddress) {
658658 // Check for an 'out-of-band' error, e.g. from an MM destructor.
659659 if (ExistingError)
660660 return std::move(ExistingError);
672672 /// Search for symbols in the remote process. Note: This should be used by
673673 /// symbol resolvers *after* they've searched the local symbol table in the
674674 /// JIT stack.
675 Expected<TargetAddress> getSymbolAddress(StringRef Name) {
675 Expected<JITTargetAddress> getSymbolAddress(StringRef Name) {
676676 // Check for an 'out-of-band' error, e.g. from an MM destructor.
677677 if (ExistingError)
678678 return std::move(ExistingError);
697697 }
698698 }
699699
700 Error deregisterEHFrames(TargetAddress Addr, uint32_t Size) {
700 Error deregisterEHFrames(JITTargetAddress Addr, uint32_t Size) {
701701 return callST(Channel, Addr, Size);
702702 }
703703
715715 return callST(Channel, Id);
716716 }
717717
718 ExpectedTargetAddress, TargetAddress, uint32_t>>
718 ExpectedJITTargetAddress, JITTargetAddress, uint32_t>>
719719 emitIndirectStubs(ResourceIdMgr::ResourceId Id, uint32_t NumStubsRequired) {
720720 return callST(Channel, Id, NumStubsRequired);
721721 }
722722
723 ExpectedTargetAddress, uint32_t>> emitTrampolineBlock() {
723 ExpectedJITTargetAddress, uint32_t>> emitTrampolineBlock() {
724724 // Check for an 'out-of-band' error, e.g. from an MM destructor.
725725 if (ExistingError)
726726 return std::move(ExistingError);
746746 // site below, but that triggers a GCC 4.7 ICE. When we move off
747747 // GCC 4.7, tidy this up.
748748 auto CompileCallback =
749 [this](TargetAddress Addr) -> Expected<TargetAddress> {
749 [this](JITTargetAddress Addr) -> ExpectedTargetAddress> {
750750 return this->CallbackManager->executeCompileCallback(Addr);
751751 };
752752
759759 return orcError(OrcErrorCode::UnexpectedRPCCall);
760760 }
761761
762 Expected> readMem(char *Dst, TargetAddress Src,
762 Expected> readMem(char *Dst, JITTargetAddress Src,
763763 uint64_t Size) {
764764 // Check for an 'out-of-band' error, e.g. from an MM destructor.
765765 if (ExistingError)
768768 return callST(Channel, Src, Size);
769769 }
770770
771 Error registerEHFrames(TargetAddress &RAddr, uint32_t Size) {
771 Error registerEHFrames(JITTargetAddress &RAddr, uint32_t Size) {
772772 return callST(Channel, RAddr, Size);
773773 }
774774
775 Expected reserveMem(ResourceIdMgr::ResourceId Id,
776 uint64_t Size, uint32_t Align) {
775 Expected reserveMem(ResourceIdMgr::ResourceId Id,
776 uint64_t Size, uint32_t Align) {
777777
778778 // Check for an 'out-of-band' error, e.g. from an MM destructor.
779779 if (ExistingError)
783783 }
784784
785785 Error setProtections(ResourceIdMgr::ResourceId Id,
786 TargetAddress RemoteSegAddr, unsigned ProtFlags) {
786 JITTargetAddress RemoteSegAddr, unsigned ProtFlags) {
787787 return callST(Channel, Id, RemoteSegAddr, ProtFlags);
788788 }
789789
790 Error writeMem(TargetAddress Addr, const char *Src, uint64_t Size) {
790 Error writeMem(JITTargetAddress Addr, const char *Src, uint64_t Size) {
791791 // Check for an 'out-of-band' error, e.g. from an MM destructor.
792792 if (ExistingError)
793793 return std::move(ExistingError);
795795 return callST(Channel, DirectBufferWriter(Src, Addr, Size));
796796 }
797797
798 Error writePointer(TargetAddress Addr, TargetAddress PtrVal) {
798 Error writePointer(JITTargetAddress Addr, JITTargetAddress PtrVal) {
799799 // Check for an 'out-of-band' error, e.g. from an MM destructor.
800800 if (ExistingError)
801801 return std::move(ExistingError);
1515 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
1616 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
1717
18 #include "JITSymbol.h"
1918 #include "RPCChannel.h"
2019 #include "RPCUtils.h"
20 #include "llvm/ExecutionEngine/JITSymbol.h"
2121
2222 namespace llvm {
2323 namespace orc {
2626 class DirectBufferWriter {
2727 public:
2828 DirectBufferWriter() = default;
29 DirectBufferWriter(const char *Src, TargetAddress Dst, uint64_t Size)
29 DirectBufferWriter(const char *Src, JITTargetAddress Dst, uint64_t Size)
3030 : Src(Src), Dst(Dst), Size(Size) {}
3131
3232 const char *getSrc() const { return Src; }
33 TargetAddress getDst() const { return Dst; }
33 JITTargetAddress getDst() const { return Dst; }
3434 uint64_t getSize() const { return Size; }
3535
3636 private:
3737 const char *Src;
38 TargetAddress Dst;
38 JITTargetAddress Dst;
3939 uint64_t Size;
4040 };
4141
4848 }
4949
5050 inline Error deserialize(RPCChannel &C, DirectBufferWriter &DBW) {
51 TargetAddress Dst;
51 JITTargetAddress Dst;
5252 if (auto EC = deserialize(C, Dst))
5353 return EC;
5454 uint64_t Size;
119119
120120 static const char *getJITFuncIdName(JITFuncId Id);
121121
122 typedef FunctionTargetAddress Addr)> CallIntVoid;
122 typedef FunctionJITTargetAddress Addr)> CallIntVoid;
123123
124124 typedef Function
125 int32_t(TargetAddress Addr, std::vector Args)>
125 int32_t(JITTargetAddress Addr,
126 std::vector Args)>
126127 CallMain;
127128
128 typedef FunctionTargetAddress FnAddr)> CallVoidVoid;
129 typedef FunctionJITTargetAddress FnAddr)> CallVoidVoid;
129130
130131 typedef Function
131132 void(ResourceIdMgr::ResourceId AllocatorID)>
136137 CreateIndirectStubsOwner;
137138
138139 typedef Function
139 void(TargetAddress Addr, uint32_t Size)>
140 void(JITTargetAddress Addr, uint32_t Size)>
140141 DeregisterEHFrames;
141142
142143 typedef Function
149150
150151 /// EmitIndirectStubs result is (StubsBase, PtrsBase, NumStubsEmitted).
151152 typedef Function
152 std::tuple<TargetAddress, TargetAddress, uint32_t>(
153 std::tuple<JITTargetAddress, JITTargetAddress, uint32_t>(
153154 ResourceIdMgr::ResourceId StubsOwnerID,
154155 uint32_t NumStubsRequired)>
155156 EmitIndirectStubs;
157158 typedef Function EmitResolverBlock;
158159
159160 /// EmitTrampolineBlock result is (BlockAddr, NumTrampolines).
160 typedef Function std::tuple()>
161 typedef Function
162 std::tuple()>
161163 EmitTrampolineBlock;
162164
163 typedef FunctionTargetAddress(std::string SymbolName)>
165 typedef FunctionJITTargetAddress(std::string SymbolName)>
164166 GetSymbolAddress;
165167
166168 /// GetRemoteInfo result is (Triple, PointerSize, PageSize, TrampolineSize,
170172 GetRemoteInfo;
171173
172174 typedef Function
173 std::vector(TargetAddress Src, uint64_t Size)>
175 std::vector(JITTargetAddress Src, uint64_t Size)>
174176 ReadMem;
175177
176 typedef FunctionTargetAddress Addr, uint32_t Size)>
178 typedef FunctionJITTargetAddress Addr, uint32_t Size)>
177179 RegisterEHFrames;
178180
179181 typedef Function
180 TargetAddress(ResourceIdMgr::ResourceId AllocID,
181 uint64_t Size, uint32_t Align)>
182 JITTargetAddress(ResourceIdMgr::ResourceId AllocID,
183 uint64_t Size, uint32_t Align)>
182184 ReserveMem;
183185
184186 typedef Function
185 TargetAddress(TargetAddress TrampolineAddr)>
187 JITTargetAddress(JITTargetAddress TrampolineAddr)>
186188 RequestCompile;
187189
188190 typedef Function
189 void(ResourceIdMgr::ResourceId AllocID, TargetAddress Dst,
191 void(ResourceIdMgr::ResourceId AllocID, JITTargetAddress Dst,
190192 uint32_t ProtFlags)>
191193 SetProtections;
192194
194196
195197 typedef Function WriteMem;
196198
197 typedef FunctionTargetAddress Dst, TargetAddress Val)>
199 typedef FunctionJITTargetAddress Dst, JITTargetAddress Val)>
198200 WritePtr;
199201 };
200202
3131 template
3232 class OrcRemoteTargetServer : public OrcRemoteTargetRPCAPI {
3333 public:
34 typedef std::function<TargetAddress(const std::string &Name)>
34 typedef std::function<JITTargetAddress(const std::string &Name)>
3535 SymbolLookupFtor;
3636
3737 typedef std::function
117117 llvm_unreachable("Unhandled JIT RPC procedure Id.");
118118 }
119119
120 Expected<TargetAddress> requestCompile(TargetAddress TrampolineAddr) {
120 Expected<JITTargetAddress> requestCompile(JITTargetAddress TrampolineAddr) {
121121 auto Listen = [&](RPCChannel &C, uint32_t Id) {
122122 return handleKnownFunction(static_cast(Id));
123123 };
170170
171171 static Error doNothing() { return Error::success(); }
172172
173 static TargetAddress reenter(void *JITTargetAddr, void *TrampolineAddr) {
173 static JITTargetAddress reenter(void *JITTargetAddr, void *TrampolineAddr) {
174174 auto T = static_cast(JITTargetAddr);
175 auto AddrOrErr = T->requestCompile(static_cast<TargetAddress>(
175 auto AddrOrErr = T->requestCompile(static_cast<JITTargetAddress>(
176176 reinterpret_cast(TrampolineAddr)));
177177 // FIXME: Allow customizable failure substitution functions.
178178 assert(AddrOrErr && "Compile request failed");
179179 return *AddrOrErr;
180180 }
181181
182 Expected handleCallIntVoid(TargetAddress Addr) {
182 Expected handleCallIntVoid(JITTargetAddress Addr) {
183183 typedef int (*IntVoidFnTy)();
184184 IntVoidFnTy Fn =
185185 reinterpret_cast(static_cast(Addr));
191191 return Result;
192192 }
193193
194 Expected handleCallMain(TargetAddress Addr,
194 Expected handleCallMain(JITTargetAddress Addr,
195195 std::vector Args) {
196196 typedef int (*MainFnTy)(int, const char *[]);
197197
210210 return Result;
211211 }
212212
213 Error handleCallVoidVoid(TargetAddress Addr) {
213 Error handleCallVoidVoid(JITTargetAddress Addr) {
214214 typedef void (*VoidVoidFnTy)();
215215 VoidVoidFnTy Fn =
216216 reinterpret_cast(static_cast(Addr));
240240 return Error::success();
241241 }
242242
243 Error handleDeregisterEHFrames(TargetAddress TAddr, uint32_t Size) {
243 Error handleDeregisterEHFrames(JITTargetAddress TAddr, uint32_t Size) {
244244 uint8_t *Addr = reinterpret_cast(static_cast(TAddr));
245245 DEBUG(dbgs() << " Registering EH frames at " << format("0x%016x", TAddr)
246246 << ", Size = " << Size << " bytes\n");
265265 return Error::success();
266266 }
267267
268 ExpectedTargetAddress, TargetAddress, uint32_t>>
268 ExpectedJITTargetAddress, JITTargetAddress, uint32_t>>
269269 handleEmitIndirectStubs(ResourceIdMgr::ResourceId Id,
270270 uint32_t NumStubsRequired) {
271271 DEBUG(dbgs() << " ISMgr " << Id << " request " << NumStubsRequired
280280 TargetT::emitIndirectStubsBlock(IS, NumStubsRequired, nullptr))
281281 return std::move(Err);
282282
283 TargetAddress StubsBase =
284 static_cast(reinterpret_cast(IS.getStub(0)));
285 TargetAddress PtrsBase =
286 static_cast(reinterpret_cast(IS.getPtr(0)));
283 JITTargetAddress StubsBase =
284 static_cast(
285 reinterpret_cast(IS.getStub(0)));
286 JITTargetAddress PtrsBase =
287 static_cast(
288 reinterpret_cast(IS.getPtr(0)));
287289 uint32_t NumStubsEmitted = IS.getNumStubs();
288290
289291 auto &BlockList = StubOwnerItr->second;
308310 sys::Memory::MF_READ | sys::Memory::MF_EXEC));
309311 }
310312
311 ExpectedTargetAddress, uint32_t>> handleEmitTrampolineBlock() {
313 ExpectedJITTargetAddress, uint32_t>> handleEmitTrampolineBlock() {
312314 std::error_code EC;
313315 auto TrampolineBlock =
314316 sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
332334 TrampolineBlocks.push_back(std::move(TrampolineBlock));
333335
334336 auto TrampolineBaseAddr =
335 static_cast<TargetAddress>(reinterpret_cast(TrampolineMem));
337 static_cast<JITTargetAddress>(
338 reinterpret_cast(TrampolineMem));
336339
337340 return std::make_tuple(TrampolineBaseAddr, NumTrampolines);
338341 }
339342
340 Expected handleGetSymbolAddress(const std::string &Name) {
341 TargetAddress Addr = SymbolLookup(Name);
343 Expected handleGetSymbolAddress(const std::string &Name) {
344 JITTargetAddress Addr = SymbolLookup(Name);
342345 DEBUG(dbgs() << " Symbol '" << Name << "' = " << format("0x%016x", Addr)
343346 << "\n");
344347 return Addr;
361364 IndirectStubSize);
362365 }
363366
364 Expected> handleReadMem(TargetAddress RSrc, uint64_t Size) {
367 Expected> handleReadMem(JITTargetAddress RSrc, uint64_t Size) {
365368 char *Src = reinterpret_cast(static_cast(RSrc));
366369
367370 DEBUG(dbgs() << " Reading " << Size << " bytes from "
375378 return Buffer;
376379 }
377380
378 Error handleRegisterEHFrames(TargetAddress TAddr, uint32_t Size) {
381 Error handleRegisterEHFrames(JITTargetAddress TAddr, uint32_t Size) {
379382 uint8_t *Addr = reinterpret_cast(static_cast(TAddr));
380383 DEBUG(dbgs() << " Registering EH frames at " << format("0x%016x", TAddr)
381384 << ", Size = " << Size << " bytes\n");
383386 return Error::success();
384387 }
385388
386 Expected handleReserveMem(ResourceIdMgr::ResourceId Id,
387 uint64_t Size, uint32_t Align) {
389 Expected handleReserveMem(ResourceIdMgr::ResourceId Id,
390 uint64_t Size, uint32_t Align) {
388391 auto I = Allocators.find(Id);
389392 if (I == Allocators.end())
390393 return orcError(OrcErrorCode::RemoteAllocatorDoesNotExist);
396399 DEBUG(dbgs() << " Allocator " << Id << " reserved " << LocalAllocAddr
397400 << " (" << Size << " bytes, alignment " << Align << ")\n");
398401
399 TargetAddress AllocAddr =
400 static_cast(reinterpret_cast(LocalAllocAddr));
402 JITTargetAddress AllocAddr =
403 static_cast(
404 reinterpret_cast(LocalAllocAddr));
401405
402406 return AllocAddr;
403407 }
404408
405 Error handleSetProtections(ResourceIdMgr::ResourceId Id, TargetAddress Addr,
409 Error handleSetProtections(ResourceIdMgr::ResourceId Id, JITTargetAddress Addr,
406410 uint32_t Flags) {
407411 auto I = Allocators.find(Id);
408412 if (I == Allocators.end())
422426 return Error::success();
423427 }
424428
425 Error handleWritePtr(TargetAddress Addr, TargetAddress PtrVal) {
429 Error handleWritePtr(JITTargetAddress Addr, JITTargetAddress PtrVal) {
426430 DEBUG(dbgs() << " Writing pointer *" << format("0x%016x", Addr) << " = "
427431 << format("0x%016x", PtrVal) << "\n");
428432 uintptr_t *Ptr =
5353 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
5454 // for the varying types of objects to be allocated.
5555 class RTDyldMemoryManager : public MCJITMemoryManager,
56 public RuntimeDyld::SymbolResolver {
56 public JITSymbolResolver {
5757 RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
5858 void operator=(const RTDyldMemoryManager&) = delete;
5959 public:
9797 /// Clients writing custom RTDyldMemoryManagers are encouraged to override
9898 /// this method and return a SymbolInfo with the flags set correctly. This is
9999 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
100 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
101 return RuntimeDyld::SymbolInfo(getSymbolAddress(Name),
102 JITSymbolFlags::Exported);
100 JITSymbol findSymbol(const std::string &Name) override {
101 return JITSymbol(getSymbolAddress(Name), JITSymbolFlags::Exported);
103102 }
104103
105104 /// Legacy symbol lookup -- DEPRECATED! Please override
120119 /// Clients writing custom RTDyldMemoryManagers are encouraged to override
121120 /// this method and return a SymbolInfo with the flags set correctly. This is
122121 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
123 RuntimeDyld::SymbolInfo
122 JITSymbol
124123 findSymbolInLogicalDylib(const std::string &Name) override {
125 return RuntimeDyld::SymbolInfo(getSymbolAddressInLogicalDylib(Name),
126 JITSymbolFlags::Exported);
124 return JITSymbol(getSymbolAddressInLogicalDylib(Name),
125 JITSymbolFlags::Exported);
127126 }
128127
129128 /// This method returns the address of the specified function. As such it is
1313 #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
1414 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
1515
16 #include "JITSymbolFlags.h"
16 #include "JITSymbol.h"
1717 #include "llvm/ADT/STLExtras.h"
1818 #include "llvm/DebugInfo/DIContext.h"
1919 #include "llvm/Object/ObjectFile.h"
5959 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
6060 public:
6161
62 /// \brief Information about a named symbol.
63 class SymbolInfo : public JITSymbolBase {
64 public:
65 SymbolInfo(std::nullptr_t) : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
66 SymbolInfo(uint64_t Address, JITSymbolFlags Flags)
67 : JITSymbolBase(Flags), Address(Address) {}
68 explicit operator bool() const { return Address != 0; }
69 uint64_t getAddress() const { return Address; }
70 private:
71 uint64_t Address;
72 };
73
7462 /// \brief Information about the loaded object.
7563 class LoadedObjectInfo : public llvm::LoadedObjectInfo {
7664 friend class RuntimeDyldImpl;
188176 bool FinalizationLocked;
189177 };
190178
191 /// \brief Symbol resolution.
192 class SymbolResolver {
193 public:
194 virtual ~SymbolResolver() {}
195
196 /// This method returns the address of the specified symbol if it exists
197 /// within the logical dynamic library represented by this
198 /// RTDyldMemoryManager. Unlike findSymbol, queries through this
199 /// interface should return addresses for hidden symbols.
200 ///
201 /// This is of particular importance for the Orc JIT APIs, which support lazy
202 /// compilation by breaking up modules: Each of those broken out modules
203 /// must be able to resolve hidden symbols provided by the others. Clients
204 /// writing memory managers for MCJIT can usually ignore this method.
205 ///
206 /// This method will be queried by RuntimeDyld when checking for previous
207 /// definitions of common symbols.
208 virtual SymbolInfo findSymbolInLogicalDylib(const std::string &Name) = 0;
209
210 /// This method returns the address of the specified function or variable.
211 /// It is used to resolve symbols during module linking.
212 ///
213 /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
214 /// skip all relocations for that symbol, and the client will be responsible
215 /// for handling them manually.
216 virtual SymbolInfo findSymbol(const std::string &Name) = 0;
217
218 private:
219 virtual void anchor();
220 };
221
222179 /// \brief Construct a RuntimeDyld instance.
223 RuntimeDyld(MemoryManager &MemMgr, SymbolResolver &Resolver);
180 RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver);
224181 ~RuntimeDyld();
225182
226183 /// Add the referenced object file to the list of objects to be loaded and
234191
235192 /// Get the target address and flags for the named symbol.
236193 /// This address is the one used for relocation.
237 SymbolInfo getSymbol(StringRef Name) const;
194 JITEvaluatedSymbol getSymbol(StringRef Name) const;
238195
239196 /// Resolve the relocations for all symbols we currently know about.
240197 void resolveRelocations();
294251 // interface.
295252 std::unique_ptr Dyld;
296253 MemoryManager &MemMgr;
297 SymbolResolver &Resolver;
254 JITSymbolResolver &Resolver;
298255 bool ProcessAllSections;
299256 RuntimeDyldCheckerImpl *Checker;
300257 };
4747 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
4848 std::unique_ptr M, std::string *ErrorStr,
4949 std::shared_ptr MemMgr,
50 std::shared_ptr Resolver,
50
51 std::shared_ptr Resolver,
5152 std::unique_ptr TM) = nullptr;
5253
5354 ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
5455 std::string *ErrorStr, std::shared_ptr MemMgr,
55 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
56 std::shared_ptr<JITSymbolResolver> Resolver,
5657 std::unique_ptr TM) = nullptr;
5758
5859 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr M,
498499 }
499500
500501 EngineBuilder&
501 EngineBuilder::setSymbolResolver(std::unique_ptr SR) {
502 Resolver = std::shared_ptr(std::move(SR));
502 EngineBuilder::setSymbolResolver(std::unique_ptr SR) {
503 Resolver = std::shared_ptr(std::move(SR));
503504 return *this;
504505 }
505506
4545 MCJIT::createJIT(std::unique_ptr M,
4646 std::string *ErrorStr,
4747 std::shared_ptr MemMgr,
48 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
48 std::shared_ptr<JITSymbolResolver> Resolver,
4949 std::unique_ptr TM) {
5050 // Try to register the program as a source of symbols to resolve against.
5151 //
6666
6767 MCJIT::MCJIT(std::unique_ptr M, std::unique_ptr TM,
6868 std::shared_ptr MemMgr,
69 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
69 std::shared_ptr<JITSymbolResolver> Resolver)
7070 : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
7171 Ctx(nullptr), MemMgr(std::move(MemMgr)),
7272 Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
275275 finalizeLoadedModules();
276276 }
277277
278 RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
278 JITSymbol MCJIT::findExistingSymbol(const std::string &Name) {
279279 SmallString<128> FullName;
280280 Mangler::getNameWithPrefix(FullName, Name, getDataLayout());
281281
282282 if (void *Addr = getPointerToGlobalIfAvailable(FullName))
283 return RuntimeDyld::SymbolInfo(static_cast(
284 reinterpret_cast(Addr)),
285 JITSymbolFlags::Exported);
283 return JITSymbol(static_cast(
284 reinterpret_cast(Addr)),
285 JITSymbolFlags::Exported);
286286
287287 return Dyld.getSymbol(FullName);
288288 }
315315 return findSymbol(Name, CheckFunctionsOnly).getAddress();
316316 }
317317
318 RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name,
319 bool CheckFunctionsOnly) {
318 JITSymbol MCJIT::findSymbol(const std::string &Name,
319 bool CheckFunctionsOnly) {
320320 MutexGuard locked(lock);
321321
322322 // First, check to see if we already have this symbol.
366366 if (LazyFunctionCreator) {
367367 auto Addr = static_cast(
368368 reinterpret_cast(LazyFunctionCreator(Name)));
369 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
369 return JITSymbol(Addr, JITSymbolFlags::Exported);
370370 }
371371
372372 return nullptr;
643643 L->NotifyFreeingObject(Obj);
644644 }
645645
646 RuntimeDyld::SymbolInfo
646 JITSymbol
647647 LinkingSymbolResolver::findSymbol(const std::string &Name) {
648648 auto Result = ParentEngine.findSymbol(Name, false);
649649 // If the symbols wasn't found and it begins with an underscore, try again
2525 // functions across modules that it owns. It aggregates the memory manager
2626 // that is passed in to the MCJIT constructor and defers most functionality
2727 // to that object.
28 class LinkingSymbolResolver : public RuntimeDyld::SymbolResolver {
28 class LinkingSymbolResolver : public JITSymbolResolver {
2929 public:
3030 LinkingSymbolResolver(MCJIT &Parent,
31 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
31 std::shared_ptr<JITSymbolResolver> Resolver)
3232 : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {}
3333
34 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override;
34 JITSymbol findSymbol(const std::string &Name) override;
3535
3636 // MCJIT doesn't support logical dylibs.
37 RuntimeDyld::SymbolInfo
38 findSymbolInLogicalDylib(const std::string &Name) override {
37 JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
3938 return nullptr;
4039 }
4140
4241 private:
4342 MCJIT &ParentEngine;
44 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
43 std::shared_ptr<JITSymbolResolver> ClientResolver;
4544 };
4645
4746 // About Module states: added->loaded->finalized.
6766 class MCJIT : public ExecutionEngine {
6867 MCJIT(std::unique_ptr M, std::unique_ptr tm,
6968 std::shared_ptr MemMgr,
70 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver);
69 std::shared_ptr<JITSymbolResolver> Resolver);
7170
7271 typedef llvm::SmallPtrSet ModulePtrSet;
7372
304303 createJIT(std::unique_ptr M,
305304 std::string *ErrorStr,
306305 std::shared_ptr MemMgr,
307 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
306 std::shared_ptr<JITSymbolResolver> Resolver,
308307 std::unique_ptr TM);
309308
310309 // @}
311310
312 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name,
313 bool CheckFunctionsOnly);
311 JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly);
314312 // DEPRECATED - Please use findSymbol instead.
315313 // This is not directly exposed via the ExecutionEngine API, but it is
316314 // used by the LinkingMemoryManager.
329327 const RuntimeDyld::LoadedObjectInfo &L);
330328 void NotifyFreeingObject(const object::ObjectFile& Obj);
331329
332 RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name);
333 Module *findModuleForSymbol(const std::string &Name,
334 bool CheckFunctionsOnly);
330 JITSymbol findExistingSymbol(const std::string &Name);
331 Module *findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly);
335332 };
336333
337334 } // end llvm namespace
2323
2424 std::unique_ptr
2525 createLocalCompileCallbackManager(const Triple &T,
26 TargetAddress ErrorHandlerAddress) {
26 JITTargetAddress ErrorHandlerAddress) {
2727 switch (T.getArch()) {
2828 default: return nullptr;
2929
7070 }
7171 }
7272
73 Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr) {
73 Constant* createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr) {
7474 Constant *AddrIntVal =
7575 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
7676 Constant *AddrPtrVal =
1313 namespace llvm {
1414 namespace orc {
1515
16 RuntimeDyld::SymbolInfo NullResolver::findSymbol(const std::string &Name) {
16 JITSymbol NullResolver::findSymbol(const std::string &Name) {
1717 llvm_unreachable("Unexpected cross-object symbol reference");
1818 }
1919
20 RuntimeDyld::SymbolInfo
21 NullResolver::findSymbolInLogicalDylib(const std::string &Name) {
20 JITSymbol NullResolver::findSymbolInLogicalDylib(const std::string &Name) {
2221 llvm_unreachable("Unexpected cross-object symbol reference");
2322 }
2423
4343 class GenericHandle {
4444 public:
4545 virtual ~GenericHandle() {}
46 virtual orc::JITSymbol findSymbolIn(const std::string &Name,
47 bool ExportedSymbolsOnly) = 0;
46 virtual JITSymbol findSymbolIn(const std::string &Name,
47 bool ExportedSymbolsOnly) = 0;
4848 virtual void removeModule() = 0;
4949 };
5050
5353 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle)
5454 : Layer(Layer), Handle(std::move(Handle)) {}
5555
56 orc::JITSymbol findSymbolIn(const std::string &Name,
57 bool ExportedSymbolsOnly) override {
56 JITSymbol findSymbolIn(const std::string &Name,
57 bool ExportedSymbolsOnly) override {
5858 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
5959 }
6060
108108 }
109109
110110 template
111 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
111 static PtrTy fromTargetAddress(JITTargetAddress Addr) {
112112 return reinterpret_cast(static_cast(Addr));
113113 }
114114
115 orc::TargetAddress
115 JITTargetAddress
116116 createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback,
117117 void *CallbackCtx) {
118118 auto CCInfo = CCMgr->getCompileCallback();
119 CCInfo.setCompileAction([=]() -> orc::TargetAddress {
119 CCInfo.setCompileAction([=]() -> JITTargetAddress {
120120 return Callback(wrap(this), CallbackCtx);
121121 });
122122 return CCInfo.getAddress();
123123 }
124124
125125 LLVMOrcErrorCode createIndirectStub(StringRef StubName,
126 orc::TargetAddress Addr) {
126 JITTargetAddress Addr) {
127127 return mapError(
128128 IndirectStubsMgr->createStub(StubName, Addr, JITSymbolFlags::Exported));
129129 }
130130
131131 LLVMOrcErrorCode setIndirectStubPointer(StringRef Name,
132 orc::TargetAddress Addr) {
132 JITTargetAddress Addr) {
133133 return mapError(IndirectStubsMgr->updatePointer(Name, Addr));
134134 }
135135
136 std::unique_ptr<RuntimeDyld::SymbolResolver>
136 std::unique_ptr<JITSymbolResolver>
137137 createResolver(LLVMOrcSymbolResolverFn ExternalResolver,
138138 void *ExternalResolverCtx) {
139139 return orc::createLambdaResolver(
140 [this, ExternalResolver, ExternalResolverCtx](const std::string &Name) {
140 [this, ExternalResolver, ExternalResolverCtx](const std::string &Name)
141 -> JITSymbol {
141142 // Search order:
142143 // 1. JIT'd symbols.
143144 // 2. Runtime overrides.
144145 // 3. External resolver (if present).
145146
146147 if (auto Sym = CODLayer.findSymbol(Name, true))
147 return Sym.toRuntimeDyldSymbol();
148 return Sym;
148149 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
149150 return Sym;
150151
151152 if (ExternalResolver)
152 return RuntimeDyld::SymbolInfo(
153 return JITSymbol(
153154 ExternalResolver(Name.c_str(), ExternalResolverCtx),
154155 llvm::JITSymbolFlags::Exported);
155156
156 return RuntimeDyld::SymbolInfo(nullptr);
157 return JITSymbol(nullptr);
157158 },
158159 [](const std::string &Name) {
159 return RuntimeDyld::SymbolInfo(nullptr);
160 return JITSymbol(nullptr);
160161 });
161162 }
162163
221222 FreeHandleIndexes.push_back(H);
222223 }
223224
224 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
225 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
225226 if (auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly))
226227 return Sym;
227228 return CODLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
228229 }
229230
230 orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
231 bool ExportedSymbolsOnly) {
231 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
232 bool ExportedSymbolsOnly) {
232233 return GenericHandles[H]->findSymbolIn(Name, ExportedSymbolsOnly);
233234 }
234235
110110 std::shared_ptr ClientMM;
111111 };
112112
113 class LinkingResolver : public RuntimeDyld::SymbolResolver {
113 class LinkingResolver : public JITSymbolResolver {
114114 public:
115115 LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
116116
117 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
117 JITSymbol findSymbol(const std::string &Name) override {
118118 return M.findMangledSymbol(Name);
119119 }
120120
121 RuntimeDyld::SymbolInfo
122 findSymbolInLogicalDylib(const std::string &Name) override {
121 JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
123122 return M.ClientResolver->findSymbol(Name);
124123 }
125124
132131 static ExecutionEngine *
133132 createOrcMCJITReplacement(std::string *ErrorMsg,
134133 std::shared_ptr MemMgr,
135 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
134 std::shared_ptr<JITSymbolResolver> Resolver,
136135 std::unique_ptr TM) {
137136 return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver),
138137 std::move(TM));
145144
146145 OrcMCJITReplacement(
147146 std::shared_ptr MemMgr,
148 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver,
147 std::shared_ptr<JITSymbolResolver> ClientResolver,
149148 std::unique_ptr TM)
150149 : ExecutionEngine(TM->createDataLayout()), TM(std::move(TM)),
151150 MemMgr(*this, std::move(MemMgr)), Resolver(*this),
192191 return findSymbol(Name).getAddress();
193192 }
194193
195 RuntimeDyld::SymbolInfo findSymbol(StringRef Name) {
194 JITSymbol findSymbol(StringRef Name) {
196195 return findMangledSymbol(Mangle(Name));
197196 }
198197
242241
243242 private:
244243
245 RuntimeDyld::SymbolInfo findMangledSymbol(StringRef Name) {
244 JITSymbol findMangledSymbol(StringRef Name) {
246245 if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
247 return Sym.toRuntimeDyldSymbol();
246 return Sym;
248247 if (auto Sym = ClientResolver->findSymbol(Name))
249248 return Sym;
250249 if (auto Sym = scanArchives(Name))
251 return Sym.toRuntimeDyldSymbol();
250 return Sym;
252251
253252 return nullptr;
254253 }
345344 std::unique_ptr TM;
346345 MCJITReplacementMemMgr MemMgr;
347346 LinkingResolver Resolver;
348 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
347 std::shared_ptr<JITSymbolResolver> ClientResolver;
349348 Mangler Mang;
350349
351350 NotifyObjectLoadedT NotifyObjectLoaded;
973973 }
974974
975975 void RuntimeDyld::MemoryManager::anchor() {}
976 void RuntimeDyld::SymbolResolver::anchor() {}
976 void JITSymbolResolver::anchor() {}
977977
978978 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
979 RuntimeDyld::SymbolResolver &Resolver)
979 JITSymbolResolver &Resolver)
980980 : MemMgr(MemMgr), Resolver(Resolver) {
981981 // FIXME: There's a potential issue lurking here if a single instance of
982982 // RuntimeDyld is used to load multiple objects. The current implementation
993993
994994 static std::unique_ptr
995995 createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
996 RuntimeDyld::SymbolResolver &Resolver,
997 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
996 JITSymbolResolver &Resolver, bool ProcessAllSections,
997 RuntimeDyldCheckerImpl *Checker) {
998998 std::unique_ptr Dyld =
999999 RuntimeDyldCOFF::create(Arch, MM, Resolver);
10001000 Dyld->setProcessAllSections(ProcessAllSections);
10041004
10051005 static std::unique_ptr
10061006 createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
1007 RuntimeDyld::SymbolResolver &Resolver,
1008 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
1007 JITSymbolResolver &Resolver, bool ProcessAllSections,
1008 RuntimeDyldCheckerImpl *Checker) {
10091009 std::unique_ptr Dyld(new RuntimeDyldELF(MM, Resolver));
10101010 Dyld->setProcessAllSections(ProcessAllSections);
10111011 Dyld->setRuntimeDyldChecker(Checker);
10141014
10151015 static std::unique_ptr
10161016 createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1017 RuntimeDyld::SymbolResolver &Resolver,
1017 JITSymbolResolver &Resolver,
10181018 bool ProcessAllSections,
10191019 RuntimeDyldCheckerImpl *Checker) {
10201020 std::unique_ptr Dyld =
10551055 return Dyld->getSymbolLocalAddress(Name);
10561056 }
10571057
1058 RuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const {
1058 JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const {
10591059 if (!Dyld)
10601060 return nullptr;
10611061 return Dyld->getSymbol(Name);
4343 std::unique_ptr
4444 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch,
4545 RuntimeDyld::MemoryManager &MemMgr,
46 RuntimeDyld::SymbolResolver &Resolver) {
46 JITSymbolResolver &Resolver) {
4747 switch (Arch) {
4848 default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
4949 case Triple::x86:
3232
3333 static std::unique_ptr
3434 create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
35 RuntimeDyld::SymbolResolver &Resolver);
35 JITSymbolResolver &Resolver);
3636
3737 protected:
3838 RuntimeDyldCOFF(RuntimeDyld::MemoryManager &MemMgr,
39 RuntimeDyld::SymbolResolver &Resolver)
39 JITSymbolResolver &Resolver)
4040 : RuntimeDyldImpl(MemMgr, Resolver) {}
4141 uint64_t getSymbolOffset(const SymbolRef &Sym);
4242 };
183183 namespace llvm {
184184
185185 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
186 RuntimeDyld::SymbolResolver &Resolver)
186 JITSymbolResolver &Resolver)
187187 : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
188188 RuntimeDyldELF::~RuntimeDyldELF() {}
189189
158158
159159 public:
160160 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
161 RuntimeDyld::SymbolResolver &Resolver);
161 JITSymbolResolver &Resolver);
162162 ~RuntimeDyldELF() override;
163163
164164 std::unique_ptr
226226 RuntimeDyld::MemoryManager &MemMgr;
227227
228228 // The symbol resolver to use for external symbols.
229 RuntimeDyld::SymbolResolver &Resolver;
229 JITSymbolResolver &Resolver;
230230
231231 // Attached RuntimeDyldChecker instance. Null if no instance attached.
232232 RuntimeDyldCheckerImpl *Checker;
419419
420420 public:
421421 RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
422 RuntimeDyld::SymbolResolver &Resolver)
422 JITSymbolResolver &Resolver)
423423 : MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr),
424424 ProcessAllSections(false), HasError(false) {
425425 }
450450 return getSectionAddress(SymInfo.getSectionID()) + SymInfo.getOffset();
451451 }
452452
453 RuntimeDyld::SymbolInfo getSymbol(StringRef Name) const {
453 JITEvaluatedSymbol getSymbol(StringRef Name) const {
454454 // FIXME: Just look up as a function for now. Overly simple of course.
455455 // Work in progress.
456456 RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
461461 if (SymEntry.getSectionID() != AbsoluteSymbolSection)
462462 SectionAddr = getSectionLoadAddress(SymEntry.getSectionID());
463463 uint64_t TargetAddr = SectionAddr + SymEntry.getOffset();
464 return RuntimeDyld::SymbolInfo(TargetAddr, SymEntry.getFlags());
464 return JITEvaluatedSymbol(TargetAddr, SymEntry.getFlags());
465465 }
466466
467467 void resolveRelocations();
342342 std::unique_ptr
343343 RuntimeDyldMachO::create(Triple::ArchType Arch,
344344 RuntimeDyld::MemoryManager &MemMgr,
345 RuntimeDyld::SymbolResolver &Resolver) {
345 JITSymbolResolver &Resolver) {
346346 switch (Arch) {
347347 default:
348348 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
4949 SmallVector UnregisteredEHFrameSections;
5050
5151 RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr,
52 RuntimeDyld::SymbolResolver &Resolver)
52 JITSymbolResolver &Resolver)
5353 : RuntimeDyldImpl(MemMgr, Resolver) {}
5454
5555 /// This convenience method uses memcpy to extract a contiguous addend (the
123123 static std::unique_ptr
124124 create(Triple::ArchType Arch,
125125 RuntimeDyld::MemoryManager &MemMgr,
126 RuntimeDyld::SymbolResolver &Resolver);
126 JITSymbolResolver &Resolver);
127127
128128 std::unique_ptr
129129 loadObject(const object::ObjectFile &O) override;
151151
152152 public:
153153 RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,
154 RuntimeDyld::SymbolResolver &Resolver)
154 JITSymbolResolver &Resolver)
155155 : RuntimeDyldMachO(MemMgr, Resolver) {}
156156
157157 Error finalizeLoad(const ObjectFile &Obj,
2424 class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
2525 public:
2626 RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
27 RuntimeDyld::SymbolResolver &Resolver)
27 JITSymbolResolver &Resolver)
2828 : RuntimeDyldCOFF(MM, Resolver) {}
2929
3030 unsigned getMaxStubSize() override {
2424 class RuntimeDyldCOFFThumb : public RuntimeDyldCOFF {
2525 public:
2626 RuntimeDyldCOFFThumb(RuntimeDyld::MemoryManager &MM,
27 RuntimeDyld::SymbolResolver &Resolver)
27 JITSymbolResolver &Resolver)
2828 : RuntimeDyldCOFF(MM, Resolver) {}
2929
3030 unsigned getMaxStubSize() override {
3232
3333 public:
3434 RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM,
35 RuntimeDyld::SymbolResolver &Resolver)
35 JITSymbolResolver &Resolver)
3636 : RuntimeDyldCOFF(MM, Resolver) {}
3737
3838 unsigned getMaxStubSize() override {
2323 typedef uint64_t TargetPtrT;
2424
2525 RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
26 JITSymbolResolver &Resolver)
2727 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2828
2929 unsigned getMaxStubSize() override { return 8; }
2626 typedef uint32_t TargetPtrT;
2727
2828 RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
29 RuntimeDyld::SymbolResolver &Resolver)
29 JITSymbolResolver &Resolver)
3030 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
3131
3232 unsigned getMaxStubSize() override { return 8; }
2323 typedef uint32_t TargetPtrT;
2424
2525 RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
26 JITSymbolResolver &Resolver)
2727 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2828
2929 unsigned getMaxStubSize() override { return 0; }
2323 typedef uint64_t TargetPtrT;
2424
2525 RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
26 JITSymbolResolver &Resolver)
2727 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
2828
2929 unsigned getMaxStubSize() override { return 8; }
100100
101101
102102 template
103 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
103 static PtrTy fromTargetAddress(JITTargetAddress Addr) {
104104 return reinterpret_cast(static_cast(Addr));
105105 }
106106
8080 // 3) Search the host process (LLI)'s symbol table.
8181 auto Resolver =
8282 orc::createLambdaResolver(
83 [this](const std::string &Name) {
83 [this](const std::string &Name) -> JITSymbol {
8484 if (auto Sym = CODLayer.findSymbol(Name, true))
85 return Sym.toRuntimeDyldSymbol();
85 return Sym;
8686 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
8787 return Sym;
8888
8989 if (auto Addr =
9090 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
91 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
91 return JITSymbol(Addr, JITSymbolFlags::Exported);
9292
93 return RuntimeDyld::SymbolInfo(nullptr);
93 return JITSymbol(nullptr);
9494 },
9595 [](const std::string &Name) {
96 return RuntimeDyld::SymbolInfo(nullptr);
96 return JITSymbol(nullptr);
9797 }
9898 );
9999
114114 return H;
115115 }
116116
117 orc::JITSymbol findSymbol(const std::string &Name) {
117 JITSymbol findSymbol(const std::string &Name) {
118118 return CODLayer.findSymbol(mangle(Name), true);
119119 }
120120
121 orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
121 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
122122 return CODLayer.findSymbolIn(H, mangle(Name), true);
123123 }
124124
8282 this->MemMgr = std::move(MemMgr);
8383 }
8484
85 void setResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver) {
85 void setResolver(std::unique_ptr<JITSymbolResolver> Resolver) {
8686 this->Resolver = std::move(Resolver);
8787 }
8888
133133 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
134134 using RTDyldMemoryManager::notifyObjectLoaded;
135135
136 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
136 JITSymbol findSymbol(const std::string &Name) override {
137137 return Resolver->findSymbol(Name);
138138 }
139139
140 RuntimeDyld::SymbolInfo
140 JITSymbol
141141 findSymbolInLogicalDylib(const std::string &Name) override {
142142 return Resolver->findSymbolInLogicalDylib(Name);
143143 }
144144
145145 private:
146146 std::unique_ptr MemMgr;
147 std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver;
147 std::unique_ptr<JITSymbolResolver> Resolver;
148148 };
149149 }
150150
668668 [](const std::string &Name) { return nullptr; },
669669 [&](const std::string &Name) {
670670 if (auto Addr = ExitOnErr(R.getSymbolAddress(Name)))
671 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
672 return RuntimeDyld::SymbolInfo(nullptr);
671 return JITSymbol(Addr, JITSymbolFlags::Exported);
672 return JITSymbol(nullptr);
673673 }
674674 ));
675675
676676 // Grab the target address of the JIT'd main function on the remote and call
677677 // it.
678678 // FIXME: argv and envp handling.
679 orc::TargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str());
679 JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str());
680680 EE->finalizeObject();
681681 DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
682682 << format("%llx", Entry) << "\n");
164164 DummyExterns[Name] = Addr;
165165 }
166166
167 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
167 JITSymbol findSymbol(const std::string &Name) override {
168168 auto I = DummyExterns.find(Name);
169169
170170 if (I != DummyExterns.end())
171 return RuntimeDyld::SymbolInfo(I->second, JITSymbolFlags::Exported);
171 return JITSymbol(I->second, JITSymbolFlags::Exported);
172172
173173 return RTDyldMemoryManager::findSymbol(Name);
174174 }
2525
2626 class DummyStubsManager : public orc::IndirectStubsManager {
2727 public:
28 Error createStub(StringRef StubName, TargetAddress InitAddr,
28 Error createStub(StringRef StubName, JITTargetAddress InitAddr,
2929 JITSymbolFlags Flags) override {
3030 llvm_unreachable("Not implemented");
3131 }
4242 llvm_unreachable("Not implemented");
4343 }
4444
45 Error updatePointer(StringRef Name, TargetAddress NewAddr) override {
45 Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override {
4646 llvm_unreachable("Not implemented");
4747 }
4848 };
2020
2121 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
2222 if (Name == "bar")
23 return llvm::orc::JITSymbol(0x4567, JITSymbolFlags::Exported);
23 return llvm::JITSymbol(0x4567, JITSymbolFlags::Exported);
2424 return nullptr;
2525 }
2626
3636
3737 // Test fall-through for symbol in base layer.
3838 auto BarSym = L.findSymbol("bar", true);
39 EXPECT_EQ(BarSym.getAddress(), static_cast<TargetAddress>(0x4567))
39 EXPECT_EQ(BarSym.getAddress(), static_cast<JITTargetAddress>(0x4567))
4040 << "Symbol lookup fall-through failed.";
4141
4242 // Test setup of a global mapping.
4343 L.setGlobalMapping("foo", 0x0123);
4444 auto FooSym2 = L.findSymbol("foo", true);
45 EXPECT_EQ(FooSym2.getAddress(), static_cast<TargetAddress>(0x0123))
45 EXPECT_EQ(FooSym2.getAddress(), static_cast<JITTargetAddress>(0x0123))
4646 << "Symbol mapping setup failed.";
4747
4848 // Test removal of a global mapping.
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/ExecutionEngine/RuntimeDyld.h"
910 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
1011 #include "gtest/gtest.h"
1112
1617 ModuleSetHandleT addModuleSet(
1718 std::list>,
1819 std::unique_ptr MemMgr,
19 std::unique_ptrRuntimeDyld::SymbolResolver> Resolver) {
20 std::unique_ptrJITSymbolResolver> Resolver) {
2021 EXPECT_FALSE(MemMgr);
2122 return 42;
2223 }
8989 auto Resolver =
9090 createLambdaResolver(
9191 [](const std::string &Name) {
92 return RuntimeDyld::SymbolInfo(nullptr);
92 return JITSymbol(nullptr);
9393 },
9494 [](const std::string &Name) {
95 return RuntimeDyld::SymbolInfo(nullptr);
95 return JITSymbol(nullptr);
9696 });
9797
9898 {
164164 createLambdaResolver(
165165 [&](const std::string &Name) {
166166 if (auto Sym = ObjLayer.findSymbol(Name, true))
167 return Sym.toRuntimeDyldSymbol();
168 return RuntimeDyld::SymbolInfo(nullptr);
167 return Sym;
168 return JITSymbol(nullptr);
169169 },
170170 [](const std::string &Name) {
171 return RuntimeDyld::SymbolInfo(nullptr);
171 return JITSymbol(nullptr);
172172 });
173173
174174 SectionMemoryManagerWrapper SMMW;
9494 resetExpectations();
9595 }
9696
97 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
97 llvm::JITSymbol findSymbol(const std::string &Name,
98 bool ExportedSymbolsOnly) {
9899 EXPECT_EQ(MockName, Name) << "Name should pass through";
99100 EXPECT_EQ(MockBool, ExportedSymbolsOnly) << "Flag should pass through";
100101 LastCalled = "findSymbol";
101 MockSymbol = JITSymbol(122, llvm::JITSymbolFlags::None);
102 MockSymbol = llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
102103 return MockSymbol;
103104 }
104105 void expectFindSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
105106 MockName = Name;
106107 MockBool = ExportedSymbolsOnly;
107108 }
108 void verifyFindSymbol(llvm::orc::JITSymbol Returned) {
109 void verifyFindSymbol(llvm::JITSymbol Returned) {
109110 EXPECT_EQ("findSymbol", LastCalled);
110111 EXPECT_EQ(MockSymbol.getAddress(), Returned.getAddress())
111112 << "Return should pass through";
112113 resetExpectations();
113114 }
114115
115 JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name,
116 bool ExportedSymbolsOnly) {
116 llvm::JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name,
117 bool ExportedSymbolsOnly) {
117118 EXPECT_EQ(MockObjSetHandle, H) << "Handle should pass through";
118119 EXPECT_EQ(MockName, Name) << "Name should pass through";
119120 EXPECT_EQ(MockBool, ExportedSymbolsOnly) << "Flag should pass through";
120121 LastCalled = "findSymbolIn";
121 MockSymbol = JITSymbol(122, llvm::JITSymbolFlags::None);
122 MockSymbol = llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
122123 return MockSymbol;
123124 }
124125 void expectFindSymbolIn(ObjSetHandleT H, const std::string &Name,
127128 MockName = Name;
128129 MockBool = ExportedSymbolsOnly;
129130 }
130 void verifyFindSymbolIn(llvm::orc::JITSymbol Returned) {
131 void verifyFindSymbolIn(llvm::JITSymbol Returned) {
131132 EXPECT_EQ("findSymbolIn", LastCalled);
132133 EXPECT_EQ(MockSymbol.getAddress(), Returned.getAddress())
133134 << "Return should pass through";
145146 }
146147
147148 void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
148 TargetAddress TargetAddr) {
149 llvm::JITTargetAddress TargetAddr) {
149150 EXPECT_EQ(MockObjSetHandle, H);
150151 EXPECT_EQ(MockLocalAddress, LocalAddress);
151152 EXPECT_EQ(MockTargetAddress, TargetAddr);
152153 LastCalled = "mapSectionAddress";
153154 }
154155 void expectMapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
155 TargetAddress TargetAddr) {
156 llvm::JITTargetAddress TargetAddr) {
156157 MockObjSetHandle = H;
157158 MockLocalAddress = LocalAddress;
158159 MockTargetAddress = TargetAddr;
171172 ObjSetHandleT MockObjSetHandle;
172173 std::string MockName;
173174 bool MockBool;
174 JITSymbol MockSymbol;
175 llvm::JITSymbol MockSymbol;
175176 const void *MockLocalAddress;
176 TargetAddress MockTargetAddress;
177 llvm::JITTargetAddress MockTargetAddress;
177178 MockMemoryBufferSet MockBufferSet;
178179
179180 // Clear remembered parameters between calls
184185 MockObjects.clear();
185186 MockObjSetHandle = 0;
186187 MockName = "bogus";
187 MockSymbol = JITSymbol(nullptr);
188 MockSymbol = llvm::JITSymbol(nullptr);
188189 MockLocalAddress = nullptr;
189190 MockTargetAddress = 0;
190191 MockBufferSet = 0;
244245 std::string Name = "foo";
245246 bool ExportedOnly = true;
246247 M.expectFindSymbol(Name, ExportedOnly);
247 JITSymbol Symbol = T2.findSymbol(Name, ExportedOnly);
248 llvm::JITSymbol Symbol = T2.findSymbol(Name, ExportedOnly);
248249 M.verifyFindSymbol(Symbol);
249250
250251 // Test findSymbolIn
261262
262263 // Test mapSectionAddress
263264 char Buffer[24];
264 TargetAddress MockAddress = 255;
265 llvm::JITTargetAddress MockAddress = 255;
265266 M.expectMapSectionAddress(H, Buffer, MockAddress);
266267 T1.mapSectionAddress(H, Buffer, MockAddress);
267268 M.verifyMapSectionAddress();
2121 #include "llvm/IR/TypeBuilder.h"
2222 #include "llvm/Object/ObjectFile.h"
2323 #include "llvm/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
24 #include "llvm/ExecutionEngine/JITSymbol.h"
2525 #include "llvm/Support/TargetSelect.h"
2626 #include
2727
123123 RemoveModuleSet(H);
124124 }
125125
126 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
126 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
127127 return FindSymbol(Name, ExportedSymbolsOnly);
128128 }
129129
130 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
130 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
131131 bool ExportedSymbolsOnly) {
132132 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
133133 }