blob: 490861d66c418a75ad6091a4030d4990258001b5 [file] [log] [blame]
Chris Lattner6701a862003-05-14 13:26:47 +00001//===-- Intercept.cpp - System function interception routines -------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencer099557b2004-12-20 06:34:02 +000020#include "llvm/Config/config.h"
Chris Lattner1b722162003-05-14 13:27:36 +000021#include <iostream>
Chris Lattnerc19aade2003-12-08 08:06:28 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Brian Gaeke70975ee2003-09-05 18:42:01 +000024// AtExitHandlers - List of functions to call when the program exits,
25// registered with the atexit() library function.
26static std::vector<void (*)()> AtExitHandlers;
Chris Lattner22080f92003-05-14 13:53:40 +000027
Brian Gaeke70975ee2003-09-05 18:42:01 +000028/// runAtExitHandlers - Run any functions registered by the program's
29/// calls to atexit(3), which we intercept and store in
30/// AtExitHandlers.
31///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000032static void runAtExitHandlers() {
Brian Gaeke70975ee2003-09-05 18:42:01 +000033 while (!AtExitHandlers.empty()) {
34 void (*Fn)() = AtExitHandlers.back();
35 AtExitHandlers.pop_back();
Chris Lattner22080f92003-05-14 13:53:40 +000036 Fn();
37 }
38}
39
Chris Lattner6701a862003-05-14 13:26:47 +000040//===----------------------------------------------------------------------===//
Brian Gaeke70975ee2003-09-05 18:42:01 +000041// Function stubs that are invoked instead of certain library calls
Chris Lattner6701a862003-05-14 13:26:47 +000042//===----------------------------------------------------------------------===//
43
Brian Gaeke00f0a292004-03-09 05:22:10 +000044// Force the following functions to be linked in to anything that uses the
45// JIT. This is a hack designed to work around the all-too-clever Glibc
46// strategy of making these functions work differently when inlined vs. when
47// not inlined, and hiding their real definitions in a separate archive file
48// that the dynamic linker can't see. For more info, search for
Reid Spencer9dce2b32006-03-14 05:54:52 +000049// 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000050#if defined(__linux__)
Reid Spencer099557b2004-12-20 06:34:02 +000051#if defined(HAVE_SYS_STAT_H)
Reid Spencer6b4bd6b2004-12-17 19:09:16 +000052#include <sys/stat.h>
Misha Brukmanf976c852005-04-21 22:55:34 +000053#endif
Brian Gaeke00f0a292004-03-09 05:22:10 +000054void *FunctionPointers[] = {
55 (void *) stat,
Brian Gaeke00f0a292004-03-09 05:22:10 +000056 (void *) fstat,
Brian Gaeke00f0a292004-03-09 05:22:10 +000057 (void *) lstat,
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000058 (void *) stat64,
59 (void *) fstat64,
Brian Gaeke00f0a292004-03-09 05:22:10 +000060 (void *) lstat64,
61 (void *) atexit,
62 (void *) mknod
63};
Brian Gaeked0f3c5e2004-03-10 17:38:28 +000064#endif // __linux__
Brian Gaeke00f0a292004-03-09 05:22:10 +000065
Chris Lattnerabf1cd32004-06-01 21:49:00 +000066// __mainFunc - If the program does not have a linked in __main function, allow
67// it to run, but print a warning.
68static void __mainFunc() {
69 fprintf(stderr, "WARNING: Program called __main but was not linked to "
70 "libcrtend.a.\nThis probably won't hurt anything unless the "
71 "program is written in C++.\n");
72}
Chris Lattner6701a862003-05-14 13:26:47 +000073
Brian Gaeke70975ee2003-09-05 18:42:01 +000074// jit_exit - Used to intercept the "exit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000075static void jit_exit(int Status) {
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000076 runAtExitHandlers(); // Run atexit handlers...
Chris Lattner22080f92003-05-14 13:53:40 +000077 exit(Status);
Chris Lattner6701a862003-05-14 13:26:47 +000078}
79
Brian Gaeke70975ee2003-09-05 18:42:01 +000080// jit_atexit - Used to intercept the "atexit" library call.
Chris Lattner6701a862003-05-14 13:26:47 +000081static int jit_atexit(void (*Fn)(void)) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000082 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
Chris Lattner22080f92003-05-14 13:53:40 +000083 return 0; // Always successful
Chris Lattner6701a862003-05-14 13:26:47 +000084}
85
86//===----------------------------------------------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +000087//
Chris Lattner6701a862003-05-14 13:26:47 +000088/// getPointerToNamedFunction - This method returns the address of the specified
Misha Brukmanf976c852005-04-21 22:55:34 +000089/// function by using the dynamic loader interface. As such it is only useful
Brian Gaeke322cdb22003-10-10 17:02:42 +000090/// for resolving library symbols, not code generated symbols.
Chris Lattner6701a862003-05-14 13:26:47 +000091///
Chris Lattner4d326fa2003-12-20 01:46:27 +000092void *JIT::getPointerToNamedFunction(const std::string &Name) {
Chris Lattnerfea34a12006-06-01 17:12:14 +000093 // Check to see if this is one of the functions we want to intercept. Note,
94 // we cast to intptr_t here to silence a -pedantic warning that complains
95 // about casting a function pointer to a normal pointer.
96 if (Name == "exit") return (void*)(intptr_t)&jit_exit;
97 if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
Chris Lattner6701a862003-05-14 13:26:47 +000098
Chris Lattnerabf1cd32004-06-01 21:49:00 +000099 // If the program does not have a linked in __main function, allow it to run,
100 // but print a warning.
Chris Lattnerfea34a12006-06-01 17:12:14 +0000101 if (Name == "__main") return (void*)(intptr_t)&__mainFunc;
Chris Lattnerabf1cd32004-06-01 21:49:00 +0000102
Chris Lattner6701a862003-05-14 13:26:47 +0000103 // If it's an external function, look it up in the process image...
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000104 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(Name);
Chris Lattnerabf1cd32004-06-01 21:49:00 +0000105 if (Ptr) return Ptr;
106
107 std::cerr << "ERROR: Program used external function '" << Name
108 << "' which could not be resolved!\n";
109 abort();
110 return 0;
Chris Lattner6701a862003-05-14 13:26:47 +0000111}