From fa325072ac9f39b088ec4d7868acae66d8f86036 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 12 Nov 2022 17:43:57 +0100 Subject: [PATCH] Kernel: Add __cxa_atexit and __cxa_finalize --- kernel/src/misc/cxxabi.cpp | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 kernel/src/misc/cxxabi.cpp diff --git a/kernel/src/misc/cxxabi.cpp b/kernel/src/misc/cxxabi.cpp new file mode 100644 index 00000000..48898377 --- /dev/null +++ b/kernel/src/misc/cxxabi.cpp @@ -0,0 +1,52 @@ +typedef void* (*cxa_atexit_func_t)(void*); + +struct cxa_atexit_entry +{ + cxa_atexit_func_t function; + void* argument; + void* dso_handle; +}; + +#define CXA_ATEXIT_MAX 64 + +int cxa_atexit_entry_count = 0; + +cxa_atexit_entry cxa_atexit_entries[CXA_ATEXIT_MAX]; + +__attribute__((visibility("hidden"))) void* __dso_handle = 0; + +extern "C" +{ + int __cxa_atexit(cxa_atexit_func_t func, void* arg, void* dso) + { + if (cxa_atexit_entry_count >= CXA_ATEXIT_MAX) return -1; + cxa_atexit_entries[cxa_atexit_entry_count].function = func; + cxa_atexit_entries[cxa_atexit_entry_count].argument = arg; + cxa_atexit_entries[cxa_atexit_entry_count].dso_handle = dso; + cxa_atexit_entry_count++; + return 0; + } + + void __cxa_finalize(void* f) + { + int i = cxa_atexit_entry_count; + if (!f) + { + while (i--) + { + if (cxa_atexit_entries[i].function) { cxa_atexit_entries[i].function(cxa_atexit_entries[i].argument); } + } + } + else + { + while (i--) + { + if (cxa_atexit_entries[i].function == (cxa_atexit_func_t)f) + { + cxa_atexit_entries[i].function(cxa_atexit_entries[i].argument); + cxa_atexit_entries[i].function = 0; + } + } + } + } +} \ No newline at end of file