1 //===-- SectionMemoryManager.cpp - The memory manager for MCJIT -----------===//
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 defines the implementation of the section-based memory manager
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/DynamicLibrary.h"
18 #include "SectionMemoryManager.h"
21 // These includes used by SectionMemoryManager::getPointerToNamedFunction()
22 // for Glibc trickery. Look comments in this function for more information.
23 #ifdef HAVE_SYS_STAT_H
32 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
37 uint8_t *Addr = (uint8_t*)calloc((Size + Alignment - 1)/Alignment, Alignment);
38 AllocatedDataMem.push_back(sys::MemoryBlock(Addr, Size));
42 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
47 unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
49 // Look in the list of free code memory regions and use a block there if one
51 for (int i = 0, e = FreeCodeMem.size(); i != e; ++i) {
52 sys::MemoryBlock &MB = FreeCodeMem[i];
53 if (MB.size() >= NeedAllocate) {
54 Addr = (uintptr_t)MB.base();
55 uintptr_t EndOfBlock = Addr + MB.size();
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
58 // Store cutted free memory block.
59 FreeCodeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
60 EndOfBlock - Addr - Size);
61 return (uint8_t*)Addr;
65 // No pre-allocated free block was large enough. Allocate a new memory region.
66 sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
68 AllocatedCodeMem.push_back(MB);
69 Addr = (uintptr_t)MB.base();
70 uintptr_t EndOfBlock = Addr + MB.size();
72 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
73 // The AllocateRWX may allocate much more memory than we need. In this case,
74 // we store the unused memory as a free memory block.
75 unsigned FreeSize = EndOfBlock-Addr-Size;
77 FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
79 // Return aligned address
80 return (uint8_t*)Addr;
83 void SectionMemoryManager::invalidateInstructionCache() {
84 for (int i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
85 sys::Memory::InvalidateInstructionCache(AllocatedCodeMem[i].base(),
86 AllocatedCodeMem[i].size());
89 void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
90 bool AbortOnFailure) {
91 #if defined(__linux__)
92 //===--------------------------------------------------------------------===//
93 // Function stubs that are invoked instead of certain library calls
95 // Force the following functions to be linked in to anything that uses the
96 // JIT. This is a hack designed to work around the all-too-clever Glibc
97 // strategy of making these functions work differently when inlined vs. when
98 // not inlined, and hiding their real definitions in a separate archive file
99 // that the dynamic linker can't see. For more info, search for
100 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
101 if (Name == "stat") return (void*)(intptr_t)&stat;
102 if (Name == "fstat") return (void*)(intptr_t)&fstat;
103 if (Name == "lstat") return (void*)(intptr_t)&lstat;
104 if (Name == "stat64") return (void*)(intptr_t)&stat64;
105 if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
106 if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
107 if (Name == "atexit") return (void*)(intptr_t)&atexit;
108 if (Name == "mknod") return (void*)(intptr_t)&mknod;
111 const char *NameStr = Name.c_str();
112 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
115 // If it wasn't found and if it starts with an underscore ('_') character,
116 // try again without the underscore.
117 if (NameStr[0] == '_') {
118 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
123 report_fatal_error("Program used external function '" + Name +
124 "' which could not be resolved!");
128 SectionMemoryManager::~SectionMemoryManager() {
129 for (unsigned i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
130 sys::Memory::ReleaseRWX(AllocatedCodeMem[i]);
131 for (unsigned i = 0, e = AllocatedDataMem.size(); i != e; ++i)
132 free(AllocatedDataMem[i].base());