2017-06-20 01:37:52 +02:00
|
|
|
//===- JITSymbol.h - JIT symbol abstraction ---------------------*- C++ -*-===//
|
2016-08-01 22:49:11 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-01 22:49:11 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Abstraction for target process addresses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
|
|
|
|
#define LLVM_EXECUTIONENGINE_JITSYMBOL_H
|
|
|
|
|
2016-11-16 19:07:33 +01:00
|
|
|
#include <algorithm>
|
2016-08-01 22:49:11 +02:00
|
|
|
#include <cassert>
|
2016-11-16 19:07:33 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2016-08-01 22:49:11 +02:00
|
|
|
#include <functional>
|
2018-01-19 23:24:13 +01:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2016-11-16 19:07:33 +01:00
|
|
|
#include <string>
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-09-02 03:28:26 +02:00
|
|
|
#include "llvm/ADT/BitmaskEnum.h"
|
2019-09-13 13:35:33 +02:00
|
|
|
#include "llvm/ADT/FunctionExtras.h"
|
2018-01-19 23:24:13 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-07-07 04:59:13 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-08-04 22:32:37 +02:00
|
|
|
class GlobalValue;
|
|
|
|
|
|
|
|
namespace object {
|
2017-06-20 01:37:52 +02:00
|
|
|
|
2018-08-02 00:42:23 +02:00
|
|
|
class SymbolRef;
|
2017-06-20 01:37:52 +02:00
|
|
|
|
2016-11-16 19:07:33 +01:00
|
|
|
} // end namespace object
|
2016-08-04 22:32:37 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Represents an address in the target process's address space.
|
2017-06-20 01:37:52 +02:00
|
|
|
using JITTargetAddress = uint64_t;
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-10-24 01:01:39 +02:00
|
|
|
/// Convert a JITTargetAddress to a pointer.
|
|
|
|
template <typename T> T jitTargetAddressToPointer(JITTargetAddress Addr) {
|
|
|
|
static_assert(std::is_pointer<T>::value, "T must be a pointer type");
|
|
|
|
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
|
|
|
|
assert(IntPtr == Addr && "JITTargetAddress value out of range for uintptr_t");
|
|
|
|
return reinterpret_cast<T>(IntPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> JITTargetAddress pointerToJITTargetAddress(T *Ptr) {
|
|
|
|
return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Ptr));
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Flags for symbols in the JIT.
|
2016-08-04 22:32:37 +02:00
|
|
|
class JITSymbolFlags {
|
|
|
|
public:
|
2017-06-20 01:37:52 +02:00
|
|
|
using UnderlyingType = uint8_t;
|
Initial implementation of JITLink - A replacement for RuntimeDyld.
Summary:
JITLink is a jit-linker that performs the same high-level task as RuntimeDyld:
it parses relocatable object files and makes their contents runnable in a target
process.
JITLink aims to improve on RuntimeDyld in several ways:
(1) A clear design intended to maximize code-sharing while minimizing coupling.
RuntimeDyld has been developed in an ad-hoc fashion for a number of years and
this had led to intermingling of code for multiple architectures (e.g. in
RuntimeDyldELF::processRelocationRef) in a way that makes the code more
difficult to read, reason about, extend. JITLink is designed to isolate
format and architecture specific code, while still sharing generic code.
(2) Support for native code models.
RuntimeDyld required the use of large code models (where calls to external
functions are made indirectly via registers) for many of platforms due to its
restrictive model for stub generation (one "stub" per symbol). JITLink allows
arbitrary mutation of the atom graph, allowing both GOT and PLT atoms to be
added naturally.
(3) Native support for asynchronous linking.
JITLink uses asynchronous calls for symbol resolution and finalization: these
callbacks are passed a continuation function that they must call to complete the
linker's work. This allows for cleaner interoperation with the new concurrent
ORC JIT APIs, while still being easily implementable in synchronous style if
asynchrony is not needed.
To maximise sharing, the design has a hierarchy of common code:
(1) Generic atom-graph data structure and algorithms (e.g. dead stripping and
| memory allocation) that are intended to be shared by all architectures.
|
+ -- (2) Shared per-format code that utilizes (1), e.g. Generic MachO to
| atom-graph parsing.
|
+ -- (3) Architecture specific code that uses (1) and (2). E.g.
JITLinkerMachO_x86_64, which adds x86-64 specific relocation
support to (2) to build and patch up the atom graph.
To support asynchronous symbol resolution and finalization, the callbacks for
these operations take continuations as arguments:
using JITLinkAsyncLookupContinuation =
std::function<void(Expected<AsyncLookupResult> LR)>;
using JITLinkAsyncLookupFunction =
std::function<void(const DenseSet<StringRef> &Symbols,
JITLinkAsyncLookupContinuation LookupContinuation)>;
using FinalizeContinuation = std::function<void(Error)>;
virtual void finalizeAsync(FinalizeContinuation OnFinalize);
In addition to its headline features, JITLink also makes other improvements:
- Dead stripping support: symbols that are not used (e.g. redundant ODR
definitions) are discarded, and take up no memory in the target process
(In contrast, RuntimeDyld supported pointer equality for weak definitions,
but the redundant definitions stayed resident in memory).
- Improved exception handling support. JITLink provides a much more extensive
eh-frame parser than RuntimeDyld, and is able to correctly fix up many
eh-frame sections that RuntimeDyld currently (silently) fails on.
- More extensive validation and error handling throughout.
This initial patch supports linking MachO/x86-64 only. Work on support for
other architectures and formats will happen in-tree.
Differential Revision: https://reviews.llvm.org/D58704
llvm-svn: 358818
2019-04-20 19:10:34 +02:00
|
|
|
using TargetFlagsType = uint8_t;
|
2016-08-04 22:32:37 +02:00
|
|
|
|
|
|
|
enum FlagNames : UnderlyingType {
|
|
|
|
None = 0,
|
2017-07-07 04:59:13 +02:00
|
|
|
HasError = 1U << 0,
|
|
|
|
Weak = 1U << 1,
|
|
|
|
Common = 1U << 2,
|
|
|
|
Absolute = 1U << 3,
|
2018-01-05 23:50:43 +01:00
|
|
|
Exported = 1U << 4,
|
2018-08-02 00:42:23 +02:00
|
|
|
Callable = 1U << 5,
|
2019-05-29 01:35:44 +02:00
|
|
|
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Callable)
|
2016-08-04 22:32:37 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Default-construct a JITSymbolFlags instance.
|
2017-06-20 01:37:52 +02:00
|
|
|
JITSymbolFlags() = default;
|
2016-08-04 22:32:37 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Construct a JITSymbolFlags instance from the given flags.
|
2016-08-04 22:32:37 +02:00
|
|
|
JITSymbolFlags(FlagNames Flags) : Flags(Flags) {}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Construct a JITSymbolFlags instance from the given flags and target
|
2017-08-09 22:19:27 +02:00
|
|
|
/// flags.
|
|
|
|
JITSymbolFlags(FlagNames Flags, TargetFlagsType TargetFlags)
|
Initial implementation of JITLink - A replacement for RuntimeDyld.
Summary:
JITLink is a jit-linker that performs the same high-level task as RuntimeDyld:
it parses relocatable object files and makes their contents runnable in a target
process.
JITLink aims to improve on RuntimeDyld in several ways:
(1) A clear design intended to maximize code-sharing while minimizing coupling.
RuntimeDyld has been developed in an ad-hoc fashion for a number of years and
this had led to intermingling of code for multiple architectures (e.g. in
RuntimeDyldELF::processRelocationRef) in a way that makes the code more
difficult to read, reason about, extend. JITLink is designed to isolate
format and architecture specific code, while still sharing generic code.
(2) Support for native code models.
RuntimeDyld required the use of large code models (where calls to external
functions are made indirectly via registers) for many of platforms due to its
restrictive model for stub generation (one "stub" per symbol). JITLink allows
arbitrary mutation of the atom graph, allowing both GOT and PLT atoms to be
added naturally.
(3) Native support for asynchronous linking.
JITLink uses asynchronous calls for symbol resolution and finalization: these
callbacks are passed a continuation function that they must call to complete the
linker's work. This allows for cleaner interoperation with the new concurrent
ORC JIT APIs, while still being easily implementable in synchronous style if
asynchrony is not needed.
To maximise sharing, the design has a hierarchy of common code:
(1) Generic atom-graph data structure and algorithms (e.g. dead stripping and
| memory allocation) that are intended to be shared by all architectures.
|
+ -- (2) Shared per-format code that utilizes (1), e.g. Generic MachO to
| atom-graph parsing.
|
+ -- (3) Architecture specific code that uses (1) and (2). E.g.
JITLinkerMachO_x86_64, which adds x86-64 specific relocation
support to (2) to build and patch up the atom graph.
To support asynchronous symbol resolution and finalization, the callbacks for
these operations take continuations as arguments:
using JITLinkAsyncLookupContinuation =
std::function<void(Expected<AsyncLookupResult> LR)>;
using JITLinkAsyncLookupFunction =
std::function<void(const DenseSet<StringRef> &Symbols,
JITLinkAsyncLookupContinuation LookupContinuation)>;
using FinalizeContinuation = std::function<void(Error)>;
virtual void finalizeAsync(FinalizeContinuation OnFinalize);
In addition to its headline features, JITLink also makes other improvements:
- Dead stripping support: symbols that are not used (e.g. redundant ODR
definitions) are discarded, and take up no memory in the target process
(In contrast, RuntimeDyld supported pointer equality for weak definitions,
but the redundant definitions stayed resident in memory).
- Improved exception handling support. JITLink provides a much more extensive
eh-frame parser than RuntimeDyld, and is able to correctly fix up many
eh-frame sections that RuntimeDyld currently (silently) fails on.
- More extensive validation and error handling throughout.
This initial patch supports linking MachO/x86-64 only. Work on support for
other architectures and formats will happen in-tree.
Differential Revision: https://reviews.llvm.org/D58704
llvm-svn: 358818
2019-04-20 19:10:34 +02:00
|
|
|
: TargetFlags(TargetFlags), Flags(Flags) {}
|
2017-08-09 22:19:27 +02:00
|
|
|
|
2018-09-02 03:28:26 +02:00
|
|
|
/// Implicitly convert to bool. Returs true if any flag is set.
|
|
|
|
explicit operator bool() const { return Flags != None || TargetFlags != 0; }
|
|
|
|
|
|
|
|
/// Compare for equality.
|
|
|
|
bool operator==(const JITSymbolFlags &RHS) const {
|
|
|
|
return Flags == RHS.Flags && TargetFlags == RHS.TargetFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bitwise AND-assignment for FlagNames.
|
2018-09-02 03:29:29 +02:00
|
|
|
JITSymbolFlags &operator&=(const FlagNames &RHS) {
|
2018-09-02 03:28:26 +02:00
|
|
|
Flags &= RHS;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bitwise OR-assignment for FlagNames.
|
2018-09-02 03:29:29 +02:00
|
|
|
JITSymbolFlags &operator|=(const FlagNames &RHS) {
|
2018-09-02 03:28:26 +02:00
|
|
|
Flags |= RHS;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Return true if there was an error retrieving this symbol.
|
2017-07-07 04:59:13 +02:00
|
|
|
bool hasError() const {
|
|
|
|
return (Flags & HasError) == HasError;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns true if the Weak flag is set.
|
2016-08-04 22:32:37 +02:00
|
|
|
bool isWeak() const {
|
|
|
|
return (Flags & Weak) == Weak;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns true if the Common flag is set.
|
2016-08-04 22:32:37 +02:00
|
|
|
bool isCommon() const {
|
|
|
|
return (Flags & Common) == Common;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns true if the symbol isn't weak or common.
|
2018-01-16 21:39:51 +01:00
|
|
|
bool isStrong() const {
|
2016-08-07 00:36:26 +02:00
|
|
|
return !isWeak() && !isCommon();
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns true if the Exported flag is set.
|
2016-08-04 22:32:37 +02:00
|
|
|
bool isExported() const {
|
|
|
|
return (Flags & Exported) == Exported;
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:42:23 +02:00
|
|
|
/// Returns true if the given symbol is known to be callable.
|
|
|
|
bool isCallable() const { return (Flags & Callable) == Callable; }
|
|
|
|
|
2018-09-02 03:28:26 +02:00
|
|
|
/// Get the underlying flags value as an integer.
|
2018-09-02 03:29:29 +02:00
|
|
|
UnderlyingType getRawFlagsValue() const {
|
|
|
|
return static_cast<UnderlyingType>(Flags);
|
|
|
|
}
|
2017-09-05 05:34:09 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Return a reference to the target-specific flags.
|
2017-08-09 22:19:27 +02:00
|
|
|
TargetFlagsType& getTargetFlags() { return TargetFlags; }
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Return a reference to the target-specific flags.
|
2017-09-05 05:34:09 +02:00
|
|
|
const TargetFlagsType& getTargetFlags() const { return TargetFlags; }
|
|
|
|
|
2016-08-04 22:32:37 +02:00
|
|
|
/// Construct a JITSymbolFlags value based on the flags of the given global
|
|
|
|
/// value.
|
|
|
|
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV);
|
|
|
|
|
|
|
|
/// Construct a JITSymbolFlags value based on the flags of the given libobject
|
|
|
|
/// symbol.
|
2018-08-02 00:42:23 +02:00
|
|
|
static Expected<JITSymbolFlags>
|
|
|
|
fromObjectSymbol(const object::SymbolRef &Symbol);
|
2016-08-04 22:32:37 +02:00
|
|
|
|
|
|
|
private:
|
2017-08-09 22:19:27 +02:00
|
|
|
TargetFlagsType TargetFlags = 0;
|
Initial implementation of JITLink - A replacement for RuntimeDyld.
Summary:
JITLink is a jit-linker that performs the same high-level task as RuntimeDyld:
it parses relocatable object files and makes their contents runnable in a target
process.
JITLink aims to improve on RuntimeDyld in several ways:
(1) A clear design intended to maximize code-sharing while minimizing coupling.
RuntimeDyld has been developed in an ad-hoc fashion for a number of years and
this had led to intermingling of code for multiple architectures (e.g. in
RuntimeDyldELF::processRelocationRef) in a way that makes the code more
difficult to read, reason about, extend. JITLink is designed to isolate
format and architecture specific code, while still sharing generic code.
(2) Support for native code models.
RuntimeDyld required the use of large code models (where calls to external
functions are made indirectly via registers) for many of platforms due to its
restrictive model for stub generation (one "stub" per symbol). JITLink allows
arbitrary mutation of the atom graph, allowing both GOT and PLT atoms to be
added naturally.
(3) Native support for asynchronous linking.
JITLink uses asynchronous calls for symbol resolution and finalization: these
callbacks are passed a continuation function that they must call to complete the
linker's work. This allows for cleaner interoperation with the new concurrent
ORC JIT APIs, while still being easily implementable in synchronous style if
asynchrony is not needed.
To maximise sharing, the design has a hierarchy of common code:
(1) Generic atom-graph data structure and algorithms (e.g. dead stripping and
| memory allocation) that are intended to be shared by all architectures.
|
+ -- (2) Shared per-format code that utilizes (1), e.g. Generic MachO to
| atom-graph parsing.
|
+ -- (3) Architecture specific code that uses (1) and (2). E.g.
JITLinkerMachO_x86_64, which adds x86-64 specific relocation
support to (2) to build and patch up the atom graph.
To support asynchronous symbol resolution and finalization, the callbacks for
these operations take continuations as arguments:
using JITLinkAsyncLookupContinuation =
std::function<void(Expected<AsyncLookupResult> LR)>;
using JITLinkAsyncLookupFunction =
std::function<void(const DenseSet<StringRef> &Symbols,
JITLinkAsyncLookupContinuation LookupContinuation)>;
using FinalizeContinuation = std::function<void(Error)>;
virtual void finalizeAsync(FinalizeContinuation OnFinalize);
In addition to its headline features, JITLink also makes other improvements:
- Dead stripping support: symbols that are not used (e.g. redundant ODR
definitions) are discarded, and take up no memory in the target process
(In contrast, RuntimeDyld supported pointer equality for weak definitions,
but the redundant definitions stayed resident in memory).
- Improved exception handling support. JITLink provides a much more extensive
eh-frame parser than RuntimeDyld, and is able to correctly fix up many
eh-frame sections that RuntimeDyld currently (silently) fails on.
- More extensive validation and error handling throughout.
This initial patch supports linking MachO/x86-64 only. Work on support for
other architectures and formats will happen in-tree.
Differential Revision: https://reviews.llvm.org/D58704
llvm-svn: 358818
2019-04-20 19:10:34 +02:00
|
|
|
FlagNames Flags = None;
|
2017-08-09 22:19:27 +02:00
|
|
|
};
|
|
|
|
|
2018-09-02 03:28:26 +02:00
|
|
|
inline JITSymbolFlags operator&(const JITSymbolFlags &LHS,
|
|
|
|
const JITSymbolFlags::FlagNames &RHS) {
|
|
|
|
JITSymbolFlags Tmp = LHS;
|
|
|
|
Tmp &= RHS;
|
|
|
|
return Tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JITSymbolFlags operator|(const JITSymbolFlags &LHS,
|
|
|
|
const JITSymbolFlags::FlagNames &RHS) {
|
|
|
|
JITSymbolFlags Tmp = LHS;
|
|
|
|
Tmp |= RHS;
|
|
|
|
return Tmp;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// ARM-specific JIT symbol flags.
|
2017-08-09 22:19:27 +02:00
|
|
|
/// FIXME: This should be moved into a target-specific header.
|
|
|
|
class ARMJITSymbolFlags {
|
|
|
|
public:
|
|
|
|
ARMJITSymbolFlags() = default;
|
|
|
|
|
|
|
|
enum FlagNames {
|
|
|
|
None = 0,
|
|
|
|
Thumb = 1 << 0
|
|
|
|
};
|
|
|
|
|
|
|
|
operator JITSymbolFlags::TargetFlagsType&() { return Flags; }
|
|
|
|
|
2018-08-02 00:42:23 +02:00
|
|
|
static ARMJITSymbolFlags fromObjectSymbol(const object::SymbolRef &Symbol);
|
|
|
|
|
2017-08-09 22:19:27 +02:00
|
|
|
private:
|
|
|
|
JITSymbolFlags::TargetFlagsType Flags = 0;
|
2016-08-04 22:32:37 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Represents a symbol that has been evaluated to an address already.
|
2016-08-04 22:32:37 +02:00
|
|
|
class JITEvaluatedSymbol {
|
2016-08-01 22:49:11 +02:00
|
|
|
public:
|
2018-01-10 01:09:38 +01:00
|
|
|
JITEvaluatedSymbol() = default;
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a 'null' symbol.
|
2017-06-20 01:37:52 +02:00
|
|
|
JITEvaluatedSymbol(std::nullptr_t) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a symbol for the given address and flags.
|
2016-08-01 22:49:11 +02:00
|
|
|
JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags)
|
2016-08-04 22:32:37 +02:00
|
|
|
: Address(Address), Flags(Flags) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// An evaluated symbol converts to 'true' if its address is non-zero.
|
2016-08-01 22:49:11 +02:00
|
|
|
explicit operator bool() const { return Address != 0; }
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Return the address of this symbol.
|
2016-08-01 22:49:11 +02:00
|
|
|
JITTargetAddress getAddress() const { return Address; }
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Return the flags for this symbol.
|
2016-08-04 22:32:37 +02:00
|
|
|
JITSymbolFlags getFlags() const { return Flags; }
|
|
|
|
|
2018-05-17 00:24:30 +02:00
|
|
|
/// Set the flags for this symbol.
|
|
|
|
void setFlags(JITSymbolFlags Flags) { this->Flags = std::move(Flags); }
|
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
private:
|
2017-06-20 01:37:52 +02:00
|
|
|
JITTargetAddress Address = 0;
|
2016-08-04 22:32:37 +02:00
|
|
|
JITSymbolFlags Flags;
|
2016-08-01 22:49:11 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Represents a symbol in the JIT.
|
2016-08-04 22:32:37 +02:00
|
|
|
class JITSymbol {
|
2016-08-01 22:49:11 +02:00
|
|
|
public:
|
2019-09-13 13:35:33 +02:00
|
|
|
using GetAddressFtor = unique_function<Expected<JITTargetAddress>()>;
|
2017-07-07 04:59:13 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a 'null' symbol, used to represent a "symbol not found"
|
2017-07-07 04:59:13 +02:00
|
|
|
/// result from a successful (non-erroneous) lookup.
|
|
|
|
JITSymbol(std::nullptr_t)
|
|
|
|
: CachedAddr(0) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a JITSymbol representing an error in the symbol lookup
|
2017-07-07 04:59:13 +02:00
|
|
|
/// process (e.g. a network failure during a remote lookup).
|
|
|
|
JITSymbol(Error Err)
|
|
|
|
: Err(std::move(Err)), Flags(JITSymbolFlags::HasError) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a symbol for a definition with a known address.
|
2016-08-01 22:49:11 +02:00
|
|
|
JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
|
2016-08-04 22:32:37 +02:00
|
|
|
: CachedAddr(Addr), Flags(Flags) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Construct a JITSymbol from a JITEvaluatedSymbol.
|
2016-08-01 22:49:11 +02:00
|
|
|
JITSymbol(JITEvaluatedSymbol Sym)
|
2016-08-04 22:32:37 +02:00
|
|
|
: CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Create a symbol for a definition that doesn't have a known address
|
2016-08-01 22:49:11 +02:00
|
|
|
/// yet.
|
|
|
|
/// @param GetAddress A functor to materialize a definition (fixing the
|
|
|
|
/// address) on demand.
|
|
|
|
///
|
|
|
|
/// This constructor allows a JIT layer to provide a reference to a symbol
|
|
|
|
/// definition without actually materializing the definition up front. The
|
|
|
|
/// user can materialize the definition at any time by calling the getAddress
|
|
|
|
/// method.
|
|
|
|
JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
|
2017-07-07 12:23:13 +02:00
|
|
|
: GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2017-07-07 04:59:13 +02:00
|
|
|
JITSymbol(const JITSymbol&) = delete;
|
|
|
|
JITSymbol& operator=(const JITSymbol&) = delete;
|
|
|
|
|
|
|
|
JITSymbol(JITSymbol &&Other)
|
|
|
|
: GetAddress(std::move(Other.GetAddress)), Flags(std::move(Other.Flags)) {
|
|
|
|
if (Flags.hasError())
|
|
|
|
Err = std::move(Other.Err);
|
|
|
|
else
|
|
|
|
CachedAddr = std::move(Other.CachedAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JITSymbol& operator=(JITSymbol &&Other) {
|
|
|
|
GetAddress = std::move(Other.GetAddress);
|
|
|
|
Flags = std::move(Other.Flags);
|
|
|
|
if (Flags.hasError())
|
|
|
|
Err = std::move(Other.Err);
|
|
|
|
else
|
|
|
|
CachedAddr = std::move(Other.CachedAddr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~JITSymbol() {
|
|
|
|
if (Flags.hasError())
|
|
|
|
Err.~Error();
|
|
|
|
else
|
|
|
|
CachedAddr.~JITTargetAddress();
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns true if the symbol exists, false otherwise.
|
2017-07-07 04:59:13 +02:00
|
|
|
explicit operator bool() const {
|
|
|
|
return !Flags.hasError() && (CachedAddr || GetAddress);
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Move the error field value out of this JITSymbol.
|
2017-07-07 04:59:13 +02:00
|
|
|
Error takeError() {
|
|
|
|
if (Flags.hasError())
|
|
|
|
return std::move(Err);
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Get the address of the symbol in the target address space. Returns
|
2016-08-01 22:49:11 +02:00
|
|
|
/// '0' if the symbol does not exist.
|
2017-07-07 04:59:13 +02:00
|
|
|
Expected<JITTargetAddress> getAddress() {
|
|
|
|
assert(!Flags.hasError() && "getAddress called on error value");
|
2016-08-01 22:49:11 +02:00
|
|
|
if (GetAddress) {
|
2017-07-07 04:59:13 +02:00
|
|
|
if (auto CachedAddrOrErr = GetAddress()) {
|
|
|
|
GetAddress = nullptr;
|
|
|
|
CachedAddr = *CachedAddrOrErr;
|
|
|
|
assert(CachedAddr && "Symbol could not be materialized.");
|
|
|
|
} else
|
|
|
|
return CachedAddrOrErr.takeError();
|
2016-08-01 22:49:11 +02:00
|
|
|
}
|
|
|
|
return CachedAddr;
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:32:37 +02:00
|
|
|
JITSymbolFlags getFlags() const { return Flags; }
|
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
private:
|
|
|
|
GetAddressFtor GetAddress;
|
2017-07-07 04:59:13 +02:00
|
|
|
union {
|
|
|
|
JITTargetAddress CachedAddr;
|
|
|
|
Error Err;
|
|
|
|
};
|
2016-08-04 22:32:37 +02:00
|
|
|
JITSymbolFlags Flags;
|
2016-08-01 22:49:11 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Symbol resolution interface.
|
2018-01-19 23:24:13 +01:00
|
|
|
///
|
|
|
|
/// Allows symbol flags and addresses to be looked up by name.
|
|
|
|
/// Symbol queries are done in bulk (i.e. you request resolution of a set of
|
|
|
|
/// symbols, rather than a single one) to reduce IPC overhead in the case of
|
|
|
|
/// remote JITing, and expose opportunities for parallel compilation.
|
2016-08-01 22:49:11 +02:00
|
|
|
class JITSymbolResolver {
|
|
|
|
public:
|
2018-01-22 04:00:31 +01:00
|
|
|
using LookupSet = std::set<StringRef>;
|
2018-01-19 23:24:13 +01:00
|
|
|
using LookupResult = std::map<StringRef, JITEvaluatedSymbol>;
|
2019-09-13 13:35:33 +02:00
|
|
|
using OnResolvedFunction = unique_function<void(Expected<LookupResult>)>;
|
2018-01-19 23:24:13 +01:00
|
|
|
|
2016-11-16 19:07:33 +01:00
|
|
|
virtual ~JITSymbolResolver() = default;
|
2016-08-01 22:49:11 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Returns the fully resolved address and flags for each of the given
|
2018-01-19 23:24:13 +01:00
|
|
|
/// symbols.
|
|
|
|
///
|
|
|
|
/// This method will return an error if any of the given symbols can not be
|
|
|
|
/// resolved, or if the resolution process itself triggers an error.
|
2018-09-25 21:48:46 +02:00
|
|
|
virtual void lookup(const LookupSet &Symbols,
|
|
|
|
OnResolvedFunction OnResolved) = 0;
|
2018-01-19 23:24:13 +01:00
|
|
|
|
2018-08-28 23:18:05 +02:00
|
|
|
/// Returns the subset of the given symbols that should be materialized by
|
|
|
|
/// the caller. Only weak/common symbols should be looked up, as strong
|
|
|
|
/// definitions are implicitly always part of the caller's responsibility.
|
|
|
|
virtual Expected<LookupSet>
|
|
|
|
getResponsibilitySet(const LookupSet &Symbols) = 0;
|
2018-01-19 23:24:13 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void anchor();
|
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Legacy symbol resolution interface.
|
2018-01-19 23:24:13 +01:00
|
|
|
class LegacyJITSymbolResolver : public JITSymbolResolver {
|
|
|
|
public:
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Performs lookup by, for each symbol, first calling
|
2018-01-19 23:24:13 +01:00
|
|
|
/// findSymbolInLogicalDylib and if that fails calling
|
|
|
|
/// findSymbol.
|
2018-09-25 21:48:46 +02:00
|
|
|
void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) final;
|
2018-01-19 23:24:13 +01:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Performs flags lookup by calling findSymbolInLogicalDylib and
|
2018-01-19 23:24:13 +01:00
|
|
|
/// returning the flags value for that symbol.
|
2018-08-28 23:18:05 +02:00
|
|
|
Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) final;
|
2018-01-19 23:24:13 +01:00
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
/// This method returns the address of the specified symbol if it exists
|
|
|
|
/// within the logical dynamic library represented by this JITSymbolResolver.
|
|
|
|
/// Unlike findSymbol, queries through this interface should return addresses
|
|
|
|
/// for hidden symbols.
|
|
|
|
///
|
|
|
|
/// This is of particular importance for the Orc JIT APIs, which support lazy
|
|
|
|
/// compilation by breaking up modules: Each of those broken out modules
|
|
|
|
/// must be able to resolve hidden symbols provided by the others. Clients
|
|
|
|
/// writing memory managers for MCJIT can usually ignore this method.
|
|
|
|
///
|
|
|
|
/// This method will be queried by RuntimeDyld when checking for previous
|
|
|
|
/// definitions of common symbols.
|
|
|
|
virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name) = 0;
|
|
|
|
|
|
|
|
/// This method returns the address of the specified function or variable.
|
|
|
|
/// It is used to resolve symbols during module linking.
|
|
|
|
///
|
|
|
|
/// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
|
|
|
|
/// skip all relocations for that symbol, and the client will be responsible
|
|
|
|
/// for handling them manually.
|
|
|
|
virtual JITSymbol findSymbol(const std::string &Name) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void anchor();
|
|
|
|
};
|
|
|
|
|
2016-11-16 19:07:33 +01:00
|
|
|
} // end namespace llvm
|
2016-08-01 22:49:11 +02:00
|
|
|
|
|
|
|
#endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H
|