blob: 448ff2d3563c9c092155522e23a5ac09e894a5db [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
Chris Lattner1b722162003-05-14 13:27:36 +000013#include <iostream>
Chris Lattner6701a862003-05-14 13:26:47 +000014
15//===----------------------------------------------------------------------===//
16// Function stubs that are invoked instead of raw system calls
17//===----------------------------------------------------------------------===//
18
19// NoopFn - Used if we have nothing else to call...
20static void NoopFn() {}
21
22// jit_exit - Used to intercept the "exit" system call.
23static 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.
28static int jit_atexit(void (*Fn)(void)) {
Chris Lattner1b722162003-05-14 13:27:36 +000029 return atexit(Fn); // Do nothing for now.
Chris Lattner6701a862003-05-14 13:26:47 +000030}
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///
38void *VM::getPointerToNamedFunction(const std::string &Name) {
39 // Check to see if this is one of the functions we want to intercept...
Chris Lattner1b722162003-05-14 13:27:36 +000040 if (Name == "exit") return (void*)&jit_exit;
41 if (Name == "at_exit") return (void*)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000042
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