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 "RemoteTargetExternal.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/Memory.h"
26 RemoteTarget *RemoteTarget::createRemoteTarget() {
27 return new RemoteTarget;
30 RemoteTarget *RemoteTarget::createExternalRemoteTarget(std::string &ChildName) {
32 return new RemoteTargetExternal(ChildName);
38 bool RemoteTarget::hostSupportsExternalRemoteTarget() {
47 ////////////////////////////////////////////////////////////////////////////////
48 // Simulated remote execution
50 // This implementation will simply move generated code and data to a new memory
51 // location in the current executable and let it run from there.
52 ////////////////////////////////////////////////////////////////////////////////
54 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
56 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
57 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
58 if (Mem.base() == NULL)
60 if ((uintptr_t)Mem.base() % Alignment) {
61 ErrorMsg = "unable to allocate sufficiently aligned memory";
64 Address = reinterpret_cast<uint64_t>(Mem.base());
68 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
69 memcpy ((void*)Address, Data, Size);
73 bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
74 memcpy ((void*)Address, Data, Size);
75 sys::MemoryBlock Mem((void*)Address, Size);
76 sys::Memory::setExecutable(Mem, &ErrorMsg);
80 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
81 int (*fn)(void) = (int(*)(void))Address;
86 void RemoteTarget::create() {
89 void RemoteTarget::stop() {
90 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
91 sys::Memory::ReleaseRWX(Allocations[i]);