2012-09-05 16:50:34 +00:00
|
|
|
//===- RemoteTarget.cpp - LLVM Remote process JIT execution --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the RemoteTarget class which executes JITed code in a
|
|
|
|
// separate address range from where it was built.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RemoteTarget.h"
|
2012-09-15 18:45:38 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
2012-09-05 16:50:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
2013-10-02 17:12:36 +00:00
|
|
|
|
2012-09-05 16:50:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-10-02 17:12:36 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Simulated remote execution
|
|
|
|
//
|
|
|
|
// This implementation will simply move generated code and data to a new memory
|
|
|
|
// location in the current executable and let it run from there.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-05 16:50:34 +00:00
|
|
|
bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
|
|
|
|
uint64_t &Address) {
|
2014-04-25 04:24:47 +00:00
|
|
|
sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : nullptr;
|
2012-09-05 16:50:34 +00:00
|
|
|
sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
|
2014-04-25 04:24:47 +00:00
|
|
|
if (Mem.base() == nullptr)
|
2014-01-14 22:43:43 +00:00
|
|
|
return false;
|
2012-09-05 16:50:34 +00:00
|
|
|
if ((uintptr_t)Mem.base() % Alignment) {
|
|
|
|
ErrorMsg = "unable to allocate sufficiently aligned memory";
|
2014-01-14 22:43:43 +00:00
|
|
|
return false;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
Address = reinterpret_cast<uint64_t>(Mem.base());
|
2014-01-23 11:04:42 +00:00
|
|
|
Allocations.push_back(Mem);
|
2014-01-14 22:43:43 +00:00
|
|
|
return true;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
|
|
|
|
memcpy ((void*)Address, Data, Size);
|
2014-01-14 22:43:43 +00:00
|
|
|
return true;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
|
|
|
|
memcpy ((void*)Address, Data, Size);
|
2012-10-31 20:37:14 +00:00
|
|
|
sys::MemoryBlock Mem((void*)Address, Size);
|
|
|
|
sys::Memory::setExecutable(Mem, &ErrorMsg);
|
2014-01-14 22:43:43 +00:00
|
|
|
return true;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
|
|
|
|
int (*fn)(void) = (int(*)(void))Address;
|
|
|
|
RetVal = fn();
|
2014-01-14 22:43:43 +00:00
|
|
|
return true;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 22:43:43 +00:00
|
|
|
bool RemoteTarget::create() {
|
|
|
|
return true;
|
2012-09-05 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTarget::stop() {
|
|
|
|
for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
|
|
|
|
sys::Memory::ReleaseRWX(Allocations[i]);
|
|
|
|
}
|