blob: 0a19f7e6c5293c250c0fe3be78e8af375df2dc1c [file] [log] [blame]
Filip Pizlo1cec8ab2013-05-21 20:24:07 +00001//===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Filip Pizlo1cec8ab2013-05-21 20:24:07 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the runtime dynamic memory manager base class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Config/config.h"
14#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Peter Collingbourne64faca92014-12-30 22:52:33 +000015#include "llvm/Support/Compiler.h"
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000016#include "llvm/Support/DynamicLibrary.h"
17#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer4fd69972013-05-24 20:54:11 +000018#include <cstdlib>
19
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000020#ifdef __linux__
21 // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
22 // for Glibc trickery. See 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
30namespace llvm {
31
32RTDyldMemoryManager::~RTDyldMemoryManager() {}
33
34// Determine whether we can register EH tables.
Sylvestre Ledrufb992ab2013-08-27 06:49:46 +000035#if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
Yaron Keren7da8e452013-12-17 08:40:11 +000036 !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__))
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000037#define HAVE_EHTABLE_SUPPORT 1
38#else
39#define HAVE_EHTABLE_SUPPORT 0
40#endif
41
42#if HAVE_EHTABLE_SUPPORT
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000043extern "C" void __register_frame(void *);
44extern "C" void __deregister_frame(void *);
Andrew Kaylorc442a762013-10-16 00:14:21 +000045#else
46// The building compiler does not have __(de)register_frame but
47// it may be found at runtime in a dynamically-loaded library.
48// For example, this happens when building LLVM with Visual C++
49// but using the MingW runtime.
50void __register_frame(void *p) {
51 static bool Searched = false;
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000052 static void((*rf)(void *)) = 0;
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000053
Andrew Kaylorc442a762013-10-16 00:14:21 +000054 if (!Searched) {
55 Searched = true;
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000056 *(void **)&rf =
57 llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame");
Andrew Kaylorc442a762013-10-16 00:14:21 +000058 }
59 if (rf)
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000060 rf(p);
Andrew Kaylorc442a762013-10-16 00:14:21 +000061}
62
63void __deregister_frame(void *p) {
64 static bool Searched = false;
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000065 static void((*df)(void *)) = 0;
Andrew Kaylorc442a762013-10-16 00:14:21 +000066
67 if (!Searched) {
68 Searched = true;
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000069 *(void **)&df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
70 "__deregister_frame");
Andrew Kaylorc442a762013-10-16 00:14:21 +000071 }
72 if (df)
Douglas Katzmanf3a3b5d2015-06-19 17:21:02 +000073 df(p);
Andrew Kaylorc442a762013-10-16 00:14:21 +000074}
75#endif
76
77#ifdef __APPLE__
78
79static const char *processFDE(const char *Entry, bool isDeregister) {
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000080 const char *P = Entry;
Benjamin Kramer7910e6c2013-06-04 09:09:15 +000081 uint32_t Length = *((const uint32_t *)P);
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000082 P += 4;
Benjamin Kramer7910e6c2013-06-04 09:09:15 +000083 uint32_t Offset = *((const uint32_t *)P);
Craig Topperc2ccbaf2013-10-16 06:50:36 +000084 if (Offset != 0) {
85 if (isDeregister)
Andrew Kaylorc442a762013-10-16 00:14:21 +000086 __deregister_frame(const_cast<char *>(Entry));
Craig Topperc2ccbaf2013-10-16 06:50:36 +000087 else
Andrew Kaylorc442a762013-10-16 00:14:21 +000088 __register_frame(const_cast<char *>(Entry));
Craig Topperc2ccbaf2013-10-16 06:50:36 +000089 }
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000090 return P + Length;
91}
Filip Pizlo1cec8ab2013-05-21 20:24:07 +000092
Andrew Kaylor7bb13442013-10-11 21:25:48 +000093// 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.
Lang Hames52c47242016-01-14 22:02:03 +000096void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
97 size_t Size) {
Andrew Kaylorc442a762013-10-16 00:14:21 +000098 // On OS X OS X __register_frame takes a single FDE as an argument.
Ed Maste08406282016-12-21 20:51:42 +000099 // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061737.html
100 // and projects/libunwind/src/UnwindLevel1-gcc-ext.c.
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000101 const char *P = (const char *)Addr;
102 const char *End = P + Size;
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000103 do {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000104 P = processFDE(P, false);
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000105 } while(P != End);
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000106}
107
Lang Hames52c47242016-01-14 22:02:03 +0000108void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
109 size_t Size) {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000110 const char *P = (const char *)Addr;
111 const char *End = P + Size;
112 do {
113 P = processFDE(P, true);
114 } while(P != End);
115}
116
117#else
118
Lang Hames52c47242016-01-14 22:02:03 +0000119void RTDyldMemoryManager::registerEHFramesInProcess(uint8_t *Addr,
120 size_t Size) {
Fangrui Songf78650a2018-07-30 19:41:25 +0000121 // On Linux __register_frame takes a single argument:
Andrew Kaylorc442a762013-10-16 00:14:21 +0000122 // a pointer to the start of the .eh_frame section.
123
Fangrui Songf78650a2018-07-30 19:41:25 +0000124 // How can it find the end? Because crtendS.o is linked
Andrew Kaylorc442a762013-10-16 00:14:21 +0000125 // in and it has an .eh_frame section with four zero chars.
Andrew Kaylorc442a762013-10-16 00:14:21 +0000126 __register_frame(Addr);
127}
128
Lang Hames52c47242016-01-14 22:02:03 +0000129void RTDyldMemoryManager::deregisterEHFramesInProcess(uint8_t *Addr,
130 size_t Size) {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000131 __deregister_frame(Addr);
132}
133
134#endif
135
Lang Hamesc936ac72017-05-09 21:32:18 +0000136void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
137 size_t Size) {
138 registerEHFramesInProcess(Addr, Size);
139 EHFrames.push_back({Addr, Size});
140}
141
142void RTDyldMemoryManager::deregisterEHFrames() {
143 for (auto &Frame : EHFrames)
144 deregisterEHFramesInProcess(Frame.Addr, Frame.Size);
145 EHFrames.clear();
146}
147
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000148static int jit_noop() {
149 return 0;
150}
151
Andrew Kaylord9f33812013-11-15 17:59:43 +0000152// ARM math functions are statically linked on Android from libgcc.a, but not
153// available at runtime for dynamic linking. On Linux these are usually placed
154// in libgcc_s.so so can be found by normal dynamic lookup.
155#if defined(__BIONIC__) && defined(__arm__)
156// List of functions which are statically linked on Android and can be generated
157// by LLVM. This is done as a nested macro which is used once to declare the
158// imported functions with ARM_MATH_DECL and once to compare them to the
159// user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
160// assumes that all functions start with __aeabi_ and getSymbolAddress must be
161// modified if that changes.
162#define ARM_MATH_IMPORTS(PP) \
163 PP(__aeabi_d2f) \
164 PP(__aeabi_d2iz) \
165 PP(__aeabi_d2lz) \
166 PP(__aeabi_d2uiz) \
167 PP(__aeabi_d2ulz) \
168 PP(__aeabi_dadd) \
169 PP(__aeabi_dcmpeq) \
170 PP(__aeabi_dcmpge) \
171 PP(__aeabi_dcmpgt) \
172 PP(__aeabi_dcmple) \
173 PP(__aeabi_dcmplt) \
174 PP(__aeabi_dcmpun) \
175 PP(__aeabi_ddiv) \
176 PP(__aeabi_dmul) \
177 PP(__aeabi_dsub) \
178 PP(__aeabi_f2d) \
179 PP(__aeabi_f2iz) \
180 PP(__aeabi_f2lz) \
181 PP(__aeabi_f2uiz) \
182 PP(__aeabi_f2ulz) \
183 PP(__aeabi_fadd) \
184 PP(__aeabi_fcmpeq) \
185 PP(__aeabi_fcmpge) \
186 PP(__aeabi_fcmpgt) \
187 PP(__aeabi_fcmple) \
188 PP(__aeabi_fcmplt) \
189 PP(__aeabi_fcmpun) \
190 PP(__aeabi_fdiv) \
191 PP(__aeabi_fmul) \
192 PP(__aeabi_fsub) \
193 PP(__aeabi_i2d) \
194 PP(__aeabi_i2f) \
195 PP(__aeabi_idiv) \
196 PP(__aeabi_idivmod) \
197 PP(__aeabi_l2d) \
198 PP(__aeabi_l2f) \
199 PP(__aeabi_lasr) \
200 PP(__aeabi_ldivmod) \
201 PP(__aeabi_llsl) \
202 PP(__aeabi_llsr) \
203 PP(__aeabi_lmul) \
204 PP(__aeabi_ui2d) \
205 PP(__aeabi_ui2f) \
206 PP(__aeabi_uidiv) \
207 PP(__aeabi_uidivmod) \
208 PP(__aeabi_ul2d) \
209 PP(__aeabi_ul2f) \
210 PP(__aeabi_uldivmod)
211
212// Declare statically linked math functions on ARM. The function declarations
213// here do not have the correct prototypes for each function in
214// ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
215// needed. In particular the __aeabi_*divmod functions do not have calling
216// conventions which match any C prototype.
217#define ARM_MATH_DECL(name) extern "C" void name();
218ARM_MATH_IMPORTS(ARM_MATH_DECL)
219#undef ARM_MATH_DECL
220#endif
221
Peter Collingbournec5de3112014-12-30 18:22:06 +0000222#if defined(__linux__) && defined(__GLIBC__) && \
223 (defined(__i386__) || defined(__x86_64__))
Peter Collingbourne64faca92014-12-30 22:52:33 +0000224extern "C" LLVM_ATTRIBUTE_WEAK void __morestack();
Peter Collingbourne06e85432014-12-30 18:06:52 +0000225#endif
226
Lang Hames2f27b2f2014-10-01 04:11:13 +0000227uint64_t
228RTDyldMemoryManager::getSymbolAddressInProcess(const std::string &Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000229 // This implementation assumes that the host program is the target.
230 // Clients generating code for a remote target should implement their own
231 // memory manager.
Andrew Kaylord9f33812013-11-15 17:59:43 +0000232#if defined(__linux__) && defined(__GLIBC__)
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000233 //===--------------------------------------------------------------------===//
234 // Function stubs that are invoked instead of certain library calls
235 //
236 // Force the following functions to be linked in to anything that uses the
237 // JIT. This is a hack designed to work around the all-too-clever Glibc
238 // strategy of making these functions work differently when inlined vs. when
239 // not inlined, and hiding their real definitions in a separate archive file
240 // that the dynamic linker can't see. For more info, search for
241 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
Andrew Kaylorea395922013-10-01 01:47:35 +0000242 if (Name == "stat") return (uint64_t)&stat;
243 if (Name == "fstat") return (uint64_t)&fstat;
244 if (Name == "lstat") return (uint64_t)&lstat;
245 if (Name == "stat64") return (uint64_t)&stat64;
246 if (Name == "fstat64") return (uint64_t)&fstat64;
247 if (Name == "lstat64") return (uint64_t)&lstat64;
248 if (Name == "atexit") return (uint64_t)&atexit;
249 if (Name == "mknod") return (uint64_t)&mknod;
Peter Collingbourne06e85432014-12-30 18:06:52 +0000250
Peter Collingbournec5de3112014-12-30 18:22:06 +0000251#if defined(__i386__) || defined(__x86_64__)
Peter Collingbourne06e85432014-12-30 18:06:52 +0000252 // __morestack lives in libgcc, a static library.
Peter Collingbourne64faca92014-12-30 22:52:33 +0000253 if (&__morestack && Name == "__morestack")
254 return (uint64_t)&__morestack;
Peter Collingbournec5de3112014-12-30 18:22:06 +0000255#endif
Andrew Kaylord9f33812013-11-15 17:59:43 +0000256#endif // __linux__ && __GLIBC__
Fangrui Songf78650a2018-07-30 19:41:25 +0000257
Andrew Kaylord9f33812013-11-15 17:59:43 +0000258 // See ARM_MATH_IMPORTS definition for explanation
259#if defined(__BIONIC__) && defined(__arm__)
260 if (Name.compare(0, 8, "__aeabi_") == 0) {
261 // Check if the user has requested any of the functions listed in
262 // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol.
263#define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn;
264 ARM_MATH_IMPORTS(ARM_MATH_CHECK)
265#undef ARM_MATH_CHECK
266 }
267#endif
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000268
269 // We should not invoke parent's ctors/dtors from generated main()!
270 // On Mingw and Cygwin, the symbol __main is resolved to
271 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
272 // (and register wrong callee's dtors with atexit(3)).
273 // We expect ExecutionEngine::runStaticConstructorsDestructors()
274 // is called before ExecutionEngine::runFunctionAsMain() is called.
Andrew Kaylorea395922013-10-01 01:47:35 +0000275 if (Name == "__main") return (uint64_t)&jit_noop;
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000276
Lang Hamesb7fbf592014-09-20 17:44:56 +0000277 const char *NameStr = Name.c_str();
278
Lang Hames3b514552016-03-03 21:23:15 +0000279 // DynamicLibrary::SearchForAddresOfSymbol expects an unmangled 'C' symbol
Lang Hames75601bf2016-08-18 01:33:28 +0000280 // name so ff we're on Darwin, strip the leading '_' off.
281#ifdef __APPLE__
Lang Hamesb7fbf592014-09-20 17:44:56 +0000282 if (NameStr[0] == '_')
Lang Hames3b514552016-03-03 21:23:15 +0000283 ++NameStr;
284#endif
Lang Hamesb7fbf592014-09-20 17:44:56 +0000285
Lang Hames27e58722014-09-21 17:21:56 +0000286 return (uint64_t)sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
Andrew Kaylorea395922013-10-01 01:47:35 +0000287}
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000288
Andrew Kaylorea395922013-10-01 01:47:35 +0000289void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
290 bool AbortOnFailure) {
291 uint64_t Addr = getSymbolAddress(Name);
292
293 if (!Addr && AbortOnFailure)
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000294 report_fatal_error("Program used external function '" + Name +
295 "' which could not be resolved!");
Lang Hames3b514552016-03-03 21:23:15 +0000296
Andrew Kaylorea395922013-10-01 01:47:35 +0000297 return (void*)Addr;
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000298}
299
Weiming Zhao1bd40002018-04-11 23:09:20 +0000300void RTDyldMemoryManager::anchor() {}
301void MCJITMemoryManager::anchor() {}
Filip Pizlo1cec8ab2013-05-21 20:24:07 +0000302} // namespace llvm