RemoteTarget.cpp 2.3 KB

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