xref: /llvm-project/llvm/tools/lli/ForwardingMemoryManager.h (revision ddaa93b095288ce459be4096193c40a4ea247b11)
16498b0e9SLang Hames //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
26498b0e9SLang Hames //
36498b0e9SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46498b0e9SLang Hames // See https://llvm.org/LICENSE.txt for license information.
56498b0e9SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66498b0e9SLang Hames //
76498b0e9SLang Hames //===----------------------------------------------------------------------===//
86498b0e9SLang Hames //
96498b0e9SLang Hames // Utilities for remote-JITing with LLI.
106498b0e9SLang Hames //
116498b0e9SLang Hames //===----------------------------------------------------------------------===//
126498b0e9SLang Hames 
136498b0e9SLang Hames #ifndef LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
146498b0e9SLang Hames #define LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
156498b0e9SLang Hames 
166498b0e9SLang Hames #include "llvm/ExecutionEngine/Orc/EPCGenericDylibManager.h"
176498b0e9SLang Hames #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
186498b0e9SLang Hames 
196498b0e9SLang Hames namespace llvm {
206498b0e9SLang Hames 
216498b0e9SLang Hames // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
226498b0e9SLang Hames // memory manager.
236498b0e9SLang Hames class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
246498b0e9SLang Hames public:
setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr)256498b0e9SLang Hames   void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
266498b0e9SLang Hames     this->MemMgr = std::move(MemMgr);
276498b0e9SLang Hames   }
286498b0e9SLang Hames 
setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver)296498b0e9SLang Hames   void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {
306498b0e9SLang Hames     this->Resolver = std::move(Resolver);
316498b0e9SLang Hames   }
326498b0e9SLang Hames 
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName)336498b0e9SLang Hames   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
346498b0e9SLang Hames                                unsigned SectionID,
356498b0e9SLang Hames                                StringRef SectionName) override {
366498b0e9SLang Hames     return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
376498b0e9SLang Hames   }
386498b0e9SLang Hames 
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool IsReadOnly)396498b0e9SLang Hames   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
406498b0e9SLang Hames                                unsigned SectionID, StringRef SectionName,
416498b0e9SLang Hames                                bool IsReadOnly) override {
426498b0e9SLang Hames     return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
436498b0e9SLang Hames                                        IsReadOnly);
446498b0e9SLang Hames   }
456498b0e9SLang Hames 
reserveAllocationSpace(uintptr_t CodeSize,Align CodeAlign,uintptr_t RODataSize,Align RODataAlign,uintptr_t RWDataSize,Align RWDataAlign)46828ce42aSGuillaume Chatelet   void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
47828ce42aSGuillaume Chatelet                               uintptr_t RODataSize, Align RODataAlign,
486498b0e9SLang Hames                               uintptr_t RWDataSize,
49828ce42aSGuillaume Chatelet                               Align RWDataAlign) override {
506498b0e9SLang Hames     MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
516498b0e9SLang Hames                                    RWDataSize, RWDataAlign);
526498b0e9SLang Hames   }
536498b0e9SLang Hames 
needsToReserveAllocationSpace()546498b0e9SLang Hames   bool needsToReserveAllocationSpace() override {
556498b0e9SLang Hames     return MemMgr->needsToReserveAllocationSpace();
566498b0e9SLang Hames   }
576498b0e9SLang Hames 
registerEHFrames(uint8_t * Addr,uint64_t LoadAddr,size_t Size)586498b0e9SLang Hames   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
596498b0e9SLang Hames                         size_t Size) override {
606498b0e9SLang Hames     MemMgr->registerEHFrames(Addr, LoadAddr, Size);
616498b0e9SLang Hames   }
626498b0e9SLang Hames 
deregisterEHFrames()636498b0e9SLang Hames   void deregisterEHFrames() override { MemMgr->deregisterEHFrames(); }
646498b0e9SLang Hames 
656498b0e9SLang Hames   bool finalizeMemory(std::string *ErrMsg = nullptr) override {
666498b0e9SLang Hames     return MemMgr->finalizeMemory(ErrMsg);
676498b0e9SLang Hames   }
686498b0e9SLang Hames 
notifyObjectLoaded(RuntimeDyld & RTDyld,const object::ObjectFile & Obj)696498b0e9SLang Hames   void notifyObjectLoaded(RuntimeDyld &RTDyld,
706498b0e9SLang Hames                           const object::ObjectFile &Obj) override {
716498b0e9SLang Hames     MemMgr->notifyObjectLoaded(RTDyld, Obj);
726498b0e9SLang Hames   }
736498b0e9SLang Hames 
746498b0e9SLang Hames   // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
756498b0e9SLang Hames   using RTDyldMemoryManager::notifyObjectLoaded;
766498b0e9SLang Hames 
findSymbol(const std::string & Name)776498b0e9SLang Hames   JITSymbol findSymbol(const std::string &Name) override {
786498b0e9SLang Hames     return Resolver->findSymbol(Name);
796498b0e9SLang Hames   }
806498b0e9SLang Hames 
findSymbolInLogicalDylib(const std::string & Name)816498b0e9SLang Hames   JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
826498b0e9SLang Hames     return Resolver->findSymbolInLogicalDylib(Name);
836498b0e9SLang Hames   }
846498b0e9SLang Hames 
856498b0e9SLang Hames private:
866498b0e9SLang Hames   std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
876498b0e9SLang Hames   std::shared_ptr<LegacyJITSymbolResolver> Resolver;
886498b0e9SLang Hames };
896498b0e9SLang Hames 
906498b0e9SLang Hames class RemoteResolver : public LegacyJITSymbolResolver {
916498b0e9SLang Hames public:
926498b0e9SLang Hames   static Expected<std::unique_ptr<RemoteResolver>>
Create(orc::ExecutorProcessControl & EPC)936498b0e9SLang Hames   Create(orc::ExecutorProcessControl &EPC) {
946498b0e9SLang Hames     auto DylibMgr =
956498b0e9SLang Hames         orc::EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(EPC);
966498b0e9SLang Hames     if (!DylibMgr)
976498b0e9SLang Hames       return DylibMgr.takeError();
986498b0e9SLang Hames     auto H = DylibMgr->open("", 0);
996498b0e9SLang Hames     if (!H)
1006498b0e9SLang Hames       return H.takeError();
101*ddaa93b0SKazu Hirata     return std::make_unique<RemoteResolver>(std::move(*DylibMgr),
102*ddaa93b0SKazu Hirata                                             std::move(*H));
1036498b0e9SLang Hames   }
1046498b0e9SLang Hames 
findSymbol(const std::string & Name)1056498b0e9SLang Hames   JITSymbol findSymbol(const std::string &Name) override {
1066498b0e9SLang Hames     orc::RemoteSymbolLookupSet R;
1076498b0e9SLang Hames     R.push_back({std::move(Name), false});
10840b4ac27SBen Langmuir     if (auto Syms = DylibMgr.lookup(H, R)) {
10940b4ac27SBen Langmuir       if (Syms->size() != 1)
1106498b0e9SLang Hames         return make_error<StringError>("Unexpected remote lookup result",
1116498b0e9SLang Hames                                        inconvertibleErrorCode());
11240b4ac27SBen Langmuir       return JITSymbol(Syms->front().getAddress().getValue(),
11340b4ac27SBen Langmuir                        Syms->front().getFlags());
1146498b0e9SLang Hames     } else
11540b4ac27SBen Langmuir       return Syms.takeError();
1166498b0e9SLang Hames   }
1176498b0e9SLang Hames 
findSymbolInLogicalDylib(const std::string & Name)1186498b0e9SLang Hames   JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
1196498b0e9SLang Hames     return nullptr;
1206498b0e9SLang Hames   }
1216498b0e9SLang Hames 
1226498b0e9SLang Hames public:
RemoteResolver(orc::EPCGenericDylibManager DylibMgr,orc::tpctypes::DylibHandle H)1236498b0e9SLang Hames   RemoteResolver(orc::EPCGenericDylibManager DylibMgr,
1246498b0e9SLang Hames                  orc::tpctypes::DylibHandle H)
1256498b0e9SLang Hames       : DylibMgr(std::move(DylibMgr)), H(std::move(H)) {}
1266498b0e9SLang Hames 
1276498b0e9SLang Hames   orc::EPCGenericDylibManager DylibMgr;
1286498b0e9SLang Hames   orc::tpctypes::DylibHandle H;
1296498b0e9SLang Hames };
1306498b0e9SLang Hames } // namespace llvm
1316498b0e9SLang Hames 
1326498b0e9SLang Hames #endif // LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
133