15 #include "llvm/Config/config.h"
27 return allocateSection(RODataMem, Size, Alignment);
28 return allocateSection(RWDataMem, Size, Alignment);
35 return allocateSection(CodeMem, Size, Alignment);
38 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
44 assert(!(Alignment & (Alignment - 1)) &&
"Alignment must be a power of two.");
46 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
51 for (
int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
53 if (MB.
size() >= RequiredSize) {
54 Addr = (uintptr_t)MB.
base();
55 uintptr_t EndOfBlock = Addr + MB.
size();
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
60 EndOfBlock - Addr - Size);
61 return (uint8_t*)Addr;
88 MemGroup.AllocatedMem.push_back(MB);
89 Addr = (uintptr_t)MB.base();
90 uintptr_t EndOfBlock = Addr + MB.size();
93 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
97 unsigned FreeSize = EndOfBlock-Addr-Size;
99 MemGroup.FreeMem.push_back(sys::MemoryBlock((
void*)(Addr + Size), FreeSize));
102 return (uint8_t*)Addr;
111 CodeMem.FreeMem.clear();
114 ec = applyMemoryGroupPermissions(CodeMem,
124 RODataMem.FreeMem.clear();
127 ec = applyMemoryGroupPermissions(RODataMem,
146 error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
147 unsigned Permissions) {
149 for (
int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
162 for (
int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
164 CodeMem.AllocatedMem[i].size());
168 for (
unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
170 for (
unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
172 for (
unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
static void InvalidateInstructionCache(const void *Addr, size_t Len)
virtual void invalidateInstructionCache()
Invalidate instruction cache for code sections.
static error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
Set memory protection state.
virtual uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName)
Allocates a memory block of (at least) the given size suitable for executable code.
Memory block abstraction.
virtual bool finalizeMemory(std::string *ErrMsg=0)
Update section-specific memory permissions and other attributes.
std::string message() const
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, error_code &EC)
Allocate mapped memory.
virtual uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool isReadOnly)
Allocates a memory block of (at least) the given size suitable for executable code.
static error_code success()
virtual ~SectionMemoryManager()
static error_code releaseMappedMemory(MemoryBlock &Block)
Release mapped memory.