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,
38 // Ensure that enough memory is requested to allow aligning.
39 size_t NumElementsAligned = 1 + (Size + Alignment - 1)/Alignment;
40 uint8_t *Addr = (uint8_t*)calloc(NumElementsAligned, Alignment);
42 // Honour the alignment requirement.
43 uint8_t *AlignedAddr = (uint8_t*)RoundUpToAlignment((uint64_t)Addr, Alignment);
45 // Store the original address from calloc so we can free it later.
46 AllocatedDataMem.push_back(sys::MemoryBlock(Addr, NumElementsAligned*Alignment));
50 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
55 unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
57 // Look in the list of free code memory regions and use a block there if one
59 for (int i = 0, e = FreeCodeMem.size(); i != e; ++i) {
60 sys::MemoryBlock &MB = FreeCodeMem[i];
61 if (MB.size() >= NeedAllocate) {
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 FreeCodeMem[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 sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
76 AllocatedCodeMem.push_back(MB);
77 Addr = (uintptr_t)MB.base();
78 uintptr_t EndOfBlock = Addr + MB.size();
80 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
81 // The AllocateRWX may allocate much more memory than we need. In this case,
82 // we store the unused memory as a free memory block.
83 unsigned FreeSize = EndOfBlock-Addr-Size;
85 FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
87 // Return aligned address
88 return (uint8_t*)Addr;
91 void SectionMemoryManager::invalidateInstructionCache() {
92 for (int i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
93 sys::Memory::InvalidateInstructionCache(AllocatedCodeMem[i].base(),
94 AllocatedCodeMem[i].size());
97 void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
98 bool AbortOnFailure) {
99 #if defined(__linux__)
100 //===--------------------------------------------------------------------===//
101 // Function stubs that are invoked instead of certain library calls
103 // Force the following functions to be linked in to anything that uses the
104 // JIT. This is a hack designed to work around the all-too-clever Glibc
105 // strategy of making these functions work differently when inlined vs. when
106 // not inlined, and hiding their real definitions in a separate archive file
107 // that the dynamic linker can't see. For more info, search for
108 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
109 if (Name == "stat") return (void*)(intptr_t)&stat;
110 if (Name == "fstat") return (void*)(intptr_t)&fstat;
111 if (Name == "lstat") return (void*)(intptr_t)&lstat;
112 if (Name == "stat64") return (void*)(intptr_t)&stat64;
113 if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
114 if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
115 if (Name == "atexit") return (void*)(intptr_t)&atexit;
116 if (Name == "mknod") return (void*)(intptr_t)&mknod;
119 const char *NameStr = Name.c_str();
120 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
123 // If it wasn't found and if it starts with an underscore ('_') character,
124 // try again without the underscore.
125 if (NameStr[0] == '_') {
126 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
131 report_fatal_error("Program used external function '" + Name +
132 "' which could not be resolved!");
136 SectionMemoryManager::~SectionMemoryManager() {
137 for (unsigned i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
138 sys::Memory::ReleaseRWX(AllocatedCodeMem[i]);
139 for (unsigned i = 0, e = AllocatedDataMem.size(); i != e; ++i)
140 free(AllocatedDataMem[i].base());