From 4189a1c7294fefb228299383a0a109f4ba305ff1 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 11 Sep 2024 14:31:26 +0300 Subject: [PATCH] LibC: Make _init and _fini weak symbols These seem to be missing sometimes when making shared executables --- userspace/libraries/LibC/arch/i686/crt0.S | 6 +++++- userspace/libraries/LibC/arch/x86_64/crt0.S | 6 +++++- userspace/libraries/LibC/stdlib.cpp | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/userspace/libraries/LibC/arch/i686/crt0.S b/userspace/libraries/LibC/arch/i686/crt0.S index 50d635ff..f50cdfb4 100644 --- a/userspace/libraries/LibC/arch/i686/crt0.S +++ b/userspace/libraries/LibC/arch/i686/crt0.S @@ -19,7 +19,11 @@ _start: call _init_libc # call global constructors - call _init + movl $_init, %eax + testl %eax, %eax + jz 1f + call *%eax + 1: movl $__init_array_start, %ebx jmp 2f diff --git a/userspace/libraries/LibC/arch/x86_64/crt0.S b/userspace/libraries/LibC/arch/x86_64/crt0.S index 46d83cdc..b5b543c9 100644 --- a/userspace/libraries/LibC/arch/x86_64/crt0.S +++ b/userspace/libraries/LibC/arch/x86_64/crt0.S @@ -20,7 +20,11 @@ _start: call _init_libc # call global constructors - call _init + movq $_init, %rax + testq %rax, %rax + jz 1f + call *%rax + 1: movq $__init_array_start, %rbx jmp 2f diff --git a/userspace/libraries/LibC/stdlib.cpp b/userspace/libraries/LibC/stdlib.cpp index 3be7c717..18773023 100644 --- a/userspace/libraries/LibC/stdlib.cpp +++ b/userspace/libraries/LibC/stdlib.cpp @@ -16,7 +16,7 @@ extern "C" char** environ; -extern "C" void _fini(); +extern "C" __attribute__((weak)) void _fini(); static void (*at_exit_funcs[64])(); static uint32_t at_exit_funcs_count = 0; @@ -34,7 +34,7 @@ void exit(int status) at_exit_funcs[i - 1](); fflush(nullptr); __cxa_finalize(nullptr); - _fini(); + if (_fini) _fini(); _exit(status); ASSERT_NOT_REACHED(); }