1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Utilities for remote-JITing with LLI.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
15 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
17 #include "llvm/ExecutionEngine/Orc/RPCChannel.h"
18 #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 FDRPCChannel : public llvm::orc::remote::RPCChannel {
29 FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
31 std::error_code readBytes(char *Dst, unsigned Size) override {
32 assert(Dst && "Attempt to read into null.");
33 ssize_t ReadResult = ::read(InFD, Dst, Size);
34 if (ReadResult != (ssize_t)Size)
35 return std::error_code(errno, std::generic_category());
36 return std::error_code();
39 std::error_code appendBytes(const char *Src, unsigned Size) override {
40 assert(Src && "Attempt to append from null.");
41 ssize_t WriteResult = ::write(OutFD, Src, Size);
42 if (WriteResult != (ssize_t)Size)
43 std::error_code(errno, std::generic_category());
44 return std::error_code();
47 std::error_code send() override { return std::error_code(); }
53 // launch the remote process (see lli.cpp) and return a channel to it.
54 std::unique_ptr<FDRPCChannel> launchRemote();
58 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote memory manager.
59 class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
61 void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
62 this->MemMgr = std::move(MemMgr);
65 void setResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver) {
66 this->Resolver = std::move(Resolver);
69 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
71 StringRef SectionName) override {
72 return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
75 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
76 unsigned SectionID, StringRef SectionName,
77 bool IsReadOnly) override {
78 return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
82 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
83 uintptr_t RODataSize, uint32_t RODataAlign,
85 uint32_t RWDataAlign) override {
86 MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
87 RWDataSize, RWDataAlign);
90 bool needsToReserveAllocationSpace() override {
91 return MemMgr->needsToReserveAllocationSpace();
94 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
95 size_t Size) override {
96 MemMgr->registerEHFrames(Addr, LoadAddr, Size);
99 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
100 size_t Size) override {
101 MemMgr->deregisterEHFrames(Addr, LoadAddr, Size);
104 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
105 return MemMgr->finalizeMemory(ErrMsg);
108 void notifyObjectLoaded(RuntimeDyld &RTDyld,
109 const object::ObjectFile &Obj) override {
110 MemMgr->notifyObjectLoaded(RTDyld, Obj);
113 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
114 using RTDyldMemoryManager::notifyObjectLoaded;
116 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
117 return Resolver->findSymbol(Name);
120 RuntimeDyld::SymbolInfo
121 findSymbolInLogicalDylib(const std::string &Name) override {
122 return Resolver->findSymbolInLogicalDylib(Name);
126 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
127 std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver;