From 57ef8feddbfe820ac96ec6ff869dd025923c9764 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 9 Nov 2022 11:38:40 +0100 Subject: [PATCH] libc: Add __cxa_atexit and __cxa_finalize To support a C++ implementation :) --- libs/libc/src/cxxabi.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 libs/libc/src/cxxabi.cpp diff --git a/libs/libc/src/cxxabi.cpp b/libs/libc/src/cxxabi.cpp new file mode 100644 index 00000000..47734a45 --- /dev/null +++ b/libs/libc/src/cxxabi.cpp @@ -0,0 +1,50 @@ +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]; + +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