1 //===- RemoteTarget.cpp - LLVM Remote process JIT execution --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implementation of the RemoteTarget class which executes JITed code in a
11 // separate address range from where it was built.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
20 #include <stdlib.h>
21 #include <string>
22
23 using namespace llvm;
24
25 // Static methods
createRemoteTarget()26 RemoteTarget *RemoteTarget::createRemoteTarget() {
27 return new RemoteTarget;
28 }
29
createExternalRemoteTarget(std::string & ChildName)30 RemoteTarget *RemoteTarget::createExternalRemoteTarget(std::string &ChildName) {
31 #ifdef LLVM_ON_UNIX
32 return new RemoteTargetExternal(ChildName);
33 #else
34 return 0;
35 #endif
36 }
37
hostSupportsExternalRemoteTarget()38 bool RemoteTarget::hostSupportsExternalRemoteTarget() {
39 #ifdef LLVM_ON_UNIX
40 return true;
41 #else
42 return false;
43 #endif
44 }
45
46
47 ////////////////////////////////////////////////////////////////////////////////
48 // Simulated remote execution
49 //
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 ////////////////////////////////////////////////////////////////////////////////
53
allocateSpace(size_t Size,unsigned Alignment,uint64_t & Address)54 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
55 uint64_t &Address) {
56 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
57 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
58 if (Mem.base() == NULL)
59 return true;
60 if ((uintptr_t)Mem.base() % Alignment) {
61 ErrorMsg = "unable to allocate sufficiently aligned memory";
62 return true;
63 }
64 Address = reinterpret_cast<uint64_t>(Mem.base());
65 return false;
66 }
67
loadData(uint64_t Address,const void * Data,size_t Size)68 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
69 memcpy ((void*)Address, Data, Size);
70 return false;
71 }
72
loadCode(uint64_t Address,const void * Data,size_t 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);
77 return false;
78 }
79
executeCode(uint64_t Address,int & RetVal)80 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
81 int (*fn)(void) = (int(*)(void))Address;
82 RetVal = fn();
83 return false;
84 }
85
create()86 void RemoteTarget::create() {
87 }
88
stop()89 void RemoteTarget::stop() {
90 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
91 sys::Memory::ReleaseRWX(Allocations[i]);
92 }
93