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