blob: da931247b6cac1aca9424dc679547ba63c363abb [file] [log] [blame]
Andrew Kaylorcc7773b2012-11-27 19:00:17 +00001//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
Andrew Kaylor2d6d5852012-10-04 20:29:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000010// This file implements the section-based memory manager used by the MCJIT
11// execution engine and RuntimeDyld
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Config/config.h"
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000016#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000017#include "llvm/Support/DynamicLibrary.h"
Amara Emersona25ad192012-10-31 17:41:51 +000018#include "llvm/Support/MathExtras.h"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000019
20#ifdef __linux__
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000021 // These includes used by SectionMemoryManager::getPointerToNamedFunction()
22 // for Glibc trickery. See comments in this function for more information.
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26 #include <fcntl.h>
27 #include <unistd.h>
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000028#endif
29
30namespace llvm {
31
32uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
33 unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +000034 unsigned SectionID,
35 bool IsReadOnly) {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000036 if (IsReadOnly)
37 return allocateSection(RODataMem, Size, Alignment);
38 return allocateSection(RWDataMem, Size, Alignment);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000039}
40
41uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000042 unsigned Alignment,
43 unsigned SectionID) {
44 return allocateSection(CodeMem, Size, Alignment);
45}
46
47uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
48 uintptr_t Size,
49 unsigned Alignment) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000050 if (!Alignment)
51 Alignment = 16;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000052
53 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
54
55 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000056 uintptr_t Addr = 0;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000057
58 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000059 // is available.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000060 for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
61 sys::MemoryBlock &MB = MemGroup.FreeMem[i];
62 if (MB.size() >= RequiredSize) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000063 Addr = (uintptr_t)MB.base();
64 uintptr_t EndOfBlock = Addr + MB.size();
65 // Align the address.
66 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
67 // Store cutted free memory block.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000068 MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
69 EndOfBlock - Addr - Size);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000070 return (uint8_t*)Addr;
71 }
72 }
73
74 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000075 // Note that all sections get allocated as read-write. The permissions will
76 // be updated later based on memory group.
77 //
78 // FIXME: It would be useful to define a default allocation size (or add
79 // it as a constructor parameter) to minimize the number of allocations.
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000080 //
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000081 // FIXME: Initialize the Near member for each memory group to avoid
82 // interleaving.
83 error_code ec;
84 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
85 &MemGroup.Near,
86 sys::Memory::MF_READ |
87 sys::Memory::MF_WRITE,
88 ec);
89 if (ec) {
90 // FIXME: Add error propogation to the interface.
91 return NULL;
92 }
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000093
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000094 // Save this address as the basis for our next request
95 MemGroup.Near = MB;
96
97 MemGroup.AllocatedMem.push_back(MB);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000098 Addr = (uintptr_t)MB.base();
99 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000100
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000101 // Align the address.
102 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000103
104 // The allocateMappedMemory may allocate much more memory than we need. In
105 // this case, we store the unused memory as a free memory block.
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000106 unsigned FreeSize = EndOfBlock-Addr-Size;
107 if (FreeSize > 16)
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000108 MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000109
110 // Return aligned address
111 return (uint8_t*)Addr;
112}
113
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000114bool SectionMemoryManager::applyPermissions(std::string *ErrMsg)
115{
116 // FIXME: Should in-progress permissions be reverted if an error occurs?
117 error_code ec;
118
119 // Make code memory executable.
120 ec = applyMemoryGroupPermissions(CodeMem,
121 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
122 if (ec) {
123 if (ErrMsg) {
124 *ErrMsg = ec.message();
125 }
126 return true;
127 }
128
129 // Make read-only data memory read-only.
130 ec = applyMemoryGroupPermissions(RODataMem,
131 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
132 if (ec) {
133 if (ErrMsg) {
134 *ErrMsg = ec.message();
135 }
136 return true;
137 }
138
139 // Read-write data memory already has the correct permissions
140
Andrew Kaylor034f4be2013-04-24 22:39:12 +0000141 // Some platforms with separate data cache and instruction cache require
142 // explicit cache flush, otherwise JIT code manipulations (like resolved
143 // relocations) will get to the data cache but not to the instruction cache.
144 invalidateInstructionCache();
145
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000146 return false;
147}
148
149error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
150 unsigned Permissions) {
151
152 for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
153 error_code ec;
154 ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
155 Permissions);
156 if (ec) {
157 return ec;
158 }
159 }
160
161 return error_code::success();
162}
163
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000164void SectionMemoryManager::invalidateInstructionCache() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000165 for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
166 sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
167 CodeMem.AllocatedMem[i].size());
168}
169
170static int jit_noop() {
171 return 0;
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000172}
173
174void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
175 bool AbortOnFailure) {
176#if defined(__linux__)
177 //===--------------------------------------------------------------------===//
178 // Function stubs that are invoked instead of certain library calls
179 //
180 // Force the following functions to be linked in to anything that uses the
181 // JIT. This is a hack designed to work around the all-too-clever Glibc
182 // strategy of making these functions work differently when inlined vs. when
183 // not inlined, and hiding their real definitions in a separate archive file
184 // that the dynamic linker can't see. For more info, search for
185 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
186 if (Name == "stat") return (void*)(intptr_t)&stat;
187 if (Name == "fstat") return (void*)(intptr_t)&fstat;
188 if (Name == "lstat") return (void*)(intptr_t)&lstat;
189 if (Name == "stat64") return (void*)(intptr_t)&stat64;
190 if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
191 if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
192 if (Name == "atexit") return (void*)(intptr_t)&atexit;
193 if (Name == "mknod") return (void*)(intptr_t)&mknod;
194#endif // __linux__
195
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000196 // We should not invoke parent's ctors/dtors from generated main()!
197 // On Mingw and Cygwin, the symbol __main is resolved to
198 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
199 // (and register wrong callee's dtors with atexit(3)).
200 // We expect ExecutionEngine::runStaticConstructorsDestructors()
201 // is called before ExecutionEngine::runFunctionAsMain() is called.
202 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
203
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000204 const char *NameStr = Name.c_str();
205 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
206 if (Ptr) return Ptr;
207
208 // If it wasn't found and if it starts with an underscore ('_') character,
209 // try again without the underscore.
210 if (NameStr[0] == '_') {
211 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
212 if (Ptr) return Ptr;
213 }
214
215 if (AbortOnFailure)
216 report_fatal_error("Program used external function '" + Name +
217 "' which could not be resolved!");
218 return 0;
219}
220
221SectionMemoryManager::~SectionMemoryManager() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000222 for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
223 sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
224 for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
225 sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
226 for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
227 sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000228}
229
230} // namespace llvm
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000231