d6baf3c9bb8e5f8a9556fe7447f97121f04111ad
[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   if (!Alignment)
37     Alignment = 16;
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);
41
42   // Honour the alignment requirement.
43   uint8_t *AlignedAddr = (uint8_t*)RoundUpToAlignment((uint64_t)Addr, Alignment);
44
45   // Store the original address from calloc so we can free it later.
46   AllocatedDataMem.push_back(sys::MemoryBlock(Addr, NumElementsAligned*Alignment));
47   return AlignedAddr;
48 }
49
50 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
51                                                     unsigned Alignment,
52                                                     unsigned SectionID) {
53   if (!Alignment)
54     Alignment = 16;
55   unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
56   uintptr_t Addr = 0;
57   // Look in the list of free code memory regions and use a block there if one
58   // is available.
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();
64       // Align the address.
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;
70     }
71   }
72
73   // No pre-allocated free block was large enough. Allocate a new memory region.
74   sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
75
76   AllocatedCodeMem.push_back(MB);
77   Addr = (uintptr_t)MB.base();
78   uintptr_t EndOfBlock = Addr + MB.size();
79   // Align the address.
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;
84   if (FreeSize > 16)
85     FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
86
87   // Return aligned address
88   return (uint8_t*)Addr;
89 }
90
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());
95 }
96
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
102   //
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;
117 #endif // __linux__
118
119   const char *NameStr = Name.c_str();
120   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
121   if (Ptr) return Ptr;
122
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);
127     if (Ptr) return Ptr;
128   }
129
130   if (AbortOnFailure)
131     report_fatal_error("Program used external function '" + Name +
132                       "' which could not be resolved!");
133   return 0;
134 }
135
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());
141 }
142
143 } // namespace llvm