1 //===----------- JITSymbol.h - JIT symbol abstraction -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Abstraction for target process addresses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
15 #define LLVM_EXECUTIONENGINE_JITSYMBOL_H
30 } // end namespace object
32 /// @brief Represents an address in the target process's address space.
33 typedef uint64_t JITTargetAddress;
35 /// @brief Flags for symbols in the JIT.
36 class JITSymbolFlags {
38 typedef uint8_t UnderlyingType;
40 enum FlagNames : UnderlyingType {
48 /// @brief Default-construct a JITSymbolFlags instance.
49 JITSymbolFlags() : Flags(None) {}
51 /// @brief Construct a JITSymbolFlags instance from the given flags.
52 JITSymbolFlags(FlagNames Flags) : Flags(Flags) {}
54 /// @brief Returns true is the Weak flag is set.
56 return (Flags & Weak) == Weak;
59 /// @brief Returns true is the Weak flag is set.
60 bool isCommon() const {
61 return (Flags & Common) == Common;
64 bool isStrongDefinition() const {
65 return !isWeak() && !isCommon();
68 /// @brief Returns true is the Weak flag is set.
69 bool isExported() const {
70 return (Flags & Exported) == Exported;
73 operator UnderlyingType&() { return Flags; }
75 /// Construct a JITSymbolFlags value based on the flags of the given global
77 static JITSymbolFlags fromGlobalValue(const GlobalValue &GV);
79 /// Construct a JITSymbolFlags value based on the flags of the given libobject
81 static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol);
87 /// @brief Represents a symbol that has been evaluated to an address already.
88 class JITEvaluatedSymbol {
90 /// @brief Create a 'null' symbol.
91 JITEvaluatedSymbol(std::nullptr_t)
94 /// @brief Create a symbol for the given address and flags.
95 JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags)
96 : Address(Address), Flags(Flags) {}
98 /// @brief An evaluated symbol converts to 'true' if its address is non-zero.
99 explicit operator bool() const { return Address != 0; }
101 /// @brief Return the address of this symbol.
102 JITTargetAddress getAddress() const { return Address; }
104 /// @brief Return the flags for this symbol.
105 JITSymbolFlags getFlags() const { return Flags; }
108 JITTargetAddress Address;
109 JITSymbolFlags Flags;
112 /// @brief Represents a symbol in the JIT.
115 typedef std::function<JITTargetAddress()> GetAddressFtor;
117 /// @brief Create a 'null' symbol that represents failure to find a symbol
119 JITSymbol(std::nullptr_t)
122 /// @brief Create a symbol for a definition with a known address.
123 JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
124 : CachedAddr(Addr), Flags(Flags) {}
126 /// @brief Construct a JITSymbol from a JITEvaluatedSymbol.
127 JITSymbol(JITEvaluatedSymbol Sym)
128 : CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {}
130 /// @brief Create a symbol for a definition that doesn't have a known address
132 /// @param GetAddress A functor to materialize a definition (fixing the
133 /// address) on demand.
135 /// This constructor allows a JIT layer to provide a reference to a symbol
136 /// definition without actually materializing the definition up front. The
137 /// user can materialize the definition at any time by calling the getAddress
139 JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
140 : GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {}
142 /// @brief Returns true if the symbol exists, false otherwise.
143 explicit operator bool() const { return CachedAddr || GetAddress; }
145 /// @brief Get the address of the symbol in the target address space. Returns
146 /// '0' if the symbol does not exist.
147 JITTargetAddress getAddress() {
149 CachedAddr = GetAddress();
150 assert(CachedAddr && "Symbol could not be materialized.");
151 GetAddress = nullptr;
156 JITSymbolFlags getFlags() const { return Flags; }
159 GetAddressFtor GetAddress;
160 JITTargetAddress CachedAddr;
161 JITSymbolFlags Flags;
164 /// \brief Symbol resolution.
165 class JITSymbolResolver {
167 virtual ~JITSymbolResolver() = default;
169 /// This method returns the address of the specified symbol if it exists
170 /// within the logical dynamic library represented by this JITSymbolResolver.
171 /// Unlike findSymbol, queries through this interface should return addresses
172 /// for hidden symbols.
174 /// This is of particular importance for the Orc JIT APIs, which support lazy
175 /// compilation by breaking up modules: Each of those broken out modules
176 /// must be able to resolve hidden symbols provided by the others. Clients
177 /// writing memory managers for MCJIT can usually ignore this method.
179 /// This method will be queried by RuntimeDyld when checking for previous
180 /// definitions of common symbols.
181 virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name) = 0;
183 /// This method returns the address of the specified function or variable.
184 /// It is used to resolve symbols during module linking.
186 /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
187 /// skip all relocations for that symbol, and the client will be responsible
188 /// for handling them manually.
189 virtual JITSymbol findSymbol(const std::string &Name) = 0;
192 virtual void anchor();
195 } // end namespace llvm
197 #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H