1 //===- RemoteTarget.h - 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 // Definition of the RemoteTarget class which executes JITed code in a
11 // separate address range from where it was built.
13 //===----------------------------------------------------------------------===//
15 #ifndef REMOTEPROCESS_H
16 #define REMOTEPROCESS_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/Memory.h"
30 typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;
31 AllocMapType Allocations;
37 StringRef getErrorMsg() const { return ErrorMsg; }
39 /// Allocate space in the remote target address space.
41 /// @param Size Amount of space, in bytes, to allocate.
42 /// @param Alignment Required minimum alignment for allocated space.
43 /// @param[out] Address Remote address of the allocated memory.
45 /// @returns True on success. On failure, ErrorMsg is updated with
46 /// descriptive text of the encountered error.
47 virtual bool allocateSpace(size_t Size,
51 bool isAllocatedMemory(uint64_t Address, uint32_t Size) {
52 uint64_t AddressEnd = Address + Size;
53 for (AllocMapType::const_iterator I = Allocations.begin(),
54 E = Allocations.end();
56 if (Address >= (uint64_t)I->base() &&
57 AddressEnd <= (uint64_t)I->base() + I->size())
63 /// Load data into the target address space.
65 /// @param Address Destination address in the target process.
66 /// @param Data Source address in the host process.
67 /// @param Size Number of bytes to copy.
69 /// @returns True on success. On failure, ErrorMsg is updated with
70 /// descriptive text of the encountered error.
71 virtual bool loadData(uint64_t Address,
75 /// Load code into the target address space and prepare it for execution.
77 /// @param Address Destination address in the target process.
78 /// @param Data Source address in the host process.
79 /// @param Size Number of bytes to copy.
81 /// @returns True on success. On failure, ErrorMsg is updated with
82 /// descriptive text of the encountered error.
83 virtual bool loadCode(uint64_t Address,
87 /// Execute code in the target process. The called function is required
88 /// to be of signature int "(*)(void)".
90 /// @param Address Address of the loaded function in the target
92 /// @param[out] RetVal The integer return value of the called function.
94 /// @returns True on success. On failure, ErrorMsg is updated with
95 /// descriptive text of the encountered error.
96 virtual bool executeCode(uint64_t Address,
99 /// Minimum alignment for memory permissions. Used to separate code and
100 /// data regions to make sure data doesn't get marked as code or vice
103 /// @returns Page alignment return value. Default of 4k.
104 virtual unsigned getPageAlignment() { return 4096; }
106 /// Start the remote process.
107 virtual bool create();
109 /// Terminate the remote process.
112 RemoteTarget() : IsRunning(false), ErrorMsg("") {}
113 virtual ~RemoteTarget() { if (IsRunning) stop(); }
115 // Main processing function for the remote target process. Command messages
116 // are received on file descriptor CmdFD and responses come back on OutFD.
117 static void doRemoteTargeting(int CmdFD, int OutFD);
120 } // end namespace llvm