Adding MCJIT and MemoryBuffer unit tests
[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
18 #include "SectionMemoryManager.h"
19
20 #ifdef __linux__
21 // These includes used by SectionMemoryManager::getPointerToNamedFunction()
22 // for Glibc trickery. Look 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>
28 #endif
29
30 namespace llvm {
31
32 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
33                                                     unsigned Alignment,
34                                                     unsigned SectionID) {
35   if (!Alignment)
36     Alignment = 16;
37   uint8_t *Addr = (uint8_t*)calloc((Size + Alignment - 1)/Alignment, Alignment);
38   AllocatedDataMem.push_back(sys::MemoryBlock(Addr, Size));
39   return Addr;
40 }
41
42 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
43                                                     unsigned Alignment,
44                                                     unsigned SectionID) {
45   if (!Alignment)
46     Alignment = 16;
47   unsigned NeedAllocate = Alignment * ((Size + Alignment - 1)/Alignment + 1);
48   uintptr_t Addr = 0;
49   // Look in the list of free code memory regions and use a block there if one
50   // is available.
51   for (int i = 0, e = FreeCodeMem.size(); i != e; ++i) {
52     sys::MemoryBlock &MB = FreeCodeMem[i];
53     if (MB.size() >= NeedAllocate) {
54       Addr = (uintptr_t)MB.base();
55       uintptr_t EndOfBlock = Addr + MB.size();
56       // Align the address.
57       Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
58       // Store cutted free memory block.
59       FreeCodeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
60                                         EndOfBlock - Addr - Size);
61       return (uint8_t*)Addr;
62     }
63   }
64
65   // No pre-allocated free block was large enough. Allocate a new memory region.
66   sys::MemoryBlock MB = sys::Memory::AllocateRWX(NeedAllocate, 0, 0);
67
68   AllocatedCodeMem.push_back(MB);
69   Addr = (uintptr_t)MB.base();
70   uintptr_t EndOfBlock = Addr + MB.size();
71   // Align the address.
72   Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
73   // The AllocateRWX may allocate much more memory than we need. In this case,
74   // we store the unused memory as a free memory block.
75   unsigned FreeSize = EndOfBlock-Addr-Size;
76   if (FreeSize > 16)
77     FreeCodeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
78
79   // Return aligned address
80   return (uint8_t*)Addr;
81 }
82
83 void SectionMemoryManager::invalidateInstructionCache() {
84   for (int i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
85     sys::Memory::InvalidateInstructionCache(AllocatedCodeMem[i].base(),
86                                             AllocatedCodeMem[i].size());
87 }
88
89 void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
90                                                        bool AbortOnFailure) {
91 #if defined(__linux__)
92   //===--------------------------------------------------------------------===//
93   // Function stubs that are invoked instead of certain library calls
94   //
95   // Force the following functions to be linked in to anything that uses the
96   // JIT. This is a hack designed to work around the all-too-clever Glibc
97   // strategy of making these functions work differently when inlined vs. when
98   // not inlined, and hiding their real definitions in a separate archive file
99   // that the dynamic linker can't see. For more info, search for
100   // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
101   if (Name == "stat") return (void*)(intptr_t)&stat;
102   if (Name == "fstat") return (void*)(intptr_t)&fstat;
103   if (Name == "lstat") return (void*)(intptr_t)&lstat;
104   if (Name == "stat64") return (void*)(intptr_t)&stat64;
105   if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
106   if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
107   if (Name == "atexit") return (void*)(intptr_t)&atexit;
108   if (Name == "mknod") return (void*)(intptr_t)&mknod;
109 #endif // __linux__
110
111   const char *NameStr = Name.c_str();
112   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
113   if (Ptr) return Ptr;
114
115   // If it wasn't found and if it starts with an underscore ('_') character,
116   // try again without the underscore.
117   if (NameStr[0] == '_') {
118     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
119     if (Ptr) return Ptr;
120   }
121
122   if (AbortOnFailure)
123     report_fatal_error("Program used external function '" + Name +
124                       "' which could not be resolved!");
125   return 0;
126 }
127
128 SectionMemoryManager::~SectionMemoryManager() {
129   for (unsigned i = 0, e = AllocatedCodeMem.size(); i != e; ++i)
130     sys::Memory::ReleaseRWX(AllocatedCodeMem[i]);
131   for (unsigned i = 0, e = AllocatedDataMem.size(); i != e; ++i)
132     free(AllocatedDataMem[i].base());
133 }
134
135 } // namespace llvm