Fix compilation problems with previous checking *blush*
[oota-llvm.git] / lib / ExecutionEngine / JIT / Intercept.cpp
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
13 #include <iostream>
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)) {
29   return atexit(Fn);    // Do nothing for now.
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...
40   if (Name == "exit") return (void*)&jit_exit;
41   if (Name == "at_exit") return (void*)&jit_atexit;
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