1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- 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 // Utilities for remote-JITing with LLI.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
14 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
16 #include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
26 /// RPC channel that reads from and writes from file descriptors.
27 class FDRawChannel final
: public llvm::orc::rpc::RawByteChannel
{
29 FDRawChannel(int InFD
, int OutFD
) : InFD(InFD
), OutFD(OutFD
) {}
31 llvm::Error
readBytes(char *Dst
, unsigned Size
) override
{
32 assert(Dst
&& "Attempt to read into null.");
33 ssize_t Completed
= 0;
34 while (Completed
< static_cast<ssize_t
>(Size
)) {
35 ssize_t Read
= ::read(InFD
, Dst
+ Completed
, Size
- Completed
);
38 if (ErrNo
== EAGAIN
|| ErrNo
== EINTR
)
41 return llvm::errorCodeToError(
42 std::error_code(errno
, std::generic_category()));
46 return llvm::Error::success();
49 llvm::Error
appendBytes(const char *Src
, unsigned Size
) override
{
50 assert(Src
&& "Attempt to append from null.");
51 ssize_t Completed
= 0;
52 while (Completed
< static_cast<ssize_t
>(Size
)) {
53 ssize_t Written
= ::write(OutFD
, Src
+ Completed
, Size
- Completed
);
56 if (ErrNo
== EAGAIN
|| ErrNo
== EINTR
)
59 return llvm::errorCodeToError(
60 std::error_code(errno
, std::generic_category()));
64 return llvm::Error::success();
67 llvm::Error
send() override
{ return llvm::Error::success(); }
73 // launch the remote process (see lli.cpp) and return a channel to it.
74 std::unique_ptr
<FDRawChannel
> launchRemote();
78 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
80 class ForwardingMemoryManager
: public llvm::RTDyldMemoryManager
{
82 void setMemMgr(std::unique_ptr
<RuntimeDyld::MemoryManager
> MemMgr
) {
83 this->MemMgr
= std::move(MemMgr
);
86 void setResolver(std::shared_ptr
<LegacyJITSymbolResolver
> Resolver
) {
87 this->Resolver
= std::move(Resolver
);
90 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
92 StringRef SectionName
) override
{
93 return MemMgr
->allocateCodeSection(Size
, Alignment
, SectionID
, SectionName
);
96 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
97 unsigned SectionID
, StringRef SectionName
,
98 bool IsReadOnly
) override
{
99 return MemMgr
->allocateDataSection(Size
, Alignment
, SectionID
, SectionName
,
103 void reserveAllocationSpace(uintptr_t CodeSize
, uint32_t CodeAlign
,
104 uintptr_t RODataSize
, uint32_t RODataAlign
,
105 uintptr_t RWDataSize
,
106 uint32_t RWDataAlign
) override
{
107 MemMgr
->reserveAllocationSpace(CodeSize
, CodeAlign
, RODataSize
, RODataAlign
,
108 RWDataSize
, RWDataAlign
);
111 bool needsToReserveAllocationSpace() override
{
112 return MemMgr
->needsToReserveAllocationSpace();
115 void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
,
116 size_t Size
) override
{
117 MemMgr
->registerEHFrames(Addr
, LoadAddr
, Size
);
120 void deregisterEHFrames() override
{
121 MemMgr
->deregisterEHFrames();
124 bool finalizeMemory(std::string
*ErrMsg
= nullptr) override
{
125 return MemMgr
->finalizeMemory(ErrMsg
);
128 void notifyObjectLoaded(RuntimeDyld
&RTDyld
,
129 const object::ObjectFile
&Obj
) override
{
130 MemMgr
->notifyObjectLoaded(RTDyld
, Obj
);
133 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
134 using RTDyldMemoryManager::notifyObjectLoaded
;
136 JITSymbol
findSymbol(const std::string
&Name
) override
{
137 return Resolver
->findSymbol(Name
);
141 findSymbolInLogicalDylib(const std::string
&Name
) override
{
142 return Resolver
->findSymbolInLogicalDylib(Name
);
146 std::unique_ptr
<RuntimeDyld::MemoryManager
> MemMgr
;
147 std::shared_ptr
<LegacyJITSymbolResolver
> Resolver
;