[VCSTARTUP] Implement atexit and _onexit

These must use a local table and cannot use the one from ucrtbase.
The table is initialized with a .CRT section initializer.
This commit is contained in:
Timo Kreuzer 2025-02-09 12:46:03 +02:00
parent fce48c3014
commit f81c82f5fa
4 changed files with 55 additions and 0 deletions

View file

@ -35,6 +35,8 @@ list(APPEND VCRT_STARTUP_SOURCES
__acrt_initialize_stub.cpp
__scrt_uninitialize_crt.cpp
__vcrt_init_stubs.c
_onexit.c
atexit.c
mainCRTStartup.cpp
wmainCRTStartup.cpp
)

View file

@ -0,0 +1,34 @@
//
// _onexit.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of _onexit.
//
// SPDX-License-Identifier: MIT
//
#include <stdlib.h>
#include <internal_shared.h>
#include <corecrt_startup.h>
#include <assert.h>
_onexit_table_t module_local_atexit_table;
int module_local_atexit_table_initialized = 0;
int __cdecl __scrt_initialize_onexit(void)
{
_initialize_onexit_table(&module_local_atexit_table);
module_local_atexit_table_initialized = 1;
return 0;
}
// CRT startup initializer
_CRTALLOC(".CRT$XIAA") _PIFV const __scrt_onexit_initializer = __scrt_initialize_onexit;
_onexit_t __cdecl _onexit(_In_opt_ _onexit_t _Func)
{
assert(module_local_atexit_table_initialized == 1);
int result = _register_onexit_function(&module_local_atexit_table, _Func);
return (result == 0) ? _Func : NULL;
}

View file

@ -0,0 +1,18 @@
//
// atexit.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of atexit.
//
// SPDX-License-Identifier: MIT
//
#include <stdlib.h>
int __cdecl atexit(void (__cdecl* _Func)(void))
{
// Go through _onexit, so that the initializer is pulled in.
_onexit_t result = _onexit((_onexit_t)_Func);
return (result == NULL) ? -1 : 0;
}

View file

@ -31,6 +31,7 @@ extern "C" {
#pragma section(".CRT$XPXA", long, read)
#pragma section(".CRT$XIA", long, read)
#pragma section(".CRT$XIAA", long, read) // CRT startup
#pragma section(".CRT$XIZ", long, read)
#pragma section(".CRT$XCA", long, read)
#pragma section(".CRT$XCZ", long, read)