blob: 09cbe28740a57779f45af0e9f127e9fc431ea929 [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
Brian Gaeke70975ee2003-09-05 18:42:01 +000015// AtExitHandlers - List of functions to call when the program exits,
16// registered with the atexit() library function.
17static std::vector<void (*)()> AtExitHandlers;
Chris Lattner22080f92003-05-14 13:53:40 +000018
Brian Gaeke70975ee2003-09-05 18:42:01 +000019/// runAtExitHandlers - Run any functions registered by the program's
20/// calls to atexit(3), which we intercept and store in
21/// AtExitHandlers.
22///
Chris Lattner22080f92003-05-14 13:53:40 +000023void VM::runAtExitHandlers() {
Brian Gaeke70975ee2003-09-05 18:42:01 +000024 while (!AtExitHandlers.empty()) {
25 void (*Fn)() = AtExitHandlers.back();
26 AtExitHandlers.pop_back();
Chris Lattner22080f92003-05-14 13:53:40 +000027 Fn();
28 }
29}
30
Chris Lattner6701a862003-05-14 13:26:47 +000031//===----------------------------------------------------------------------===//
Brian Gaeke70975ee2003-09-05 18:42:01 +000032// Function stubs that are invoked instead of certain library calls
Chris Lattner6701a862003-05-14 13:26:47 +000033//===----------------------------------------------------------------------===//
34
35// NoopFn - Used if we have nothing else to call...
36static void NoopFn() {}
37
Brian Gaeke70975ee2003-09-05 18:42:01 +000038// jit_exit - Used to intercept the "exit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000039static void jit_exit(int Status) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000040 VM::runAtExitHandlers(); // Run atexit handlers...
Chris Lattner22080f92003-05-14 13:53:40 +000041 exit(Status);
Chris Lattner6701a862003-05-14 13:26:47 +000042}
43
Brian Gaeke70975ee2003-09-05 18:42:01 +000044// jit_atexit - Used to intercept the "atexit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000045static int jit_atexit(void (*Fn)(void)) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000046 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
Chris Lattner22080f92003-05-14 13:53:40 +000047 return 0; // Always successful
Chris Lattner6701a862003-05-14 13:26:47 +000048}
49
50//===----------------------------------------------------------------------===//
51//
52/// getPointerToNamedFunction - This method returns the address of the specified
53/// function by using the dlsym function call. As such it is only useful for
54/// resolving library symbols, not code generated symbols.
55///
56void *VM::getPointerToNamedFunction(const std::string &Name) {
57 // Check to see if this is one of the functions we want to intercept...
Chris Lattner1b722162003-05-14 13:27:36 +000058 if (Name == "exit") return (void*)&jit_exit;
Chris Lattner22080f92003-05-14 13:53:40 +000059 if (Name == "atexit") return (void*)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000060
61 // If it's an external function, look it up in the process image...
Misha Brukmand94a50f2003-07-28 19:07:30 +000062 // On Sparc, RTLD_SELF is already defined and it's not zero
63 // Linux/x86 wants to use a 0, other systems may differ
64#ifndef RTLD_SELF
65#define RTLD_SELF 0
Misha Brukman88fe3582003-06-04 01:57:22 +000066#endif
Misha Brukmand94a50f2003-07-28 19:07:30 +000067 void *Ptr = dlsym(RTLD_SELF, Name.c_str());
Chris Lattner6701a862003-05-14 13:26:47 +000068 if (Ptr == 0) {
69 std::cerr << "WARNING: Cannot resolve fn '" << Name
70 << "' using a dummy noop function instead!\n";
71 Ptr = (void*)NoopFn;
72 }
73
74 return Ptr;
75}