blob: 0b1b976836b5a2b43c17b567e048ea2cfda1c59e [file] [log] [blame]
Chris Lattner6701a862003-05-14 13:26:47 +00001//===-- Intercept.cpp - System function interception routines -------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner6701a862003-05-14 13:26:47 +00009//
10// If a function call occurs to an external function, the JIT is designed to use
Brian Gaeke322cdb22003-10-10 17:02:42 +000011// the dynamic loader interface to find a function to call. This is useful for
Chris Lattner6701a862003-05-14 13:26:47 +000012// calling system calls and library functions that are not available in LLVM.
13// Some system calls, however, need to be handled specially. For this reason,
14// we intercept some of them here and use our own stubs to handle them.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner4d326fa2003-12-20 01:46:27 +000018#include "JIT.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000019#include "Support/DynamicLinker.h"
Chris Lattner1b722162003-05-14 13:27:36 +000020#include <iostream>
Chris Lattnerc19aade2003-12-08 08:06:28 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Brian Gaeke70975ee2003-09-05 18:42:01 +000023// AtExitHandlers - List of functions to call when the program exits,
24// registered with the atexit() library function.
25static std::vector<void (*)()> AtExitHandlers;
Chris Lattner22080f92003-05-14 13:53:40 +000026
Brian Gaeke70975ee2003-09-05 18:42:01 +000027/// runAtExitHandlers - Run any functions registered by the program's
28/// calls to atexit(3), which we intercept and store in
29/// AtExitHandlers.
30///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000031static void runAtExitHandlers() {
Brian Gaeke70975ee2003-09-05 18:42:01 +000032 while (!AtExitHandlers.empty()) {
33 void (*Fn)() = AtExitHandlers.back();
34 AtExitHandlers.pop_back();
Chris Lattner22080f92003-05-14 13:53:40 +000035 Fn();
36 }
37}
38
Chris Lattner6701a862003-05-14 13:26:47 +000039//===----------------------------------------------------------------------===//
Brian Gaeke70975ee2003-09-05 18:42:01 +000040// Function stubs that are invoked instead of certain library calls
Chris Lattner6701a862003-05-14 13:26:47 +000041//===----------------------------------------------------------------------===//
42
43// NoopFn - Used if we have nothing else to call...
44static void NoopFn() {}
45
Brian Gaeke70975ee2003-09-05 18:42:01 +000046// jit_exit - Used to intercept the "exit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000047static void jit_exit(int Status) {
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000048 runAtExitHandlers(); // Run atexit handlers...
Chris Lattner22080f92003-05-14 13:53:40 +000049 exit(Status);
Chris Lattner6701a862003-05-14 13:26:47 +000050}
51
Brian Gaeke70975ee2003-09-05 18:42:01 +000052// jit_atexit - Used to intercept the "atexit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000053static int jit_atexit(void (*Fn)(void)) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000054 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
Chris Lattner22080f92003-05-14 13:53:40 +000055 return 0; // Always successful
Chris Lattner6701a862003-05-14 13:26:47 +000056}
57
58//===----------------------------------------------------------------------===//
59//
60/// getPointerToNamedFunction - This method returns the address of the specified
Brian Gaeke322cdb22003-10-10 17:02:42 +000061/// function by using the dynamic loader interface. As such it is only useful
62/// for resolving library symbols, not code generated symbols.
Chris Lattner6701a862003-05-14 13:26:47 +000063///
Chris Lattner4d326fa2003-12-20 01:46:27 +000064void *JIT::getPointerToNamedFunction(const std::string &Name) {
Chris Lattner6701a862003-05-14 13:26:47 +000065 // Check to see if this is one of the functions we want to intercept...
Chris Lattner1b722162003-05-14 13:27:36 +000066 if (Name == "exit") return (void*)&jit_exit;
Chris Lattner22080f92003-05-14 13:53:40 +000067 if (Name == "atexit") return (void*)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000068
69 // If it's an external function, look it up in the process image...
Brian Gaeke322cdb22003-10-10 17:02:42 +000070 void *Ptr = GetAddressOfSymbol(Name);
Chris Lattner6701a862003-05-14 13:26:47 +000071 if (Ptr == 0) {
72 std::cerr << "WARNING: Cannot resolve fn '" << Name
73 << "' using a dummy noop function instead!\n";
74 Ptr = (void*)NoopFn;
75 }
76
77 return Ptr;
78}