1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utilities for remote-JITing with LLI.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
14 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
15 
16 #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18 #include <mutex>
19 
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
21 #include <unistd.h>
22 #else
23 #include <io.h>
24 #endif
25 
26 // launch the remote process (see lli.cpp) and return a channel to it.
27 std::unique_ptr<llvm::orc::shared::FDRawByteChannel> launchRemote();
28 
29 namespace llvm {
30 
31 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
32 // memory manager.
33 class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
34 public:
setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr)35   void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
36     this->MemMgr = std::move(MemMgr);
37   }
38 
setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver)39   void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {
40     this->Resolver = std::move(Resolver);
41   }
42 
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName)43   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
44                                unsigned SectionID,
45                                StringRef SectionName) override {
46     return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
47   }
48 
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool IsReadOnly)49   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
50                                unsigned SectionID, StringRef SectionName,
51                                bool IsReadOnly) override {
52     return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
53                                        IsReadOnly);
54   }
55 
reserveAllocationSpace(uintptr_t CodeSize,uint32_t CodeAlign,uintptr_t RODataSize,uint32_t RODataAlign,uintptr_t RWDataSize,uint32_t RWDataAlign)56   void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
57                               uintptr_t RODataSize, uint32_t RODataAlign,
58                               uintptr_t RWDataSize,
59                               uint32_t RWDataAlign) override {
60     MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
61                                    RWDataSize, RWDataAlign);
62   }
63 
needsToReserveAllocationSpace()64   bool needsToReserveAllocationSpace() override {
65     return MemMgr->needsToReserveAllocationSpace();
66   }
67 
registerEHFrames(uint8_t * Addr,uint64_t LoadAddr,size_t Size)68   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
69                         size_t Size) override {
70     MemMgr->registerEHFrames(Addr, LoadAddr, Size);
71   }
72 
deregisterEHFrames()73   void deregisterEHFrames() override {
74     MemMgr->deregisterEHFrames();
75   }
76 
77   bool finalizeMemory(std::string *ErrMsg = nullptr) override {
78     return MemMgr->finalizeMemory(ErrMsg);
79   }
80 
notifyObjectLoaded(RuntimeDyld & RTDyld,const object::ObjectFile & Obj)81   void notifyObjectLoaded(RuntimeDyld &RTDyld,
82                           const object::ObjectFile &Obj) override {
83     MemMgr->notifyObjectLoaded(RTDyld, Obj);
84   }
85 
86   // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
87   using RTDyldMemoryManager::notifyObjectLoaded;
88 
findSymbol(const std::string & Name)89   JITSymbol findSymbol(const std::string &Name) override {
90     return Resolver->findSymbol(Name);
91   }
92 
93   JITSymbol
findSymbolInLogicalDylib(const std::string & Name)94   findSymbolInLogicalDylib(const std::string &Name) override {
95     return Resolver->findSymbolInLogicalDylib(Name);
96   }
97 
98 private:
99   std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
100   std::shared_ptr<LegacyJITSymbolResolver> Resolver;
101 };
102 
103 template <typename RemoteT>
104 class RemoteResolver : public LegacyJITSymbolResolver {
105 public:
106 
RemoteResolver(RemoteT & R)107   RemoteResolver(RemoteT &R) : R(R) {}
108 
findSymbol(const std::string & Name)109   JITSymbol findSymbol(const std::string &Name) override {
110     if (auto Addr = R.getSymbolAddress(Name))
111       return JITSymbol(*Addr, JITSymbolFlags::Exported);
112     else
113       return Addr.takeError();
114   }
115 
findSymbolInLogicalDylib(const std::string & Name)116   JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
117     return nullptr;
118   }
119 
120 public:
121   RemoteT &R;
122 };
123 }
124 
125 #endif
126