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_FORWARDINGMEMORYMANAGER_H
14 #define LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
16 #include "llvm/ExecutionEngine/Orc/EPCGenericDylibManager.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
21 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
23 class ForwardingMemoryManager
: public llvm::RTDyldMemoryManager
{
25 void setMemMgr(std::unique_ptr
<RuntimeDyld::MemoryManager
> MemMgr
) {
26 this->MemMgr
= std::move(MemMgr
);
29 void setResolver(std::shared_ptr
<LegacyJITSymbolResolver
> Resolver
) {
30 this->Resolver
= std::move(Resolver
);
33 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
35 StringRef SectionName
) override
{
36 return MemMgr
->allocateCodeSection(Size
, Alignment
, SectionID
, SectionName
);
39 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
40 unsigned SectionID
, StringRef SectionName
,
41 bool IsReadOnly
) override
{
42 return MemMgr
->allocateDataSection(Size
, Alignment
, SectionID
, SectionName
,
46 void reserveAllocationSpace(uintptr_t CodeSize
, Align CodeAlign
,
47 uintptr_t RODataSize
, Align RODataAlign
,
49 Align RWDataAlign
) override
{
50 MemMgr
->reserveAllocationSpace(CodeSize
, CodeAlign
, RODataSize
, RODataAlign
,
51 RWDataSize
, RWDataAlign
);
54 bool needsToReserveAllocationSpace() override
{
55 return MemMgr
->needsToReserveAllocationSpace();
58 void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
,
59 size_t Size
) override
{
60 MemMgr
->registerEHFrames(Addr
, LoadAddr
, Size
);
63 void deregisterEHFrames() override
{ MemMgr
->deregisterEHFrames(); }
65 bool finalizeMemory(std::string
*ErrMsg
= nullptr) override
{
66 return MemMgr
->finalizeMemory(ErrMsg
);
69 void notifyObjectLoaded(RuntimeDyld
&RTDyld
,
70 const object::ObjectFile
&Obj
) override
{
71 MemMgr
->notifyObjectLoaded(RTDyld
, Obj
);
74 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
75 using RTDyldMemoryManager::notifyObjectLoaded
;
77 JITSymbol
findSymbol(const std::string
&Name
) override
{
78 return Resolver
->findSymbol(Name
);
81 JITSymbol
findSymbolInLogicalDylib(const std::string
&Name
) override
{
82 return Resolver
->findSymbolInLogicalDylib(Name
);
86 std::unique_ptr
<RuntimeDyld::MemoryManager
> MemMgr
;
87 std::shared_ptr
<LegacyJITSymbolResolver
> Resolver
;
90 class RemoteResolver
: public LegacyJITSymbolResolver
{
92 static Expected
<std::unique_ptr
<RemoteResolver
>>
93 Create(orc::ExecutorProcessControl
&EPC
) {
95 orc::EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(EPC
);
97 return DylibMgr
.takeError();
98 auto H
= DylibMgr
->open("", 0);
100 return H
.takeError();
101 return std::unique_ptr
<RemoteResolver
>(
102 new RemoteResolver(std::move(*DylibMgr
), std::move(*H
)));
105 JITSymbol
findSymbol(const std::string
&Name
) override
{
106 orc::RemoteSymbolLookupSet R
;
107 R
.push_back({std::move(Name
), false});
108 if (auto Addrs
= DylibMgr
.lookup(H
, R
)) {
109 if (Addrs
->size() != 1)
110 return make_error
<StringError
>("Unexpected remote lookup result",
111 inconvertibleErrorCode());
112 return JITSymbol(Addrs
->front().getValue(), JITSymbolFlags::Exported
);
114 return Addrs
.takeError();
117 JITSymbol
findSymbolInLogicalDylib(const std::string
&Name
) override
{
122 RemoteResolver(orc::EPCGenericDylibManager DylibMgr
,
123 orc::tpctypes::DylibHandle H
)
124 : DylibMgr(std::move(DylibMgr
)), H(std::move(H
)) {}
126 orc::EPCGenericDylibManager DylibMgr
;
127 orc::tpctypes::DylibHandle H
;
131 #endif // LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H