1 //===-- Intercept.cpp - System function interception routines -------------===//
3 // If a function call occurs to an external function, the JIT is designed to use
4 // dlsym on the current process to find a function to call. This is useful for
5 // calling system calls and library functions that are not available in LLVM.
6 // Some system calls, however, need to be handled specially. For this reason,
7 // we intercept some of them here and use our own stubs to handle them.
9 //===----------------------------------------------------------------------===//
12 #include "Config/dlfcn.h" // dlsym access
15 // AtExitList - List of functions registered with the at_exit function
16 static std::vector<void (*)()> AtExitList;
18 void VM::runAtExitHandlers() {
19 while (!AtExitList.empty()) {
20 void (*Fn)() = AtExitList.back();
21 AtExitList.pop_back();
26 //===----------------------------------------------------------------------===//
27 // Function stubs that are invoked instead of raw system calls
28 //===----------------------------------------------------------------------===//
30 // NoopFn - Used if we have nothing else to call...
31 static void NoopFn() {}
33 // jit_exit - Used to intercept the "exit" system call.
34 static void jit_exit(int Status) {
35 VM::runAtExitHandlers(); // Run at_exit handlers...
39 // jit_atexit - Used to intercept the "at_exit" system call.
40 static int jit_atexit(void (*Fn)(void)) {
41 AtExitList.push_back(Fn); // Take note of at_exit handler...
42 return 0; // Always successful
45 //===----------------------------------------------------------------------===//
47 /// getPointerToNamedFunction - This method returns the address of the specified
48 /// function by using the dlsym function call. As such it is only useful for
49 /// resolving library symbols, not code generated symbols.
51 void *VM::getPointerToNamedFunction(const std::string &Name) {
52 // Check to see if this is one of the functions we want to intercept...
53 if (Name == "exit") return (void*)&jit_exit;
54 if (Name == "atexit") return (void*)&jit_atexit;
56 // If it's an external function, look it up in the process image...
57 // On Sparc, RTLD_SELF is already defined and it's not zero
58 // Linux/x86 wants to use a 0, other systems may differ
62 void *Ptr = dlsym(RTLD_SELF, Name.c_str());
64 std::cerr << "WARNING: Cannot resolve fn '" << Name
65 << "' using a dummy noop function instead!\n";