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" |
| 12 | #include <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 | |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // Function stubs that are invoked instead of raw system calls |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | // NoopFn - Used if we have nothing else to call... |
| 20 | static void NoopFn() {} |
| 21 | |
| 22 | // jit_exit - Used to intercept the "exit" system call. |
| 23 | static void jit_exit(int Status) { |
| 24 | exit(Status); // Do nothing for now. |
| 25 | } |
| 26 | |
| 27 | // jit_atexit - Used to intercept the "at_exit" system call. |
| 28 | static int jit_atexit(void (*Fn)(void)) { |
Chris Lattner | 1b72216 | 2003-05-14 13:27:36 +0000 | [diff] [blame^] | 29 | return atexit(Fn); // Do nothing for now. |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // |
| 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. |
| 37 | /// |
| 38 | void *VM::getPointerToNamedFunction(const std::string &Name) { |
| 39 | // 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^] | 40 | if (Name == "exit") return (void*)&jit_exit; |
| 41 | if (Name == "at_exit") return (void*)&jit_atexit; |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 42 | |
| 43 | // If it's an external function, look it up in the process image... |
| 44 | void *Ptr = dlsym(0, Name.c_str()); |
| 45 | if (Ptr == 0) { |
| 46 | std::cerr << "WARNING: Cannot resolve fn '" << Name |
| 47 | << "' using a dummy noop function instead!\n"; |
| 48 | Ptr = (void*)NoopFn; |
| 49 | } |
| 50 | |
| 51 | return Ptr; |
| 52 | } |
| 53 | |