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"
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
33 RTDyldMemoryManager::~RTDyldMemoryManager() {}
35 // Determine whether we can register EH tables.
36 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
37 !defined(__USING_SJLJ_EXCEPTIONS__))
38 #define HAVE_EHTABLE_SUPPORT 1
40 #define HAVE_EHTABLE_SUPPORT 0
43 #if HAVE_EHTABLE_SUPPORT
44 extern "C" void __register_frame(void*);
45 extern "C" void __deregister_frame(void*);
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;
57 rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
61 ((void (*)(void *))rf)(p);
64 void __deregister_frame(void *p) {
65 static bool Searched = false;
70 df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
71 "__deregister_frame");
74 ((void (*)(void *))df)(p);
80 static const char *processFDE(const char *Entry, bool isDeregister) {
81 const char *P = Entry;
82 uint32_t Length = *((const uint32_t *)P);
84 uint32_t Offset = *((const uint32_t *)P);
87 __deregister_frame(const_cast<char *>(Entry));
89 __register_frame(const_cast<char *>(Entry));
94 // 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.
97 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
100 // 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
102 const char *P = (const char *)Addr;
103 const char *End = P + Size;
105 P = processFDE(P, false);
109 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
112 const char *P = (const char *)Addr;
113 const char *End = P + Size;
115 P = processFDE(P, true);
121 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
124 // On Linux __register_frame takes a single argument:
125 // a pointer to the start of the .eh_frame section.
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.
129 // FIXME: make sure EH frame is followed by four zero bytes.
130 // This should be done in the linker RuntimeDyldELF::getEHFrameSection(),
131 // return pointer to .eh_frame properly appended by four zero bytes.
132 // If the linker can not fixed, do it here.
133 __register_frame(Addr);
136 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
139 __deregister_frame(Addr);
144 static int jit_noop() {
148 uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
149 // This implementation assumes that the host program is the target.
150 // Clients generating code for a remote target should implement their own
152 #if defined(__linux__)
153 //===--------------------------------------------------------------------===//
154 // Function stubs that are invoked instead of certain library calls
156 // Force the following functions to be linked in to anything that uses the
157 // JIT. This is a hack designed to work around the all-too-clever Glibc
158 // strategy of making these functions work differently when inlined vs. when
159 // not inlined, and hiding their real definitions in a separate archive file
160 // that the dynamic linker can't see. For more info, search for
161 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
162 if (Name == "stat") return (uint64_t)&stat;
163 if (Name == "fstat") return (uint64_t)&fstat;
164 if (Name == "lstat") return (uint64_t)&lstat;
165 if (Name == "stat64") return (uint64_t)&stat64;
166 if (Name == "fstat64") return (uint64_t)&fstat64;
167 if (Name == "lstat64") return (uint64_t)&lstat64;
168 if (Name == "atexit") return (uint64_t)&atexit;
169 if (Name == "mknod") return (uint64_t)&mknod;
172 // We should not invoke parent's ctors/dtors from generated main()!
173 // On Mingw and Cygwin, the symbol __main is resolved to
174 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
175 // (and register wrong callee's dtors with atexit(3)).
176 // We expect ExecutionEngine::runStaticConstructorsDestructors()
177 // is called before ExecutionEngine::runFunctionAsMain() is called.
178 if (Name == "__main") return (uint64_t)&jit_noop;
180 const char *NameStr = Name.c_str();
181 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
183 return (uint64_t)Ptr;
185 // If it wasn't found and if it starts with an underscore ('_') character,
186 // try again without the underscore.
187 if (NameStr[0] == '_') {
188 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
190 return (uint64_t)Ptr;
195 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
196 bool AbortOnFailure) {
197 uint64_t Addr = getSymbolAddress(Name);
199 if (!Addr && AbortOnFailure)
200 report_fatal_error("Program used external function '" + Name +
201 "' which could not be resolved!");