1 //===- JITSymbol.h - JIT symbol abstraction ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Abstraction for target process addresses.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
14 #define LLVM_EXECUTIONENGINE_JITSYMBOL_H
25 #include "llvm/ADT/BitmaskEnum.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Support/Error.h"
37 } // end namespace object
39 /// Represents an address in the target process's address space.
40 using JITTargetAddress
= uint64_t;
42 /// Convert a JITTargetAddress to a pointer.
43 template <typename T
> T
jitTargetAddressToPointer(JITTargetAddress Addr
) {
44 static_assert(std::is_pointer
<T
>::value
, "T must be a pointer type");
45 uintptr_t IntPtr
= static_cast<uintptr_t>(Addr
);
46 assert(IntPtr
== Addr
&& "JITTargetAddress value out of range for uintptr_t");
47 return reinterpret_cast<T
>(IntPtr
);
50 template <typename T
> JITTargetAddress
pointerToJITTargetAddress(T
*Ptr
) {
51 return static_cast<JITTargetAddress
>(reinterpret_cast<uintptr_t>(Ptr
));
54 /// Flags for symbols in the JIT.
55 class JITSymbolFlags
{
57 using UnderlyingType
= uint8_t;
58 using TargetFlagsType
= uint8_t;
60 enum FlagNames
: UnderlyingType
{
68 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Callable
)
71 /// Default-construct a JITSymbolFlags instance.
72 JITSymbolFlags() = default;
74 /// Construct a JITSymbolFlags instance from the given flags.
75 JITSymbolFlags(FlagNames Flags
) : Flags(Flags
) {}
77 /// Construct a JITSymbolFlags instance from the given flags and target
79 JITSymbolFlags(FlagNames Flags
, TargetFlagsType TargetFlags
)
80 : TargetFlags(TargetFlags
), Flags(Flags
) {}
82 /// Implicitly convert to bool. Returs true if any flag is set.
83 explicit operator bool() const { return Flags
!= None
|| TargetFlags
!= 0; }
85 /// Compare for equality.
86 bool operator==(const JITSymbolFlags
&RHS
) const {
87 return Flags
== RHS
.Flags
&& TargetFlags
== RHS
.TargetFlags
;
90 /// Bitwise AND-assignment for FlagNames.
91 JITSymbolFlags
&operator&=(const FlagNames
&RHS
) {
96 /// Bitwise OR-assignment for FlagNames.
97 JITSymbolFlags
&operator|=(const FlagNames
&RHS
) {
102 /// Return true if there was an error retrieving this symbol.
103 bool hasError() const {
104 return (Flags
& HasError
) == HasError
;
107 /// Returns true if the Weak flag is set.
108 bool isWeak() const {
109 return (Flags
& Weak
) == Weak
;
112 /// Returns true if the Common flag is set.
113 bool isCommon() const {
114 return (Flags
& Common
) == Common
;
117 /// Returns true if the symbol isn't weak or common.
118 bool isStrong() const {
119 return !isWeak() && !isCommon();
122 /// Returns true if the Exported flag is set.
123 bool isExported() const {
124 return (Flags
& Exported
) == Exported
;
127 /// Returns true if the given symbol is known to be callable.
128 bool isCallable() const { return (Flags
& Callable
) == Callable
; }
130 /// Get the underlying flags value as an integer.
131 UnderlyingType
getRawFlagsValue() const {
132 return static_cast<UnderlyingType
>(Flags
);
135 /// Return a reference to the target-specific flags.
136 TargetFlagsType
& getTargetFlags() { return TargetFlags
; }
138 /// Return a reference to the target-specific flags.
139 const TargetFlagsType
& getTargetFlags() const { return TargetFlags
; }
141 /// Construct a JITSymbolFlags value based on the flags of the given global
143 static JITSymbolFlags
fromGlobalValue(const GlobalValue
&GV
);
145 /// Construct a JITSymbolFlags value based on the flags of the given libobject
147 static Expected
<JITSymbolFlags
>
148 fromObjectSymbol(const object::SymbolRef
&Symbol
);
151 TargetFlagsType TargetFlags
= 0;
152 FlagNames Flags
= None
;
155 inline JITSymbolFlags
operator&(const JITSymbolFlags
&LHS
,
156 const JITSymbolFlags::FlagNames
&RHS
) {
157 JITSymbolFlags Tmp
= LHS
;
162 inline JITSymbolFlags
operator|(const JITSymbolFlags
&LHS
,
163 const JITSymbolFlags::FlagNames
&RHS
) {
164 JITSymbolFlags Tmp
= LHS
;
169 /// ARM-specific JIT symbol flags.
170 /// FIXME: This should be moved into a target-specific header.
171 class ARMJITSymbolFlags
{
173 ARMJITSymbolFlags() = default;
180 operator JITSymbolFlags::TargetFlagsType
&() { return Flags
; }
182 static ARMJITSymbolFlags
fromObjectSymbol(const object::SymbolRef
&Symbol
);
185 JITSymbolFlags::TargetFlagsType Flags
= 0;
188 /// Represents a symbol that has been evaluated to an address already.
189 class JITEvaluatedSymbol
{
191 JITEvaluatedSymbol() = default;
193 /// Create a 'null' symbol.
194 JITEvaluatedSymbol(std::nullptr_t
) {}
196 /// Create a symbol for the given address and flags.
197 JITEvaluatedSymbol(JITTargetAddress Address
, JITSymbolFlags Flags
)
198 : Address(Address
), Flags(Flags
) {}
200 /// An evaluated symbol converts to 'true' if its address is non-zero.
201 explicit operator bool() const { return Address
!= 0; }
203 /// Return the address of this symbol.
204 JITTargetAddress
getAddress() const { return Address
; }
206 /// Return the flags for this symbol.
207 JITSymbolFlags
getFlags() const { return Flags
; }
209 /// Set the flags for this symbol.
210 void setFlags(JITSymbolFlags Flags
) { this->Flags
= std::move(Flags
); }
213 JITTargetAddress Address
= 0;
214 JITSymbolFlags Flags
;
217 /// Represents a symbol in the JIT.
220 using GetAddressFtor
= std::function
<Expected
<JITTargetAddress
>()>;
222 /// Create a 'null' symbol, used to represent a "symbol not found"
223 /// result from a successful (non-erroneous) lookup.
224 JITSymbol(std::nullptr_t
)
227 /// Create a JITSymbol representing an error in the symbol lookup
228 /// process (e.g. a network failure during a remote lookup).
230 : Err(std::move(Err
)), Flags(JITSymbolFlags::HasError
) {}
232 /// Create a symbol for a definition with a known address.
233 JITSymbol(JITTargetAddress Addr
, JITSymbolFlags Flags
)
234 : CachedAddr(Addr
), Flags(Flags
) {}
236 /// Construct a JITSymbol from a JITEvaluatedSymbol.
237 JITSymbol(JITEvaluatedSymbol Sym
)
238 : CachedAddr(Sym
.getAddress()), Flags(Sym
.getFlags()) {}
240 /// Create a symbol for a definition that doesn't have a known address
242 /// @param GetAddress A functor to materialize a definition (fixing the
243 /// address) on demand.
245 /// This constructor allows a JIT layer to provide a reference to a symbol
246 /// definition without actually materializing the definition up front. The
247 /// user can materialize the definition at any time by calling the getAddress
249 JITSymbol(GetAddressFtor GetAddress
, JITSymbolFlags Flags
)
250 : GetAddress(std::move(GetAddress
)), CachedAddr(0), Flags(Flags
) {}
252 JITSymbol(const JITSymbol
&) = delete;
253 JITSymbol
& operator=(const JITSymbol
&) = delete;
255 JITSymbol(JITSymbol
&&Other
)
256 : GetAddress(std::move(Other
.GetAddress
)), Flags(std::move(Other
.Flags
)) {
257 if (Flags
.hasError())
258 Err
= std::move(Other
.Err
);
260 CachedAddr
= std::move(Other
.CachedAddr
);
263 JITSymbol
& operator=(JITSymbol
&&Other
) {
264 GetAddress
= std::move(Other
.GetAddress
);
265 Flags
= std::move(Other
.Flags
);
266 if (Flags
.hasError())
267 Err
= std::move(Other
.Err
);
269 CachedAddr
= std::move(Other
.CachedAddr
);
274 if (Flags
.hasError())
277 CachedAddr
.~JITTargetAddress();
280 /// Returns true if the symbol exists, false otherwise.
281 explicit operator bool() const {
282 return !Flags
.hasError() && (CachedAddr
|| GetAddress
);
285 /// Move the error field value out of this JITSymbol.
287 if (Flags
.hasError())
288 return std::move(Err
);
289 return Error::success();
292 /// Get the address of the symbol in the target address space. Returns
293 /// '0' if the symbol does not exist.
294 Expected
<JITTargetAddress
> getAddress() {
295 assert(!Flags
.hasError() && "getAddress called on error value");
297 if (auto CachedAddrOrErr
= GetAddress()) {
298 GetAddress
= nullptr;
299 CachedAddr
= *CachedAddrOrErr
;
300 assert(CachedAddr
&& "Symbol could not be materialized.");
302 return CachedAddrOrErr
.takeError();
307 JITSymbolFlags
getFlags() const { return Flags
; }
310 GetAddressFtor GetAddress
;
312 JITTargetAddress CachedAddr
;
315 JITSymbolFlags Flags
;
318 /// Symbol resolution interface.
320 /// Allows symbol flags and addresses to be looked up by name.
321 /// Symbol queries are done in bulk (i.e. you request resolution of a set of
322 /// symbols, rather than a single one) to reduce IPC overhead in the case of
323 /// remote JITing, and expose opportunities for parallel compilation.
324 class JITSymbolResolver
{
326 using LookupSet
= std::set
<StringRef
>;
327 using LookupResult
= std::map
<StringRef
, JITEvaluatedSymbol
>;
328 using OnResolvedFunction
= std::function
<void(Expected
<LookupResult
>)>;
330 virtual ~JITSymbolResolver() = default;
332 /// Returns the fully resolved address and flags for each of the given
335 /// This method will return an error if any of the given symbols can not be
336 /// resolved, or if the resolution process itself triggers an error.
337 virtual void lookup(const LookupSet
&Symbols
,
338 OnResolvedFunction OnResolved
) = 0;
340 /// Returns the subset of the given symbols that should be materialized by
341 /// the caller. Only weak/common symbols should be looked up, as strong
342 /// definitions are implicitly always part of the caller's responsibility.
343 virtual Expected
<LookupSet
>
344 getResponsibilitySet(const LookupSet
&Symbols
) = 0;
347 virtual void anchor();
350 /// Legacy symbol resolution interface.
351 class LegacyJITSymbolResolver
: public JITSymbolResolver
{
353 /// Performs lookup by, for each symbol, first calling
354 /// findSymbolInLogicalDylib and if that fails calling
356 void lookup(const LookupSet
&Symbols
, OnResolvedFunction OnResolved
) final
;
358 /// Performs flags lookup by calling findSymbolInLogicalDylib and
359 /// returning the flags value for that symbol.
360 Expected
<LookupSet
> getResponsibilitySet(const LookupSet
&Symbols
) final
;
362 /// This method returns the address of the specified symbol if it exists
363 /// within the logical dynamic library represented by this JITSymbolResolver.
364 /// Unlike findSymbol, queries through this interface should return addresses
365 /// for hidden symbols.
367 /// This is of particular importance for the Orc JIT APIs, which support lazy
368 /// compilation by breaking up modules: Each of those broken out modules
369 /// must be able to resolve hidden symbols provided by the others. Clients
370 /// writing memory managers for MCJIT can usually ignore this method.
372 /// This method will be queried by RuntimeDyld when checking for previous
373 /// definitions of common symbols.
374 virtual JITSymbol
findSymbolInLogicalDylib(const std::string
&Name
) = 0;
376 /// This method returns the address of the specified function or variable.
377 /// It is used to resolve symbols during module linking.
379 /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
380 /// skip all relocations for that symbol, and the client will be responsible
381 /// for handling them manually.
382 virtual JITSymbol
findSymbol(const std::string
&Name
) = 0;
385 virtual void anchor();
388 } // end namespace llvm
390 #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H