1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00
llvm-mirror/tools/llvm-jitlink/llvm-jitlink.h
Lang Hames a020f7f14c [ORC][ORC-RT] Introduce ORC-runtime based MachO-Platform.
Adds support for MachO static initializers/deinitializers and eh-frame
registration via the ORC runtime.

This commit introduces cooperative support code into the ORC runtime and ORC
LLVM libraries (especially the MachOPlatform class) to support macho runtime
features for JIT'd code. This commit introduces support for static
initializers, static destructors (via cxa_atexit interposition), and eh-frame
registration. Near-future commits will add support for MachO native
thread-local variables, and language runtime registration (e.g. for Objective-C
and Swift).

The llvm-jitlink tool is updated to use the ORC runtime where available, and
regression tests for the new MachOPlatform support are added to compiler-rt.

Notable changes on the ORC runtime side:

1. The new macho_platform.h / macho_platform.cpp files contain the bulk of the
runtime-side support. This includes eh-frame registration; jit versions of
dlopen, dlsym, and dlclose; a cxa_atexit interpose to record static destructors,
and an '__orc_rt_macho_run_program' function that defines running a JIT'd MachO
program in terms of the jit- dlopen/dlsym/dlclose functions.

2. Replaces JITTargetAddress (and casting operations) with ExecutorAddress
(copied from LLVM) to improve type-safety of address management.

3. Adds serialization support for ExecutorAddress and unordered_map types to
the runtime-side Simple Packed Serialization code.

4. Adds orc-runtime regression tests to ensure that static initializers and
cxa-atexit interposes work as expected.

Notable changes on the LLVM side:

1. The MachOPlatform class is updated to:

  1.1. Load the ORC runtime into the ExecutionSession.
  1.2. Set up standard aliases for macho-specific runtime functions. E.g.
       ___cxa_atexit -> ___orc_rt_macho_cxa_atexit.
  1.3. Install the MachOPlatformPlugin to scrape LinkGraphs for information
       needed to support MachO features (e.g. eh-frames, mod-inits), and
       communicate this information to the runtime.
  1.4. Provide entry-points that the runtime can call to request initializers,
       perform symbol lookup, and request deinitialiers (the latter is
       implemented as an empty placeholder as macho object deinits are rarely
       used).
  1.5. Create a MachO header object for each JITDylib (defining the __mh_header
       and __dso_handle symbols).

2. The llvm-jitlink tool (and llvm-jitlink-executor) are updated to use the
runtime when available.

3. A `lookupInitSymbolsAsync` method is added to the Platform base class. This
can be used to issue an async lookup for initializer symbols. The existing
`lookupInitSymbols` method is retained (the GenericIRPlatform code is still
using it), but is deprecated and will be removed soon.

4. JIT-dispatch support code is added to ExecutorProcessControl.

The JIT-dispatch system allows handlers in the JIT process to be associated with
'tag' symbols in the executor, and allows the executor to make remote procedure
calls back to the JIT process (via __orc_rt_jit_dispatch) using those tags.

The primary use case is ORC runtime code that needs to call bakc to handlers in
orc::Platform subclasses. E.g. __orc_rt_macho_jit_dlopen calling back to
MachOPlatform::rt_getInitializers using __orc_rt_macho_get_initializers_tag.
(The system is generic however, and could be used by non-runtime code).

The new ExecutorProcessControl::JITDispatchInfo struct provides the address
(in the executor) of the jit-dispatch function and a jit-dispatch context
object, and implementations of the dispatch function are added to
SelfExecutorProcessControl and OrcRPCExecutorProcessControl.

5. OrcRPCTPCServer is updated to support JIT-dispatch calls over ORC-RPC.

6. Serialization support for StringMap is added to the LLVM-side Simple Packed
Serialization code.

7. A JITLink::allocateBuffer operation is introduced to allocate writable memory
attached to the graph. This is used by the MachO header synthesis code, and will
be generically useful for other clients who want to create new graph content
from scratch.
2021-07-19 19:50:16 +10:00

171 lines
5.8 KiB
C++

