Initial implementation of JITLink - A replacement for RuntimeDyld.
authorLang Hames <lhames@gmail.com>
Sat, 20 Apr 2019 17:10:34 +0000 (17:10 +0000)
committerLang Hames <lhames@gmail.com>
Sat, 20 Apr 2019 17:10:34 +0000 (17:10 +0000)
commit11c8dfa583f50edb7c81e5c84cc7797133e9c5c1
tree00883ae768b29dddf3148c867922514a0aad2ed7
parent3980d1ca6b39dd37b12b873a19847bbebd5a07bc
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
40 files changed:
llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h [new file with mode: 0644]
llvm/include/llvm/ExecutionEngine/JITLink/JITLink_EHFrameSupport.h [new file with mode: 0644]
llvm/include/llvm/ExecutionEngine/JITLink/JITLink_MachO.h [new file with mode: 0644]
llvm/include/llvm/ExecutionEngine/JITLink/JITLink_MachO_x86_64.h [new file with mode: 0644]
llvm/include/llvm/ExecutionEngine/JITSymbol.h
llvm/include/llvm/ExecutionEngine/Orc/Core.h
llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
llvm/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h [new file with mode: 0644]
llvm/include/llvm/Support/Memory.h
llvm/lib/ExecutionEngine/CMakeLists.txt
llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLink.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLink_EHFrameSupport.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLink_EHFrameSupportImpl.h [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLink_MachO.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/JITLink_MachO_x86_64.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/LLVMBuild.txt [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.h [new file with mode: 0644]
llvm/lib/ExecutionEngine/LLVMBuild.txt
llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
llvm/lib/ExecutionEngine/Orc/Core.cpp
llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp
llvm/lib/Support/Memory.cpp
llvm/test/ExecutionEngine/JITLink/MachO_x86-64_relocations.s [new file with mode: 0644]
llvm/tools/lli/lli.cpp
llvm/tools/llvm-jitlink/CMakeLists.txt [new file with mode: 0644]
llvm/tools/llvm-jitlink/LLVMBuild.txt [new file with mode: 0644]
llvm/tools/llvm-jitlink/llvm-jitlink-macho.cpp [new file with mode: 0644]
llvm/tools/llvm-jitlink/llvm-jitlink.cpp [new file with mode: 0644]
llvm/tools/llvm-jitlink/llvm-jitlink.h [new file with mode: 0644]
llvm/unittests/ExecutionEngine/CMakeLists.txt
llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt [new file with mode: 0644]
llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp [new file with mode: 0644]
llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h [new file with mode: 0644]
llvm/unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp [new file with mode: 0644]