LLVM API Documentation

Intercept.cpp

Go to the documentation of this file.
00001 //===-- Intercept.cpp - System function interception routines -------------===//
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 // If a function call occurs to an external function, the JIT is designed to use
00011 // the dynamic loader interface to find a function to call.  This is useful for
00012 // calling system calls and library functions that are not available in LLVM.
00013 // Some system calls, however, need to be handled specially.  For this reason,
00014 // we intercept some of them here and use our own stubs to handle them.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "JIT.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 #include "llvm/Support/Streams.h"
00021 #include "llvm/System/DynamicLibrary.h"
00022 #include "llvm/Config/config.h"
00023 using namespace llvm;
00024 
00025 // AtExitHandlers - List of functions to call when the program exits,
00026 // registered with the atexit() library function.
00027 static std::vector<void (*)()> AtExitHandlers;
00028 
00029 /// runAtExitHandlers - Run any functions registered by the program's
00030 /// calls to atexit(3), which we intercept and store in
00031 /// AtExitHandlers.
00032 ///
00033 static void runAtExitHandlers() {
00034   while (!AtExitHandlers.empty()) {
00035     void (*Fn)() = AtExitHandlers.back();
00036     AtExitHandlers.pop_back();
00037     Fn();
00038   }
00039 }
00040 
00041 //===----------------------------------------------------------------------===//
00042 // Function stubs that are invoked instead of certain library calls
00043 //===----------------------------------------------------------------------===//
00044 
00045 // Force the following functions to be linked in to anything that uses the
00046 // JIT. This is a hack designed to work around the all-too-clever Glibc
00047 // strategy of making these functions work differently when inlined vs. when
00048 // not inlined, and hiding their real definitions in a separate archive file
00049 // that the dynamic linker can't see. For more info, search for
00050 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
00051 #if defined(__linux__)
00052 #if defined(HAVE_SYS_STAT_H)
00053 #include <sys/stat.h>
00054 #endif
00055 #include <fcntl.h>
00056 /* stat functions are redirecting to __xstat with a version number.  On x86-64 
00057  * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat' 
00058  * available as an exported symbol, so we have to add it explicitly.
00059  */
00060 namespace {
00061 class StatSymbols {
00062 public:
00063   StatSymbols() {
00064     sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
00065     sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
00066     sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
00067     sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
00068     sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64);
00069     sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64);
00070     sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64);
00071     sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
00072     sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
00073     sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
00074     sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
00075   }
00076 };
00077 }
00078 static StatSymbols initStatSymbols;
00079 #endif // __linux__
00080 
00081 // jit_exit - Used to intercept the "exit" library call.
00082 static void jit_exit(int Status) {
00083   runAtExitHandlers();   // Run atexit handlers...
00084   exit(Status);
00085 }
00086 
00087 // jit_atexit - Used to intercept the "atexit" library call.
00088 static int jit_atexit(void (*Fn)()) {
00089   AtExitHandlers.push_back(Fn);    // Take note of atexit handler...
00090   return 0;  // Always successful
00091 }
00092 
00093 //===----------------------------------------------------------------------===//
00094 //
00095 /// getPointerToNamedFunction - This method returns the address of the specified
00096 /// function by using the dynamic loader interface.  As such it is only useful
00097 /// for resolving library symbols, not code generated symbols.
00098 ///
00099 void *JIT::getPointerToNamedFunction(const std::string &Name,
00100                                      bool AbortOnFailure) {
00101   if (!isSymbolSearchingDisabled()) {
00102     // Check to see if this is one of the functions we want to intercept.  Note,
00103     // we cast to intptr_t here to silence a -pedantic warning that complains
00104     // about casting a function pointer to a normal pointer.
00105     if (Name == "exit") return (void*)(intptr_t)&jit_exit;
00106     if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
00107 
00108     const char *NameStr = Name.c_str();
00109     // If this is an asm specifier, skip the sentinal.
00110     if (NameStr[0] == 1) ++NameStr;
00111     
00112     // If it's an external function, look it up in the process image...
00113     void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
00114     if (Ptr) return Ptr;
00115     
00116     // If it wasn't found and if it starts with an underscore ('_') character,
00117     // and has an asm specifier, try again without the underscore.
00118     if (Name[0] == 1 && NameStr[0] == '_') {
00119       Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
00120       if (Ptr) return Ptr;
00121     }
00122     
00123     // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf.  These
00124     // are references to hidden visibility symbols that dlsym cannot resolve.
00125     // If we have one of these, strip off $LDBLStub and try again.
00126 #if defined(__APPLE__) && defined(__ppc__)
00127     if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
00128         memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
00129       // First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
00130       // This mirrors logic in libSystemStubs.a.
00131       std::string Prefix = std::string(Name.begin(), Name.end()-9);
00132       if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false))
00133         return Ptr;
00134       if (void *Ptr = getPointerToNamedFunction(Prefix, false))
00135         return Ptr;
00136     }
00137 #endif
00138   }
00139   
00140   /// If a LazyFunctionCreator is installed, use it to get/create the function.
00141   if (LazyFunctionCreator)
00142     if (void *RP = LazyFunctionCreator(Name))
00143       return RP;
00144 
00145   if (AbortOnFailure) {
00146     llvm_report_error("Program used external function '"+Name+
00147                       "' which could not be resolved!");
00148   }
00149   return 0;
00150 }