RemoteJITUtils.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_REMOTEJITUTILS_H
  13. #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
  14. #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
  15. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  16. #include <mutex>
  17. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  18. #include <unistd.h>
  19. #else
  20. #include <io.h>
  21. #endif
  22. // launch the remote process (see lli.cpp) and return a channel to it.
  23. std::unique_ptr<llvm::orc::shared::FDRawByteChannel> launchRemote();
  24. namespace llvm {
  25. // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
  26. // memory manager.
  27. class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
  28. public:
  29. void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
  30. this->MemMgr = std::move(MemMgr);
  31. }
  32. void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {
  33. this->Resolver = std::move(Resolver);
  34. }
  35. uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  36. unsigned SectionID,
  37. StringRef SectionName) override {
  38. return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
  39. }
  40. uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  41. unsigned SectionID, StringRef SectionName,
  42. bool IsReadOnly) override {
  43. return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
  44. IsReadOnly);
  45. }
  46. void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
  47. uintptr_t RODataSize, uint32_t RODataAlign,
  48. uintptr_t RWDataSize,
  49. uint32_t RWDataAlign) override {
  50. MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
  51. RWDataSize, RWDataAlign);
  52. }
  53. bool needsToReserveAllocationSpace() override {
  54. return MemMgr->needsToReserveAllocationSpace();
  55. }
  56. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  57. size_t Size) override {
  58. MemMgr->registerEHFrames(Addr, LoadAddr, Size);
  59. }
  60. void deregisterEHFrames() override {
  61. MemMgr->deregisterEHFrames();
  62. }
  63. bool finalizeMemory(std::string *ErrMsg = nullptr) override {
  64. return MemMgr->finalizeMemory(ErrMsg);
  65. }
  66. void notifyObjectLoaded(RuntimeDyld &RTDyld,
  67. const object::ObjectFile &Obj) override {
  68. MemMgr->notifyObjectLoaded(RTDyld, Obj);
  69. }
  70. // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
  71. using RTDyldMemoryManager::notifyObjectLoaded;
  72. JITSymbol findSymbol(const std::string &Name) override {
  73. return Resolver->findSymbol(Name);
  74. }
  75. JITSymbol
  76. findSymbolInLogicalDylib(const std::string &Name) override {
  77. return Resolver->findSymbolInLogicalDylib(Name);
  78. }
  79. private:
  80. std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
  81. std::shared_ptr<LegacyJITSymbolResolver> Resolver;
  82. };
  83. template <typename RemoteT>
  84. class RemoteResolver : public LegacyJITSymbolResolver {
  85. public:
  86. RemoteResolver(RemoteT &R) : R(R) {}
  87. JITSymbol findSymbol(const std::string &Name) override {
  88. if (auto Addr = R.getSymbolAddress(Name))
  89. return JITSymbol(*Addr, JITSymbolFlags::Exported);
  90. else
  91. return Addr.takeError();
  92. }
  93. JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
  94. return nullptr;
  95. }
  96. public:
  97. RemoteT &R;
  98. };
  99. }
  100. #endif