annotate include/llvm/ExecutionEngine/JITSymbol.h @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents
children 803732b1fca8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
1 //===----------- JITSymbol.h - JIT symbol abstraction -----------*- C++ -*-===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
2 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
4 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
7 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
9 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
10 // Abstraction for target process addresses.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
11 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
13
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
14 #ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
15 #define LLVM_EXECUTIONENGINE_JITSYMBOL_H
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
16
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
17 #include <algorithm>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
18 #include <cassert>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
19 #include <cstddef>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
20 #include <cstdint>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
21 #include <functional>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
22 #include <string>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
23
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
24 namespace llvm {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
25
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
26 class GlobalValue;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
27
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
28 namespace object {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
29 class BasicSymbolRef;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
30 } // end namespace object
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
31
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
32 /// @brief Represents an address in the target process's address space.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
33 typedef uint64_t JITTargetAddress;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
34
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
35 /// @brief Flags for symbols in the JIT.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
36 class JITSymbolFlags {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
37 public:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
38 typedef uint8_t UnderlyingType;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
39
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
40 enum FlagNames : UnderlyingType {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
41 None = 0,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
42 Weak = 1U << 0,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
43 Common = 1U << 1,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
44 Absolute = 1U << 2,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
45 Exported = 1U << 3
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
46 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
47
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
48 /// @brief Default-construct a JITSymbolFlags instance.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
49 JITSymbolFlags() : Flags(None) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
50
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
51 /// @brief Construct a JITSymbolFlags instance from the given flags.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
52 JITSymbolFlags(FlagNames Flags) : Flags(Flags) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
53
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
54 /// @brief Returns true is the Weak flag is set.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
55 bool isWeak() const {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
56 return (Flags & Weak) == Weak;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
57 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
58
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
59 /// @brief Returns true is the Weak flag is set.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
60 bool isCommon() const {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
61 return (Flags & Common) == Common;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
62 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
63
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
64 bool isStrongDefinition() const {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
65 return !isWeak() && !isCommon();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
66 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
67
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
68 /// @brief Returns true is the Weak flag is set.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
69 bool isExported() const {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
70 return (Flags & Exported) == Exported;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
71 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
72
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
73 operator UnderlyingType&() { return Flags; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
74
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
75 /// Construct a JITSymbolFlags value based on the flags of the given global
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
76 /// value.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
77 static JITSymbolFlags fromGlobalValue(const GlobalValue &GV);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
78
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
79 /// Construct a JITSymbolFlags value based on the flags of the given libobject
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
80 /// symbol.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
81 static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
82
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
83 private:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
84 UnderlyingType Flags;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
85 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
86
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
87 /// @brief Represents a symbol that has been evaluated to an address already.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
88 class JITEvaluatedSymbol {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
89 public:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
90 /// @brief Create a 'null' symbol.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
91 JITEvaluatedSymbol(std::nullptr_t)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
92 : Address(0) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
93
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
94 /// @brief Create a symbol for the given address and flags.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
95 JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
96 : Address(Address), Flags(Flags) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
97
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
98 /// @brief An evaluated symbol converts to 'true' if its address is non-zero.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
99 explicit operator bool() const { return Address != 0; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
100
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
101 /// @brief Return the address of this symbol.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
102 JITTargetAddress getAddress() const { return Address; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
103
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
104 /// @brief Return the flags for this symbol.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
105 JITSymbolFlags getFlags() const { return Flags; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
106
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
107 private:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
108 JITTargetAddress Address;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
109 JITSymbolFlags Flags;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
110 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
111
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
112 /// @brief Represents a symbol in the JIT.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
113 class JITSymbol {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
114 public:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
115 typedef std::function<JITTargetAddress()> GetAddressFtor;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
116
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
117 /// @brief Create a 'null' symbol that represents failure to find a symbol
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
118 /// definition.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
119 JITSymbol(std::nullptr_t)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
120 : CachedAddr(0) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
121
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
122 /// @brief Create a symbol for a definition with a known address.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
123 JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
124 : CachedAddr(Addr), Flags(Flags) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
125
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
126 /// @brief Construct a JITSymbol from a JITEvaluatedSymbol.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
127 JITSymbol(JITEvaluatedSymbol Sym)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
128 : CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
129
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
130 /// @brief Create a symbol for a definition that doesn't have a known address
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
131 /// yet.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
132 /// @param GetAddress A functor to materialize a definition (fixing the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
133 /// address) on demand.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
134 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
135 /// This constructor allows a JIT layer to provide a reference to a symbol
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
136 /// definition without actually materializing the definition up front. The
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
137 /// user can materialize the definition at any time by calling the getAddress
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
138 /// method.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
139 JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
140 : GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {}
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
141
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
142 /// @brief Returns true if the symbol exists, false otherwise.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
143 explicit operator bool() const { return CachedAddr || GetAddress; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
144
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
145 /// @brief Get the address of the symbol in the target address space. Returns
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
146 /// '0' if the symbol does not exist.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
147 JITTargetAddress getAddress() {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
148 if (GetAddress) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
149 CachedAddr = GetAddress();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
150 assert(CachedAddr && "Symbol could not be materialized.");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
151 GetAddress = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
152 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
153 return CachedAddr;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
154 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
155
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
156 JITSymbolFlags getFlags() const { return Flags; }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
157
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
158 private:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
159 GetAddressFtor GetAddress;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
160 JITTargetAddress CachedAddr;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
161 JITSymbolFlags Flags;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
162 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
163
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
164 /// \brief Symbol resolution.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
165 class JITSymbolResolver {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
166 public:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
167 virtual ~JITSymbolResolver() = default;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
168
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
169 /// This method returns the address of the specified symbol if it exists
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
170 /// within the logical dynamic library represented by this JITSymbolResolver.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
171 /// Unlike findSymbol, queries through this interface should return addresses
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
172 /// for hidden symbols.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
173 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
174 /// This is of particular importance for the Orc JIT APIs, which support lazy
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
175 /// compilation by breaking up modules: Each of those broken out modules
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
176 /// must be able to resolve hidden symbols provided by the others. Clients
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
177 /// writing memory managers for MCJIT can usually ignore this method.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
178 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
179 /// This method will be queried by RuntimeDyld when checking for previous
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
180 /// definitions of common symbols.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
181 virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name) = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
182
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
183 /// This method returns the address of the specified function or variable.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
184 /// It is used to resolve symbols during module linking.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
185 ///
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
186 /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
187 /// skip all relocations for that symbol, and the client will be responsible
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
188 /// for handling them manually.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
189 virtual JITSymbol findSymbol(const std::string &Name) = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
190
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
191 private:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
192 virtual void anchor();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
193 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
194
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
195 } // end namespace llvm
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
196
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
197 #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H