2003-05-14 13:26:47 +00:00
|
|
|
//===-- Intercept.cpp - System function interception routines -------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-05-14 13:26:47 +00:00
|
|
|
//
|
|
|
|
// If a function call occurs to an external function, the JIT is designed to use
|
2003-10-10 17:02:42 +00:00
|
|
|
// the dynamic loader interface to find a function to call. This is useful for
|
2003-05-14 13:26:47 +00:00
|
|
|
// calling system calls and library functions that are not available in LLVM.
|
|
|
|
// Some system calls, however, need to be handled specially. For this reason,
|
|
|
|
// we intercept some of them here and use our own stubs to handle them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "JIT.h"
|
2004-11-29 14:11:29 +00:00
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
2004-12-20 06:34:02 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2003-12-08 08:06:28 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// AtExitHandlers - List of functions to call when the program exits,
|
|
|
|
// registered with the atexit() library function.
|
|
|
|
static std::vector<void (*)()> AtExitHandlers;
|
2003-05-14 13:53:40 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
/// runAtExitHandlers - Run any functions registered by the program's
|
|
|
|
/// calls to atexit(3), which we intercept and store in
|
|
|
|
/// AtExitHandlers.
|
|
|
|
///
|
2003-12-26 06:13:47 +00:00
|
|
|
static void runAtExitHandlers() {
|
2003-09-05 18:42:01 +00:00
|
|
|
while (!AtExitHandlers.empty()) {
|
|
|
|
void (*Fn)() = AtExitHandlers.back();
|
|
|
|
AtExitHandlers.pop_back();
|
2003-05-14 13:53:40 +00:00
|
|
|
Fn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-14 13:26:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-05 18:42:01 +00:00
|
|
|
// Function stubs that are invoked instead of certain library calls
|
2003-05-14 13:26:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-03-09 05:22:10 +00:00
|
|
|
// Force the following functions to be linked in to anything that uses the
|
|
|
|
// JIT. This is a hack designed to work around the all-too-clever Glibc
|
|
|
|
// strategy of making these functions work differently when inlined vs. when
|
|
|
|
// not inlined, and hiding their real definitions in a separate archive file
|
|
|
|
// that the dynamic linker can't see. For more info, search for
|
2006-03-14 05:54:52 +00:00
|
|
|
// 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
|
2004-03-10 17:38:28 +00:00
|
|
|
#if defined(__linux__)
|
2004-12-20 06:34:02 +00:00
|
|
|
#if defined(HAVE_SYS_STAT_H)
|
2004-12-17 19:09:16 +00:00
|
|
|
#include <sys/stat.h>
|
2005-04-21 22:55:34 +00:00
|
|
|
#endif
|
2008-01-03 07:10:51 +00:00
|
|
|
|
2008-01-03 22:15:32 +00:00
|
|
|
/* stat functions are redirecting to __xstat with a version number. On x86-64
|
|
|
|
* linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat'
|
|
|
|
* available as an exported symbol, so we have to add it explicitly.
|
|
|
|
*/
|
2008-01-03 07:10:51 +00:00
|
|
|
class StatSymbols {
|
2008-01-03 22:15:32 +00:00
|
|
|
public:
|
|
|
|
StatSymbols() {
|
|
|
|
sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
|
|
|
|
sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
|
|
|
|
sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
|
|
|
|
sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
|
|
|
|
sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
|
|
|
|
sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
|
|
|
|
sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
|
|
|
|
sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
|
|
|
|
}
|
2004-03-09 05:22:10 +00:00
|
|
|
};
|
2008-01-03 07:10:51 +00:00
|
|
|
static StatSymbols initStatSymbols;
|
2004-03-10 17:38:28 +00:00
|
|
|
#endif // __linux__
|
2004-03-09 05:22:10 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// jit_exit - Used to intercept the "exit" library call.
|
2003-05-14 13:26:47 +00:00
|
|
|
static void jit_exit(int Status) {
|
2003-12-26 06:13:47 +00:00
|
|
|
runAtExitHandlers(); // Run atexit handlers...
|
2003-05-14 13:53:40 +00:00
|
|
|
exit(Status);
|
2003-05-14 13:26:47 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// jit_atexit - Used to intercept the "atexit" library call.
|
2003-05-14 13:26:47 +00:00
|
|
|
static int jit_atexit(void (*Fn)(void)) {
|
2003-09-05 18:42:01 +00:00
|
|
|
AtExitHandlers.push_back(Fn); // Take note of atexit handler...
|
2003-05-14 13:53:40 +00:00
|
|
|
return 0; // Always successful
|
2003-05-14 13:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-05-14 13:26:47 +00:00
|
|
|
/// getPointerToNamedFunction - This method returns the address of the specified
|
2005-04-21 22:55:34 +00:00
|
|
|
/// function by using the dynamic loader interface. As such it is only useful
|
2003-10-10 17:02:42 +00:00
|
|
|
/// for resolving library symbols, not code generated symbols.
|
2003-05-14 13:26:47 +00:00
|
|
|
///
|
2003-12-20 01:46:27 +00:00
|
|
|
void *JIT::getPointerToNamedFunction(const std::string &Name) {
|
2006-06-01 17:12:14 +00:00
|
|
|
// Check to see if this is one of the functions we want to intercept. Note,
|
|
|
|
// we cast to intptr_t here to silence a -pedantic warning that complains
|
|
|
|
// about casting a function pointer to a normal pointer.
|
|
|
|
if (Name == "exit") return (void*)(intptr_t)&jit_exit;
|
|
|
|
if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
|
2003-05-14 13:26:47 +00:00
|
|
|
|
2006-07-28 21:11:31 +00:00
|
|
|
const char *NameStr = Name.c_str();
|
|
|
|
// If this is an asm specifier, skip the sentinal.
|
|
|
|
if (NameStr[0] == 1) ++NameStr;
|
|
|
|
|
2003-05-14 13:26:47 +00:00
|
|
|
// If it's an external function, look it up in the process image...
|
2006-07-28 21:11:31 +00:00
|
|
|
void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
|
2004-06-01 21:49:00 +00:00
|
|
|
if (Ptr) return Ptr;
|
2006-07-28 21:11:31 +00:00
|
|
|
|
|
|
|
// If it wasn't found and if it starts with an underscore ('_') character, and
|
|
|
|
// has an asm specifier, try again without the underscore.
|
|
|
|
if (Name[0] == 1 && NameStr[0] == '_') {
|
|
|
|
Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
|
|
|
|
if (Ptr) return Ptr;
|
|
|
|
}
|
2007-10-22 02:50:12 +00:00
|
|
|
|
2007-11-27 20:41:32 +00:00
|
|
|
// darwin/ppc adds $LDBLStub suffixes to various symbols like printf. These
|
|
|
|
// are references to hidden visibility symbols that dlsym cannot resolve. If
|
|
|
|
// we have one of these, strip off $LDBLStub and try again.
|
|
|
|
#if defined(__APPLE__) && defined(__ppc__)
|
|
|
|
if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
|
2007-11-27 20:45:25 +00:00
|
|
|
memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
|
|
|
|
// First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
|
|
|
|
// This mirrors logic in libSystemStubs.a.
|
|
|
|
std::string Prefix = std::string(Name.begin(), Name.end()-9);
|
|
|
|
if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128"))
|
|
|
|
return Ptr;
|
2007-11-28 18:30:18 +00:00
|
|
|
if (void *Ptr = getPointerToNamedFunction(Prefix))
|
|
|
|
return Ptr;
|
2007-11-27 20:45:25 +00:00
|
|
|
}
|
2007-11-27 20:41:32 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-22 02:50:12 +00:00
|
|
|
/// If a LazyFunctionCreator is installed, use it to get/create the function.
|
|
|
|
if (LazyFunctionCreator)
|
|
|
|
if (void *RP = LazyFunctionCreator(Name))
|
|
|
|
return RP;
|
2004-06-01 21:49:00 +00:00
|
|
|
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "ERROR: Program used external function '" << Name
|
|
|
|
<< "' which could not be resolved!\n";
|
2004-06-01 21:49:00 +00:00
|
|
|
abort();
|
|
|
|
return 0;
|
2003-05-14 13:26:47 +00:00
|
|
|
}
|