2003-05-14 21:26:47 +08:00
|
|
|
//===-- Intercept.cpp - System function interception routines -------------===//
|
2003-10-21 03:43:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-05-14 21:26:47 +08:00
|
|
|
//
|
|
|
|
// If a function call occurs to an external function, the JIT is designed to use
|
2003-10-11 01:02:42 +08:00
|
|
|
// the dynamic loader interface to find a function to call. This is useful for
|
2003-05-14 21:26:47 +08:00
|
|
|
// calling system calls and library functions that are not available in LLVM.
|
|
|
|
// Some system calls, however, need to be handled specially. For this reason,
|
|
|
|
// we intercept some of them here and use our own stubs to handle them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "JIT.h"
|
2003-10-11 01:02:42 +08:00
|
|
|
#include "Support/DynamicLinker.h"
|
2003-05-14 21:27:36 +08:00
|
|
|
#include <iostream>
|
2003-12-08 16:06:28 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// AtExitHandlers - List of functions to call when the program exits,
|
|
|
|
// registered with the atexit() library function.
|
|
|
|
static std::vector<void (*)()> AtExitHandlers;
|
2003-05-14 21:53:40 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
/// runAtExitHandlers - Run any functions registered by the program's
|
|
|
|
/// calls to atexit(3), which we intercept and store in
|
|
|
|
/// AtExitHandlers.
|
|
|
|
///
|
2003-12-20 09:46:27 +08:00
|
|
|
void JIT::runAtExitHandlers() {
|
2003-09-06 02:42:01 +08:00
|
|
|
while (!AtExitHandlers.empty()) {
|
|
|
|
void (*Fn)() = AtExitHandlers.back();
|
|
|
|
AtExitHandlers.pop_back();
|
2003-05-14 21:53:40 +08:00
|
|
|
Fn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-14 21:26:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-06 02:42:01 +08:00
|
|
|
// Function stubs that are invoked instead of certain library calls
|
2003-05-14 21:26:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// NoopFn - Used if we have nothing else to call...
|
|
|
|
static void NoopFn() {}
|
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// jit_exit - Used to intercept the "exit" library call.
|
2003-05-14 21:26:47 +08:00
|
|
|
static void jit_exit(int Status) {
|
2003-12-20 09:46:27 +08:00
|
|
|
JIT::runAtExitHandlers(); // Run atexit handlers...
|
2003-05-14 21:53:40 +08:00
|
|
|
exit(Status);
|
2003-05-14 21:26:47 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// jit_atexit - Used to intercept the "atexit" library call.
|
2003-05-14 21:26:47 +08:00
|
|
|
static int jit_atexit(void (*Fn)(void)) {
|
2003-09-06 02:42:01 +08:00
|
|
|
AtExitHandlers.push_back(Fn); // Take note of atexit handler...
|
2003-05-14 21:53:40 +08:00
|
|
|
return 0; // Always successful
|
2003-05-14 21:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// getPointerToNamedFunction - This method returns the address of the specified
|
2003-10-11 01:02:42 +08:00
|
|
|
/// function by using the dynamic loader interface. As such it is only useful
|
|
|
|
/// for resolving library symbols, not code generated symbols.
|
2003-05-14 21:26:47 +08:00
|
|
|
///
|
2003-12-20 09:46:27 +08:00
|
|
|
void *JIT::getPointerToNamedFunction(const std::string &Name) {
|
2003-05-14 21:26:47 +08:00
|
|
|
// Check to see if this is one of the functions we want to intercept...
|
2003-05-14 21:27:36 +08:00
|
|
|
if (Name == "exit") return (void*)&jit_exit;
|
2003-05-14 21:53:40 +08:00
|
|
|
if (Name == "atexit") return (void*)&jit_atexit;
|
2003-05-14 21:26:47 +08:00
|
|
|
|
|
|
|
// If it's an external function, look it up in the process image...
|
2003-10-11 01:02:42 +08:00
|
|
|
void *Ptr = GetAddressOfSymbol(Name);
|
2003-05-14 21:26:47 +08:00
|
|
|
if (Ptr == 0) {
|
|
|
|
std::cerr << "WARNING: Cannot resolve fn '" << Name
|
|
|
|
<< "' using a dummy noop function instead!\n";
|
|
|
|
Ptr = (void*)NoopFn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ptr;
|
|
|
|
}
|