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 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 15 | // AtExitHandlers - List of functions to call when the program exits, |
| 16 | // registered with the atexit() library function. |
| 17 | static std::vector<void (*)()> AtExitHandlers; |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 18 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 19 | /// runAtExitHandlers - Run any functions registered by the program's |
| 20 | /// calls to atexit(3), which we intercept and store in |
| 21 | /// AtExitHandlers. |
| 22 | /// |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 23 | void VM::runAtExitHandlers() { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 24 | while (!AtExitHandlers.empty()) { |
| 25 | void (*Fn)() = AtExitHandlers.back(); |
| 26 | AtExitHandlers.pop_back(); |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 27 | Fn(); |
| 28 | } |
| 29 | } |
| 30 | |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 32 | // Function stubs that are invoked instead of certain library calls |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | // NoopFn - Used if we have nothing else to call... |
| 36 | static void NoopFn() {} |
| 37 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 38 | // jit_exit - Used to intercept the "exit" library call. |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 39 | static void jit_exit(int Status) { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 40 | VM::runAtExitHandlers(); // Run atexit handlers... |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 41 | exit(Status); |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 44 | // jit_atexit - Used to intercept the "atexit" library call. |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 45 | static int jit_atexit(void (*Fn)(void)) { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 46 | AtExitHandlers.push_back(Fn); // Take note of atexit handler... |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 47 | return 0; // Always successful |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | // |
| 52 | /// getPointerToNamedFunction - This method returns the address of the specified |
| 53 | /// function by using the dlsym function call. As such it is only useful for |
| 54 | /// resolving library symbols, not code generated symbols. |
| 55 | /// |
| 56 | void *VM::getPointerToNamedFunction(const std::string &Name) { |
| 57 | // 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] | 58 | if (Name == "exit") return (void*)&jit_exit; |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 59 | if (Name == "atexit") return (void*)&jit_atexit; |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 60 | |
| 61 | // If it's an external function, look it up in the process image... |
Misha Brukman | d94a50f | 2003-07-28 19:07:30 +0000 | [diff] [blame] | 62 | // On Sparc, RTLD_SELF is already defined and it's not zero |
| 63 | // Linux/x86 wants to use a 0, other systems may differ |
| 64 | #ifndef RTLD_SELF |
| 65 | #define RTLD_SELF 0 |
Misha Brukman | 88fe358 | 2003-06-04 01:57:22 +0000 | [diff] [blame] | 66 | #endif |
Misha Brukman | d94a50f | 2003-07-28 19:07:30 +0000 | [diff] [blame] | 67 | void *Ptr = dlsym(RTLD_SELF, Name.c_str()); |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 68 | if (Ptr == 0) { |
| 69 | std::cerr << "WARNING: Cannot resolve fn '" << Name |
| 70 | << "' using a dummy noop function instead!\n"; |
| 71 | Ptr = (void*)NoopFn; |
| 72 | } |
| 73 | |
| 74 | return Ptr; |
| 75 | } |