blob: ba49bbde05d2f23503966b0374e57bc9901f77b0 [file] [log] [blame]
Chris Lattner6701a862003-05-14 13:26:47 +00001//===-- 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 Criswell7a73b802003-06-30 21:59:07 +000012#include "Config/dlfcn.h" // dlsym access
Chris Lattner1b722162003-05-14 13:27:36 +000013#include <iostream>
Chris Lattner6701a862003-05-14 13:26:47 +000014
Chris Lattner22080f92003-05-14 13:53:40 +000015// AtExitList - List of functions registered with the at_exit function
16static std::vector<void (*)()> AtExitList;
17
18void VM::runAtExitHandlers() {
19 while (!AtExitList.empty()) {
20 void (*Fn)() = AtExitList.back();
21 AtExitList.pop_back();
22 Fn();
23 }
24}
25
Chris Lattner6701a862003-05-14 13:26:47 +000026//===----------------------------------------------------------------------===//
27// Function stubs that are invoked instead of raw system calls
28//===----------------------------------------------------------------------===//
29
30// NoopFn - Used if we have nothing else to call...
31static void NoopFn() {}
32
33// jit_exit - Used to intercept the "exit" system call.
34static void jit_exit(int Status) {
Chris Lattner22080f92003-05-14 13:53:40 +000035 VM::runAtExitHandlers(); // Run at_exit handlers...
36 exit(Status);
Chris Lattner6701a862003-05-14 13:26:47 +000037}
38
39// jit_atexit - Used to intercept the "at_exit" system call.
40static int jit_atexit(void (*Fn)(void)) {
Chris Lattner22080f92003-05-14 13:53:40 +000041 AtExitList.push_back(Fn); // Take note of at_exit handler...
42 return 0; // Always successful
Chris Lattner6701a862003-05-14 13:26:47 +000043}
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///
51void *VM::getPointerToNamedFunction(const std::string &Name) {
52 // Check to see if this is one of the functions we want to intercept...
Chris Lattner1b722162003-05-14 13:27:36 +000053 if (Name == "exit") return (void*)&jit_exit;
Chris Lattner22080f92003-05-14 13:53:40 +000054 if (Name == "atexit") return (void*)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000055
56 // If it's an external function, look it up in the process image...
Misha Brukman88fe3582003-06-04 01:57:22 +000057#if defined(i386) || defined(__i386__) || defined(__x86__)
Chris Lattner6701a862003-05-14 13:26:47 +000058 void *Ptr = dlsym(0, Name.c_str());
Misha Brukman88fe3582003-06-04 01:57:22 +000059#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
60 void *Ptr = dlsym(RTLD_SELF, Name.c_str());
61#endif
Chris Lattner6701a862003-05-14 13:26:47 +000062 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