RemoteMemoryManager.cpp [plain text]
#define DEBUG_TYPE "lli"
#include "RemoteMemoryManager.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
using namespace llvm;
RemoteMemoryManager::~RemoteMemoryManager() {
for (SmallVector<Allocation, 2>::iterator
I = AllocatedSections.begin(), E = AllocatedSections.end();
I != E; ++I)
sys::Memory::releaseMappedMemory(I->MB);
}
uint8_t *RemoteMemoryManager::
allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) {
sys::MemoryBlock Block = allocateSection(Size);
AllocatedSections.push_back( Allocation(Block, Alignment, true) );
UnmappedSections.push_back( Allocation(Block, Alignment, true) );
return (uint8_t*)Block.base();
}
uint8_t *RemoteMemoryManager::
allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) {
sys::MemoryBlock Block = allocateSection(Size);
AllocatedSections.push_back( Allocation(Block, Alignment, false) );
UnmappedSections.push_back( Allocation(Block, Alignment, false) );
return (uint8_t*)Block.base();
}
sys::MemoryBlock RemoteMemoryManager::allocateSection(uintptr_t Size) {
error_code ec;
sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(Size,
&Near,
sys::Memory::MF_READ |
sys::Memory::MF_WRITE,
ec);
assert(!ec && MB.base());
Near = MB;
return MB;
}
void RemoteMemoryManager::notifyObjectLoaded(ExecutionEngine *EE,
const ObjectImage *Obj) {
assert(Target);
if (!Target)
return;
uint64_t CurOffset = 0;
unsigned MaxAlign = Target->getPageAlignment();
SmallVector<std::pair<Allocation, uint64_t>, 16> Offsets;
unsigned NumSections = UnmappedSections.size();
for (size_t i = 0, e = NumSections; i != e; ++i) {
Allocation &Section = UnmappedSections[i];
if (Section.IsCode) {
unsigned Size = Section.MB.size();
unsigned Align = Section.Alignment;
DEBUG(dbgs() << "code region: size " << Size
<< ", alignment " << Align << "\n");
CurOffset = (CurOffset + Align - 1) / Align * Align;
Offsets.push_back(std::pair<Allocation,uint64_t>(Section, CurOffset));
CurOffset += Size;
}
}
CurOffset = (CurOffset + MaxAlign - 1) / MaxAlign * MaxAlign;
for (size_t i = 0, e = NumSections; i != e; ++i) {
Allocation &Section = UnmappedSections[i];
if (!Section.IsCode) {
unsigned Size = Section.MB.size();
unsigned Align = Section.Alignment;
DEBUG(dbgs() << "data region: size " << Size
<< ", alignment " << Align << "\n");
CurOffset = (CurOffset + Align - 1) / Align * Align;
Offsets.push_back(std::pair<Allocation,uint64_t>(Section, CurOffset));
CurOffset += Size;
}
}
uint64_t RemoteAddr;
if (!Target->allocateSpace(CurOffset, MaxAlign, RemoteAddr))
report_fatal_error(Target->getErrorMsg());
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
uint64_t Addr = RemoteAddr + Offsets[i].second;
EE->mapSectionAddress(const_cast<void*>(Offsets[i].first.MB.base()), Addr);
DEBUG(dbgs() << " Mapping local: " << Offsets[i].first.MB.base()
<< " to remote: 0x" << format("%llx", Addr) << "\n");
MappedSections[Addr] = Offsets[i].first;
}
UnmappedSections.clear();
}
bool RemoteMemoryManager::finalizeMemory(std::string *ErrMsg) {
for (DenseMap<uint64_t, Allocation>::iterator
I = MappedSections.begin(), E = MappedSections.end();
I != E; ++I) {
uint64_t RemoteAddr = I->first;
const Allocation &Section = I->second;
if (Section.IsCode) {
if (!Target->loadCode(RemoteAddr, Section.MB.base(), Section.MB.size()))
report_fatal_error(Target->getErrorMsg());
DEBUG(dbgs() << " loading code: " << Section.MB.base()
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
} else {
if (!Target->loadData(RemoteAddr, Section.MB.base(), Section.MB.size()))
report_fatal_error(Target->getErrorMsg());
DEBUG(dbgs() << " loading data: " << Section.MB.base()
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
}
}
MappedSections.clear();
return false;
}
void RemoteMemoryManager::setMemoryWritable() { llvm_unreachable("Unexpected!"); }
void RemoteMemoryManager::setMemoryExecutable() { llvm_unreachable("Unexpected!"); }
void RemoteMemoryManager::setPoisonMemory(bool poison) { llvm_unreachable("Unexpected!"); }
void RemoteMemoryManager::AllocateGOT() { llvm_unreachable("Unexpected!"); }
uint8_t *RemoteMemoryManager::getGOTBase() const {
llvm_unreachable("Unexpected!");
return 0;
}
uint8_t *RemoteMemoryManager::startFunctionBody(const Function *F, uintptr_t &ActualSize){
llvm_unreachable("Unexpected!");
return 0;
}
uint8_t *RemoteMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
unsigned Alignment) {
llvm_unreachable("Unexpected!");
return 0;
}
void RemoteMemoryManager::endFunctionBody(const Function *F, uint8_t *FunctionStart,
uint8_t *FunctionEnd) {
llvm_unreachable("Unexpected!");
}
uint8_t *RemoteMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) {
llvm_unreachable("Unexpected!");
return 0;
}
uint8_t *RemoteMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment) {
llvm_unreachable("Unexpected!");
return 0;
}
void RemoteMemoryManager::deallocateFunctionBody(void *Body) {
llvm_unreachable("Unexpected!");
}