1 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the runtime dynamic memory manager base class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Config/config.h"
15 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "llvm/Support/ErrorHandling.h"
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
32 RTDyldMemoryManager::~RTDyldMemoryManager() {}
34 // Determine whether we can register EH tables.
35 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
36 !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__))
37 #define HAVE_EHTABLE_SUPPORT 1
39 #define HAVE_EHTABLE_SUPPORT 0
42 #if HAVE_EHTABLE_SUPPORT
43 extern "C" void __register_frame(void*);
44 extern "C" void __deregister_frame(void*);
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.
50 void __register_frame(void *p) {
51 static bool Searched = false;
56 rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
60 ((void (*)(void *))rf)(p);
63 void __deregister_frame(void *p) {
64 static bool Searched = false;
69 df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
70 "__deregister_frame");
73 ((void (*)(void *))df)(p);
79 static const char *processFDE(const char *Entry, bool isDeregister) {
80 const char *P = Entry;
81 uint32_t Length = *((const uint32_t *)P);
83 uint32_t Offset = *((const uint32_t *)P);
86 __deregister_frame(const_cast<char *>(Entry));
88 __register_frame(const_cast<char *>(Entry));
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,
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
101 const char *P = (const char *)Addr;
102 const char *End = P + Size;
104 P = processFDE(P, false);
108 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
111 const char *P = (const char *)Addr;
112 const char *End = P + Size;
114 P = processFDE(P, true);
120 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
123 // On Linux __register_frame takes a single argument:
124 // a pointer to the start of the .eh_frame section.
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 __register_frame(Addr);
131 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
134 __deregister_frame(Addr);
139 static int jit_noop() {
143 // ARM math functions are statically linked on Android from libgcc.a, but not
144 // available at runtime for dynamic linking. On Linux these are usually placed
145 // in libgcc_s.so so can be found by normal dynamic lookup.
146 #if defined(__BIONIC__) && defined(__arm__)
147 // List of functions which are statically linked on Android and can be generated
148 // by LLVM. This is done as a nested macro which is used once to declare the
149 // imported functions with ARM_MATH_DECL and once to compare them to the
150 // user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
151 // assumes that all functions start with __aeabi_ and getSymbolAddress must be
152 // modified if that changes.
153 #define ARM_MATH_IMPORTS(PP) \
187 PP(__aeabi_idivmod) \
191 PP(__aeabi_ldivmod) \
198 PP(__aeabi_uidivmod) \
203 // Declare statically linked math functions on ARM. The function declarations
204 // here do not have the correct prototypes for each function in
205 // ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
206 // needed. In particular the __aeabi_*divmod functions do not have calling
207 // conventions which match any C prototype.
208 #define ARM_MATH_DECL(name) extern "C" void name();
209 ARM_MATH_IMPORTS(ARM_MATH_DECL)
213 uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
214 // This implementation assumes that the host program is the target.
215 // Clients generating code for a remote target should implement their own
217 #if defined(__linux__) && defined(__GLIBC__)
218 //===--------------------------------------------------------------------===//
219 // Function stubs that are invoked instead of certain library calls
221 // Force the following functions to be linked in to anything that uses the
222 // JIT. This is a hack designed to work around the all-too-clever Glibc
223 // strategy of making these functions work differently when inlined vs. when
224 // not inlined, and hiding their real definitions in a separate archive file
225 // that the dynamic linker can't see. For more info, search for
226 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
227 if (Name == "stat") return (uint64_t)&stat;
228 if (Name == "fstat") return (uint64_t)&fstat;
229 if (Name == "lstat") return (uint64_t)&lstat;
230 if (Name == "stat64") return (uint64_t)&stat64;
231 if (Name == "fstat64") return (uint64_t)&fstat64;
232 if (Name == "lstat64") return (uint64_t)&lstat64;
233 if (Name == "atexit") return (uint64_t)&atexit;
234 if (Name == "mknod") return (uint64_t)&mknod;
235 #endif // __linux__ && __GLIBC__
237 // See ARM_MATH_IMPORTS definition for explanation
238 #if defined(__BIONIC__) && defined(__arm__)
239 if (Name.compare(0, 8, "__aeabi_") == 0) {
240 // Check if the user has requested any of the functions listed in
241 // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol.
242 #define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn;
243 ARM_MATH_IMPORTS(ARM_MATH_CHECK)
244 #undef ARM_MATH_CHECK
248 // We should not invoke parent's ctors/dtors from generated main()!
249 // On Mingw and Cygwin, the symbol __main is resolved to
250 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
251 // (and register wrong callee's dtors with atexit(3)).
252 // We expect ExecutionEngine::runStaticConstructorsDestructors()
253 // is called before ExecutionEngine::runFunctionAsMain() is called.
254 if (Name == "__main") return (uint64_t)&jit_noop;
256 const char *NameStr = Name.c_str();
257 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
259 return (uint64_t)Ptr;
261 // If it wasn't found and if it starts with an underscore ('_') character,
262 // try again without the underscore.
263 if (NameStr[0] == '_') {
264 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
266 return (uint64_t)Ptr;
271 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
272 bool AbortOnFailure) {
273 uint64_t Addr = getSymbolAddress(Name);
275 if (!Addr && AbortOnFailure)
276 report_fatal_error("Program used external function '" + Name +
277 "' which could not be resolved!");