From a41b8e416fc83344d5ef88db1f69a44dbdf550ed Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 2 Jun 2023 17:49:21 +0300 Subject: [PATCH] LibC: add __cxa_at_exit() for libc --- libc/CMakeLists.txt | 1 + libc/icxxabi.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 libc/icxxabi.cpp diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 0873bf0ffb..c42271df3c 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -13,6 +13,7 @@ set(LIBC_SOURCES termios.cpp unistd.cpp math.S + icxxabi.cpp ) add_custom_target(libc-headers diff --git a/libc/icxxabi.cpp b/libc/icxxabi.cpp new file mode 100644 index 0000000000..fb234e603f --- /dev/null +++ b/libc/icxxabi.cpp @@ -0,0 +1,35 @@ +#define ATEXIT_MAX_FUNCS 128 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned uarch_t; + +struct atexit_func_entry_t +{ + /* + * Each member is at least 4 bytes large. Such that each entry is 12bytes. + * 128 * 12 = 1.5KB exact. + **/ + void (*destructor_func)(void *); + void *obj_ptr; + void *dso_handle; +}; + +atexit_func_entry_t __atexit_funcs[ATEXIT_MAX_FUNCS]; +uarch_t __atexit_func_count = 0; + +int __cxa_atexit(void (*f)(void *), void *objptr, void *dso) +{ + if (__atexit_func_count >= ATEXIT_MAX_FUNCS) {return -1;}; + __atexit_funcs[__atexit_func_count].destructor_func = f; + __atexit_funcs[__atexit_func_count].obj_ptr = objptr; + __atexit_funcs[__atexit_func_count].dso_handle = dso; + __atexit_func_count++; + return 0; /*I would prefer if functions returned 1 on success, but the ABI says...*/ +}; + +#ifdef __cplusplus +}; +#endif