RemoteMemoryManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===//
  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. // This memory manager allocates local storage and keeps a record of each
  11. // allocation. Iterators are provided for all data and code allocations.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
  15. #define LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
  16. #include "RemoteTarget.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/Memory.h"
  22. #include <utility>
  23. namespace llvm {
  24. class RemoteMemoryManager : public RTDyldMemoryManager {
  25. public:
  26. // Notice that this structure takes ownership of the memory allocated.
  27. struct Allocation {
  28. Allocation() {}
  29. Allocation(sys::MemoryBlock mb, unsigned a, bool code)
  30. : MB(mb), Alignment(a), IsCode(code) {}
  31. sys::MemoryBlock MB;
  32. unsigned Alignment;
  33. bool IsCode;
  34. };
  35. private:
  36. // This vector contains Allocation objects for all sections which we have
  37. // allocated. This vector effectively owns the memory associated with the
  38. // allocations.
  39. SmallVector<Allocation, 2> AllocatedSections;
  40. // This vector contains pointers to Allocation objects for any sections we
  41. // have allocated locally but have not yet remapped for the remote target.
  42. // When we receive notification of a completed module load, we will map
  43. // these sections into the remote target.
  44. SmallVector<Allocation, 2> UnmappedSections;
  45. // This map tracks the sections we have remapped for the remote target
  46. // but have not yet copied to the target.
  47. DenseMap<uint64_t, Allocation> MappedSections;
  48. // FIXME: This is part of a work around to keep sections near one another
  49. // when MCJIT performs relocations after code emission but before
  50. // the generated code is moved to the remote target.
  51. sys::MemoryBlock Near;
  52. sys::MemoryBlock allocateSection(uintptr_t Size);
  53. RemoteTarget *Target;
  54. public:
  55. RemoteMemoryManager() : Target(nullptr) {}
  56. ~RemoteMemoryManager() override;
  57. uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  58. unsigned SectionID,
  59. StringRef SectionName) override;
  60. uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  61. unsigned SectionID, StringRef SectionName,
  62. bool IsReadOnly) override;
  63. // For now, remote symbol resolution is not support in lli. The MCJIT
  64. // interface does support this, but clients must provide their own
  65. // mechanism for finding remote symbol addresses. MCJIT will resolve
  66. // symbols from Modules it contains.
  67. uint64_t getSymbolAddress(const std::string &Name) override { return 0; }
  68. void notifyObjectLoaded(ExecutionEngine *EE,
  69. const object::ObjectFile &Obj) override;
  70. bool finalizeMemory(std::string *ErrMsg) override;
  71. // For now, remote EH frame registration isn't supported. Remote symbol
  72. // resolution is a prerequisite to supporting remote EH frame registration.
  73. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  74. size_t Size) override {}
  75. void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  76. size_t Size) override {}
  77. // This is a non-interface function used by lli
  78. void setRemoteTarget(RemoteTarget *T) { Target = T; }
  79. };
  80. } // end namespace llvm
  81. #endif