blob: 6af66dcea2b83bcb7d5ee57cb251f3abcb480bba [file] [log] [blame]
Filip Pizloa4ea4a72013-05-21 20:24:07 +00001//===-- 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. Spencerc93cdef2013-05-24 20:54:11 +000019#include <cstdlib>
20
Filip Pizloa4ea4a72013-05-21 20:24:07 +000021#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
31namespace llvm {
32
33RTDyldMemoryManager::~RTDyldMemoryManager() {}
34
35// Determine whether we can register EH tables.
Sylvestre Ledru47c7eee2013-08-27 06:49:46 +000036#if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
Filip Pizloa4ea4a72013-05-21 20:24:07 +000037 !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
44extern "C" void __register_frame(void*);
Andrew Kaylor43507d02013-10-16 00:14:21 +000045extern "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.
51void __register_frame(void *p) {
52 static bool Searched = false;
53 static void *rf = 0;
Filip Pizloa4ea4a72013-05-21 20:24:07 +000054
Andrew Kaylor43507d02013-10-16 00:14:21 +000055 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
64void __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
80static const char *processFDE(const char *Entry, bool isDeregister) {
Filip Pizloa4ea4a72013-05-21 20:24:07 +000081 const char *P = Entry;
Benjamin Kramerf102f312013-06-04 09:09:15 +000082 uint32_t Length = *((const uint32_t *)P);
Filip Pizloa4ea4a72013-05-21 20:24:07 +000083 P += 4;
Benjamin Kramerf102f312013-06-04 09:09:15 +000084 uint32_t Offset = *((const uint32_t *)P);
Craig Topper2369bf92013-10-16 06:50:36 +000085 if (Offset != 0) {
86 if (isDeregister)
Andrew Kaylor43507d02013-10-16 00:14:21 +000087 __deregister_frame(const_cast<char *>(Entry));
Craig Topper2369bf92013-10-16 06:50:36 +000088 else
Andrew Kaylor43507d02013-10-16 00:14:21 +000089 __register_frame(const_cast<char *>(Entry));
Craig Topper2369bf92013-10-16 06:50:36 +000090 }
Filip Pizloa4ea4a72013-05-21 20:24:07 +000091 return P + Length;
92}
Filip Pizloa4ea4a72013-05-21 20:24:07 +000093
Andrew Kaylor528f6d72013-10-11 21:25:48 +000094// This implementation handles frame registration for local targets.
95// Memory managers for remote targets should re-implement this function
96// and use the LoadAddr parameter.
97void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
98 uint64_t LoadAddr,
99 size_t Size) {
Andrew Kaylor43507d02013-10-16 00:14:21 +0000100 // On OS X OS X __register_frame takes a single FDE as an argument.
101 // See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000102 const char *P = (const char *)Addr;
103 const char *End = P + Size;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000104 do {
Andrew Kaylor43507d02013-10-16 00:14:21 +0000105 P = processFDE(P, false);
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000106 } while(P != End);
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000107}
108
Andrew Kaylor43507d02013-10-16 00:14:21 +0000109void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
110 uint64_t LoadAddr,
111 size_t Size) {
112 const char *P = (const char *)Addr;
113 const char *End = P + Size;
114 do {
115 P = processFDE(P, true);
116 } while(P != End);
117}
118
119#else
120
121void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
122 uint64_t LoadAddr,
123 size_t Size) {
124 // On Linux __register_frame takes a single argument:
125 // a pointer to the start of the .eh_frame section.
126
127 // How can it find the end? Because crtendS.o is linked
128 // in and it has an .eh_frame section with four zero chars.
Andrew Kaylor43507d02013-10-16 00:14:21 +0000129 __register_frame(Addr);
130}
131
132void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
133 uint64_t LoadAddr,
134 size_t Size) {
135 __deregister_frame(Addr);
136}
137
138#endif
139
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000140static int jit_noop() {
141 return 0;
142}
143
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000144uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
145 // This implementation assumes that the host program is the target.
146 // Clients generating code for a remote target should implement their own
147 // memory manager.
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000148#if defined(__linux__)
149 //===--------------------------------------------------------------------===//
150 // Function stubs that are invoked instead of certain library calls
151 //
152 // Force the following functions to be linked in to anything that uses the
153 // JIT. This is a hack designed to work around the all-too-clever Glibc
154 // strategy of making these functions work differently when inlined vs. when
155 // not inlined, and hiding their real definitions in a separate archive file
156 // that the dynamic linker can't see. For more info, search for
157 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000158 if (Name == "stat") return (uint64_t)&stat;
159 if (Name == "fstat") return (uint64_t)&fstat;
160 if (Name == "lstat") return (uint64_t)&lstat;
161 if (Name == "stat64") return (uint64_t)&stat64;
162 if (Name == "fstat64") return (uint64_t)&fstat64;
163 if (Name == "lstat64") return (uint64_t)&lstat64;
164 if (Name == "atexit") return (uint64_t)&atexit;
165 if (Name == "mknod") return (uint64_t)&mknod;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000166#endif // __linux__
167
168 // We should not invoke parent's ctors/dtors from generated main()!
169 // On Mingw and Cygwin, the symbol __main is resolved to
170 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
171 // (and register wrong callee's dtors with atexit(3)).
172 // We expect ExecutionEngine::runStaticConstructorsDestructors()
173 // is called before ExecutionEngine::runFunctionAsMain() is called.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000174 if (Name == "__main") return (uint64_t)&jit_noop;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000175
176 const char *NameStr = Name.c_str();
177 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000178 if (Ptr)
179 return (uint64_t)Ptr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000180
181 // If it wasn't found and if it starts with an underscore ('_') character,
182 // try again without the underscore.
183 if (NameStr[0] == '_') {
184 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000185 if (Ptr)
186 return (uint64_t)Ptr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000187 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000188 return 0;
189}
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000190
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000191void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
192 bool AbortOnFailure) {
193 uint64_t Addr = getSymbolAddress(Name);
194
195 if (!Addr && AbortOnFailure)
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000196 report_fatal_error("Program used external function '" + Name +
197 "' which could not be resolved!");
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000198 return (void*)Addr;
Filip Pizloa4ea4a72013-05-21 20:24:07 +0000199}
200
201} // namespace llvm