1 //===- SectionMemoryManager.cpp - 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 implements the section-based memory manager used by the MCJIT
11 // execution engine and RuntimeDyld
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
17 #include "llvm/Support/MathExtras.h"
20 // These includes used by SectionMemoryManager::getPointerToNamedFunction()
21 // for Glibc trickery. See comments in this function for more information.
22 #ifdef HAVE_SYS_STAT_H
31 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
36 return allocateSection(RODataMem, Size, Alignment);
37 return allocateSection(RWDataMem, Size, Alignment);
40 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
43 return allocateSection(CodeMem, Size, Alignment);
46 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
52 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
54 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
57 // Look in the list of free memory regions and use a block there if one
59 for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
60 sys::MemoryBlock &MB = MemGroup.FreeMem[i];
61 if (MB.size() >= RequiredSize) {
62 Addr = (uintptr_t)MB.base();
63 uintptr_t EndOfBlock = Addr + MB.size();
65 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
66 // Store cutted free memory block.
67 MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
68 EndOfBlock - Addr - Size);
69 return (uint8_t*)Addr;
73 // No pre-allocated free block was large enough. Allocate a new memory region.
74 // Note that all sections get allocated as read-write. The permissions will
75 // be updated later based on memory group.
77 // FIXME: It would be useful to define a default allocation size (or add
78 // it as a constructor parameter) to minimize the number of allocations.
80 // FIXME: Initialize the Near member for each memory group to avoid
83 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
85 sys::Memory::MF_READ |
86 sys::Memory::MF_WRITE,
89 // FIXME: Add error propogation to the interface.
93 // Save this address as the basis for our next request
96 MemGroup.AllocatedMem.push_back(MB);
97 Addr = (uintptr_t)MB.base();
98 uintptr_t EndOfBlock = Addr + MB.size();
100 // Align the address.
101 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
103 // The allocateMappedMemory may allocate much more memory than we need. In
104 // this case, we store the unused memory as a free memory block.
105 unsigned FreeSize = EndOfBlock-Addr-Size;
107 MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
109 // Return aligned address
110 return (uint8_t*)Addr;
113 bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
115 // FIXME: Should in-progress permissions be reverted if an error occurs?
118 // Make code memory executable.
119 ec = applyMemoryGroupPermissions(CodeMem,
120 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
123 *ErrMsg = ec.message();
128 // Make read-only data memory read-only.
129 ec = applyMemoryGroupPermissions(RODataMem,
130 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
133 *ErrMsg = ec.message();
138 // Read-write data memory already has the correct permissions
140 // Some platforms with separate data cache and instruction cache require
141 // explicit cache flush, otherwise JIT code manipulations (like resolved
142 // relocations) will get to the data cache but not to the instruction cache.
143 invalidateInstructionCache();
148 error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
149 unsigned Permissions) {
151 for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
153 ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
160 return error_code::success();
163 void SectionMemoryManager::invalidateInstructionCache() {
164 for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
165 sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
166 CodeMem.AllocatedMem[i].size());
169 SectionMemoryManager::~SectionMemoryManager() {
170 for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
171 sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
172 for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
173 sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
174 for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
175 sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);