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