EPCGenericRTDyldMemoryManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- EPCGenericRTDyldMemoryManager.h - EPC-based MemMgr ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Defines a RuntimeDyld::MemoryManager that uses EPC and the ORC runtime
  15. // bootstrap functions.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
  19. #define LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
  20. #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
  21. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  22. #define DEBUG_TYPE "orc"
  23. namespace llvm {
  24. namespace orc {
  25. /// Remote-mapped RuntimeDyld-compatible memory manager.
  26. class EPCGenericRTDyldMemoryManager : public RuntimeDyld::MemoryManager {
  27. public:
  28. /// Symbol addresses for memory access.
  29. struct SymbolAddrs {
  30. ExecutorAddr Instance;
  31. ExecutorAddr Reserve;
  32. ExecutorAddr Finalize;
  33. ExecutorAddr Deallocate;
  34. ExecutorAddr RegisterEHFrame;
  35. ExecutorAddr DeregisterEHFrame;
  36. };
  37. /// Create an EPCGenericRTDyldMemoryManager using the given EPC, looking up
  38. /// the default symbol names in the bootstrap symbol set.
  39. static Expected<std::unique_ptr<EPCGenericRTDyldMemoryManager>>
  40. CreateWithDefaultBootstrapSymbols(ExecutorProcessControl &EPC);
  41. /// Create an EPCGenericRTDyldMemoryManager using the given EPC and symbol
  42. /// addrs.
  43. EPCGenericRTDyldMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs);
  44. EPCGenericRTDyldMemoryManager(const EPCGenericRTDyldMemoryManager &) = delete;
  45. EPCGenericRTDyldMemoryManager &
  46. operator=(const EPCGenericRTDyldMemoryManager &) = delete;
  47. EPCGenericRTDyldMemoryManager(EPCGenericRTDyldMemoryManager &&) = delete;
  48. EPCGenericRTDyldMemoryManager &
  49. operator=(EPCGenericRTDyldMemoryManager &&) = delete;
  50. ~EPCGenericRTDyldMemoryManager();
  51. uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  52. unsigned SectionID,
  53. StringRef SectionName) override;
  54. uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  55. unsigned SectionID, StringRef SectionName,
  56. bool IsReadOnly) override;
  57. void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
  58. uintptr_t RODataSize, Align RODataAlign,
  59. uintptr_t RWDataSize, Align RWDataAlign) override;
  60. bool needsToReserveAllocationSpace() override;
  61. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
  62. void deregisterEHFrames() override;
  63. void notifyObjectLoaded(RuntimeDyld &Dyld,
  64. const object::ObjectFile &Obj) override;
  65. bool finalizeMemory(std::string *ErrMsg = nullptr) override;
  66. private:
  67. struct SectionAlloc {
  68. public:
  69. SectionAlloc(uint64_t Size, unsigned Align)
  70. : Size(Size), Align(Align),
  71. Contents(std::make_unique<uint8_t[]>(Size + Align - 1)) {}
  72. uint64_t Size;
  73. unsigned Align;
  74. std::unique_ptr<uint8_t[]> Contents;
  75. ExecutorAddr RemoteAddr;
  76. };
  77. // Group of section allocations to be allocated together in the executor. The
  78. // RemoteCodeAddr will stand in as the id of the group for deallocation
  79. // purposes.
  80. struct SectionAllocGroup {
  81. SectionAllocGroup() = default;
  82. SectionAllocGroup(const SectionAllocGroup &) = delete;
  83. SectionAllocGroup &operator=(const SectionAllocGroup &) = delete;
  84. SectionAllocGroup(SectionAllocGroup &&) = default;
  85. SectionAllocGroup &operator=(SectionAllocGroup &&) = default;
  86. ExecutorAddrRange RemoteCode;
  87. ExecutorAddrRange RemoteROData;
  88. ExecutorAddrRange RemoteRWData;
  89. std::vector<ExecutorAddrRange> UnfinalizedEHFrames;
  90. std::vector<SectionAlloc> CodeAllocs, RODataAllocs, RWDataAllocs;
  91. };
  92. // Maps all allocations in SectionAllocs to aligned blocks
  93. void mapAllocsToRemoteAddrs(RuntimeDyld &Dyld,
  94. std::vector<SectionAlloc> &SecAllocs,
  95. ExecutorAddr NextAddr);
  96. ExecutorProcessControl &EPC;
  97. SymbolAddrs SAs;
  98. std::mutex M;
  99. std::vector<SectionAllocGroup> Unmapped;
  100. std::vector<SectionAllocGroup> Unfinalized;
  101. std::vector<ExecutorAddr> FinalizedAllocs;
  102. std::string ErrMsg;
  103. };
  104. } // end namespace orc
  105. } // end namespace llvm
  106. #undef DEBUG_TYPE
  107. #endif // LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
  108. #ifdef __GNUC__
  109. #pragma GCC diagnostic pop
  110. #endif