aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2003-05-14 13:53:40 +0000
committerChris Lattner <sabre@nondot.org>2003-05-14 13:53:40 +0000
commit22080f9f168b0129d0ed3a2a29a145e17723c3ba (patch)
tree1ee2ae5caa38865739d1561bc07cf7758c9bf071 /lib
parent1b72216a711cddf5fc53cf0a4f5407b41b8303c9 (diff)
downloadexternal_llvm-22080f9f168b0129d0ed3a2a29a145e17723c3ba.zip
external_llvm-22080f9f168b0129d0ed3a2a29a145e17723c3ba.tar.gz
external_llvm-22080f9f168b0129d0ed3a2a29a145e17723c3ba.tar.bz2
Add support for atexit handlers to the JIT, fixing 2003-05-14-AtExit.c
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6193 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/ExecutionEngine/JIT/Intercept.cpp19
-rw-r--r--lib/ExecutionEngine/JIT/JIT.cpp6
-rw-r--r--lib/ExecutionEngine/JIT/JIT.h7
-rw-r--r--lib/ExecutionEngine/JIT/VM.h7
4 files changed, 35 insertions, 4 deletions
diff --git a/lib/ExecutionEngine/JIT/Intercept.cpp b/lib/ExecutionEngine/JIT/Intercept.cpp
index 448ff2d..aa58186 100644
--- a/lib/ExecutionEngine/JIT/Intercept.cpp
+++ b/lib/ExecutionEngine/JIT/Intercept.cpp
@@ -12,6 +12,17 @@
#include <dlfcn.h> // dlsym access
#include <iostream>
+// AtExitList - List of functions registered with the at_exit function
+static std::vector<void (*)()> AtExitList;
+
+void VM::runAtExitHandlers() {
+ while (!AtExitList.empty()) {
+ void (*Fn)() = AtExitList.back();
+ AtExitList.pop_back();
+ Fn();
+ }
+}
+
//===----------------------------------------------------------------------===//
// Function stubs that are invoked instead of raw system calls
//===----------------------------------------------------------------------===//
@@ -21,12 +32,14 @@ static void NoopFn() {}
// jit_exit - Used to intercept the "exit" system call.
static void jit_exit(int Status) {
- exit(Status); // Do nothing for now.
+ VM::runAtExitHandlers(); // Run at_exit handlers...
+ exit(Status);
}
// jit_atexit - Used to intercept the "at_exit" system call.
static int jit_atexit(void (*Fn)(void)) {
- return atexit(Fn); // Do nothing for now.
+ AtExitList.push_back(Fn); // Take note of at_exit handler...
+ return 0; // Always successful
}
//===----------------------------------------------------------------------===//
@@ -38,7 +51,7 @@ static int jit_atexit(void (*Fn)(void)) {
void *VM::getPointerToNamedFunction(const std::string &Name) {
// Check to see if this is one of the functions we want to intercept...
if (Name == "exit") return (void*)&jit_exit;
- if (Name == "at_exit") return (void*)&jit_atexit;
+ if (Name == "atexit") return (void*)&jit_atexit;
// If it's an external function, look it up in the process image...
void *Ptr = dlsym(0, Name.c_str());
diff --git a/lib/ExecutionEngine/JIT/JIT.cpp b/lib/ExecutionEngine/JIT/JIT.cpp
index 39f305e..05aa934 100644
--- a/lib/ExecutionEngine/JIT/JIT.cpp
+++ b/lib/ExecutionEngine/JIT/JIT.cpp
@@ -50,5 +50,9 @@ int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
char **Argv = (char**)CreateArgv(Args);
// Call the main function...
- return PF(Args.size(), Argv);
+ int Result = PF(Args.size(), Argv);
+
+ // Run any atexit handlers now!
+ runAtExitHandlers();
+ return Result;
}
diff --git a/lib/ExecutionEngine/JIT/JIT.h b/lib/ExecutionEngine/JIT/JIT.h
index 17c4ddd..a720c96 100644
--- a/lib/ExecutionEngine/JIT/JIT.h
+++ b/lib/ExecutionEngine/JIT/JIT.h
@@ -27,6 +27,7 @@ class VM : public ExecutionEngine {
// handler to lazily patch up references...
//
std::map<void*, Function*> FunctionRefs;
+
public:
VM(Module *M, TargetMachine *tm);
~VM();
@@ -54,6 +55,12 @@ public:
// which causes lazy compilation of the target function.
//
static void CompilationCallback();
+
+ /// runAtExitHandlers - Before exiting the program, at_exit functions must be
+ /// called. This method calls them.
+ ///
+ static void runAtExitHandlers();
+
private:
static MachineCodeEmitter *createEmitter(VM &V);
void setupPassManager();
diff --git a/lib/ExecutionEngine/JIT/VM.h b/lib/ExecutionEngine/JIT/VM.h
index 17c4ddd..a720c96 100644
--- a/lib/ExecutionEngine/JIT/VM.h
+++ b/lib/ExecutionEngine/JIT/VM.h
@@ -27,6 +27,7 @@ class VM : public ExecutionEngine {
// handler to lazily patch up references...
//
std::map<void*, Function*> FunctionRefs;
+
public:
VM(Module *M, TargetMachine *tm);
~VM();
@@ -54,6 +55,12 @@ public:
// which causes lazy compilation of the target function.
//
static void CompilationCallback();
+
+ /// runAtExitHandlers - Before exiting the program, at_exit functions must be
+ /// called. This method calls them.
+ ///
+ static void runAtExitHandlers();
+
private:
static MachineCodeEmitter *createEmitter(VM &V);
void setupPassManager();