blob: 212bdfda1cdbea83bc05595c40efe57fcbe8d4ed [file] [log] [blame]
Jim Grosbach706f03a2012-09-05 16:50:34 +00001//===- 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"
Craig Topperdbf54572012-09-15 18:45:38 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/DataTypes.h"
18#include "llvm/Support/Memory.h"
Jim Grosbach706f03a2012-09-05 16:50:34 +000019#include <stdlib.h>
20#include <string>
21using namespace llvm;
22
23bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
24 uint64_t &Address) {
25 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
26 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
27 if (Mem.base() == NULL)
28 return true;
29 if ((uintptr_t)Mem.base() % Alignment) {
30 ErrorMsg = "unable to allocate sufficiently aligned memory";
31 return true;
32 }
33 Address = reinterpret_cast<uint64_t>(Mem.base());
34 return false;
35}
36
37bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
38 memcpy ((void*)Address, Data, Size);
Jim Grosbach706f03a2012-09-05 16:50:34 +000039 return false;
40}
41
42bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
43 memcpy ((void*)Address, Data, Size);
Andrew Kaylor29322842012-10-31 20:37:14 +000044 sys::MemoryBlock Mem((void*)Address, Size);
45 sys::Memory::setExecutable(Mem, &ErrorMsg);
Jim Grosbach706f03a2012-09-05 16:50:34 +000046 return false;
47}
48
49bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
50 int (*fn)(void) = (int(*)(void))Address;
51 RetVal = fn();
52 return false;
53}
54
55void RemoteTarget::create() {
56}
57
58void RemoteTarget::stop() {
59 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
60 sys::Memory::ReleaseRWX(Allocations[i]);
61}