Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 1 | //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===// |
| 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 | // Implementation of the runtime dynamic memory manager base class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Config/config.h" |
| 15 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
| 16 | #include "llvm/Support/DynamicLibrary.h" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | |
Michael J. Spencer | c93cdef | 2013-05-24 20:54:11 +0000 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 21 | #ifdef __linux__ |
| 22 | // These includes used by RTDyldMemoryManager::getPointerToNamedFunction() |
| 23 | // for Glibc trickery. See 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 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
| 34 | |
| 35 | // Determine whether we can register EH tables. |
Sylvestre Ledru | 47c7eee | 2013-08-27 06:49:46 +0000 | [diff] [blame] | 36 | #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \ |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 37 | !defined(__USING_SJLJ_EXCEPTIONS__)) |
| 38 | #define HAVE_EHTABLE_SUPPORT 1 |
| 39 | #else |
| 40 | #define HAVE_EHTABLE_SUPPORT 0 |
| 41 | #endif |
| 42 | |
| 43 | #if HAVE_EHTABLE_SUPPORT |
| 44 | extern "C" void __register_frame(void*); |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 45 | extern "C" void __deregister_frame(void*); |
| 46 | #else |
| 47 | // The building compiler does not have __(de)register_frame but |
| 48 | // it may be found at runtime in a dynamically-loaded library. |
| 49 | // For example, this happens when building LLVM with Visual C++ |
| 50 | // but using the MingW runtime. |
| 51 | void __register_frame(void *p) { |
| 52 | static bool Searched = false; |
| 53 | static void *rf = 0; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 54 | |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 55 | if (!Searched) { |
| 56 | Searched = true; |
| 57 | rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( |
| 58 | "__register_frame"); |
| 59 | } |
| 60 | if (rf) |
| 61 | ((void (*)(void *))rf)(p); |
| 62 | } |
| 63 | |
| 64 | void __deregister_frame(void *p) { |
| 65 | static bool Searched = false; |
| 66 | static void *df = 0; |
| 67 | |
| 68 | if (!Searched) { |
| 69 | Searched = true; |
| 70 | df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( |
| 71 | "__deregister_frame"); |
| 72 | } |
| 73 | if (df) |
| 74 | ((void (*)(void *))df)(p); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | #ifdef __APPLE__ |
| 79 | |
| 80 | static const char *processFDE(const char *Entry, bool isDeregister) { |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 81 | const char *P = Entry; |
Benjamin Kramer | f102f31 | 2013-06-04 09:09:15 +0000 | [diff] [blame] | 82 | uint32_t Length = *((const uint32_t *)P); |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 83 | P += 4; |
Benjamin Kramer | f102f31 | 2013-06-04 09:09:15 +0000 | [diff] [blame] | 84 | uint32_t Offset = *((const uint32_t *)P); |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 85 | if (Offset != 0) |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 86 | if (isDeregister) |
| 87 | __deregister_frame(const_cast<char *>(Entry)); |
| 88 | else |
| 89 | __register_frame(const_cast<char *>(Entry)); |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 90 | return P + Length; |
| 91 | } |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 92 | |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 93 | // This implementation handles frame registration for local targets. |
| 94 | // Memory managers for remote targets should re-implement this function |
| 95 | // and use the LoadAddr parameter. |
| 96 | void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, |
| 97 | uint64_t LoadAddr, |
| 98 | size_t Size) { |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 99 | // On OS X OS X __register_frame takes a single FDE as an argument. |
| 100 | // See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 101 | const char *P = (const char *)Addr; |
| 102 | const char *End = P + Size; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 103 | do { |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 104 | P = processFDE(P, false); |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 105 | } while(P != End); |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 108 | void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, |
| 109 | uint64_t LoadAddr, |
| 110 | size_t Size) { |
| 111 | const char *P = (const char *)Addr; |
| 112 | const char *End = P + Size; |
| 113 | do { |
| 114 | P = processFDE(P, true); |
| 115 | } while(P != End); |
| 116 | } |
| 117 | |
| 118 | #else |
| 119 | |
| 120 | void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, |
| 121 | uint64_t LoadAddr, |
| 122 | size_t Size) { |
| 123 | // On Linux __register_frame takes a single argument: |
| 124 | // a pointer to the start of the .eh_frame section. |
| 125 | |
| 126 | // How can it find the end? Because crtendS.o is linked |
| 127 | // in and it has an .eh_frame section with four zero chars. |
| 128 | // FIXME: make sure EH frame is followed by four zero bytes. |
| 129 | // This should be done in the linker RuntimeDyldELF::getEHFrameSection(), |
| 130 | // return pointer to .eh_frame properly appended by four zero bytes. |
| 131 | // If the linker can not fixed, do it here. |
| 132 | __register_frame(Addr); |
| 133 | } |
| 134 | |
| 135 | void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, |
| 136 | uint64_t LoadAddr, |
| 137 | size_t Size) { |
| 138 | __deregister_frame(Addr); |
| 139 | } |
| 140 | |
| 141 | #endif |
| 142 | |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 143 | static int jit_noop() { |
| 144 | return 0; |
| 145 | } |
| 146 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 147 | uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) { |
| 148 | // This implementation assumes that the host program is the target. |
| 149 | // Clients generating code for a remote target should implement their own |
| 150 | // memory manager. |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 151 | #if defined(__linux__) |
| 152 | //===--------------------------------------------------------------------===// |
| 153 | // Function stubs that are invoked instead of certain library calls |
| 154 | // |
| 155 | // Force the following functions to be linked in to anything that uses the |
| 156 | // JIT. This is a hack designed to work around the all-too-clever Glibc |
| 157 | // strategy of making these functions work differently when inlined vs. when |
| 158 | // not inlined, and hiding their real definitions in a separate archive file |
| 159 | // that the dynamic linker can't see. For more info, search for |
| 160 | // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274. |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 161 | if (Name == "stat") return (uint64_t)&stat; |
| 162 | if (Name == "fstat") return (uint64_t)&fstat; |
| 163 | if (Name == "lstat") return (uint64_t)&lstat; |
| 164 | if (Name == "stat64") return (uint64_t)&stat64; |
| 165 | if (Name == "fstat64") return (uint64_t)&fstat64; |
| 166 | if (Name == "lstat64") return (uint64_t)&lstat64; |
| 167 | if (Name == "atexit") return (uint64_t)&atexit; |
| 168 | if (Name == "mknod") return (uint64_t)&mknod; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 169 | #endif // __linux__ |
| 170 | |
| 171 | // We should not invoke parent's ctors/dtors from generated main()! |
| 172 | // On Mingw and Cygwin, the symbol __main is resolved to |
| 173 | // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors |
| 174 | // (and register wrong callee's dtors with atexit(3)). |
| 175 | // We expect ExecutionEngine::runStaticConstructorsDestructors() |
| 176 | // is called before ExecutionEngine::runFunctionAsMain() is called. |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 177 | if (Name == "__main") return (uint64_t)&jit_noop; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 178 | |
| 179 | const char *NameStr = Name.c_str(); |
| 180 | void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 181 | if (Ptr) |
| 182 | return (uint64_t)Ptr; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 183 | |
| 184 | // If it wasn't found and if it starts with an underscore ('_') character, |
| 185 | // try again without the underscore. |
| 186 | if (NameStr[0] == '_') { |
| 187 | Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 188 | if (Ptr) |
| 189 | return (uint64_t)Ptr; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 190 | } |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 193 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 194 | void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name, |
| 195 | bool AbortOnFailure) { |
| 196 | uint64_t Addr = getSymbolAddress(Name); |
| 197 | |
| 198 | if (!Addr && AbortOnFailure) |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 199 | report_fatal_error("Program used external function '" + Name + |
| 200 | "' which could not be resolved!"); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 201 | return (void*)Addr; |
Filip Pizlo | a4ea4a7 | 2013-05-21 20:24:07 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace llvm |