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 <dlfcn.h> // dlsym access
15 //===----------------------------------------------------------------------===//
16 // Function stubs that are invoked instead of raw system calls
17 //===----------------------------------------------------------------------===//
19 // NoopFn - Used if we have nothing else to call...
20 static void NoopFn() {}
22 // jit_exit - Used to intercept the "exit" system call.
23 static void jit_exit(int Status) {
24 exit(Status); // Do nothing for now.
27 // jit_atexit - Used to intercept the "at_exit" system call.
28 static int jit_atexit(void (*Fn)(void)) {
29 return atexit(Fn); // Do nothing for now.
32 //===----------------------------------------------------------------------===//
34 /// getPointerToNamedFunction - This method returns the address of the specified
35 /// function by using the dlsym function call. As such it is only useful for
36 /// resolving library symbols, not code generated symbols.
38 void *VM::getPointerToNamedFunction(const std::string &Name) {
39 // Check to see if this is one of the functions we want to intercept...
40 if (Name == "exit") return (void*)&jit_exit;
41 if (Name == "at_exit") return (void*)&jit_atexit;
43 // If it's an external function, look it up in the process image...
44 void *Ptr = dlsym(0, Name.c_str());
46 std::cerr << "WARNING: Cannot resolve fn '" << Name
47 << "' using a dummy noop function instead!\n";