diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 0873bf0f..c42271df 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 00000000..fb234e60 --- /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