Path: blob/main/contrib/llvm-project/llvm/tools/lli/ForwardingMemoryManager.h
35260 views
//===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Utilities for remote-JITing with LLI.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H13#define LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H1415#include "llvm/ExecutionEngine/Orc/EPCGenericDylibManager.h"16#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"1718namespace llvm {1920// ForwardingMM - Adapter to connect MCJIT to Orc's Remote21// memory manager.22class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {23public:24void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {25this->MemMgr = std::move(MemMgr);26}2728void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {29this->Resolver = std::move(Resolver);30}3132uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,33unsigned SectionID,34StringRef SectionName) override {35return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);36}3738uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,39unsigned SectionID, StringRef SectionName,40bool IsReadOnly) override {41return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,42IsReadOnly);43}4445void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,46uintptr_t RODataSize, Align RODataAlign,47uintptr_t RWDataSize,48Align RWDataAlign) override {49MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,50RWDataSize, RWDataAlign);51}5253bool needsToReserveAllocationSpace() override {54return MemMgr->needsToReserveAllocationSpace();55}5657void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,58size_t Size) override {59MemMgr->registerEHFrames(Addr, LoadAddr, Size);60}6162void deregisterEHFrames() override { MemMgr->deregisterEHFrames(); }6364bool finalizeMemory(std::string *ErrMsg = nullptr) override {65return MemMgr->finalizeMemory(ErrMsg);66}6768void notifyObjectLoaded(RuntimeDyld &RTDyld,69const object::ObjectFile &Obj) override {70MemMgr->notifyObjectLoaded(RTDyld, Obj);71}7273// Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.74using RTDyldMemoryManager::notifyObjectLoaded;7576JITSymbol findSymbol(const std::string &Name) override {77return Resolver->findSymbol(Name);78}7980JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {81return Resolver->findSymbolInLogicalDylib(Name);82}8384private:85std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;86std::shared_ptr<LegacyJITSymbolResolver> Resolver;87};8889class RemoteResolver : public LegacyJITSymbolResolver {90public:91static Expected<std::unique_ptr<RemoteResolver>>92Create(orc::ExecutorProcessControl &EPC) {93auto DylibMgr =94orc::EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(EPC);95if (!DylibMgr)96return DylibMgr.takeError();97auto H = DylibMgr->open("", 0);98if (!H)99return H.takeError();100return std::make_unique<RemoteResolver>(std::move(*DylibMgr),101std::move(*H));102}103104JITSymbol findSymbol(const std::string &Name) override {105orc::RemoteSymbolLookupSet R;106R.push_back({std::move(Name), false});107if (auto Syms = DylibMgr.lookup(H, R)) {108if (Syms->size() != 1)109return make_error<StringError>("Unexpected remote lookup result",110inconvertibleErrorCode());111return JITSymbol(Syms->front().getAddress().getValue(),112Syms->front().getFlags());113} else114return Syms.takeError();115}116117JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {118return nullptr;119}120121public:122RemoteResolver(orc::EPCGenericDylibManager DylibMgr,123orc::tpctypes::DylibHandle H)124: DylibMgr(std::move(DylibMgr)), H(std::move(H)) {}125126orc::EPCGenericDylibManager DylibMgr;127orc::tpctypes::DylibHandle H;128};129} // namespace llvm130131#endif // LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H132133134