//===---- llvm-jitlink.h - Session and format-specific decls ----*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// llvm-jitlink Session class and tool utilities.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
#define LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/OrcRPCExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
#include "llvm/ExecutionEngine/Orc/Shared/RPCUtils.h"
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
namespace llvm {
struct Session;
/// ObjectLinkingLayer with additional support for symbol promotion.
class LLVMJITLinkObjectLinkingLayer : public orc::ObjectLinkingLayer {
public:
using orc::ObjectLinkingLayer::add;
LLVMJITLinkObjectLinkingLayer(Session &S,
jitlink::JITLinkMemoryManager &MemMgr);
Error add(orc::ResourceTrackerSP RT,
std::unique_ptr<MemoryBuffer> O) override;
private:
Session &S;
};
using LLVMJITLinkChannel = orc::shared::FDRawByteChannel;
using LLVMJITLinkRPCEndpoint =
orc::shared::MultiThreadedRPCEndpoint<LLVMJITLinkChannel>;
using LLVMJITLinkRemoteMemoryAccess =
orc::OrcRPCEPCMemoryAccess<LLVMJITLinkRPCEndpoint>;
class LLVMJITLinkRemoteExecutorProcessControl
: public orc::OrcRPCExecutorProcessControlBase<LLVMJITLinkRPCEndpoint> {
public:
using BaseT = orc::OrcRPCExecutorProcessControlBase<LLVMJITLinkRPCEndpoint>;
static Expected<std::unique_ptr<ExecutorProcessControl>> LaunchExecutor();
static Expected<std::unique_ptr<ExecutorProcessControl>> ConnectToExecutor();
Error disconnect() override;
private:
using LLVMJITLinkRemoteMemoryAccess =
orc::OrcRPCEPCMemoryAccess<LLVMJITLinkRemoteExecutorProcessControl>;
using LLVMJITLinkRemoteMemoryManager = orc::OrcRPCEPCJITLinkMemoryManager<
LLVMJITLinkRemoteExecutorProcessControl>;
LLVMJITLinkRemoteExecutorProcessControl(
std::shared_ptr<orc::SymbolStringPool> SSP,
std::unique_ptr<LLVMJITLinkChannel> Channel,
std::unique_ptr<LLVMJITLinkRPCEndpoint> Endpoint,
ErrorReporter ReportError, Error &Err)
: BaseT(std::move(SSP), *Endpoint, std::move(ReportError)),
Channel(std::move(Channel)), Endpoint(std::move(Endpoint)) {
ErrorAsOutParameter _(&Err);
ListenerThread = std::thread([&]() {
while (!Finished) {
if (auto Err = this->Endpoint->handleOne()) {
reportError(std::move(Err));
return;
}
}
});
if (auto Err2 = initializeORCRPCEPCBase()) {
Err = joinErrors(std::move(Err2), disconnect());
return;
}
OwnedMemAccess = std::make_unique<LLVMJITLinkRemoteMemoryAccess>(*this);
MemAccess = OwnedMemAccess.get();
OwnedMemMgr = std::make_unique<LLVMJITLinkRemoteMemoryManager>(*this);
MemMgr = OwnedMemMgr.get();
}
std::unique_ptr<LLVMJITLinkChannel> Channel;
std::unique_ptr<LLVMJITLinkRPCEndpoint> Endpoint;
std::unique_ptr<ExecutorProcessControl::MemoryAccess> OwnedMemAccess;
std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
std::atomic<bool> Finished{false};
std::thread ListenerThread;
};
struct Session {
std::unique_ptr<orc::ExecutorProcessControl> EPC;
orc::ExecutionSession ES;
orc::JITDylib *MainJD = nullptr;
LLVMJITLinkObjectLinkingLayer ObjLayer;
std::vector<orc::JITDylib *> JDSearchOrder;
~Session();
static Expected<std::unique_ptr<Session>> Create(Triple TT);
void dumpSessionInfo(raw_ostream &OS);
void modifyPassConfig(const Triple &FTT,
jitlink::PassConfiguration &PassConfig);
using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
struct FileInfo {
StringMap<MemoryRegionInfo> SectionInfos;
StringMap<MemoryRegionInfo> StubInfos;
StringMap<MemoryRegionInfo> GOTEntryInfos;
};
using SymbolInfoMap = StringMap<MemoryRegionInfo>;
using FileInfoMap = StringMap<FileInfo>;
Expected<FileInfo &> findFileInfo(StringRef FileName);
Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
StringRef SectionName);
Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
StringRef TargetName);
Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
StringRef TargetName);
bool isSymbolRegistered(StringRef Name);
Expected<MemoryRegionInfo &> findSymbolInfo(StringRef SymbolName,
Twine ErrorMsgStem);
SymbolInfoMap SymbolInfos;
FileInfoMap FileInfos;
uint64_t SizeBeforePruning = 0;
uint64_t SizeAfterFixups = 0;
StringSet<> HarnessFiles;
StringSet<> HarnessExternals;
StringSet<> HarnessDefinitions;
DenseMap<StringRef, StringRef> CanonicalWeakDefs;
private:
Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
};
/// Record symbols, GOT entries, stubs, and sections for ELF file.
Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
/// Record symbols, GOT entries, stubs, and sections for MachO file.
Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
} // end namespace llvm
#endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H