1 //===- RemoteTarget.cpp - LLVM Remote process JIT execution -----*- C++ -*-===//
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 "llvm/ADT/StringRef.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Memory.h"
19 #include <stdlib.h>
20 #include <string>
21
22 using namespace llvm;
23
24 ////////////////////////////////////////////////////////////////////////////////
25 // Simulated remote execution
26 //
27 // This implementation will simply move generated code and data to a new memory
28 // location in the current executable and let it run from there.
29 ////////////////////////////////////////////////////////////////////////////////
30
allocateSpace(size_t Size,unsigned Alignment,uint64_t & Address)31 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
32 uint64_t &Address) {
33 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : nullptr;
34 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
35 if (Mem.base() == nullptr)
36 return false;
37 if ((uintptr_t)Mem.base() % Alignment) {
38 ErrorMsg = "unable to allocate sufficiently aligned memory";
39 return false;
40 }
41 Address = reinterpret_cast<uint64_t>(Mem.base());
42 Allocations.push_back(Mem);
43 return true;
44 }
45
loadData(uint64_t Address,const void * Data,size_t Size)46 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
47 memcpy ((void*)Address, Data, Size);
48 return true;
49 }
50
loadCode(uint64_t Address,const void * Data,size_t Size)51 bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
52 memcpy ((void*)Address, Data, Size);
53 sys::MemoryBlock Mem((void*)Address, Size);
54 sys::Memory::setExecutable(Mem, &ErrorMsg);
55 return true;
56 }
57
executeCode(uint64_t Address,int & RetVal)58 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
59 int (*fn)() = (int(*)())Address;
60 RetVal = fn();
61 return true;
62 }
63
create()64 bool RemoteTarget::create() {
65 return true;
66 }
67
stop()68 void RemoteTarget::stop() {
69 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
70 sys::Memory::ReleaseRWX(Allocations[i]);
71 }
72