225106ecab2c092598f41547525feb41d75711c1
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / SectionMemoryManager.cpp
1 //===-- SectionMemoryManager.cpp - The memory manager for MCJIT -----------===//
2 //
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 //
10 // This file defines the implementation of the section-based memory manager
11 // used by MCJIT.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "llvm/Support/MathExtras.h"
18
19 #include "SectionMemoryManager.h"
20
21 #ifdef __linux__
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
25 #include <sys/stat.h>
26 #endif
27 #include <fcntl.h>
28 #include <unistd.h>
29 #endif
30
31 namespace llvm {
32
33 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
34                                                     unsigned Alignment,
35                                                     unsigned SectionID,
36                                                     bool IsReadOnly) {
37   if (!Alignment)
38     Alignment = 16;
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);
42
43   // Honour the alignment requirement.
44   uint8_t *AlignedAddr = (uint8_t*)RoundUpToAlignment((uint64_t)Addr, Alignment);
45
46   // Store the original address from calloc so we can free it later.
47   AllocatedDataMem.push_back(sys::MemoryBlock(Addr, NumElementsAligned*Alignment));
48   return AlignedAddr;
49 }
50
51 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
52                                                     unsigned Alignment,
53                                                     unsigned SectionID) {
54   if (!Alignment)
55     Alignment = 16;
56   unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
57   uintptr_t Addr = 0;
58   // Look in the list of free code memory regions and use a block there if one
59   // is available.
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();
65       // Align the address.
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;
71     }
72   }
73
74   // No pre-allocated free block was large enough. Allocate a new memory region.
75   sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
76
77   AllocatedCodeMem.push_back(MB);
78   Addr = (uintptr_t)MB.base();
79   uintptr_t EndOfBlock = Addr + MB.size();
80   // Align the address.
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;
85   if (FreeSize > 16)
86     FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
87
88   // Return aligned address
89   return (uint8_t*)Addr;
90 }
91
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());
96 }
97
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
103   //
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;
118 #endif // __linux__
119
120   const char *NameStr = Name.c_str();
121   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
122   if (Ptr) return Ptr;
123
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);
128     if (Ptr) return Ptr;
129   }
130
131   if (AbortOnFailure)
132     report_fatal_error("Program used external function '" + Name +
133                       "' which could not be resolved!");
134   return 0;
135 }
136
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());
142 }
143
144 } // namespace llvm