Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- JITSymbol.h - JIT symbol abstraction ---------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 <algorithm> |
| 17 | #include <cassert> |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/BitmaskEnum.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | #include "llvm/ADT/StringRef.h" |
| 27 | #include "llvm/Support/Error.h" |
| 28 | |
| 29 | namespace llvm { |
| 30 | |
| 31 | class GlobalValue; |
| 32 | |
| 33 | namespace object { |
| 34 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 35 | class SymbolRef; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
| 37 | } // end namespace object |
| 38 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 39 | /// Represents an address in the target process's address space. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 40 | using JITTargetAddress = uint64_t; |
| 41 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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); |
| 48 | } |
| 49 | |
| 50 | template <typename T> JITTargetAddress pointerToJITTargetAddress(T *Ptr) { |
| 51 | return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Ptr)); |
| 52 | } |
| 53 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 54 | /// Flags for symbols in the JIT. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | class JITSymbolFlags { |
| 56 | public: |
| 57 | using UnderlyingType = uint8_t; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 58 | using TargetFlagsType = uint8_t; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | |
| 60 | enum FlagNames : UnderlyingType { |
| 61 | None = 0, |
| 62 | HasError = 1U << 0, |
| 63 | Weak = 1U << 1, |
| 64 | Common = 1U << 2, |
| 65 | Absolute = 1U << 3, |
| 66 | Exported = 1U << 4, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 67 | Callable = 1U << 5, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 68 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Callable) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// Default-construct a JITSymbolFlags instance. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | JITSymbolFlags() = default; |
| 73 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 74 | /// Construct a JITSymbolFlags instance from the given flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 75 | JITSymbolFlags(FlagNames Flags) : Flags(Flags) {} |
| 76 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 77 | /// Construct a JITSymbolFlags instance from the given flags and target |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | /// flags. |
| 79 | JITSymbolFlags(FlagNames Flags, TargetFlagsType TargetFlags) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 80 | : TargetFlags(TargetFlags), Flags(Flags) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 82 | /// Implicitly convert to bool. Returs true if any flag is set. |
| 83 | explicit operator bool() const { return Flags != None || TargetFlags != 0; } |
| 84 | |
| 85 | /// Compare for equality. |
| 86 | bool operator==(const JITSymbolFlags &RHS) const { |
| 87 | return Flags == RHS.Flags && TargetFlags == RHS.TargetFlags; |
| 88 | } |
| 89 | |
| 90 | /// Bitwise AND-assignment for FlagNames. |
| 91 | JITSymbolFlags &operator&=(const FlagNames &RHS) { |
| 92 | Flags &= RHS; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | /// Bitwise OR-assignment for FlagNames. |
| 97 | JITSymbolFlags &operator|=(const FlagNames &RHS) { |
| 98 | Flags |= RHS; |
| 99 | return *this; |
| 100 | } |
| 101 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 102 | /// Return true if there was an error retrieving this symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 103 | bool hasError() const { |
| 104 | return (Flags & HasError) == HasError; |
| 105 | } |
| 106 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 107 | /// Returns true if the Weak flag is set. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 108 | bool isWeak() const { |
| 109 | return (Flags & Weak) == Weak; |
| 110 | } |
| 111 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 112 | /// Returns true if the Common flag is set. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 113 | bool isCommon() const { |
| 114 | return (Flags & Common) == Common; |
| 115 | } |
| 116 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 117 | /// Returns true if the symbol isn't weak or common. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | bool isStrong() const { |
| 119 | return !isWeak() && !isCommon(); |
| 120 | } |
| 121 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 122 | /// Returns true if the Exported flag is set. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 123 | bool isExported() const { |
| 124 | return (Flags & Exported) == Exported; |
| 125 | } |
| 126 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 127 | /// Returns true if the given symbol is known to be callable. |
| 128 | bool isCallable() const { return (Flags & Callable) == Callable; } |
| 129 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 130 | /// Get the underlying flags value as an integer. |
| 131 | UnderlyingType getRawFlagsValue() const { |
| 132 | return static_cast<UnderlyingType>(Flags); |
| 133 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 134 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 135 | /// Return a reference to the target-specific flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 136 | TargetFlagsType& getTargetFlags() { return TargetFlags; } |
| 137 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 138 | /// Return a reference to the target-specific flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | const TargetFlagsType& getTargetFlags() const { return TargetFlags; } |
| 140 | |
| 141 | /// Construct a JITSymbolFlags value based on the flags of the given global |
| 142 | /// value. |
| 143 | static JITSymbolFlags fromGlobalValue(const GlobalValue &GV); |
| 144 | |
| 145 | /// Construct a JITSymbolFlags value based on the flags of the given libobject |
| 146 | /// symbol. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 147 | static Expected<JITSymbolFlags> |
| 148 | fromObjectSymbol(const object::SymbolRef &Symbol); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 149 | |
| 150 | private: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | TargetFlagsType TargetFlags = 0; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 152 | FlagNames Flags = None; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 153 | }; |
| 154 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 155 | inline JITSymbolFlags operator&(const JITSymbolFlags &LHS, |
| 156 | const JITSymbolFlags::FlagNames &RHS) { |
| 157 | JITSymbolFlags Tmp = LHS; |
| 158 | Tmp &= RHS; |
| 159 | return Tmp; |
| 160 | } |
| 161 | |
| 162 | inline JITSymbolFlags operator|(const JITSymbolFlags &LHS, |
| 163 | const JITSymbolFlags::FlagNames &RHS) { |
| 164 | JITSymbolFlags Tmp = LHS; |
| 165 | Tmp |= RHS; |
| 166 | return Tmp; |
| 167 | } |
| 168 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 169 | /// ARM-specific JIT symbol flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 170 | /// FIXME: This should be moved into a target-specific header. |
| 171 | class ARMJITSymbolFlags { |
| 172 | public: |
| 173 | ARMJITSymbolFlags() = default; |
| 174 | |
| 175 | enum FlagNames { |
| 176 | None = 0, |
| 177 | Thumb = 1 << 0 |
| 178 | }; |
| 179 | |
| 180 | operator JITSymbolFlags::TargetFlagsType&() { return Flags; } |
| 181 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 182 | static ARMJITSymbolFlags fromObjectSymbol(const object::SymbolRef &Symbol); |
| 183 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 184 | private: |
| 185 | JITSymbolFlags::TargetFlagsType Flags = 0; |
| 186 | }; |
| 187 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 188 | /// Represents a symbol that has been evaluated to an address already. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 189 | class JITEvaluatedSymbol { |
| 190 | public: |
| 191 | JITEvaluatedSymbol() = default; |
| 192 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 193 | /// Create a 'null' symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 194 | JITEvaluatedSymbol(std::nullptr_t) {} |
| 195 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 196 | /// Create a symbol for the given address and flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 197 | JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags) |
| 198 | : Address(Address), Flags(Flags) {} |
| 199 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 200 | /// An evaluated symbol converts to 'true' if its address is non-zero. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 201 | explicit operator bool() const { return Address != 0; } |
| 202 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 203 | /// Return the address of this symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 204 | JITTargetAddress getAddress() const { return Address; } |
| 205 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 206 | /// Return the flags for this symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 207 | JITSymbolFlags getFlags() const { return Flags; } |
| 208 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 209 | /// Set the flags for this symbol. |
| 210 | void setFlags(JITSymbolFlags Flags) { this->Flags = std::move(Flags); } |
| 211 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 212 | private: |
| 213 | JITTargetAddress Address = 0; |
| 214 | JITSymbolFlags Flags; |
| 215 | }; |
| 216 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 217 | /// Represents a symbol in the JIT. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 218 | class JITSymbol { |
| 219 | public: |
| 220 | using GetAddressFtor = std::function<Expected<JITTargetAddress>()>; |
| 221 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 222 | /// Create a 'null' symbol, used to represent a "symbol not found" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 223 | /// result from a successful (non-erroneous) lookup. |
| 224 | JITSymbol(std::nullptr_t) |
| 225 | : CachedAddr(0) {} |
| 226 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 227 | /// Create a JITSymbol representing an error in the symbol lookup |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 228 | /// process (e.g. a network failure during a remote lookup). |
| 229 | JITSymbol(Error Err) |
| 230 | : Err(std::move(Err)), Flags(JITSymbolFlags::HasError) {} |
| 231 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 232 | /// Create a symbol for a definition with a known address. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 233 | JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags) |
| 234 | : CachedAddr(Addr), Flags(Flags) {} |
| 235 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 236 | /// Construct a JITSymbol from a JITEvaluatedSymbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 237 | JITSymbol(JITEvaluatedSymbol Sym) |
| 238 | : CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {} |
| 239 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 240 | /// Create a symbol for a definition that doesn't have a known address |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 241 | /// yet. |
| 242 | /// @param GetAddress A functor to materialize a definition (fixing the |
| 243 | /// address) on demand. |
| 244 | /// |
| 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 |
| 248 | /// method. |
| 249 | JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags) |
| 250 | : GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {} |
| 251 | |
| 252 | JITSymbol(const JITSymbol&) = delete; |
| 253 | JITSymbol& operator=(const JITSymbol&) = delete; |
| 254 | |
| 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); |
| 259 | else |
| 260 | CachedAddr = std::move(Other.CachedAddr); |
| 261 | } |
| 262 | |
| 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); |
| 268 | else |
| 269 | CachedAddr = std::move(Other.CachedAddr); |
| 270 | return *this; |
| 271 | } |
| 272 | |
| 273 | ~JITSymbol() { |
| 274 | if (Flags.hasError()) |
| 275 | Err.~Error(); |
| 276 | else |
| 277 | CachedAddr.~JITTargetAddress(); |
| 278 | } |
| 279 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 280 | /// Returns true if the symbol exists, false otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 281 | explicit operator bool() const { |
| 282 | return !Flags.hasError() && (CachedAddr || GetAddress); |
| 283 | } |
| 284 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 285 | /// Move the error field value out of this JITSymbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 286 | Error takeError() { |
| 287 | if (Flags.hasError()) |
| 288 | return std::move(Err); |
| 289 | return Error::success(); |
| 290 | } |
| 291 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 292 | /// Get the address of the symbol in the target address space. Returns |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 293 | /// '0' if the symbol does not exist. |
| 294 | Expected<JITTargetAddress> getAddress() { |
| 295 | assert(!Flags.hasError() && "getAddress called on error value"); |
| 296 | if (GetAddress) { |
| 297 | if (auto CachedAddrOrErr = GetAddress()) { |
| 298 | GetAddress = nullptr; |
| 299 | CachedAddr = *CachedAddrOrErr; |
| 300 | assert(CachedAddr && "Symbol could not be materialized."); |
| 301 | } else |
| 302 | return CachedAddrOrErr.takeError(); |
| 303 | } |
| 304 | return CachedAddr; |
| 305 | } |
| 306 | |
| 307 | JITSymbolFlags getFlags() const { return Flags; } |
| 308 | |
| 309 | private: |
| 310 | GetAddressFtor GetAddress; |
| 311 | union { |
| 312 | JITTargetAddress CachedAddr; |
| 313 | Error Err; |
| 314 | }; |
| 315 | JITSymbolFlags Flags; |
| 316 | }; |
| 317 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 318 | /// Symbol resolution interface. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 319 | /// |
| 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 { |
| 325 | public: |
| 326 | using LookupSet = std::set<StringRef>; |
| 327 | using LookupResult = std::map<StringRef, JITEvaluatedSymbol>; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 328 | using OnResolvedFunction = std::function<void(Expected<LookupResult>)>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 329 | |
| 330 | virtual ~JITSymbolResolver() = default; |
| 331 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 332 | /// Returns the fully resolved address and flags for each of the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 333 | /// symbols. |
| 334 | /// |
| 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. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 337 | virtual void lookup(const LookupSet &Symbols, |
| 338 | OnResolvedFunction OnResolved) = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 339 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 345 | |
| 346 | private: |
| 347 | virtual void anchor(); |
| 348 | }; |
| 349 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 350 | /// Legacy symbol resolution interface. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 351 | class LegacyJITSymbolResolver : public JITSymbolResolver { |
| 352 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 353 | /// Performs lookup by, for each symbol, first calling |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 354 | /// findSymbolInLogicalDylib and if that fails calling |
| 355 | /// findSymbol. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 356 | void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) final; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 357 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 358 | /// Performs flags lookup by calling findSymbolInLogicalDylib and |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 359 | /// returning the flags value for that symbol. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 360 | Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) final; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 361 | |
| 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. |
| 366 | /// |
| 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. |
| 371 | /// |
| 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; |
| 375 | |
| 376 | /// This method returns the address of the specified function or variable. |
| 377 | /// It is used to resolve symbols during module linking. |
| 378 | /// |
| 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; |
| 383 | |
| 384 | private: |
| 385 | virtual void anchor(); |
| 386 | }; |
| 387 | |
| 388 | } // end namespace llvm |
| 389 | |
| 390 | #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H |