blob: 6df6088a3a89c96b3119d896f3eeb3ae69a0ce94 [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
Brian Gaeke322cdb22003-10-10 17:02:42 +00004// the dynamic loader interface to find a function to call. This is useful for
Chris Lattner6701a862003-05-14 13:26:47 +00005// 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"
Brian Gaeke322cdb22003-10-10 17:02:42 +000012#include "Support/DynamicLinker.h"
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
Brian Gaeke322cdb22003-10-10 17:02:42 +000053/// function by using the dynamic loader interface. As such it is only useful
54/// for resolving library symbols, not code generated symbols.
Chris Lattner6701a862003-05-14 13:26:47 +000055///
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...
Brian Gaeke322cdb22003-10-10 17:02:42 +000062 void *Ptr = GetAddressOfSymbol(Name);
Chris Lattner6701a862003-05-14 13:26:47 +000063 if (Ptr == 0) {
64 std::cerr << "WARNING: Cannot resolve fn '" << Name
65 << "' using a dummy noop function instead!\n";
66 Ptr = (void*)NoopFn;
67 }
68
69 return Ptr;
70}