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