LLVM API Documentation
00001 //===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of a section-based memory manager used by 00011 // the MCJIT execution engine and RuntimeDyld. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H 00016 #define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H 00017 00018 #include "llvm/ADT/SmallVector.h" 00019 #include "llvm/ExecutionEngine/RuntimeDyld.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 #include "llvm/Support/Memory.h" 00022 00023 namespace llvm { 00024 /// This is a simple memory manager which implements the methods called by 00025 /// the RuntimeDyld class to allocate memory for section-based loading of 00026 /// objects, usually those generated by the MCJIT execution engine. 00027 /// 00028 /// This memory manager allocates all section memory as read-write. The 00029 /// RuntimeDyld will copy JITed section memory into these allocated blocks 00030 /// and perform any necessary linking and relocations. 00031 /// 00032 /// Any client using this memory manager MUST ensure that section-specific 00033 /// page permissions have been applied before attempting to execute functions 00034 /// in the JITed object. Permissions can be applied either by calling 00035 /// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory 00036 /// directly. Clients of MCJIT should call MCJIT::finalizeObject. 00037 class SectionMemoryManager : public RTDyldMemoryManager { 00038 SectionMemoryManager(const SectionMemoryManager&) LLVM_DELETED_FUNCTION; 00039 void operator=(const SectionMemoryManager&) LLVM_DELETED_FUNCTION; 00040 00041 public: 00042 SectionMemoryManager() { } 00043 virtual ~SectionMemoryManager(); 00044 00045 /// \brief Allocates a memory block of (at least) the given size suitable for 00046 /// executable code. 00047 /// 00048 /// The value of \p Alignment must be a power of two. If \p Alignment is zero 00049 /// a default alignment of 16 will be used. 00050 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 00051 unsigned SectionID, 00052 StringRef SectionName) override; 00053 00054 /// \brief Allocates a memory block of (at least) the given size suitable for 00055 /// executable code. 00056 /// 00057 /// The value of \p Alignment must be a power of two. If \p Alignment is zero 00058 /// a default alignment of 16 will be used. 00059 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 00060 unsigned SectionID, StringRef SectionName, 00061 bool isReadOnly) override; 00062 00063 /// \brief Update section-specific memory permissions and other attributes. 00064 /// 00065 /// This method is called when object loading is complete and section page 00066 /// permissions can be applied. It is up to the memory manager implementation 00067 /// to decide whether or not to act on this method. The memory manager will 00068 /// typically allocate all sections as read-write and then apply specific 00069 /// permissions when this method is called. Code sections cannot be executed 00070 /// until this function has been called. In addition, any cache coherency 00071 /// operations needed to reliably use the memory are also performed. 00072 /// 00073 /// \returns true if an error occurred, false otherwise. 00074 bool finalizeMemory(std::string *ErrMsg = nullptr) override; 00075 00076 /// \brief Invalidate instruction cache for code sections. 00077 /// 00078 /// Some platforms with separate data cache and instruction cache require 00079 /// explicit cache flush, otherwise JIT code manipulations (like resolved 00080 /// relocations) will get to the data cache but not to the instruction cache. 00081 /// 00082 /// This method is called from finalizeMemory. 00083 virtual void invalidateInstructionCache(); 00084 00085 private: 00086 struct MemoryGroup { 00087 SmallVector<sys::MemoryBlock, 16> AllocatedMem; 00088 SmallVector<sys::MemoryBlock, 16> FreeMem; 00089 sys::MemoryBlock Near; 00090 }; 00091 00092 uint8_t *allocateSection(MemoryGroup &MemGroup, uintptr_t Size, 00093 unsigned Alignment); 00094 00095 std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup, 00096 unsigned Permissions); 00097 00098 MemoryGroup CodeMem; 00099 MemoryGroup RWDataMem; 00100 MemoryGroup RODataMem; 00101 }; 00102 00103 } 00104 00105 #endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H 00106