blob: 0877467ab597d092058c89fe11cdb297f6cce4e7 [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"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000019#include "llvm/System/DynamicLibrary.h"
Chris Lattner1b722162003-05-14 13:27:36 +000020#include <iostream>
Reid Spencer382343d2004-12-17 18:56:29 +000021#if defined(__linux__)
Brian Gaeke00f0a292004-03-09 05:22:10 +000022#include <sys/stat.h>
Reid Spencer382343d2004-12-17 18:56:29 +000023#endif
Chris Lattnerc19aade2003-12-08 08:06:28 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Brian Gaeke70975ee2003-09-05 18:42:01 +000026// AtExitHandlers - List of functions to call when the program exits,
27// registered with the atexit() library function.
28static std::vector<void (*)()> AtExitHandlers;
Chris Lattner22080f92003-05-14 13:53:40 +000029
Brian Gaeke70975ee2003-09-05 18:42:01 +000030/// runAtExitHandlers - Run any functions registered by the program's
31/// calls to atexit(3), which we intercept and store in
32/// AtExitHandlers.
33///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000034static void runAtExitHandlers() {
Brian Gaeke70975ee2003-09-05 18:42:01 +000035 while (!AtExitHandlers.empty()) {
36 void (*Fn)() = AtExitHandlers.back();
37 AtExitHandlers.pop_back();
Chris Lattner22080f92003-05-14 13:53:40 +000038 Fn();
39 }
40}
41
Chris Lattner6701a862003-05-14 13:26:47 +000042//===----------------------------------------------------------------------===//
Brian Gaeke70975ee2003-09-05 18:42:01 +000043// Function stubs that are invoked instead of certain library calls
Chris Lattner6701a862003-05-14 13:26:47 +000044//===----------------------------------------------------------------------===//
45
Brian Gaeke00f0a292004-03-09 05:22:10 +000046// Force the following functions to be linked in to anything that uses the
47// JIT. This is a hack designed to work around the all-too-clever Glibc
48// strategy of making these functions work differently when inlined vs. when
49// not inlined, and hiding their real definitions in a separate archive file
50// that the dynamic linker can't see. For more info, search for
51// 'libc_nonshared.a' on Google, or read http://llvm.cs.uiuc.edu/PR274.
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000052#if defined(__linux__)
Brian Gaeke00f0a292004-03-09 05:22:10 +000053void *FunctionPointers[] = {
54 (void *) stat,
Brian Gaeke00f0a292004-03-09 05:22:10 +000055 (void *) fstat,
Brian Gaeke00f0a292004-03-09 05:22:10 +000056 (void *) lstat,
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000057 (void *) stat64,
58 (void *) fstat64,
Brian Gaeke00f0a292004-03-09 05:22:10 +000059 (void *) lstat64,
60 (void *) atexit,
61 (void *) mknod
62};
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000063#endif // __linux__
Brian Gaeke00f0a292004-03-09 05:22:10 +000064
Chris Lattnerabf1cd32004-06-01 21:49:00 +000065// __mainFunc - If the program does not have a linked in __main function, allow
66// it to run, but print a warning.
67static void __mainFunc() {
68 fprintf(stderr, "WARNING: Program called __main but was not linked to "
69 "libcrtend.a.\nThis probably won't hurt anything unless the "
70 "program is written in C++.\n");
71}
Chris Lattner6701a862003-05-14 13:26:47 +000072
Brian Gaeke70975ee2003-09-05 18:42:01 +000073// jit_exit - Used to intercept the "exit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000074static void jit_exit(int Status) {
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000075 runAtExitHandlers(); // Run atexit handlers...
Chris Lattner22080f92003-05-14 13:53:40 +000076 exit(Status);
Chris Lattner6701a862003-05-14 13:26:47 +000077}
78
Brian Gaeke70975ee2003-09-05 18:42:01 +000079// jit_atexit - Used to intercept the "atexit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000080static int jit_atexit(void (*Fn)(void)) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000081 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
Chris Lattner22080f92003-05-14 13:53:40 +000082 return 0; // Always successful
Chris Lattner6701a862003-05-14 13:26:47 +000083}
84
85//===----------------------------------------------------------------------===//
86//
87/// getPointerToNamedFunction - This method returns the address of the specified
Brian Gaeke322cdb22003-10-10 17:02:42 +000088/// function by using the dynamic loader interface. As such it is only useful
89/// for resolving library symbols, not code generated symbols.
Chris Lattner6701a862003-05-14 13:26:47 +000090///
Chris Lattner4d326fa2003-12-20 01:46:27 +000091void *JIT::getPointerToNamedFunction(const std::string &Name) {
Chris Lattner6701a862003-05-14 13:26:47 +000092 // Check to see if this is one of the functions we want to intercept...
Chris Lattner1b722162003-05-14 13:27:36 +000093 if (Name == "exit") return (void*)&jit_exit;
Chris Lattner22080f92003-05-14 13:53:40 +000094 if (Name == "atexit") return (void*)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000095
Chris Lattnerabf1cd32004-06-01 21:49:00 +000096 // If the program does not have a linked in __main function, allow it to run,
97 // but print a warning.
98 if (Name == "__main") return (void*)&__mainFunc;
99
Chris Lattner6701a862003-05-14 13:26:47 +0000100 // If it's an external function, look it up in the process image...
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000101 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(Name);
Chris Lattnerabf1cd32004-06-01 21:49:00 +0000102 if (Ptr) return Ptr;
103
104 std::cerr << "ERROR: Program used external function '" << Name
105 << "' which could not be resolved!\n";
106 abort();
107 return 0;
Chris Lattner6701a862003-05-14 13:26:47 +0000108}