1 //===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of a section-based memory manager used by
11 // the MCJIT execution engine and RuntimeDyld.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
16 #define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
21 #include "llvm/Support/Memory.h"
24 #include <system_error>
28 /// This is a simple memory manager which implements the methods called by
29 /// the RuntimeDyld class to allocate memory for section-based loading of
30 /// objects, usually those generated by the MCJIT execution engine.
32 /// This memory manager allocates all section memory as read-write. The
33 /// RuntimeDyld will copy JITed section memory into these allocated blocks
34 /// and perform any necessary linking and relocations.
36 /// Any client using this memory manager MUST ensure that section-specific
37 /// page permissions have been applied before attempting to execute functions
38 /// in the JITed object. Permissions can be applied either by calling
39 /// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
40 /// directly. Clients of MCJIT should call MCJIT::finalizeObject.
41 class SectionMemoryManager : public RTDyldMemoryManager {
43 SectionMemoryManager() = default;
44 SectionMemoryManager(const SectionMemoryManager&) = delete;
45 void operator=(const SectionMemoryManager&) = delete;
46 ~SectionMemoryManager() override;
48 /// \brief Allocates a memory block of (at least) the given size suitable for
51 /// The value of \p Alignment must be a power of two. If \p Alignment is zero
52 /// a default alignment of 16 will be used.
53 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
55 StringRef SectionName) override;
57 /// \brief Allocates a memory block of (at least) the given size suitable for
60 /// The value of \p Alignment must be a power of two. If \p Alignment is zero
61 /// a default alignment of 16 will be used.
62 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
63 unsigned SectionID, StringRef SectionName,
64 bool isReadOnly) override;
66 /// \brief Update section-specific memory permissions and other attributes.
68 /// This method is called when object loading is complete and section page
69 /// permissions can be applied. It is up to the memory manager implementation
70 /// to decide whether or not to act on this method. The memory manager will
71 /// typically allocate all sections as read-write and then apply specific
72 /// permissions when this method is called. Code sections cannot be executed
73 /// until this function has been called. In addition, any cache coherency
74 /// operations needed to reliably use the memory are also performed.
76 /// \returns true if an error occurred, false otherwise.
77 bool finalizeMemory(std::string *ErrMsg = nullptr) override;
79 /// \brief Invalidate instruction cache for code sections.
81 /// Some platforms with separate data cache and instruction cache require
82 /// explicit cache flush, otherwise JIT code manipulations (like resolved
83 /// relocations) will get to the data cache but not to the instruction cache.
85 /// This method is called from finalizeMemory.
86 virtual void invalidateInstructionCache();
90 // The actual block of free memory
91 sys::MemoryBlock Free;
92 // If there is a pending allocation from the same reservation right before
93 // this block, store it's index in PendingMem, to be able to update the
94 // pending region if part of this block is allocated, rather than having to
96 unsigned PendingPrefixIndex;
100 // PendingMem contains all blocks of memory (subblocks of AllocatedMem)
101 // which have not yet had their permissions applied, but have been given
102 // out to the user. FreeMem contains all block of memory, which have
103 // neither had their permissions applied, nor been given out to the user.
104 SmallVector<sys::MemoryBlock, 16> PendingMem;
105 SmallVector<FreeMemBlock, 16> FreeMem;
107 // All memory blocks that have been requested from the system
108 SmallVector<sys::MemoryBlock, 16> AllocatedMem;
110 sys::MemoryBlock Near;
113 uint8_t *allocateSection(MemoryGroup &MemGroup, uintptr_t Size,
116 std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
117 unsigned Permissions);
120 MemoryGroup RWDataMem;
121 MemoryGroup RODataMem;
124 } // end namespace llvm
126 #endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H