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/ADT/StringRef.h" 20#include "llvm/Support/CBindingWrapping.h" 21#include "llvm/Support/Memory.h" 22 23namespace llvm { 24 25class ExecutionEngine; 26 27 namespace object { 28 class ObjectFile; 29 } 30 31class MCJITMemoryManager : public RuntimeDyld::MemoryManager { 32public: 33 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. 47 virtual void notifyObjectLoaded(ExecutionEngine *EE, 48 const object::ObjectFile &) {} 49}; 50 51// RuntimeDyld clients often want to handle the memory management of 52// what gets placed where. For JIT clients, this is the subset of 53// JITMemoryManager required for dynamic loading of binaries. 54// 55// FIXME: As the RuntimeDyld fills out, additional routines will be needed 56// for the varying types of objects to be allocated. 57class RTDyldMemoryManager : public MCJITMemoryManager, 58 public RuntimeDyld::SymbolResolver { 59 RTDyldMemoryManager(const RTDyldMemoryManager&) = delete; 60 void operator=(const RTDyldMemoryManager&) = delete; 61public: 62 RTDyldMemoryManager() {} 63 ~RTDyldMemoryManager() override; 64 65 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override; 66 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override; 67 68 /// This method returns the address of the specified function or variable in 69 /// the current process. 70 static uint64_t getSymbolAddressInProcess(const std::string &Name); 71 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 80 /// This method returns a RuntimeDyld::SymbolInfo for the specified function 81 /// or variable. It is used to resolve symbols during module linking. 82 /// 83 /// By default this falls back on the legacy lookup method: 84 /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as 85 /// a strong, exported symbol, consistent with historical treatment by 86 /// RuntimeDyld. 87 /// 88 /// Clients writing custom RTDyldMemoryManagers are encouraged to override 89 /// this method and return a SymbolInfo with the flags set correctly. This is 90 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols. 91 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override { 92 return RuntimeDyld::SymbolInfo(getSymbolAddress(Name), 93 JITSymbolFlags::Exported); 94 } 95 96 /// Legacy symbol lookup -- DEPRECATED! Please override 97 /// findSymbolInLogicalDylib instead. 98 /// 99 /// Default to treating all modules as separate. 100 virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) { 101 return 0; 102 } 103 104 /// Default to treating all modules as separate. 105 /// 106 /// By default this falls back on the legacy lookup method: 107 /// 'getSymbolAddressInLogicalDylib'. The address returned by 108 /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol, 109 /// consistent with historical treatment by RuntimeDyld. 110 /// 111 /// Clients writing custom RTDyldMemoryManagers are encouraged to override 112 /// this method and return a SymbolInfo with the flags set correctly. This is 113 /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols. 114 RuntimeDyld::SymbolInfo 115 findSymbolInLogicalDylib(const std::string &Name) override { 116 return RuntimeDyld::SymbolInfo(getSymbolAddressInLogicalDylib(Name), 117 JITSymbolFlags::Exported); 118 } 119 120 /// This method returns the address of the specified function. As such it is 121 /// only useful for resolving library symbols, not code generated symbols. 122 /// 123 /// If \p AbortOnFailure is false and no function with the given name is 124 /// found, this function returns a null pointer. Otherwise, it prints a 125 /// message to stderr and aborts. 126 /// 127 /// This function is deprecated for memory managers to be used with 128 /// MCJIT or RuntimeDyld. Use getSymbolAddress instead. 129 virtual void *getPointerToNamedFunction(const std::string &Name, 130 bool AbortOnFailure = true); 131}; 132 133// Create wrappers for C Binding types (see CBindingWrapping.h). 134DEFINE_SIMPLE_CONVERSION_FUNCTIONS( 135 RTDyldMemoryManager, LLVMMCJITMemoryManagerRef) 136 137} // namespace llvm 138 139 140#endif 141