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 "llvm-c/ExecutionEngine.h" 18 #include "llvm/ADT/StringRef.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 // RuntimeDyld clients often want to handle the memory management of 31 // what gets placed where. For JIT clients, this is the subset of 32 // JITMemoryManager required for dynamic loading of binaries. 33 // 34 // FIXME: As the RuntimeDyld fills out, additional routines will be needed 35 // for the varying types of objects to be allocated. 36 class RTDyldMemoryManager { 37 RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION; 38 void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION; 39 public: RTDyldMemoryManager()40 RTDyldMemoryManager() {} 41 virtual ~RTDyldMemoryManager(); 42 43 /// Allocate a memory block of (at least) the given size suitable for 44 /// executable code. The SectionID is a unique identifier assigned by the JIT 45 /// engine, and optionally recorded by the memory manager to access a loaded 46 /// section. 47 virtual uint8_t *allocateCodeSection( 48 uintptr_t Size, unsigned Alignment, unsigned SectionID, 49 StringRef SectionName) = 0; 50 51 /// Allocate a memory block of (at least) the given size suitable for data. 52 /// The SectionID is a unique identifier assigned by the JIT engine, and 53 /// optionally recorded by the memory manager to access a loaded section. 54 virtual uint8_t *allocateDataSection( 55 uintptr_t Size, unsigned Alignment, unsigned SectionID, 56 StringRef SectionName, bool IsReadOnly) = 0; 57 58 /// Inform the memory manager about the total amount of memory required to 59 /// allocate all sections to be loaded: 60 /// \p CodeSize - the total size of all code sections 61 /// \p DataSizeRO - the total size of all read-only data sections 62 /// \p DataSizeRW - the total size of all read-write data sections 63 /// 64 /// Note that by default the callback is disabled. To enable it 65 /// redefine the method needsToReserveAllocationSpace to return true. reserveAllocationSpace(uintptr_t CodeSize,uintptr_t DataSizeRO,uintptr_t DataSizeRW)66 virtual void reserveAllocationSpace( 67 uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { } 68 69 /// Override to return true to enable the reserveAllocationSpace callback. needsToReserveAllocationSpace()70 virtual bool needsToReserveAllocationSpace() { return false; } 71 72 /// Register the EH frames with the runtime so that c++ exceptions work. 73 /// 74 /// \p Addr parameter provides the local address of the EH frame section 75 /// data, while \p LoadAddr provides the address of the data in the target 76 /// address space. If the section has not been remapped (which will usually 77 /// be the case for local execution) these two values will be the same. 78 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size); 79 80 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size); 81 82 /// This method returns the address of the specified function or variable in 83 /// the current process. 84 static uint64_t getSymbolAddressInProcess(const std::string &Name); 85 86 /// This method returns the address of the specified function or variable. 87 /// It is used to resolve symbols during module linking. getSymbolAddress(const std::string & Name)88 virtual uint64_t getSymbolAddress(const std::string &Name) { 89 return getSymbolAddressInProcess(Name); 90 } 91 92 /// This method returns the address of the specified function. As such it is 93 /// only useful for resolving library symbols, not code generated symbols. 94 /// 95 /// If \p AbortOnFailure is false and no function with the given name is 96 /// found, this function returns a null pointer. Otherwise, it prints a 97 /// message to stderr and aborts. 98 /// 99 /// This function is deprecated for memory managers to be used with 100 /// MCJIT or RuntimeDyld. Use getSymbolAddress instead. 101 virtual void *getPointerToNamedFunction(const std::string &Name, 102 bool AbortOnFailure = true); 103 104 /// This method is called after an object has been loaded into memory but 105 /// before relocations are applied to the loaded sections. The object load 106 /// may have been initiated by MCJIT to resolve an external symbol for another 107 /// object that is being finalized. In that case, the object about which 108 /// the memory manager is being notified will be finalized immediately after 109 /// the memory manager returns from this call. 110 /// 111 /// Memory managers which are preparing code for execution in an external 112 /// address space can use this call to remap the section addresses for the 113 /// newly loaded object. notifyObjectLoaded(ExecutionEngine * EE,const object::ObjectFile &)114 virtual void notifyObjectLoaded(ExecutionEngine *EE, 115 const object::ObjectFile &) {} 116 117 /// This method is called when object loading is complete and section page 118 /// permissions can be applied. It is up to the memory manager implementation 119 /// to decide whether or not to act on this method. The memory manager will 120 /// typically allocate all sections as read-write and then apply specific 121 /// permissions when this method is called. Code sections cannot be executed 122 /// until this function has been called. In addition, any cache coherency 123 /// operations needed to reliably use the memory are also performed. 124 /// 125 /// Returns true if an error occurred, false otherwise. 126 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0; 127 }; 128 129 // Create wrappers for C Binding types (see CBindingWrapping.h). 130 DEFINE_SIMPLE_CONVERSION_FUNCTIONS( 131 RTDyldMemoryManager, LLVMMCJITMemoryManagerRef) 132 133 } // namespace llvm 134 135 #endif 136