Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 1 | //===-- Intercept.cpp - System function interception routines -------------===// |
| 2 | // |
| 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. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "VM.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 12 | #include "Config/dlfcn.h" // dlsym access |
Chris Lattner | 1b72216 | 2003-05-14 13:27:36 +0000 | [diff] [blame] | 13 | #include <iostream> |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 14 | |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 15 | // AtExitList - List of functions registered with the at_exit function |
| 16 | static std::vector<void (*)()> AtExitList; |
| 17 | |
| 18 | void VM::runAtExitHandlers() { |
| 19 | while (!AtExitList.empty()) { |
| 20 | void (*Fn)() = AtExitList.back(); |
| 21 | AtExitList.pop_back(); |
| 22 | Fn(); |
| 23 | } |
| 24 | } |
| 25 | |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Function stubs that are invoked instead of raw system calls |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | // NoopFn - Used if we have nothing else to call... |
| 31 | static void NoopFn() {} |
| 32 | |
| 33 | // jit_exit - Used to intercept the "exit" system call. |
| 34 | static void jit_exit(int Status) { |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 35 | VM::runAtExitHandlers(); // Run at_exit handlers... |
| 36 | exit(Status); |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // jit_atexit - Used to intercept the "at_exit" system call. |
| 40 | static int jit_atexit(void (*Fn)(void)) { |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 41 | AtExitList.push_back(Fn); // Take note of at_exit handler... |
| 42 | return 0; // Always successful |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | // |
| 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. |
| 50 | /// |
| 51 | void *VM::getPointerToNamedFunction(const std::string &Name) { |
| 52 | // Check to see if this is one of the functions we want to intercept... |
Chris Lattner | 1b72216 | 2003-05-14 13:27:36 +0000 | [diff] [blame] | 53 | if (Name == "exit") return (void*)&jit_exit; |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 54 | if (Name == "atexit") return (void*)&jit_atexit; |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 55 | |
| 56 | // If it's an external function, look it up in the process image... |
Misha Brukman | 88fe358 | 2003-06-04 01:57:22 +0000 | [diff] [blame] | 57 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 58 | void *Ptr = dlsym(0, Name.c_str()); |
Misha Brukman | 88fe358 | 2003-06-04 01:57:22 +0000 | [diff] [blame] | 59 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 60 | void *Ptr = dlsym(RTLD_SELF, Name.c_str()); |
| 61 | #endif |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 62 | if (Ptr == 0) { |
| 63 | std::cerr << "WARNING: Cannot resolve fn '" << Name |
| 64 | << "' using a dummy noop function instead!\n"; |
| 65 | Ptr = (void*)NoopFn; |
| 66 | } |
| 67 | |
| 68 | return Ptr; |
| 69 | } |
| 70 | |