RTDyldMemoryManager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- 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. // Interface of the runtime dynamic memory manager base class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
  13. #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
  14. #include "llvm-c/ExecutionEngine.h"
  15. #include "llvm/ExecutionEngine/JITSymbol.h"
  16. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  17. #include "llvm/Support/CBindingWrapping.h"
  18. #include <cstddef>
  19. #include <cstdint>
  20. #include <string>
  21. namespace llvm {
  22. class ExecutionEngine;
  23. namespace object {
  24. class ObjectFile;
  25. } // end namespace object
  26. class MCJITMemoryManager : public RuntimeDyld::MemoryManager {
  27. public:
  28. // Don't hide the notifyObjectLoaded method from RuntimeDyld::MemoryManager.
  29. using RuntimeDyld::MemoryManager::notifyObjectLoaded;
  30. /// This method is called after an object has been loaded into memory but
  31. /// before relocations are applied to the loaded sections. The object load
  32. /// may have been initiated by MCJIT to resolve an external symbol for another
  33. /// object that is being finalized. In that case, the object about which
  34. /// the memory manager is being notified will be finalized immediately after
  35. /// the memory manager returns from this call.
  36. ///
  37. /// Memory managers which are preparing code for execution in an external
  38. /// address space can use this call to remap the section addresses for the
  39. /// newly loaded object.
  40. virtual void notifyObjectLoaded(ExecutionEngine *EE,
  41. const object::ObjectFile &) {}
  42. private:
  43. void anchor() override;
  44. };
  45. // RuntimeDyld clients often want to handle the memory management of
  46. // what gets placed where. For JIT clients, this is the subset of
  47. // JITMemoryManager required for dynamic loading of binaries.
  48. //
  49. // FIXME: As the RuntimeDyld fills out, additional routines will be needed
  50. // for the varying types of objects to be allocated.
  51. class RTDyldMemoryManager : public MCJITMemoryManager,
  52. public LegacyJITSymbolResolver {
  53. public:
  54. RTDyldMemoryManager() = default;
  55. RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
  56. void operator=(const RTDyldMemoryManager&) = delete;
  57. ~RTDyldMemoryManager() override;
  58. /// Register EH frames in the current process.
  59. static void registerEHFramesInProcess(uint8_t *Addr, size_t Size);
  60. /// Deregister EH frames in the current proces.
  61. static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
  62. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
  63. void deregisterEHFrames() override;
  64. /// This method returns the address of the specified function or variable in
  65. /// the current process.
  66. static uint64_t getSymbolAddressInProcess(const std::string &Name);
  67. /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
  68. ///
  69. /// This method returns the address of the specified function or variable.
  70. /// It is used to resolve symbols during module linking.
  71. virtual uint64_t getSymbolAddress(const std::string &Name) {
  72. return getSymbolAddressInProcess(Name);
  73. }
  74. /// This method returns a RuntimeDyld::SymbolInfo for the specified function
  75. /// or variable. It is used to resolve symbols during module linking.
  76. ///
  77. /// By default this falls back on the legacy lookup method:
  78. /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
  79. /// a strong, exported symbol, consistent with historical treatment by
  80. /// RuntimeDyld.
  81. ///
  82. /// Clients writing custom RTDyldMemoryManagers are encouraged to override
  83. /// this method and return a SymbolInfo with the flags set correctly. This is
  84. /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
  85. JITSymbol findSymbol(const std::string &Name) override {
  86. return JITSymbol(getSymbolAddress(Name), JITSymbolFlags::Exported);
  87. }
  88. /// Legacy symbol lookup -- DEPRECATED! Please override
  89. /// findSymbolInLogicalDylib instead.
  90. ///
  91. /// Default to treating all modules as separate.
  92. virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
  93. return 0;
  94. }
  95. /// Default to treating all modules as separate.
  96. ///
  97. /// By default this falls back on the legacy lookup method:
  98. /// 'getSymbolAddressInLogicalDylib'. The address returned by
  99. /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
  100. /// consistent with historical treatment by RuntimeDyld.
  101. ///
  102. /// Clients writing custom RTDyldMemoryManagers are encouraged to override
  103. /// this method and return a SymbolInfo with the flags set correctly. This is
  104. /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
  105. JITSymbol
  106. findSymbolInLogicalDylib(const std::string &Name) override {
  107. return JITSymbol(getSymbolAddressInLogicalDylib(Name),
  108. JITSymbolFlags::Exported);
  109. }
  110. /// This method returns the address of the specified function. As such it is
  111. /// only useful for resolving library symbols, not code generated symbols.
  112. ///
  113. /// If \p AbortOnFailure is false and no function with the given name is
  114. /// found, this function returns a null pointer. Otherwise, it prints a
  115. /// message to stderr and aborts.
  116. ///
  117. /// This function is deprecated for memory managers to be used with
  118. /// MCJIT or RuntimeDyld. Use getSymbolAddress instead.
  119. virtual void *getPointerToNamedFunction(const std::string &Name,
  120. bool AbortOnFailure = true);
  121. protected:
  122. struct EHFrame {
  123. uint8_t *Addr;
  124. size_t Size;
  125. };
  126. typedef std::vector<EHFrame> EHFrameInfos;
  127. EHFrameInfos EHFrames;
  128. private:
  129. void anchor() override;
  130. };
  131. // Create wrappers for C Binding types (see CBindingWrapping.h).
  132. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
  133. RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
  134. } // end namespace llvm
  135. #endif // LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H