ForwardingMemoryManager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Utilities for remote-JITing with LLI.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
  13. #define LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H
  14. #include "llvm/ExecutionEngine/Orc/EPCGenericDylibManager.h"
  15. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  16. namespace llvm {
  17. // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
  18. // memory manager.
  19. class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
  20. public:
  21. void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
  22. this->MemMgr = std::move(MemMgr);
  23. }
  24. void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {
  25. this->Resolver = std::move(Resolver);
  26. }
  27. uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  28. unsigned SectionID,
  29. StringRef SectionName) override {
  30. return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
  31. }
  32. uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  33. unsigned SectionID, StringRef SectionName,
  34. bool IsReadOnly) override {
  35. return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
  36. IsReadOnly);
  37. }
  38. void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
  39. uintptr_t RODataSize, uint32_t RODataAlign,
  40. uintptr_t RWDataSize,
  41. uint32_t RWDataAlign) override {
  42. MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
  43. RWDataSize, RWDataAlign);
  44. }
  45. bool needsToReserveAllocationSpace() override {
  46. return MemMgr->needsToReserveAllocationSpace();
  47. }
  48. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  49. size_t Size) override {
  50. MemMgr->registerEHFrames(Addr, LoadAddr, Size);
  51. }
  52. void deregisterEHFrames() override { MemMgr->deregisterEHFrames(); }
  53. bool finalizeMemory(std::string *ErrMsg = nullptr) override {
  54. return MemMgr->finalizeMemory(ErrMsg);
  55. }
  56. void notifyObjectLoaded(RuntimeDyld &RTDyld,
  57. const object::ObjectFile &Obj) override {
  58. MemMgr->notifyObjectLoaded(RTDyld, Obj);
  59. }
  60. // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
  61. using RTDyldMemoryManager::notifyObjectLoaded;
  62. JITSymbol findSymbol(const std::string &Name) override {
  63. return Resolver->findSymbol(Name);
  64. }
  65. JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
  66. return Resolver->findSymbolInLogicalDylib(Name);
  67. }
  68. private:
  69. std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
  70. std::shared_ptr<LegacyJITSymbolResolver> Resolver;
  71. };
  72. class RemoteResolver : public LegacyJITSymbolResolver {
  73. public:
  74. static Expected<std::unique_ptr<RemoteResolver>>
  75. Create(orc::ExecutorProcessControl &EPC) {
  76. auto DylibMgr =
  77. orc::EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(EPC);
  78. if (!DylibMgr)
  79. return DylibMgr.takeError();
  80. auto H = DylibMgr->open("", 0);
  81. if (!H)
  82. return H.takeError();
  83. return std::unique_ptr<RemoteResolver>(
  84. new RemoteResolver(std::move(*DylibMgr), std::move(*H)));
  85. }
  86. JITSymbol findSymbol(const std::string &Name) override {
  87. orc::RemoteSymbolLookupSet R;
  88. R.push_back({std::move(Name), false});
  89. if (auto Addrs = DylibMgr.lookup(H, R)) {
  90. if (Addrs->size() != 1)
  91. return make_error<StringError>("Unexpected remote lookup result",
  92. inconvertibleErrorCode());
  93. return JITSymbol(Addrs->front().getValue(), JITSymbolFlags::Exported);
  94. } else
  95. return Addrs.takeError();
  96. }
  97. JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
  98. return nullptr;
  99. }
  100. public:
  101. RemoteResolver(orc::EPCGenericDylibManager DylibMgr,
  102. orc::tpctypes::DylibHandle H)
  103. : DylibMgr(std::move(DylibMgr)), H(std::move(H)) {}
  104. orc::EPCGenericDylibManager DylibMgr;
  105. orc::tpctypes::DylibHandle H;
  106. };
  107. } // namespace llvm
  108. #endif // LLVM_TOOLS_LLI_FORWARDINGMEMORYMANAGER_H