RTDyldMemoryManager.h 5.5 KB

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