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"
17 #include "llvm/Support/MathExtras.h"
19 #include "SectionMemoryManager.h"
22 // These includes used by SectionMemoryManager::getPointerToNamedFunction()
23 // for Glibc trickery. Look comments in this function for more information.
24 #ifdef HAVE_SYS_STAT_H
33 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
39 // Ensure that enough memory is requested to allow aligning.
40 size_t NumElementsAligned = 1 + (Size + Alignment - 1)/Alignment;
41 uint8_t *Addr = (uint8_t*)calloc(NumElementsAligned, Alignment);
43 // Honour the alignment requirement.
44 uint8_t *AlignedAddr = (uint8_t*)RoundUpToAlignment((uint64_t)Addr, Alignment);
46 // Store the original address from calloc so we can free it later.
47 AllocatedDataMem.push_back(sys::MemoryBlock(Addr, NumElementsAligned*Alignment));
51 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
56 unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
58 // Look in the list of free code memory regions and use a block there if one
60 for (int i = 0, e = FreeCodeMem.size(); i != e; ++i) {
61 sys::MemoryBlock &MB = FreeCodeMem[i];
62 if (MB.size() >= NeedAllocate) {
63 Addr = (uintptr_t)MB.base();
64 uintptr_t EndOfBlock = Addr + MB.size();
66 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
67 // Store cutted free memory block.
68 FreeCodeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
69 EndOfBlock - Addr - Size);
70 return (uint8_t*)Addr;
74 // No pre-allocated free block was large enough. Allocate a new memory region.
75 sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
77 AllocatedCodeMem.push_back(MB);
78 Addr = (uintptr_t)MB.base();
79 uintptr_t EndOfBlock = Addr + MB.size();
81 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
82 // The AllocateRWX may allocate much more memory than we need. In this case,
83 // we store the unused memory as a free memory block.
84 unsigned FreeSize = EndOfBlock-Addr-Size;
86 FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
88 // Return aligned address
89 return (uint8_t*)Addr;
92 void SectionMemoryManager::invalidateInstructionCache() {
93 for (int i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
94 sys::Memory::InvalidateInstructionCache(AllocatedCodeMem[i].base(),
95 AllocatedCodeMem[i].size());
98 void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
99 bool AbortOnFailure) {
100 #if defined(__linux__)
101 //===--------------------------------------------------------------------===//
102 // Function stubs that are invoked instead of certain library calls
104 // Force the following functions to be linked in to anything that uses the
105 // JIT. This is a hack designed to work around the all-too-clever Glibc
106 // strategy of making these functions work differently when inlined vs. when
107 // not inlined, and hiding their real definitions in a separate archive file
108 // that the dynamic linker can't see. For more info, search for
109 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
110 if (Name == "stat") return (void*)(intptr_t)&stat;
111 if (Name == "fstat") return (void*)(intptr_t)&fstat;
112 if (Name == "lstat") return (void*)(intptr_t)&lstat;
113 if (Name == "stat64") return (void*)(intptr_t)&stat64;
114 if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
115 if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
116 if (Name == "atexit") return (void*)(intptr_t)&atexit;
117 if (Name == "mknod") return (void*)(intptr_t)&mknod;
120 const char *NameStr = Name.c_str();
121 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
124 // If it wasn't found and if it starts with an underscore ('_') character,
125 // try again without the underscore.
126 if (NameStr[0] == '_') {
127 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
132 report_fatal_error("Program used external function '" + Name +
133 "' which could not be resolved!");
137 SectionMemoryManager::~SectionMemoryManager() {
138 for (unsigned i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
139 sys::Memory::ReleaseRWX(AllocatedCodeMem[i]);
140 for (unsigned i = 0, e = AllocatedDataMem.size(); i != e; ++i)
141 free(AllocatedDataMem[i].base());