1 //===- RemoteTarget.cpp - LLVM Remote process JIT execution --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the RemoteTarget class which executes JITed code in a
11 // separate address range from where it was built.
13 //===----------------------------------------------------------------------===//
15 #include "RemoteTarget.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Memory.h"
23 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
25 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
26 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
27 if (Mem.base() == NULL)
29 if ((uintptr_t)Mem.base() % Alignment) {
30 ErrorMsg = "unable to allocate sufficiently aligned memory";
33 Address = reinterpret_cast<uint64_t>(Mem.base());
37 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
38 memcpy ((void*)Address, Data, Size);
42 bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
43 memcpy ((void*)Address, Data, Size);
44 sys::MemoryBlock Mem((void*)Address, Size);
45 sys::Memory::setExecutable(Mem, &ErrorMsg);
49 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
50 int (*fn)(void) = (int(*)(void))Address;
55 void RemoteTarget::create() {
58 void RemoteTarget::stop() {
59 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
60 sys::Memory::ReleaseRWX(Allocations[i]);