summaryrefslogtreecommitdiff
path: root/Userland/DynamicLoader
diff options
context:
space:
mode:
authorSahan Fernando <sahan.h.fernando@gmail.com>2020-12-24 23:06:59 +1100
committerAndreas Kling <kling@serenityos.org>2020-12-24 20:49:05 +0100
commitbcecd2fa2fa6e9d4dbb84d1163ffbc56f7d96c14 (patch)
treeccaa408ecd4d79c4966f84ec67d33c74ca610487 /Userland/DynamicLoader
parent12f214e2f06a85fa9f7c9e4315e60de3f13738c8 (diff)
downloadserenity-bcecd2fa2fa6e9d4dbb84d1163ffbc56f7d96c14.zip
DynamicLoader: Call libc's exit when exitting, to flush standard streams
Diffstat (limited to 'Userland/DynamicLoader')
-rw-r--r--Userland/DynamicLoader/main.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/Userland/DynamicLoader/main.cpp b/Userland/DynamicLoader/main.cpp
index 6da94306c8..b3886cc8ca 100644
--- a/Userland/DynamicLoader/main.cpp
+++ b/Userland/DynamicLoader/main.cpp
@@ -61,9 +61,13 @@ char* __static_environ[] = { nullptr }; // We don't get the environment without
static HashMap<String, NonnullRefPtr<ELF::DynamicLoader>> g_loaders;
static HashMap<String, NonnullRefPtr<ELF::DynamicObject>> g_loaded_objects;
+using MainFunction = int (*)(int, char**, char**);
+using LibCExitFunction = void (*)(int);
+
static size_t g_current_tls_offset = 0;
static size_t g_total_tls_size = 0;
static char** g_envp = nullptr;
+static LibCExitFunction g_libc_exit = nullptr;
static void init_libc()
{
@@ -208,6 +212,10 @@ static void initialize_libc()
ASSERT(res.found);
*((bool*)res.address) = false;
+ res = global_symbol_lookup("exit");
+ ASSERT(res.found);
+ g_libc_exit = (LibCExitFunction)res.address;
+
res = global_symbol_lookup("__libc_init");
ASSERT(res.found);
typedef void libc_init_func();
@@ -262,7 +270,7 @@ static FlatPtr loader_main(auxv_t* auxvp)
VERBOSE("loaded all dependencies");
for ([[maybe_unused]] auto& lib : g_loaders) {
- VERBOSE("%s - tls size: $u, tls offset: %u", lib.key.characters(), lib.value->tls_size(), lib.value->tls_offset());
+ VERBOSE("%s - tls size: %u, tls offset: %u", lib.key.characters(), lib.value->tls_size(), lib.value->tls_offset());
}
allocate_tls();
@@ -284,8 +292,6 @@ extern "C" {
// The compiler expects a previous declaration
void _start(int, char**, char**);
-using MainFunction = int (*)(int, char**, char**);
-
void _start(int argc, char** argv, char** envp)
{
g_envp = envp;
@@ -307,6 +313,10 @@ void _start(int argc, char** argv, char** envp)
VERBOSE("jumping to main program entry point: %p", main_function);
int rc = main_function(argc, argv, envp);
VERBOSE("rc: %d", rc);
- _exit(rc);
+ if (g_libc_exit != nullptr) {
+ g_libc_exit(rc);
+ } else {
+ _exit(rc);
+ }
}
}