LLVM API Documentation

RTDyldMemoryManager.cpp
Go to the documentation of this file.
00001 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Implementation of the runtime dynamic memory manager base class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Config/config.h"
00015 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
00016 #include "llvm/Support/DynamicLibrary.h"
00017 #include "llvm/Support/ErrorHandling.h"
00018 #include <cstdlib>
00019 
00020 #ifdef __linux__
00021   // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
00022   // for Glibc trickery. See comments in this function for more information.
00023   #ifdef HAVE_SYS_STAT_H
00024     #include <sys/stat.h>
00025   #endif
00026   #include <fcntl.h>
00027   #include <unistd.h>
00028 #endif
00029 
00030 namespace llvm {
00031 
00032 RTDyldMemoryManager::~RTDyldMemoryManager() {}
00033 
00034 // Determine whether we can register EH tables.
00035 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
00036      !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__))
00037 #define HAVE_EHTABLE_SUPPORT 1
00038 #else
00039 #define HAVE_EHTABLE_SUPPORT 0
00040 #endif
00041 
00042 #if HAVE_EHTABLE_SUPPORT
00043 extern "C" void __register_frame(void*);
00044 extern "C" void __deregister_frame(void*);
00045 #else
00046 // The building compiler does not have __(de)register_frame but
00047 // it may be found at runtime in a dynamically-loaded library.
00048 // For example, this happens when building LLVM with Visual C++
00049 // but using the MingW runtime.
00050 void __register_frame(void *p) {
00051   static bool Searched = false;
00052   static void *rf = 0;
00053 
00054   if (!Searched) {
00055     Searched = true;
00056     rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
00057                                                       "__register_frame");
00058   }
00059   if (rf)
00060     ((void (*)(void *))rf)(p);
00061 }
00062 
00063 void __deregister_frame(void *p) {
00064   static bool Searched = false;
00065   static void *df = 0;
00066 
00067   if (!Searched) {
00068     Searched = true;
00069     df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
00070                                                       "__deregister_frame");
00071   }
00072   if (df)
00073     ((void (*)(void *))df)(p);
00074 }
00075 #endif
00076 
00077 #ifdef __APPLE__
00078 
00079 static const char *processFDE(const char *Entry, bool isDeregister) {
00080   const char *P = Entry;
00081   uint32_t Length = *((const uint32_t *)P);
00082   P += 4;
00083   uint32_t Offset = *((const uint32_t *)P);
00084   if (Offset != 0) {
00085     if (isDeregister)
00086       __deregister_frame(const_cast<char *>(Entry));
00087     else
00088       __register_frame(const_cast<char *>(Entry));
00089   }
00090   return P + Length;
00091 }
00092 
00093 // This implementation handles frame registration for local targets.
00094 // Memory managers for remote targets should re-implement this function
00095 // and use the LoadAddr parameter.
00096 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
00097                                            uint64_t LoadAddr,
00098                                            size_t Size) {
00099   // On OS X OS X __register_frame takes a single FDE as an argument.
00100   // See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html
00101   const char *P = (const char *)Addr;
00102   const char *End = P + Size;
00103   do  {
00104     P = processFDE(P, false);
00105   } while(P != End);
00106 }
00107 
00108 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
00109                                            uint64_t LoadAddr,
00110                                            size_t Size) {
00111   const char *P = (const char *)Addr;
00112   const char *End = P + Size;
00113   do  {
00114     P = processFDE(P, true);
00115   } while(P != End);
00116 }
00117 
00118 #else
00119 
00120 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr,
00121                                            uint64_t LoadAddr,
00122                                            size_t Size) {
00123   // On Linux __register_frame takes a single argument: 
00124   // a pointer to the start of the .eh_frame section.
00125 
00126   // How can it find the end? Because crtendS.o is linked 
00127   // in and it has an .eh_frame section with four zero chars.
00128   __register_frame(Addr);
00129 }
00130 
00131 void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr,
00132                                            uint64_t LoadAddr,
00133                                            size_t Size) {
00134   __deregister_frame(Addr);
00135 }
00136 
00137 #endif
00138 
00139 static int jit_noop() {
00140   return 0;
00141 }
00142 
00143 // ARM math functions are statically linked on Android from libgcc.a, but not
00144 // available at runtime for dynamic linking. On Linux these are usually placed
00145 // in libgcc_s.so so can be found by normal dynamic lookup.
00146 #if defined(__BIONIC__) && defined(__arm__)
00147 // List of functions which are statically linked on Android and can be generated
00148 // by LLVM. This is done as a nested macro which is used once to declare the
00149 // imported functions with ARM_MATH_DECL and once to compare them to the
00150 // user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
00151 // assumes that all functions start with __aeabi_ and getSymbolAddress must be
00152 // modified if that changes.
00153 #define ARM_MATH_IMPORTS(PP) \
00154   PP(__aeabi_d2f) \
00155   PP(__aeabi_d2iz) \
00156   PP(__aeabi_d2lz) \
00157   PP(__aeabi_d2uiz) \
00158   PP(__aeabi_d2ulz) \
00159   PP(__aeabi_dadd) \
00160   PP(__aeabi_dcmpeq) \
00161   PP(__aeabi_dcmpge) \
00162   PP(__aeabi_dcmpgt) \
00163   PP(__aeabi_dcmple) \
00164   PP(__aeabi_dcmplt) \
00165   PP(__aeabi_dcmpun) \
00166   PP(__aeabi_ddiv) \
00167   PP(__aeabi_dmul) \
00168   PP(__aeabi_dsub) \
00169   PP(__aeabi_f2d) \
00170   PP(__aeabi_f2iz) \
00171   PP(__aeabi_f2lz) \
00172   PP(__aeabi_f2uiz) \
00173   PP(__aeabi_f2ulz) \
00174   PP(__aeabi_fadd) \
00175   PP(__aeabi_fcmpeq) \
00176   PP(__aeabi_fcmpge) \
00177   PP(__aeabi_fcmpgt) \
00178   PP(__aeabi_fcmple) \
00179   PP(__aeabi_fcmplt) \
00180   PP(__aeabi_fcmpun) \
00181   PP(__aeabi_fdiv) \
00182   PP(__aeabi_fmul) \
00183   PP(__aeabi_fsub) \
00184   PP(__aeabi_i2d) \
00185   PP(__aeabi_i2f) \
00186   PP(__aeabi_idiv) \
00187   PP(__aeabi_idivmod) \
00188   PP(__aeabi_l2d) \
00189   PP(__aeabi_l2f) \
00190   PP(__aeabi_lasr) \
00191   PP(__aeabi_ldivmod) \
00192   PP(__aeabi_llsl) \
00193   PP(__aeabi_llsr) \
00194   PP(__aeabi_lmul) \
00195   PP(__aeabi_ui2d) \
00196   PP(__aeabi_ui2f) \
00197   PP(__aeabi_uidiv) \
00198   PP(__aeabi_uidivmod) \
00199   PP(__aeabi_ul2d) \
00200   PP(__aeabi_ul2f) \
00201   PP(__aeabi_uldivmod)
00202 
00203 // Declare statically linked math functions on ARM. The function declarations
00204 // here do not have the correct prototypes for each function in
00205 // ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
00206 // needed. In particular the __aeabi_*divmod functions do not have calling
00207 // conventions which match any C prototype.
00208 #define ARM_MATH_DECL(name) extern "C" void name();
00209 ARM_MATH_IMPORTS(ARM_MATH_DECL)
00210 #undef ARM_MATH_DECL
00211 #endif
00212 
00213 uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) {
00214   // This implementation assumes that the host program is the target.
00215   // Clients generating code for a remote target should implement their own
00216   // memory manager.
00217 #if defined(__linux__) && defined(__GLIBC__)
00218   //===--------------------------------------------------------------------===//
00219   // Function stubs that are invoked instead of certain library calls
00220   //
00221   // Force the following functions to be linked in to anything that uses the
00222   // JIT. This is a hack designed to work around the all-too-clever Glibc
00223   // strategy of making these functions work differently when inlined vs. when
00224   // not inlined, and hiding their real definitions in a separate archive file
00225   // that the dynamic linker can't see. For more info, search for
00226   // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
00227   if (Name == "stat") return (uint64_t)&stat;
00228   if (Name == "fstat") return (uint64_t)&fstat;
00229   if (Name == "lstat") return (uint64_t)&lstat;
00230   if (Name == "stat64") return (uint64_t)&stat64;
00231   if (Name == "fstat64") return (uint64_t)&fstat64;
00232   if (Name == "lstat64") return (uint64_t)&lstat64;
00233   if (Name == "atexit") return (uint64_t)&atexit;
00234   if (Name == "mknod") return (uint64_t)&mknod;
00235 #endif // __linux__ && __GLIBC__
00236   
00237   // See ARM_MATH_IMPORTS definition for explanation
00238 #if defined(__BIONIC__) && defined(__arm__)
00239   if (Name.compare(0, 8, "__aeabi_") == 0) {
00240     // Check if the user has requested any of the functions listed in
00241     // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol.
00242 #define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn;
00243     ARM_MATH_IMPORTS(ARM_MATH_CHECK)
00244 #undef ARM_MATH_CHECK
00245   }
00246 #endif
00247 
00248   // We should not invoke parent's ctors/dtors from generated main()!
00249   // On Mingw and Cygwin, the symbol __main is resolved to
00250   // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
00251   // (and register wrong callee's dtors with atexit(3)).
00252   // We expect ExecutionEngine::runStaticConstructorsDestructors()
00253   // is called before ExecutionEngine::runFunctionAsMain() is called.
00254   if (Name == "__main") return (uint64_t)&jit_noop;
00255 
00256   const char *NameStr = Name.c_str();
00257   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
00258   if (Ptr)
00259     return (uint64_t)Ptr;
00260 
00261   // If it wasn't found and if it starts with an underscore ('_') character,
00262   // try again without the underscore.
00263   if (NameStr[0] == '_') {
00264     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
00265     if (Ptr)
00266       return (uint64_t)Ptr;
00267   }
00268   return 0;
00269 }
00270 
00271 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
00272                                                      bool AbortOnFailure) {
00273   uint64_t Addr = getSymbolAddress(Name);
00274 
00275   if (!Addr && AbortOnFailure)
00276     report_fatal_error("Program used external function '" + Name +
00277                        "' which could not be resolved!");
00278   return (void*)Addr;
00279 }
00280 
00281 } // namespace llvm