From: Yuxuan Shui <yshui@codeweavers.com> --- dlls/msvcrt/crt_init.h | 49 +++++++++++++++++++++++++++++++++++++ dlls/msvcrt/crt_main.c | 4 +-- dlls/msvcrt/crt_wmain.c | 4 +-- dlls/msvcrt/data.c | 5 ++++ dlls/winecrt0/crt_dllmain.c | 21 +++++++++++++++- 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 dlls/msvcrt/crt_init.h diff --git a/dlls/msvcrt/crt_init.h b/dlls/msvcrt/crt_init.h new file mode 100644 index 00000000000..5128f133b6a --- /dev/null +++ b/dlls/msvcrt/crt_init.h @@ -0,0 +1,49 @@ +/* + * Copyright 2025 Yuxuan Shui for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#if 0 +#pragma makedep implib +#endif + +#include <process.h> +#include <stdlib.h> +#include "minwindef.h" + +extern _PIFV __xi_a[]; +extern _PIFV __xi_z[]; +extern _PVFV __xc_a[]; +extern _PVFV __xc_z[]; +extern _PVFV __xt_a[]; +extern _PVFV __xt_z[]; + +static __cdecl void do_global_dtors(void) +{ + _initterm(__xt_a, __xt_z); +} + +static void do_global_ctors(void) +{ + if (_initterm_e(__xi_a, __xi_z) != 0) return; + _initterm(__xc_a, __xc_z); + +#ifdef _UCRT + _crt_atexit(do_global_dtors); +#else + _onexit((_onexit_t)do_global_dtors); +#endif +} diff --git a/dlls/msvcrt/crt_main.c b/dlls/msvcrt/crt_main.c index eb785e30bb0..fe900295cf1 100644 --- a/dlls/msvcrt/crt_main.c +++ b/dlls/msvcrt/crt_main.c @@ -23,12 +23,11 @@ #endif #include <stdarg.h> -#include <stdlib.h> -#include <process.h> #include "windef.h" #include "winbase.h" #include "winternl.h" +#include "crt_init.h" int __cdecl main(int argc, char **argv, char **env); @@ -54,6 +53,7 @@ int __cdecl mainCRTStartup(void) __getmainargs(&argc, &argv, &env, 0, &new_mode); #endif _set_app_type(get_nt_header()->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI ? _crt_gui_app : _crt_console_app); + do_global_ctors(); ret = main(argc, argv, env); diff --git a/dlls/msvcrt/crt_wmain.c b/dlls/msvcrt/crt_wmain.c index 79755d1a6e9..c3ea3fffd6f 100644 --- a/dlls/msvcrt/crt_wmain.c +++ b/dlls/msvcrt/crt_wmain.c @@ -23,12 +23,11 @@ #endif #include <stdarg.h> -#include <stdlib.h> -#include <process.h> #include "windef.h" #include "winbase.h" #include "winternl.h" +#include "crt_init.h" int __cdecl wmain(int argc, WCHAR **argv, WCHAR **env); @@ -54,6 +53,7 @@ int __cdecl wmainCRTStartup(void) __wgetmainargs(&argc, &argv, &env, 0, &new_mode); #endif _set_app_type(get_nt_header()->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI ? _crt_gui_app : _crt_console_app); + do_global_ctors(); ret = wmain(argc, argv, env); diff --git a/dlls/msvcrt/data.c b/dlls/msvcrt/data.c index 6e764e52eae..5b3d78009e9 100644 --- a/dlls/msvcrt/data.c +++ b/dlls/msvcrt/data.c @@ -22,6 +22,7 @@ #include <math.h> #include "msvcrt.h" #include <winnls.h> +#include "wine/asm.h" #include "wine/debug.h" WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); @@ -557,6 +558,8 @@ void CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end) } } +__ASM_GLOBAL_IMPORT(_initterm) + /********************************************************************* * _initterm_e (MSVCRT.@) * @@ -581,6 +584,8 @@ int CDECL _initterm_e(_INITTERM_E_FN *table, _INITTERM_E_FN *end) return res; } +__ASM_GLOBAL_IMPORT(_initterm_e) + /********************************************************************* * __set_app_type (MSVCRT.@) */ diff --git a/dlls/winecrt0/crt_dllmain.c b/dlls/winecrt0/crt_dllmain.c index 181760c884a..08a3336a9cc 100644 --- a/dlls/winecrt0/crt_dllmain.c +++ b/dlls/winecrt0/crt_dllmain.c @@ -22,12 +22,31 @@ #include <stdarg.h> #include <stdio.h> +#include <process.h> #include "windef.h" #include "winbase.h" +extern _PIFV __xi_a[]; +extern _PIFV __xi_z[]; +extern _PVFV __xc_a[]; +extern _PVFV __xc_z[]; +extern _PVFV __xt_a[]; +extern _PVFV __xt_z[]; + BOOL WINAPI DllMainCRTStartup( HINSTANCE inst, DWORD reason, void *reserved ) { - return DllMain( inst, reason, reserved ); + BOOL ret; + + if (reason == DLL_PROCESS_ATTACH) + { + if (_initterm_e( __xi_a, __xi_z ) != 0) return FALSE; + _initterm( __xc_a, __xc_z ); + } + + ret = DllMain( inst, reason, reserved ); + + if (reason == DLL_PROCESS_DETACH) _initterm( __xt_a, __xt_z ); + return ret; } #endif -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/9265