Home | History | Annotate | Download | only in ExecutionEngine
      1 //===-- 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 
     19 #include <cstdlib>
     20 
     21 #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 
     31 namespace llvm {
     32 
     33 RTDyldMemoryManager::~RTDyldMemoryManager() {}
     34 
     35 // Determine whether we can register EH tables.
     36 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && \
     37      !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
     44 extern "C" void __register_frame(void*);
     45 
     46 static const char *processFDE(const char *Entry) {
     47   const char *P = Entry;
     48   uint32_t Length = *((const uint32_t *)P);
     49   P += 4;
     50   uint32_t Offset = *((const uint32_t *)P);
     51   if (Offset != 0)
     52     __register_frame(const_cast<char *>(Entry));
     53   return P + Length;
     54 }
     55 #endif
     56 
     57 void RTDyldMemoryManager::registerEHFrames(StringRef SectionData) {
     58 #if HAVE_EHTABLE_SUPPORT
     59   const char *P = SectionData.data();
     60   const char *End = SectionData.data() + SectionData.size();
     61   do  {
     62     P = processFDE(P);
     63   } while(P != End);
     64 #endif
     65 }
     66 
     67 static int jit_noop() {
     68   return 0;
     69 }
     70 
     71 void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,
     72                                                      bool AbortOnFailure) {
     73 #if defined(__linux__)
     74   //===--------------------------------------------------------------------===//
     75   // Function stubs that are invoked instead of certain library calls
     76   //
     77   // Force the following functions to be linked in to anything that uses the
     78   // JIT. This is a hack designed to work around the all-too-clever Glibc
     79   // strategy of making these functions work differently when inlined vs. when
     80   // not inlined, and hiding their real definitions in a separate archive file
     81   // that the dynamic linker can't see. For more info, search for
     82   // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
     83   if (Name == "stat") return (void*)(intptr_t)&stat;
     84   if (Name == "fstat") return (void*)(intptr_t)&fstat;
     85   if (Name == "lstat") return (void*)(intptr_t)&lstat;
     86   if (Name == "stat64") return (void*)(intptr_t)&stat64;
     87   if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
     88   if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
     89   if (Name == "atexit") return (void*)(intptr_t)&atexit;
     90   if (Name == "mknod") return (void*)(intptr_t)&mknod;
     91 #endif // __linux__
     92 
     93   // We should not invoke parent's ctors/dtors from generated main()!
     94   // On Mingw and Cygwin, the symbol __main is resolved to
     95   // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
     96   // (and register wrong callee's dtors with atexit(3)).
     97   // We expect ExecutionEngine::runStaticConstructorsDestructors()
     98   // is called before ExecutionEngine::runFunctionAsMain() is called.
     99   if (Name == "__main") return (void*)(intptr_t)&jit_noop;
    100 
    101   const char *NameStr = Name.c_str();
    102   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
    103   if (Ptr) return Ptr;
    104 
    105   // If it wasn't found and if it starts with an underscore ('_') character,
    106   // try again without the underscore.
    107   if (NameStr[0] == '_') {
    108     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
    109     if (Ptr) return Ptr;
    110   }
    111 
    112   if (AbortOnFailure)
    113     report_fatal_error("Program used external function '" + Name +
    114                        "' which could not be resolved!");
    115   return 0;
    116 }
    117 
    118 } // namespace llvm
    119