RTDyldMemoryManager.h 6.3 KB

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