[VCRUNTIME] Add initialization / cleanup of wine code

This commit is contained in:
Timo Kreuzer 2025-01-29 13:15:16 +02:00
parent 32c349d30e
commit 92d479a54b
3 changed files with 79 additions and 7 deletions

View file

@ -20,7 +20,6 @@ list(APPEND VCRT_COMMON_SOURCES
__report_gsfailure.c
__report_rangecheckfailure.c
__security_init_cookie.c
__vcrt_init.c
_fltused.c
initializers.cpp
isa_available.cpp
@ -29,11 +28,13 @@ list(APPEND VCRT_COMMON_SOURCES
list(APPEND VCRT_RUNTIME_SOURCES
__std_terminate.c
__vcrt_init.c
)
list(APPEND VCRT_STARTUP_SOURCES
__acrt_initialize_stub.cpp
__scrt_uninitialize_crt.cpp
__vcrt_init_stubs.c
mainCRTStartup.cpp
wmainCRTStartup.cpp
)

View file

@ -8,29 +8,64 @@
// SPDX-License-Identifier: MIT
//
#include <vcruntime_startup.h>
#include <internal_shared.h>
void msvcrt_init_exception(HINSTANCE hinstDLL);
BOOL msvcrt_init_tls(void);
void msvcrt_free_tls_mem(void);
BOOL msvcrt_free_tls(void);
__vcrt_bool __cdecl __vcrt_initialize(void)
{
return 1;
msvcrt_init_exception(GetModuleHandle(NULL));
if (!msvcrt_init_tls())
return FALSE;
return TRUE;
}
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
{
return 1;
if (!msvcrt_free_tls())
return FALSE;
return TRUE;
}
__vcrt_bool __cdecl __vcrt_uninitialize_critical(void)
{
return 1;
return TRUE;
}
__vcrt_bool __cdecl __vcrt_thread_attach(void)
{
return 1;
// Not called by UCRT
return TRUE;
}
__vcrt_bool __cdecl __vcrt_thread_detach(void)
{
return 1;
// Not called by UCRT
return TRUE;
}
// UCRT doesn't have a thread detach callback for the vcruntime TLS, because
// the native vcruntime uses FlsAlloc and provides a cleanup callback there.
// Since we don't have that, we use TLS callbacks.
const IMAGE_TLS_DIRECTORY* __p_tls_used = &_tls_used;
static
VOID
WINAPI
wine_tls_cleanup_callback(PVOID hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
// For the last thread, only DLL_PROCESS_DETACH is called
if ((fdwReason == DLL_THREAD_DETACH) ||
(fdwReason == DLL_PROCESS_DETACH))
{
msvcrt_free_tls_mem();
}
}
_CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK wine_tls_cleanup_ptr = wine_tls_cleanup_callback;

View file

@ -0,0 +1,36 @@
//
// __vcrt_init_stubs.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Stubs for vcruntime initialization and termination in vcstartup.
//
// SPDX-License-Identifier: MIT
//
#include <vcruntime_startup.h>
__vcrt_bool __cdecl __vcrt_initialize(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_uninitialize_critical(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_thread_attach(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_thread_detach(void)
{
return 1;
}