Hello, in the attachment I'm sending second batch of changes which
cleanups and improves various mingw-w64 crt parts of TLS code.

Kirill run CI on github and tests passed:
https://github.com/maiddaisuki/mingw-w64/actions/runs/36212375264

There are various changes which:
- deduplicate code
- rename variables (I tried to give them better names)
- fixes mingw-w64 emulation of CRT _register_thread_local_exe_atexit_callback 
function for msvcrt builds
- use tentative variables to link on demand 
_register_thread_local_exe_atexit_callback and helper code around
- fix __dyn_tls_dtor to be executed before atexit callbacks
- fixes/extends of existing tests and new tests for order verification

Note that the last test t_cpp_dtor_atexit_order.cpp is failing for
mingw-w64/gcc combination, so I explicitly skipped it. But this test is
passing for mingw-w64/clang and also for linux/gcc combinations, so it
is possible that there is a bug in gcc when targeting mingw-w64 or PE.
>From 20d6447be5922db06577a3a5d1fe79d3eecc5a8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 14:44:43 +0200
Subject: [PATCH 01/14] crt: tls: Deduplicate code for running mingw cxa dtors

Code for this functionality inside the tls_atexit.c file is already in the
local tls_atexit_callback() function.
---
 mingw-w64-crt/crt/tls_atexit.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/mingw-w64-crt/crt/tls_atexit.c b/mingw-w64-crt/crt/tls_atexit.c
index 337a1a8d3ba8..0e4d57c5a19f 100644
--- a/mingw-w64-crt/crt/tls_atexit.c
+++ b/mingw-w64-crt/crt/tls_atexit.c
@@ -149,18 +149,13 @@ static WINBOOL WINAPI tls_callback(HANDLE hDllHandle, 
DWORD dwReason, LPVOID __U
      * but none get called for the main executable. This matches what the
      * standard says, but differs from what MSVC does with a dynamically
      * linked CRT (which still runs TLS destructors for the main thread).
+     *
+     * For DLLs, run dtors when detached. For EXEs, run dtors via the
+     * thread local atexit callback, to make sure they don't run when
+     * exiting the process with _exit or ExitProcess.
      */
-    if (__mingw_module_is_dll) {
-      run_thread_dtor_list();
-      /* For DLLs, run dtors when detached. For EXEs, run dtors via the
-       * thread local atexit callback, to make sure they don't run when
-       * exiting the process with _exit or ExitProcess. */
-      run_dtor_list(&global_dtors);
-      if (tls_dtors_slot != TLS_OUT_OF_INDEXES) {
-        TlsFree(tls_dtors_slot);
-        tls_dtors_slot = TLS_OUT_OF_INDEXES;
-      }
-    }
+    if (__mingw_module_is_dll)
+      tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL);
     if (inited == 1) {
       inited = 0;
       DeleteCriticalSection(&lock);
-- 
2.20.1


>From b7c134faae3286c01caddd203b7d0dc425b4f11d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 18:02:14 +0200
Subject: [PATCH 02/14] crt: tls: Do not export __mingw_TLScallback function,
 declare it as static

This is internal mingw-w64 function and does not have to be exported.
---
 mingw-w64-crt/crt/tlsthrd.c          | 3 +--
 mingw-w64-crt/crt/tlsthrd_register.c | 5 +++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/mingw-w64-crt/crt/tlsthrd.c b/mingw-w64-crt/crt/tlsthrd.c
index aede039cd75e..ce7ed3b87de8 100644
--- a/mingw-w64-crt/crt/tlsthrd.c
+++ b/mingw-w64-crt/crt/tlsthrd.c
@@ -136,8 +136,7 @@ __mingwthr_remove_all_key_dtors (void)
   LeaveCriticalSection (&__mingwthr_cs);
 }
 
-void WINAPI __mingw_TLScallback(HANDLE, DWORD, LPVOID);
-void WINAPI
+static void WINAPI
 __mingw_TLScallback (HANDLE __UNUSED_PARAM(hDllHandle),
                     DWORD reason,
                     LPVOID __UNUSED_PARAM(reserved))
diff --git a/mingw-w64-crt/crt/tlsthrd_register.c 
b/mingw-w64-crt/crt/tlsthrd_register.c
index 650b846f13b4..299660b32e3c 100644
--- a/mingw-w64-crt/crt/tlsthrd_register.c
+++ b/mingw-w64-crt/crt/tlsthrd_register.c
@@ -11,8 +11,9 @@
 #include <sect_attribs.h>
 
 const int __mingw_TLScallback_caller_provider = 1;
-void WINAPI __mingw_TLScallback(HANDLE, DWORD, LPVOID);
-static _CRTALLOC(".CRT$XLD") const PIMAGE_TLS_CALLBACK __mingw_TLScallback_ptr 
= __mingw_TLScallback;
+extern void (WINAPI *const __mingw_TLScallback_ptr)(HANDLE,DWORD,LPVOID);
+static void WINAPI tls_callback(HANDLE handle, DWORD reason, LPVOID reserved) 
{ __mingw_TLScallback_ptr(handle, reason, reserved); }
+static _CRTALLOC(".CRT$XLD") const PIMAGE_TLS_CALLBACK tls_callback_ptr = 
tls_callback;
 
 /* Force tlssup.c (_tls_used symbol for .tls linker section) to be linked.  */
 extern const IMAGE_TLS_DIRECTORY _tls_used;
-- 
2.20.1


>From ac58afa1c539ceac852124ee97a38aeb7bb1e186 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 18:03:57 +0200
Subject: [PATCH 03/14] crt: tls: Rename __mingw_TLScallback_ptr to
 __w64_mingwthr_callback_ptr

It is a better description of the callback which is used for w64_mingwthr API 
functionality.
---
 mingw-w64-crt/crt/crtdll.c           | 16 ++++++++--------
 mingw-w64-crt/crt/tlsthrd.c          |  8 ++++----
 mingw-w64-crt/crt/tlsthrd_register.c |  6 +++---
 3 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 31a137f34012..ecabf67abc19 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -29,8 +29,8 @@ extern _PVFV __xc_z[];
 /* TLS initialization hook.  */
 const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback __attribute__((common)); /* 
tentative */
 
-void (WINAPI *const __mingw_TLScallback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
-const int __mingw_TLScallback_caller_provider = 1; /* crtdll.c calls 
__mingw_TLScallback_ptr */
+void (WINAPI *const __w64_mingwthr_callback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
+const int __w64_mingwthr_callback_caller_provider = 1; /* crtdll.c calls 
__w64_mingwthr_callback_ptr */
 
 WINBOOL (WINAPI *const __mingw_atexit_tls_callback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
 const int __mingw_atexit_tls_callback_caller_provider = 1; /* crtdll.c calls 
__mingw_atexit_tls_callback_ptr */
@@ -77,8 +77,8 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
          __native_startup_state = __initializing;
          
          _pei386_runtime_relocator ();
-         if (__mingw_TLScallback_ptr != NULL)
-           __mingw_TLScallback_ptr (hDllHandle, dwReason, lpreserved);
+         if (__w64_mingwthr_callback_ptr != NULL)
+           __w64_mingwthr_callback_ptr (hDllHandle, dwReason, lpreserved);
          if (__mingw_dll_atexit_table_func_ptr != NULL)
            {
              ret = __mingw_dll_atexit_table_func_ptr (0 /*init*/);
@@ -140,8 +140,8 @@ i__leave:
        }
       else
        {
-         if (__mingw_TLScallback_ptr != NULL)
-           __mingw_TLScallback_ptr (hDllHandle, dwReason, lpreserved);
+         if (__w64_mingwthr_callback_ptr != NULL)
+           __w64_mingwthr_callback_ptr (hDllHandle, dwReason, lpreserved);
          if (__mingw_atexit_tls_callback_ptr != NULL)
            __mingw_atexit_tls_callback_ptr (hDllHandle, dwReason, lpreserved);
          if (__mingw_dll_atexit_table_func_ptr != NULL)
@@ -161,8 +161,8 @@ i__leave:
     }
   else if (dwReason == DLL_THREAD_DETACH)
     {
-      if (__mingw_TLScallback_ptr != NULL)
-        __mingw_TLScallback_ptr (hDllHandle, dwReason, lpreserved);
+      if (__w64_mingwthr_callback_ptr != NULL)
+        __w64_mingwthr_callback_ptr (hDllHandle, dwReason, lpreserved);
       if (__mingw_atexit_tls_callback_ptr != NULL)
        {
          if (! __mingw_atexit_tls_callback_ptr (hDllHandle, dwReason, 
lpreserved))
diff --git a/mingw-w64-crt/crt/tlsthrd.c b/mingw-w64-crt/crt/tlsthrd.c
index ce7ed3b87de8..11594db56f39 100644
--- a/mingw-w64-crt/crt/tlsthrd.c
+++ b/mingw-w64-crt/crt/tlsthrd.c
@@ -166,8 +166,8 @@ __mingw_TLScallback (HANDLE __UNUSED_PARAM(hDllHandle),
       break;
     }
 }
-void (WINAPI *const __mingw_TLScallback_ptr)(HANDLE,DWORD,LPVOID) = 
__mingw_TLScallback;
+void (WINAPI *const __w64_mingwthr_callback_ptr)(HANDLE,DWORD,LPVOID) = 
__mingw_TLScallback;
 
-/* Force inclusion of code which calls __mingw_TLScallback */
-extern const int __mingw_TLScallback_caller_provider;
-static __attribute__ ((used)) const void *const 
_include_mingw_TLScallback_caller = &__mingw_TLScallback_caller_provider;
+/* Force inclusion of code which calls __w64_mingwthr_callback_ptr */
+extern const int __w64_mingwthr_callback_caller_provider;
+static __attribute__ ((used)) const void *const 
_include_w64_mingwthr_callback_caller = 
&__w64_mingwthr_callback_caller_provider;
diff --git a/mingw-w64-crt/crt/tlsthrd_register.c 
b/mingw-w64-crt/crt/tlsthrd_register.c
index 299660b32e3c..47973288fa60 100644
--- a/mingw-w64-crt/crt/tlsthrd_register.c
+++ b/mingw-w64-crt/crt/tlsthrd_register.c
@@ -10,9 +10,9 @@
 #include <windows.h>
 #include <sect_attribs.h>
 
-const int __mingw_TLScallback_caller_provider = 1;
-extern void (WINAPI *const __mingw_TLScallback_ptr)(HANDLE,DWORD,LPVOID);
-static void WINAPI tls_callback(HANDLE handle, DWORD reason, LPVOID reserved) 
{ __mingw_TLScallback_ptr(handle, reason, reserved); }
+const int __w64_mingwthr_callback_caller_provider = 1;
+extern void (WINAPI *const __w64_mingwthr_callback_ptr)(HANDLE,DWORD,LPVOID);
+static void WINAPI tls_callback(HANDLE handle, DWORD reason, LPVOID reserved) 
{ __w64_mingwthr_callback_ptr(handle, reason, reserved); }
 static _CRTALLOC(".CRT$XLD") const PIMAGE_TLS_CALLBACK tls_callback_ptr = 
tls_callback;
 
 /* Force tlssup.c (_tls_used symbol for .tls linker section) to be linked.  */
-- 
2.20.1


>From 5a40220525d5a2ac9e1c4a49c9a2c08487c1dcb2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 18:18:44 +0200
Subject: [PATCH 04/14] crt: tls: Rename __mingw_atexit_tls_callback_ptr to
 __mingw_cxa_tls_callback_ptr

It is a better description of the callback which is used for mingw cxa TLS API 
functionality.
---
 mingw-w64-crt/crt/crtdll.c              | 20 ++++++++++----------
 mingw-w64-crt/crt/tls_atexit.c          |  8 ++++----
 mingw-w64-crt/crt/tls_atexit_register.c |  6 +++---
 3 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index ecabf67abc19..af7a47772297 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -32,8 +32,8 @@ const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback 
__attribute__((common)); /* te
 void (WINAPI *const __w64_mingwthr_callback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
 const int __w64_mingwthr_callback_caller_provider = 1; /* crtdll.c calls 
__w64_mingwthr_callback_ptr */
 
-WINBOOL (WINAPI *const __mingw_atexit_tls_callback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
-const int __mingw_atexit_tls_callback_caller_provider = 1; /* crtdll.c calls 
__mingw_atexit_tls_callback_ptr */
+WINBOOL (WINAPI *const __mingw_cxa_tls_callback_ptr)(HANDLE,DWORD,LPVOID) 
__attribute__((common)); /* tentative */
+const int __mingw_cxa_tls_callback_caller_provider = 1; /* crtdll.c calls 
__mingw_cxa_tls_callback_ptr */
 
 int (__cdecl *const __mingw_dll_atexit_table_func_ptr)(int) 
__attribute__((common)); /* tentative */
 
@@ -85,9 +85,9 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
              if (ret != 0)
                goto i__leave;
            }
-         if (__mingw_atexit_tls_callback_ptr != NULL)
+         if (__mingw_cxa_tls_callback_ptr != NULL)
            {
-             if (! __mingw_atexit_tls_callback_ptr (hDllHandle, dwReason, 
lpreserved))
+             if (! __mingw_cxa_tls_callback_ptr (hDllHandle, dwReason, 
lpreserved))
                {
                  ret = 1;
                  goto i__leave;
@@ -108,8 +108,8 @@ i__leave:
        }
       if (ret != 0)
        {
-         if (__mingw_atexit_tls_callback_ptr != NULL)
-           __mingw_atexit_tls_callback_ptr (hDllHandle, DLL_PROCESS_DETACH, 
lpreserved);
+         if (__mingw_cxa_tls_callback_ptr != NULL)
+           __mingw_cxa_tls_callback_ptr (hDllHandle, DLL_PROCESS_DETACH, 
lpreserved);
          return FALSE;
        }
       if (__dyn_tls_init_callback != NULL)
@@ -142,8 +142,8 @@ i__leave:
        {
          if (__w64_mingwthr_callback_ptr != NULL)
            __w64_mingwthr_callback_ptr (hDllHandle, dwReason, lpreserved);
-         if (__mingw_atexit_tls_callback_ptr != NULL)
-           __mingw_atexit_tls_callback_ptr (hDllHandle, dwReason, lpreserved);
+         if (__mingw_cxa_tls_callback_ptr != NULL)
+           __mingw_cxa_tls_callback_ptr (hDllHandle, dwReason, lpreserved);
          if (__mingw_dll_atexit_table_func_ptr != NULL)
            __mingw_dll_atexit_table_func_ptr (1 /*execute*/);
          __mingw_dll_do_global_dtors ();
@@ -163,9 +163,9 @@ i__leave:
     {
       if (__w64_mingwthr_callback_ptr != NULL)
         __w64_mingwthr_callback_ptr (hDllHandle, dwReason, lpreserved);
-      if (__mingw_atexit_tls_callback_ptr != NULL)
+      if (__mingw_cxa_tls_callback_ptr != NULL)
        {
-         if (! __mingw_atexit_tls_callback_ptr (hDllHandle, dwReason, 
lpreserved))
+         if (! __mingw_cxa_tls_callback_ptr (hDllHandle, dwReason, lpreserved))
            return FALSE;
        }
     }
diff --git a/mingw-w64-crt/crt/tls_atexit.c b/mingw-w64-crt/crt/tls_atexit.c
index 0e4d57c5a19f..44dd5c83e655 100644
--- a/mingw-w64-crt/crt/tls_atexit.c
+++ b/mingw-w64-crt/crt/tls_atexit.c
@@ -169,8 +169,8 @@ static WINBOOL WINAPI tls_callback(HANDLE hDllHandle, DWORD 
dwReason, LPVOID __U
   }
   return TRUE;
 }
-WINBOOL (WINAPI *const __mingw_atexit_tls_callback_ptr)(HANDLE,DWORD,LPVOID) = 
tls_callback;
+WINBOOL (WINAPI *const __mingw_cxa_tls_callback_ptr)(HANDLE,DWORD,LPVOID) = 
tls_callback;
 
-/* Force inclusion of code which calls __mingw_atexit_tls_callback */
-extern const int __mingw_atexit_tls_callback_caller_provider;
-static __attribute__((used)) const void *const 
_include_mingw_atexit_tls_callback_caller = 
&__mingw_atexit_tls_callback_caller_provider;
+/* Force inclusion of code which calls __mingw_cxa_tls_callback_ptr */
+extern const int __mingw_cxa_tls_callback_caller_provider;
+static __attribute__((used)) const void *const 
_include_mingw_cxa_tls_callback_caller = 
&__mingw_cxa_tls_callback_caller_provider;
diff --git a/mingw-w64-crt/crt/tls_atexit_register.c 
b/mingw-w64-crt/crt/tls_atexit_register.c
index 316c69c71cba..5432c171092f 100644
--- a/mingw-w64-crt/crt/tls_atexit_register.c
+++ b/mingw-w64-crt/crt/tls_atexit_register.c
@@ -11,10 +11,10 @@
 #include <sect_attribs.h>
 #include <internal.h>
 
-const int __mingw_atexit_tls_callback_caller_provider = 1;
-extern WINBOOL (WINAPI *const 
__mingw_atexit_tls_callback_ptr)(HANDLE,DWORD,LPVOID);
+const int __mingw_cxa_tls_callback_caller_provider = 1;
+extern WINBOOL (WINAPI *const 
__mingw_cxa_tls_callback_ptr)(HANDLE,DWORD,LPVOID);
 static void WINAPI callback(HANDLE handle, DWORD reason, LPVOID reserved) {
-  if (!__mingw_atexit_tls_callback_ptr(handle, reason, reserved))
+  if (!__mingw_cxa_tls_callback_ptr(handle, reason, reserved))
     _amsg_exit(16); /* _RT_THREAD - not enough space for thread data */
 }
 static _CRTALLOC(".CRT$XLB") const PIMAGE_TLS_CALLBACK callback_ptr = callback;
-- 
2.20.1


>From 83c789feda0b9092d08eac01248667114be9dce9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 19:23:24 +0200
Subject: [PATCH 05/14] crt: Improve mingw-w64
 _register_thread_local_exe_atexit_callback implementation which is used for
 msvcrt builds

Check that the _register_thread_local_exe_atexit_callback function is not
called multiple times. UCRT also disallow to call it multiple times.

Improve guard in run_callback to ensure that the callback is not executed
more times even if the run_callback is called more times.

Add mingw-w64 wrapper around the CRT exit() function which will register
the callback via atexit before calling the CRT exit function. This ensures
that the CRT exit function would call the callback as the first atexit
callback.
---
 mingw-w64-crt/Makefile.am                    |  1 +
 mingw-w64-crt/lib-common/msvcr120_app.def.in |  2 +-
 mingw-w64-crt/lib-common/msvcrt.def.in       |  2 +-
 mingw-w64-crt/lib32/crtdll.def.in            |  2 +-
 mingw-w64-crt/lib32/msvcr100.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr100d.def.in         |  2 +-
 mingw-w64-crt/lib32/msvcr110.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr110d.def.in         |  2 +-
 mingw-w64-crt/lib32/msvcr120.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr120d.def.in         |  2 +-
 mingw-w64-crt/lib32/msvcr40d.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr70.def.in           |  2 +-
 mingw-w64-crt/lib32/msvcr70d.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr71.def.in           |  2 +-
 mingw-w64-crt/lib32/msvcr71d.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr80.def.in           |  2 +-
 mingw-w64-crt/lib32/msvcr80d.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcr90.def.in           |  2 +-
 mingw-w64-crt/lib32/msvcr90d.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcrt10.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcrt20.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcrt40.def.in          |  2 +-
 mingw-w64-crt/lib32/msvcrtd.def.in           |  2 +-
 mingw-w64-crt/lib64/msvcr100.def.in          |  2 +-
 mingw-w64-crt/lib64/msvcr100d.def.in         |  2 +-
 mingw-w64-crt/lib64/msvcr110.def.in          |  2 +-
 mingw-w64-crt/lib64/msvcr110d.def.in         |  2 +-
 mingw-w64-crt/lib64/msvcr120.def.in          |  2 +-
 mingw-w64-crt/lib64/msvcr120d.def.in         |  2 +-
 mingw-w64-crt/lib64/msvcr80.def.in           |  2 +-
 mingw-w64-crt/lib64/msvcr80d.def.in          |  2 +-
 mingw-w64-crt/lib64/msvcr90.def.in           |  2 +-
 mingw-w64-crt/lib64/msvcr90d.def.in          |  2 +-
 mingw-w64-crt/libarm32/msvcr110.def.in       |  2 +-
 mingw-w64-crt/libarm32/msvcr110d.def.in      |  2 +-
 mingw-w64-crt/libarm32/msvcr120.def.in       |  2 +-
 mingw-w64-crt/libarm32/msvcr120d.def.in      |  2 +-
 mingw-w64-crt/misc/exit.c                    | 21 +++++++++++++++
 mingw-w64-crt/misc/register_tls_atexit.c     | 27 ++++++++++++++++----
 39 files changed, 80 insertions(+), 41 deletions(-)
 create mode 100644 mingw-w64-crt/misc/exit.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 61c2b3d18f7d..07c050d63de3 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -182,6 +182,7 @@ src_msvcrt_common=\
   misc/__mingw_mbrtowc_cp.c \
   misc/__mingw_wcrtomb_cp.c \
   misc/_onexit.c \
+  misc/exit.c \
   misc/mbrlen.c \
   misc/mbrtowc.c \
   misc/mbsinit.c \
diff --git a/mingw-w64-crt/lib-common/msvcr120_app.def.in 
b/mingw-w64-crt/lib-common/msvcr120_app.def.in
index 06260eee3fc5..6bbbf54ee65f 100644
--- a/mingw-w64-crt/lib-common/msvcr120_app.def.in
+++ b/mingw-w64-crt/lib-common/msvcr120_app.def.in
@@ -1970,7 +1970,7 @@ erfcf
 F_ARM32(erfcl) ; Can't use long double functions from the CRT on x86
 erff
 F_ARM32(erfl) ; Can't use long double functions from the CRT on x86
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/lib-common/msvcrt.def.in 
b/mingw-w64-crt/lib-common/msvcrt.def.in
index 39ece04fe497..fdf8eb8adcd5 100644
--- a/mingw-w64-crt/lib-common/msvcrt.def.in
+++ b/mingw-w64-crt/lib-common/msvcrt.def.in
@@ -956,7 +956,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp F_X86_NATIVE(DATA)
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/crtdll.def.in 
b/mingw-w64-crt/lib32/crtdll.def.in
index 154d7add2f8e..5dd3c996420b 100644
--- a/mingw-w64-crt/lib32/crtdll.def.in
+++ b/mingw-w64-crt/lib32/crtdll.def.in
@@ -350,7 +350,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr100.def.in 
b/mingw-w64-crt/lib32/msvcr100.def.in
index 1db3e07209fe..a08fb20d90a3 100644
--- a/mingw-w64-crt/lib32/msvcr100.def.in
+++ b/mingw-w64-crt/lib32/msvcr100.def.in
@@ -1672,7 +1672,7 @@ cos DATA
 ; If we implement cosh too, we can set it to DATA only.
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr100d.def.in 
b/mingw-w64-crt/lib32/msvcr100d.def.in
index 9899fe057f46..6939f0dfe76a 100644
--- a/mingw-w64-crt/lib32/msvcr100d.def.in
+++ b/mingw-w64-crt/lib32/msvcr100d.def.in
@@ -1738,7 +1738,7 @@ clock
 cos DATA ; overwritten
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA ; overwritten
 fabs DATA ; overwritten
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr110.def.in 
b/mingw-w64-crt/lib32/msvcr110.def.in
index 1a4db8110b00..9a7f16f14661 100644
--- a/mingw-w64-crt/lib32/msvcr110.def.in
+++ b/mingw-w64-crt/lib32/msvcr110.def.in
@@ -1804,7 +1804,7 @@ clock
 cos DATA
 cosh ; If we implement cosh, we can set it to DATA only.
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr110d.def.in 
b/mingw-w64-crt/lib32/msvcr110d.def.in
index 0b52690468ce..bf4ba2d3c27b 100644
--- a/mingw-w64-crt/lib32/msvcr110d.def.in
+++ b/mingw-w64-crt/lib32/msvcr110d.def.in
@@ -1871,7 +1871,7 @@ clock
 cos DATA ; overwritten
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA ; overwritten
 fabs DATA ; overwritten
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr120.def.in 
b/mingw-w64-crt/lib32/msvcr120.def.in
index af88bbbd5339..43ae5cbae6e9 100644
--- a/mingw-w64-crt/lib32/msvcr120.def.in
+++ b/mingw-w64-crt/lib32/msvcr120.def.in
@@ -1943,7 +1943,7 @@ erfcf
 ; erfcl ; Can't use long double functions from the CRT on x86
 erff
 ; erfl ; Can't use long double functions from the CRT on x86
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/lib32/msvcr120d.def.in 
b/mingw-w64-crt/lib32/msvcr120d.def.in
index b8d4c9bb18f6..df3d1a09887b 100644
--- a/mingw-w64-crt/lib32/msvcr120d.def.in
+++ b/mingw-w64-crt/lib32/msvcr120d.def.in
@@ -2010,7 +2010,7 @@ erfcf
 ; erfcl ; Can't use long double functions from the CRT on x86
 erff
 ; erfl ; Can't use long double functions from the CRT on x86
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/lib32/msvcr40d.def.in 
b/mingw-w64-crt/lib32/msvcr40d.def.in
index aced5b3c5d58..bcf0e6a1d0ab 100644
--- a/mingw-w64-crt/lib32/msvcr40d.def.in
+++ b/mingw-w64-crt/lib32/msvcr40d.def.in
@@ -1475,7 +1475,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr70.def.in 
b/mingw-w64-crt/lib32/msvcr70.def.in
index 53e2bfe2dff2..1c44a281562f 100644
--- a/mingw-w64-crt/lib32/msvcr70.def.in
+++ b/mingw-w64-crt/lib32/msvcr70.def.in
@@ -725,7 +725,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr70d.def.in 
b/mingw-w64-crt/lib32/msvcr70d.def.in
index 7784d76f2a3b..1eb2a6c10524 100644
--- a/mingw-w64-crt/lib32/msvcr70d.def.in
+++ b/mingw-w64-crt/lib32/msvcr70d.def.in
@@ -769,7 +769,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr71.def.in 
b/mingw-w64-crt/lib32/msvcr71.def.in
index aa33cd3d0c34..576109f77547 100644
--- a/mingw-w64-crt/lib32/msvcr71.def.in
+++ b/mingw-w64-crt/lib32/msvcr71.def.in
@@ -719,7 +719,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr71d.def.in 
b/mingw-w64-crt/lib32/msvcr71d.def.in
index a317d1ef2d24..f64aaa00ecc7 100644
--- a/mingw-w64-crt/lib32/msvcr71d.def.in
+++ b/mingw-w64-crt/lib32/msvcr71d.def.in
@@ -763,7 +763,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr80.def.in 
b/mingw-w64-crt/lib32/msvcr80.def.in
index 7d579983ed1c..2153e69d1b6b 100644
--- a/mingw-w64-crt/lib32/msvcr80.def.in
+++ b/mingw-w64-crt/lib32/msvcr80.def.in
@@ -1312,7 +1312,7 @@ clock
 cos DATA
 cosh ; If we implement cosh, we can set it to DATA only.
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr80d.def.in 
b/mingw-w64-crt/lib32/msvcr80d.def.in
index d4deb170db4c..3597f3ad17d3 100644
--- a/mingw-w64-crt/lib32/msvcr80d.def.in
+++ b/mingw-w64-crt/lib32/msvcr80d.def.in
@@ -1395,7 +1395,7 @@ clock
 cos DATA ; overwritten
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA ; overwritten
 fabs DATA ; overwritten
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr90.def.in 
b/mingw-w64-crt/lib32/msvcr90.def.in
index a6f575212208..5c4c9c2298c6 100644
--- a/mingw-w64-crt/lib32/msvcr90.def.in
+++ b/mingw-w64-crt/lib32/msvcr90.def.in
@@ -1306,7 +1306,7 @@ cos DATA
 ; If we have cosh implementation, we can set it to DATA only.
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcr90d.def.in 
b/mingw-w64-crt/lib32/msvcr90d.def.in
index eb4f68ec0acc..f636d3491874 100644
--- a/mingw-w64-crt/lib32/msvcr90d.def.in
+++ b/mingw-w64-crt/lib32/msvcr90d.def.in
@@ -1378,7 +1378,7 @@ cos DATA
 ; If we implement cosh too, we can set it to DATA only.
 cosh
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 fabs DATA
 fclose
diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in 
b/mingw-w64-crt/lib32/msvcrt10.def.in
index 8a7d40842465..4ba98bfffdf3 100644
--- a/mingw-w64-crt/lib32/msvcrt10.def.in
+++ b/mingw-w64-crt/lib32/msvcrt10.def.in
@@ -1126,7 +1126,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcrt20.def.in 
b/mingw-w64-crt/lib32/msvcrt20.def.in
index 8a87561df4ed..ee350877bae4 100644
--- a/mingw-w64-crt/lib32/msvcrt20.def.in
+++ b/mingw-w64-crt/lib32/msvcrt20.def.in
@@ -1343,7 +1343,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcrt40.def.in 
b/mingw-w64-crt/lib32/msvcrt40.def.in
index 4c698c153df3..6347304031ba 100644
--- a/mingw-w64-crt/lib32/msvcrt40.def.in
+++ b/mingw-w64-crt/lib32/msvcrt40.def.in
@@ -1443,7 +1443,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib32/msvcrtd.def.in 
b/mingw-w64-crt/lib32/msvcrtd.def.in
index ea0c17c3ca5a..130113d67b7a 100644
--- a/mingw-w64-crt/lib32/msvcrtd.def.in
+++ b/mingw-w64-crt/lib32/msvcrtd.def.in
@@ -669,7 +669,7 @@ cosh
 ctime
 difftime
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 fabs
 fclose
diff --git a/mingw-w64-crt/lib64/msvcr100.def.in 
b/mingw-w64-crt/lib64/msvcr100.def.in
index 2e7991c6dbbf..b082f74fea9e 100644
--- a/mingw-w64-crt/lib64/msvcr100.def.in
+++ b/mingw-w64-crt/lib64/msvcr100.def.in
@@ -1624,7 +1624,7 @@ cosf DATA
 cosh
 coshf DATA
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 expf DATA
 fabs DATA
diff --git a/mingw-w64-crt/lib64/msvcr100d.def.in 
b/mingw-w64-crt/lib64/msvcr100d.def.in
index 26c568dc4941..b737e020ac79 100644
--- a/mingw-w64-crt/lib64/msvcr100d.def.in
+++ b/mingw-w64-crt/lib64/msvcr100d.def.in
@@ -1689,7 +1689,7 @@ cosf DATA ; overwritten
 cosh
 coshf DATA ; overwritten
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA ; overwritten
 expf DATA ; overwritten
 fabs DATA ; overwritten
diff --git a/mingw-w64-crt/lib64/msvcr110.def.in 
b/mingw-w64-crt/lib64/msvcr110.def.in
index 83362bce9402..9df2ee93abcf 100644
--- a/mingw-w64-crt/lib64/msvcr110.def.in
+++ b/mingw-w64-crt/lib64/msvcr110.def.in
@@ -1748,7 +1748,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 expf
 fabs
diff --git a/mingw-w64-crt/lib64/msvcr110d.def.in 
b/mingw-w64-crt/lib64/msvcr110d.def.in
index 621768782ff4..c657ad623b98 100644
--- a/mingw-w64-crt/lib64/msvcr110d.def.in
+++ b/mingw-w64-crt/lib64/msvcr110d.def.in
@@ -1813,7 +1813,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 expf
 fabs
diff --git a/mingw-w64-crt/lib64/msvcr120.def.in 
b/mingw-w64-crt/lib64/msvcr120.def.in
index 9095011ac403..130f10fcc5db 100644
--- a/mingw-w64-crt/lib64/msvcr120.def.in
+++ b/mingw-w64-crt/lib64/msvcr120.def.in
@@ -1886,7 +1886,7 @@ erfcf
 ; erfcl ; Can't use long double functions from the CRT on x86
 erff
 ; erfl ; Can't use long double functions from the CRT on x86
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/lib64/msvcr120d.def.in 
b/mingw-w64-crt/lib64/msvcr120d.def.in
index 31c325805b9b..278ec14d1486 100644
--- a/mingw-w64-crt/lib64/msvcr120d.def.in
+++ b/mingw-w64-crt/lib64/msvcr120d.def.in
@@ -1951,7 +1951,7 @@ erfcf
 ; erfcl ; Can't use long double functions from the CRT on x86
 erff
 ; erfl ; Can't use long double functions from the CRT on x86
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/lib64/msvcr80.def.in 
b/mingw-w64-crt/lib64/msvcr80.def.in
index f81f10e290c0..a46fbaedcba4 100644
--- a/mingw-w64-crt/lib64/msvcr80.def.in
+++ b/mingw-w64-crt/lib64/msvcr80.def.in
@@ -1247,7 +1247,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 expf DATA
 fabs
diff --git a/mingw-w64-crt/lib64/msvcr80d.def.in 
b/mingw-w64-crt/lib64/msvcr80d.def.in
index 3fbac9478076..dc2ece7c1bda 100644
--- a/mingw-w64-crt/lib64/msvcr80d.def.in
+++ b/mingw-w64-crt/lib64/msvcr80d.def.in
@@ -1324,7 +1324,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA ; overwritten
 expf DATA ; overwritten
 fabs
diff --git a/mingw-w64-crt/lib64/msvcr90.def.in 
b/mingw-w64-crt/lib64/msvcr90.def.in
index 3efea2608479..22828329d3c7 100644
--- a/mingw-w64-crt/lib64/msvcr90.def.in
+++ b/mingw-w64-crt/lib64/msvcr90.def.in
@@ -1244,7 +1244,7 @@ cosf DATA
 cosh
 coshf DATA
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 expf DATA
 fabs DATA
diff --git a/mingw-w64-crt/lib64/msvcr90d.def.in 
b/mingw-w64-crt/lib64/msvcr90d.def.in
index c95a741a868b..5bdfa14fd592 100644
--- a/mingw-w64-crt/lib64/msvcr90d.def.in
+++ b/mingw-w64-crt/lib64/msvcr90d.def.in
@@ -1310,7 +1310,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp DATA
 expf DATA
 fabs
diff --git a/mingw-w64-crt/libarm32/msvcr110.def.in 
b/mingw-w64-crt/libarm32/msvcr110.def.in
index 3c067e002142..4decd8f69e2f 100644
--- a/mingw-w64-crt/libarm32/msvcr110.def.in
+++ b/mingw-w64-crt/libarm32/msvcr110.def.in
@@ -1734,7 +1734,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 expf
 fabs
diff --git a/mingw-w64-crt/libarm32/msvcr110d.def.in 
b/mingw-w64-crt/libarm32/msvcr110d.def.in
index 7164dbb88420..81fc74679fb2 100644
--- a/mingw-w64-crt/libarm32/msvcr110d.def.in
+++ b/mingw-w64-crt/libarm32/msvcr110d.def.in
@@ -1799,7 +1799,7 @@ cosf
 cosh
 coshf
 div
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 expf
 fabs
diff --git a/mingw-w64-crt/libarm32/msvcr120.def.in 
b/mingw-w64-crt/libarm32/msvcr120.def.in
index 8e3b65f26e43..2baa561d1d33 100644
--- a/mingw-w64-crt/libarm32/msvcr120.def.in
+++ b/mingw-w64-crt/libarm32/msvcr120.def.in
@@ -1853,7 +1853,7 @@ erfcf
 erfcl
 erff
 erfl
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/libarm32/msvcr120d.def.in 
b/mingw-w64-crt/libarm32/msvcr120d.def.in
index ef278913e2a5..afd9d451fa29 100644
--- a/mingw-w64-crt/libarm32/msvcr120d.def.in
+++ b/mingw-w64-crt/libarm32/msvcr120d.def.in
@@ -1918,7 +1918,7 @@ erfcf
 erfcl
 erff
 erfl
-exit
+__msvcrt_exit DATA == exit ; mingw-w64 provides exit() function as wrapper 
around renamed __msvcrt_exit symbol
 exp
 exp2
 exp2f
diff --git a/mingw-w64-crt/misc/exit.c b/mingw-w64-crt/misc/exit.c
new file mode 100644
index 000000000000..88e1b3ac49dd
--- /dev/null
+++ b/mingw-w64-crt/misc/exit.c
@@ -0,0 +1,21 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <stdlib.h>
+
+void (__cdecl *__mingw_prepare_thread_local_exe_atexit_callback_ptr)(void) 
__attribute__((common)); /* tentative */
+extern void (__cdecl *__MINGW_IMP_SYMBOL(__msvcrt_exit))(int code) 
__MINGW_ATTRIB_NORETURN;
+
+__MINGW_ATTRIB_NORETURN void __cdecl exit(int code)
+{
+  /* __mingw_prepare_thread_local_exe_atexit_callback_ptr is thread safe and
+   * also can be called multiple times. Registration is done under CRT lock
+   * and execution of the registered callback is done exactly once. */
+  if (__mingw_prepare_thread_local_exe_atexit_callback_ptr)
+    __mingw_prepare_thread_local_exe_atexit_callback_ptr();
+  __MINGW_IMP_SYMBOL(__msvcrt_exit)(code);
+}
+typeof(exit) *__MINGW_IMP_SYMBOL(exit) = exit;
diff --git a/mingw-w64-crt/misc/register_tls_atexit.c 
b/mingw-w64-crt/misc/register_tls_atexit.c
index 09d9270e4dd4..1875a67113ab 100644
--- a/mingw-w64-crt/misc/register_tls_atexit.c
+++ b/mingw-w64-crt/misc/register_tls_atexit.c
@@ -5,25 +5,42 @@
  */
 
 #include <process.h>
-#include <windows.h>
-#include <winnt.h>
 #include <stdlib.h>
+#include <internal.h>
 
 static _tls_callback_type callback;
 
-static void run_callback(void)
+static void __cdecl run_callback(void)
 {
+  /* Ensure that callback is called only once. */
+  static int called = 0;
+  if (called)
+    return;
+  called = 1;
   if (callback)
     callback(NULL, DLL_PROCESS_DETACH, 0);
-  callback = NULL;
 }
 
+/* mingw-w64 exit() wrapper calls this function pointer prior CRT atexit() call
+ * to ensure that the registered callback is called before any other callback.
+ * If some other code calls the exported exit DLL function directly then the
+ * atexit() call inside _register_thread_local_exe_atexit_callback guarantees
+ * at least that the callback is called (but does not have to as a first one). 
*/
+static void __cdecl set_atexit_callback(void)
+{
+  atexit(run_callback);
+}
+void (__cdecl *__mingw_prepare_thread_local_exe_atexit_callback_ptr)(void) = 
set_atexit_callback;
+
 void __cdecl _register_thread_local_exe_atexit_callback(_tls_callback_type cb)
 {
+  /* _register_thread_local_exe_atexit_callback cannot be called multiple 
times. */
+  if (callback)
+    _amsg_exit(10); /* _RT_ABORT */
   callback = cb;
   /* This should guarantee that the callback is called. It won't be run in the
    * exact right spot as intended to, but it will be run. */
-  atexit(run_callback);
+  set_atexit_callback();
 }
 
 typeof(_register_thread_local_exe_atexit_callback) 
*__MINGW_IMP_SYMBOL(_register_thread_local_exe_atexit_callback) = 
_register_thread_local_exe_atexit_callback;
-- 
2.20.1


>From a97a3e6596719c2f8582dc2223317850d49dd189 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 21:05:02 +0200
Subject: [PATCH 06/14] crt: tls: Move
 _register_thread_local_exe_atexit_callback into own source unit and do not
 call it for DLL builds

Add a new internal function __mingw_register_thread_local_exe_atexit_callback
which will call the CRT _register_thread_local_exe_atexit_callback() function
with the mingw-w64 supplied callback function. Supplied callback function
for now just calls the __mingw_cxa_atexit_callback_ptr (which is pointer to
the tls_atexit_callback) previously directly passed as argument to
_register_thread_local_exe_atexit_callback().

Stores pointer of the __mingw_register_thread_local_exe_atexit_callback
into the tentative __mingw_register_thread_local_exe_atexit_callback_ptr
variable and call it from crtexe.c on demand when is linked into final
exectuable. And reference the __mingw_register_thread_local_exe_atexit_callback
symbol from tls_atexit.c where is the __mingw_cxa_atexit_callback_ptr
defined and expected to be __mingw_register_thread_local_exe_atexit_callback
called from CRT EXE startup code.

With these tenative definitions, the 
_register_thread_local_exe_atexit_callback()
would be included in final binary image only if the callback is really
required and only for EXE builds.

Before this change the _register_thread_local_exe_atexit_callback() with
tls_atexit_callback argument was called from PE TLS DLL_PROCESS_ATTACH
callback. After this change, _register_thread_local_exe_atexit_callback()
(with new mingw-w64 callback which calls tls_atexit_callback via
__mingw_cxa_atexit_callback_ptr) is called from new function
__mingw_register_thread_local_exe_atexit_callback() which is called from
CRT EXE startup code (crtexe.c).

Also extend documentation for tls_atexit_callback and tls_callback.
---
 mingw-w64-crt/Makefile.am                   |  1 +
 mingw-w64-crt/crt/crtdll.c                  |  4 ++
 mingw-w64-crt/crt/crtexe.c                  |  5 ++
 mingw-w64-crt/crt/tls_atexit.c              | 74 +++++++++++++++------
 mingw-w64-crt/crt/tls_exe_atexit_callback.c | 27 ++++++++
 5 files changed, 91 insertions(+), 20 deletions(-)
 create mode 100644 mingw-w64-crt/crt/tls_exe_atexit_callback.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 07c050d63de3..b1963724edd3 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -128,6 +128,7 @@ src_libmingw32=include/internal.h include/sect_attribs.h 
crt/seh_signal_dispatch
   crt/usermatherr.c   \
   crt/xtxtmode.c      \
   crt/crtdll_atexit_register.c \
+  crt/tls_exe_atexit_callback.c \
   crt/tlsdyn.c        crt/tlsdtor.c        \
   crt/tlsthrd_register.c crt/tls_atexit_register.c \
   crt/tlsthrd.c       crt/tlsmthread.c     crt/tlsmcrt.c   \
diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index af7a47772297..15ee52a7c334 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -216,4 +216,8 @@ i__leave:
   return retcode ;
 }
 
+/* not available for DLL builds, must be non-tentative to not be overridden */
+void (__cdecl *const 
__mingw_register_thread_local_exe_atexit_callback_ptr)(void) = NULL;
+extern const uintptr_t 
__mingw_register_thread_local_exe_atexit_callback_provider 
__attribute__((alias("__mingw_register_thread_local_exe_atexit_callback_ptr")));
+
 const char __mingw_module_is_dll = 1;
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index e7dbcff82131..ca9b0069160c 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -51,6 +51,8 @@ extern _PVFV __xc_z[];
 /* TLS initialization hook.  */
 const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback __attribute__((common)); /* 
tentative */
 
+void (__cdecl *const 
__mingw_register_thread_local_exe_atexit_callback_ptr)(void) 
__attribute__((common)); /* tentative */
+
 extern int __mingw_app_type;
 
 static int argc;
@@ -258,6 +260,9 @@ __tmainCRTStartup (void)
     if (__dyn_tls_init_callback != NULL)
       __dyn_tls_init_callback (NULL, DLL_THREAD_ATTACH, NULL);
 
+    if (__mingw_register_thread_local_exe_atexit_callback_ptr != NULL)
+      __mingw_register_thread_local_exe_atexit_callback_ptr ();
+
 #ifdef _UNICODE
     __winitenv = envp;
 #else
diff --git a/mingw-w64-crt/crt/tls_atexit.c b/mingw-w64-crt/crt/tls_atexit.c
index 44dd5c83e655..bb0390fa0c6a 100644
--- a/mingw-w64-crt/crt/tls_atexit.c
+++ b/mingw-w64-crt/crt/tls_atexit.c
@@ -93,10 +93,38 @@ static void run_thread_dtor_list(void) {
   TlsSetValue(tls_dtors_slot, NULL);
 }
 
-#if defined(__i386__)
-/* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
-__attribute__((force_align_arg_pointer))
-#endif
+/*
+ * For EXE builds the tls_atexit_callback() function is called by the exit()
+ * function before it calls atexit callbacks. For UCRT builds this is achieved
+ * by UCRT directly, for non-UCRT builds this is achieved by the mingw-w64
+ * exit() wrapper function. The tls_atexit_callback() function is called
+ * indirectly via the __mingw_cxa_atexit_callback_ptr from the mingw-w64
+ * callback registered by the CRT _register_thread_local_exe_atexit_callback.
+ * If the process does not call CRT exit function and is terminated by 
different
+ * way (e.g. _exit(), _Exit() or ExitProcess()) then the tls_atexit_callback()
+ * function is not called at all.
+ *
+ * For DLL builds the tls_atexit_callback() function is called explicitly from
+ * the tls_callback() function which is below. This function is called also 
when
+ * process is terminated by different way (e.g. _exit(), _Exit() or 
ExitProcess())
+ * not just by exit() function.
+ *
+ * Reason for these differences is the fact that for DLL builds the 
tls_callback()
+ * function is called before invocation of atexit callbacks, but for EXE builds
+ * it is called after all atexit callbacks. For DLL builds, all atexit 
callbacks
+ * are executed from the DLL entry point when DLL_PROCESS_DETACH event happens
+ * and for EXE builds, they are executed from the CRT exit() function which is
+ * called before execution of native PE TLS callbacks.
+ *
+ * For EXE builds the tls_callback() function is called PE TLS callback and
+ * hence after all atexit callbacks. For DLL builds the tls_callback() function
+ * is called from DLL entry point before all atexit callbacks. Note that for
+ * DLL builds also PE TLS callbacks are executed before all atexit callbacks.
+ *
+ * mingw cxa dtors (executed by tls_atexit_callback) has to be run before 
atexit
+ * callbacks because it is required for execution of global C++ destructors.
+ * So this is why the different behavior for DLL and EXE builds is needed.
+ */
 static void WINAPI tls_atexit_callback(HANDLE __UNUSED_PARAM(hDllHandle), 
DWORD dwReason, LPVOID __UNUSED_PARAM(lpReserved)) {
   if (dwReason == DLL_PROCESS_DETACH) {
     run_thread_dtor_list();
@@ -107,7 +135,23 @@ static void WINAPI tls_atexit_callback(HANDLE 
__UNUSED_PARAM(hDllHandle), DWORD
     }
   }
 }
-
+const _tls_callback_type __mingw_cxa_atexit_callback_ptr = tls_atexit_callback;
+
+/* Force inclusion of code which registers __mingw_cxa_atexit_callback_ptr for 
EXE builds */
+extern const uintptr_t 
__mingw_register_thread_local_exe_atexit_callback_provider;
+static __attribute__((used)) const void *const 
_include_mingw_cxa_atexit_callback_caller = 
&__mingw_register_thread_local_exe_atexit_callback_provider;
+
+
+/*
+ * For EXE builds the tls_callback is called PE TLS callback. If the CRT exit
+ * function was called then the PE TLS callback for DLL_PROCESS_DETACH event
+ * is called after execution of exit function and so after execution of all
+ * registered atexit callbacks.
+ *
+ * For DLL builds the tls_callback is called from DLL entry point. And for
+ * the DLL_PROCESS_DETACH event, the tls_callback is called before execution
+ * of atexit callbacks.
+ */
 static WINBOOL WINAPI tls_callback(HANDLE hDllHandle, DWORD dwReason, LPVOID 
__UNUSED_PARAM(lpReserved)) {
   switch (dwReason) {
   case DLL_PROCESS_ATTACH:
@@ -117,18 +161,6 @@ static WINBOOL WINAPI tls_callback(HANDLE hDllHandle, 
DWORD dwReason, LPVOID __U
         return FALSE;
       InitializeCriticalSection(&lock);
       __dso_handle = hDllHandle;
-      /*
-       * We can only call _register_thread_local_exe_atexit_callback once
-       * in a process; if we call it a second time the process terminates.
-       * When DLLs are unloaded, this callback is invoked before we run the
-       * _onexit tables, but for exes, we need to ask this to be called before
-       * all other registered atexit functions.
-       * Since we are registered as a normal TLS callback, we will be called
-       * another time later as well, but that doesn't matter, it's safe to
-       * invoke this with DLL_PROCESS_DETACH twice.
-       */
-      if (!__mingw_module_is_dll)
-        _register_thread_local_exe_atexit_callback(tls_atexit_callback);
     }
     inited = 1;
     break;
@@ -150,9 +182,11 @@ static WINBOOL WINAPI tls_callback(HANDLE hDllHandle, 
DWORD dwReason, LPVOID __U
      * standard says, but differs from what MSVC does with a dynamically
      * linked CRT (which still runs TLS destructors for the main thread).
      *
-     * For DLLs, run dtors when detached. For EXEs, run dtors via the
-     * thread local atexit callback, to make sure they don't run when
-     * exiting the process with _exit or ExitProcess.
+     * For DLLs, run dtors when detached explicitly by tls_atexit_callback().
+     * For EXEs, run dtors via the thread local atexit callback, to make sure
+     * they don't run when exiting the process with _exit or ExitProcess.
+     * mingw-w64 thread local atexit callback calls the tls_atexit_callback()
+     * and is registered for EXEs by 
_register_thread_local_exe_atexit_callback.
      */
     if (__mingw_module_is_dll)
       tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL);
diff --git a/mingw-w64-crt/crt/tls_exe_atexit_callback.c 
b/mingw-w64-crt/crt/tls_exe_atexit_callback.c
new file mode 100644
index 000000000000..fa9d27073746
--- /dev/null
+++ b/mingw-w64-crt/crt/tls_exe_atexit_callback.c
@@ -0,0 +1,27 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <process.h>
+
+const _tls_callback_type __mingw_cxa_atexit_callback_ptr 
__attribute__((common)); /* tentative */
+
+#if defined(__i386__)
+/* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
+__attribute__((force_align_arg_pointer))
+#endif
+static void __stdcall tls_exe_atexit_callback(void *handle, unsigned long 
reason, void *reserved)
+{
+  if (__mingw_cxa_atexit_callback_ptr)
+    __mingw_cxa_atexit_callback_ptr(handle, reason, reserved);
+}
+
+static void __cdecl __mingw_register_thread_local_exe_atexit_callback(void)
+{
+  _register_thread_local_exe_atexit_callback(tls_exe_atexit_callback);
+}
+
+void (__cdecl *const 
__mingw_register_thread_local_exe_atexit_callback_ptr)(void) = 
__mingw_register_thread_local_exe_atexit_callback; /* override tentative */
+extern const uintptr_t 
__mingw_register_thread_local_exe_atexit_callback_provider 
__attribute__((alias("__mingw_register_thread_local_exe_atexit_callback_ptr")));
 /* nontentative alias */
-- 
2.20.1


>From eb3cf9af8b54fe27ca723513b5ef2f1817344e03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 20:45:32 +0200
Subject: [PATCH 07/14] crt: tls: Call __dyn_tls_dtor also from the
 thread_local_exe_atexit_callback

msvc/UCRT calls the __dyn_tls_dtor function two times, once during the
exit() call via thread_local_exe_atexit_callback and second time from the
PE TLS callback.

So do the same thing in mingw-w64. Stores the __dyn_tls_dtor pointer into
the tentative __dyn_tls_dtor_callback variable, so it can be referenced and
called on demand. msvc also provides the __dyn_tls_dtor_callback symbol.

Note that __dyn_tls_dtor has already guard which prevents duplicate
execution of individual callbacks.

This change fixes execution order of atexit and __dyn_tls_dtor callbacks by
executing all __dyn_tls_dtor callbacks before atexit callbacks. This
matches the msvc behavior.
---
 mingw-w64-crt/crt/tls_exe_atexit_callback.c | 3 +++
 mingw-w64-crt/crt/tlsdtor.c                 | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/mingw-w64-crt/crt/tls_exe_atexit_callback.c 
b/mingw-w64-crt/crt/tls_exe_atexit_callback.c
index fa9d27073746..9db49ac539f7 100644
--- a/mingw-w64-crt/crt/tls_exe_atexit_callback.c
+++ b/mingw-w64-crt/crt/tls_exe_atexit_callback.c
@@ -6,6 +6,7 @@
 
 #include <process.h>
 
+const _tls_callback_type __dyn_tls_dtor_callback __attribute__((common)); /* 
tentative */
 const _tls_callback_type __mingw_cxa_atexit_callback_ptr 
__attribute__((common)); /* tentative */
 
 #if defined(__i386__)
@@ -14,6 +15,8 @@ __attribute__((force_align_arg_pointer))
 #endif
 static void __stdcall tls_exe_atexit_callback(void *handle, unsigned long 
reason, void *reserved)
 {
+  if (__dyn_tls_dtor_callback)
+    __dyn_tls_dtor_callback(handle, reason, reserved);
   if (__mingw_cxa_atexit_callback_ptr)
     __mingw_cxa_atexit_callback_ptr(handle, reason, reserved);
 }
diff --git a/mingw-w64-crt/crt/tlsdtor.c b/mingw-w64-crt/crt/tlsdtor.c
index 22880ce153e9..2441590e9d18 100644
--- a/mingw-w64-crt/crt/tlsdtor.c
+++ b/mingw-w64-crt/crt/tlsdtor.c
@@ -13,6 +13,7 @@
 #include <sect_attribs.h>
 #include <memory.h>
 #include <malloc.h>
+#include <process.h>
 #include <corecrt_startup.h>
 #include <thread_local.h>
 
@@ -97,8 +98,13 @@ __dyn_tls_dtor (HANDLE hDllHandle __attribute__((unused)), 
DWORD dwReason, LPVOI
     }
 }
 
+const _tls_callback_type __dyn_tls_dtor_callback = __dyn_tls_dtor;
 static _CRTALLOC(".CRT$XLD") const PIMAGE_TLS_CALLBACK __xl_d = __dyn_tls_dtor;
 
+/* Force inclusion of code which registers __dyn_tls_dtor_callback for EXE 
builds */
+extern const uintptr_t 
__mingw_register_thread_local_exe_atexit_callback_provider;
+static __attribute__((used)) const void *const 
_include_dyn_tls_dtor_callback_caller = 
&__mingw_register_thread_local_exe_atexit_callback_provider;
+
 /* Force tlssup.c (_tls_used symbol for .tls linker section) to be linked.  */
 extern const IMAGE_TLS_DIRECTORY _tls_used;
 static __attribute__((destructor)) void _include_tls_used(void) { asm volatile 
("" :: "r" (&_tls_used)); }
-- 
2.20.1


>From 4a3a9cae041579de421a5fb8fca56c8340d3c334 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 5 Sep 2026 23:47:25 +0200
Subject: [PATCH 08/14] crt: test: Check event counter in t_tls_exe_dtor test
 for main process callback

Verifies that callback registered via __tlregdtor for the main process
thread (which is called after the main function returns) is not called more
than once.
---
 mingw-w64-crt/testcases/t_tls_exe_dtor.c | 45 ++++++++++++++++++------
 1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/mingw-w64-crt/testcases/t_tls_exe_dtor.c 
b/mingw-w64-crt/testcases/t_tls_exe_dtor.c
index 5c3ca7619033..5ced214cac99 100644
--- a/mingw-w64-crt/testcases/t_tls_exe_dtor.c
+++ b/mingw-w64-crt/testcases/t_tls_exe_dtor.c
@@ -11,11 +11,24 @@
 /* mingw-w64 headers */
 #include "libtest.h"
 
-static int thread_detach = 0;
+extern int __cdecl __tlregdtor(void (__cdecl*func)(void));
 
-static void __cdecl dtor_tls_callback(void)
+static int thread_dtor_counter = 0;
+static int process_dtor_counter = 0;
+
+static void __cdecl thread_dtor_callback(void)
+{
+  thread_dtor_counter++;
+}
+
+static void __cdecl process_dtor_callback(void)
 {
-  thread_detach++;
+  if (process_dtor_counter != 0) {
+    printf("Error after main: dtor TLS callback on main process thread was 
called more times\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+  process_dtor_counter++;
 }
 
 #if defined(__i386__)
@@ -24,10 +37,11 @@ __attribute__((force_align_arg_pointer))
 #endif
 static DWORD WINAPI thread_main(LPVOID user_data __attribute__((unused)))
 {
-  /* Register dtor_tls_callback as oneshot dtor TLS callback */
-  extern int __cdecl __tlregdtor(void (__cdecl*func)(void));
-  if (__tlregdtor(&dtor_tls_callback) != 0)
-    printf("Error: __tlregdtor() failed\n");
+  /* Register thread_dtor_callback as oneshot dtor TLS callback */
+  if (__tlregdtor(&thread_dtor_callback) != 0) {
+    printf("Error: __tlregdtor() on second thread failed\n");
+    return 1;
+  }
   return 0;
 }
 
@@ -62,6 +76,12 @@ int main(void)
     return 77;
   }
 
+  /* Register process_dtor_callback as oneshot dtor TLS callback */
+  if (__tlregdtor(&process_dtor_callback) != 0) {
+    printf("Error: __tlregdtor() on main process thread failed\n");
+    ret = 1;
+  }
+
   printf("Creating new thread\n");
   HANDLE thread = CreateThread(NULL, 0, thread_main, NULL, 0, &(DWORD){0} 
/*out: ThreadId*/);
   if (!thread) {
@@ -81,14 +101,19 @@ int main(void)
       ret = 1;
     }
 
-    if (thread_detach != 1) {
-      printf("Error: dtor TLS callback for DLL_THREAD_DETACH was not 
called\n");
+    if (thread_dtor_counter != 1) {
+      printf("Error: dtor TLS callback on second thread was not called\n");
       ret = 1;
     } else {
-      printf("dtor TLS callback for DLL_THREAD_DETACH was called\n");
+      printf("dtor TLS callback on second thread was called\n");
     }
   }
 
+  if (process_dtor_counter != 0) {
+    printf("Error: dtor TLS callback on main process thread was called before 
exiting process\n");
+    ret = 1;
+  }
+
   if (ret) printf("FAILED\n"); else printf("PASSED\n");
   return ret;
 }
-- 
2.20.1


>From 5672f455f86b8cf31236c777b8a31521daf77987 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 13 Sep 2026 20:24:57 +0200
Subject: [PATCH 09/14] crt: test: Check that
 __mingw_register_thread_local_exe_atexit_callback_ptr is non-NULL in in
 t_tls_exe_dtor test

---
 mingw-w64-crt/testcases/t_tls_exe_dtor.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mingw-w64-crt/testcases/t_tls_exe_dtor.c 
b/mingw-w64-crt/testcases/t_tls_exe_dtor.c
index 5ced214cac99..3befaf4520b7 100644
--- a/mingw-w64-crt/testcases/t_tls_exe_dtor.c
+++ b/mingw-w64-crt/testcases/t_tls_exe_dtor.c
@@ -13,6 +13,12 @@
 
 extern int __cdecl __tlregdtor(void (__cdecl*func)(void));
 
+/* Tentative symbol __mingw_register_thread_local_exe_atexit_callback_ptr is
+ * mingw-w64 internal symbol only for EXE applications. It is non-NULL if the
+ * EXE application is using __tlregdtor.
+ */
+void (__cdecl *const 
__mingw_register_thread_local_exe_atexit_callback_ptr)(void) 
__attribute__((common)); /* tentative */
+
 static int thread_dtor_counter = 0;
 static int process_dtor_counter = 0;
 
@@ -69,6 +75,11 @@ int main(void)
     printf("PE TLS section is present\n");
   }
 
+  if (!__mingw_register_thread_local_exe_atexit_callback_ptr) {
+    printf("Error: __mingw_register_thread_local_exe_atexit_callback_ptr is 
NULL\n");
+    ret = 1;
+  }
+
   if (_osplatform == VER_PLATFORM_WIN32_WINDOWS) {
     printf("PE TLS callbacks are not supported on Win9x\n");
     if (ret)
-- 
2.20.1


>From ff5ac20655b54bbff6e7d85893c44f477d0b5138 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 6 Sep 2026 00:22:28 +0200
Subject: [PATCH 10/14] crt: test: Do not return from
 t_tls_dll_native_process_detach test on non-success

Test's PE TLS callback changes exit code to 0. So use TerminateProcess() on 
non-success.
---
 mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c 
b/mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c
index fd86e6d845b6..bf8a9f6930f8 100644
--- a/mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c
+++ b/mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c
@@ -17,7 +17,8 @@ int main(void)
 
   if (_osplatform == VER_PLATFORM_WIN32_WINDOWS) {
     printf("PE TLS callbacks are not supported on Win9x\n");
-    return 77;
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 77);
   }
 
   /* Force linking libtlsnatprocdet.dll library */
-- 
2.20.1


>From c2a093eee9b4f84be05a94df4a5196f6f49719a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 6 Sep 2026 00:23:20 +0200
Subject: [PATCH 11/14] crt: test: Do not exit t_tls_exe_native_process_detach
 with zero status on non-success

Detect non-success via new global variable.
---
 mingw-w64-crt/testcases/t_tls_exe_native_process_detach.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/testcases/t_tls_exe_native_process_detach.c 
b/mingw-w64-crt/testcases/t_tls_exe_native_process_detach.c
index c67bae81cda3..e1c02c17d56d 100644
--- a/mingw-w64-crt/testcases/t_tls_exe_native_process_detach.c
+++ b/mingw-w64-crt/testcases/t_tls_exe_native_process_detach.c
@@ -9,6 +9,8 @@
 /* mingw-w64 headers */
 #include "libtest.h"
 
+static int main_success = 0;
+
 #if defined(__i386__)
 /* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
 __attribute__((force_align_arg_pointer))
@@ -18,7 +20,7 @@ static void WINAPI pe_tls_callback(HANDLE handle 
__attribute__((unused)), DWORD
   if (reason == DLL_PROCESS_DETACH) {
     printf("SUCCESS: PE TLS callback for DLL_PROCESS_DETACH was called\n");
     /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
-    TerminateProcess(GetCurrentProcess(), 0);
+    TerminateProcess(GetCurrentProcess(), main_success ? 0 : 1);
   }
 }
 
@@ -35,9 +37,11 @@ int main(void)
 
   if (_osplatform == VER_PLATFORM_WIN32_WINDOWS) {
     printf("PE TLS callbacks are not supported on Win9x\n");
-    return 77;
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 77);
   }
 
   printf("Checking if the PE TLS callback for DLL_PROCESS_DETACH would be 
called...\n");
+  main_success = 1;
   return 1; /* TLS callback changes return code to 0 */
 }
-- 
2.20.1


>From c90975da2ca4ab25c79a80df7f8eb8c1064b3121 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 6 Sep 2026 00:27:45 +0200
Subject: [PATCH 12/14] crt: test: Add new t_tls_exe_dtor_atexit_order test
 which verifies order of atexit and __tlregdtor callbacks

The __tlregdtor callback must be called before any atexit callback 
independently of the registration order.
---
 mingw-w64-crt/testcases/Makefile.am           |  1 +
 .../testcases/t_tls_exe_dtor_atexit_order.c   | 65 +++++++++++++++++++
 2 files changed, 66 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_tls_exe_dtor_atexit_order.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 6cfa3f88369c..f80fdce38aa4 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -140,6 +140,7 @@ testcase_progs = \
   t_tls_dll_native \
   t_tls_dll_native_process_detach \
   t_tls_exe_dtor \
+  t_tls_exe_dtor_atexit_order \
   t_tls_exe_dyn \
   t_tls_exe_native \
   t_tls_exe_native_process_detach \
diff --git a/mingw-w64-crt/testcases/t_tls_exe_dtor_atexit_order.c 
b/mingw-w64-crt/testcases/t_tls_exe_dtor_atexit_order.c
new file mode 100644
index 000000000000..6554ae396924
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_tls_exe_dtor_atexit_order.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
+/* mingw-w64 headers */
+#include "libtest.h"
+
+extern int __cdecl __tlregdtor(void (__cdecl*func)(void));
+
+static int main_success = 0;
+
+static void __cdecl atexit1_callback(void)
+{
+  printf("Error: atexit1 calllback was called before __tlregdtor callback\n");
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), 1);
+}
+
+static void __cdecl atexit2_callback(void)
+{
+  printf("Error: atexit2 calllback was called before __tlregdtor callback\n");
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), 1);
+}
+
+static void __cdecl dtor_callback(void)
+{
+  printf("SUCCESS: __tlregdtor callback was called\n");
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), main_success ? 0 : 1);
+}
+
+int main(void)
+{
+  mingw_test_init();
+
+  /* Register two atexit callbacks, one before __tlregdtor() call and one 
after __tlregdtor() call.
+   * The __tlregdtor callback must be called before any atexit callback 
independently of the registration order. */
+
+  if (atexit(atexit1_callback) != 0) {
+    printf("Error: atexit() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  if (__tlregdtor(&dtor_callback) != 0) {
+    printf("Error: __tlregdtor() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  if (atexit(atexit2_callback) != 0) {
+    printf("Error: atexit() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  printf("Checking if the __tlregdtor callback would be called before all 
atexit callbacks...\n");
+  main_success = 1;
+  return 1; /* __tlregdtor callback changes return code to 0 */
+}
-- 
2.20.1


>From 3990bc585b74e71ac034217000b21b8f84773361 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 13 Sep 2026 20:21:29 +0200
Subject: [PATCH 13/14] crt: test: Add new t_tls_exe_cxa_atexit_order test
 which verifies order of atexit and mingw_cxa_atexit callbacks

The mingw_cxa_atexit callback must be called before any atexit callback 
independently of the registration order.
---
 mingw-w64-crt/testcases/Makefile.am           |  1 +
 .../testcases/t_tls_exe_cxa_atexit_order.c    | 70 +++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_tls_exe_cxa_atexit_order.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index f80fdce38aa4..8459e28f1f9e 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -139,6 +139,7 @@ testcase_progs = \
   t_tls_dll \
   t_tls_dll_native \
   t_tls_dll_native_process_detach \
+  t_tls_exe_cxa_atexit_order \
   t_tls_exe_dtor \
   t_tls_exe_dtor_atexit_order \
   t_tls_exe_dyn \
diff --git a/mingw-w64-crt/testcases/t_tls_exe_cxa_atexit_order.c 
b/mingw-w64-crt/testcases/t_tls_exe_cxa_atexit_order.c
new file mode 100644
index 000000000000..003b21247091
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_tls_exe_cxa_atexit_order.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
+/* mingw-w64 headers */
+#include "libtest.h"
+
+extern int __mingw_cxa_atexit(void (__thiscall *dtor)(void *obj), void *obj, 
void *dso);
+
+static int main_success = 0;
+
+static void __cdecl atexit1_callback(void)
+{
+  printf("Error: atexit1 calllback was called before mingw_cxa_atexit 
callback\n");
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), 1);
+}
+
+static void __cdecl atexit2_callback(void)
+{
+  printf("Error: atexit2 calllback was called before mingw_cxa_atexit 
callback\n");
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), 1);
+}
+
+static void __thiscall mingw_cxa_atexit_callback(void *obj)
+{
+  if (obj != (void*)42) {
+    printf("Error: mingw_cxa_atexit callback was called with wrong obj\n");
+    main_success = 0;
+  } else {
+    printf("SUCCESS: mingw_cxa_atexit callback was called\n");
+  }
+  /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+  TerminateProcess(GetCurrentProcess(), main_success ? 0 : 1);
+}
+
+int main(void)
+{
+  mingw_test_init();
+
+  /* Register two atexit callbacks, one before __mingw_cxa_atexit() call and 
one after __mingw_cxa_atexit() call.
+   * The mingw_cxa_atexit callback must be called before any atexit callback 
independently of the registration order. */
+
+  if (atexit(atexit1_callback) != 0) {
+    printf("Error: atexit() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  if (__mingw_cxa_atexit(&mingw_cxa_atexit_callback, (void*)42, NULL) != 0) {
+    printf("Error: __mingw_cxa_atexit() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  if (atexit(atexit2_callback) != 0) {
+    printf("Error: atexit() failed\n");
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use 
TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 1);
+  }
+
+  printf("Checking if the mingw_cxa_atexit callback would be called before all 
atexit callbacks...\n");
+  main_success = 1;
+  return 1; /* mingw_cxa_atexit callback changes return code to 0 */
+}
-- 
2.20.1


>From 737e6d8539ce743d1153ec799295771e1a931b7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 6 Sep 2026 00:40:37 +0200
Subject: [PATCH 14/14] crt: test: Add new t_cpp_dtor_atexit_order test which
 verifies order of atexit callbacks and C++ thread local destructors

---
 mingw-w64-crt/testcases/Makefile.am           |  4 ++
 .../testcases/t_cpp_dtor_atexit_order.cpp     | 53 +++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_cpp_dtor_atexit_order.cpp

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 8459e28f1f9e..41ce4677b985 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -96,6 +96,7 @@ testcase_progs = \
   t_assert \
   t_btowc \
   t_char16_t \
+  t_cpp_dtor_atexit_order \
   t_ctype \
   t_excpt_gcc_cpp \
   t_excpt_int_divzero \
@@ -211,6 +212,7 @@ endif
 # Most of the tests are C programs, but there are some C++ tests as well.
 # For C++ targets, we have to explicitly specify source files so that
 # Automake does not assume .c extension.
+t_cpp_dtor_atexit_order_SOURCES = t_cpp_dtor_atexit_order.cpp
 t_intrin_cpp_SOURCES = t_intrin_cpp.cpp
 t_iostream_SOURCES = t_iostream.cpp
 t_isfine_SOURCES = t_isfine.cpp
@@ -222,6 +224,7 @@ t_wrongret_SOURCES = t_wrongret.cpp
 
 # On arm64ec, link all C++ tests statically; the toolchain (and the C++ DLL in
 # PATH) may be regular native aarch64, but we build and run arm64ec binaries.
+t_cpp_dtor_atexit_order_LDFLAGS = $(AM_LDFLAGS)
 t_intrin_cpp_LDFLAGS = $(AM_LDFLAGS)
 t_iostream_LDFLAGS = $(AM_LDFLAGS)
 t_isfine_LDFLAGS = $(AM_LDFLAGS)
@@ -232,6 +235,7 @@ t_trycatch_LDFLAGS = $(AM_LDFLAGS)
 t_wrongret_LDFLAGS = $(AM_LDFLAGS)
 
 if ARM64EC
+t_cpp_dtor_atexit_order_LDFLAGS += -static
 t_intrin_cpp_LDFLAGS += -static
 t_iostream_LDFLAGS += -static
 t_isfine_LDFLAGS += -static
diff --git a/mingw-w64-crt/testcases/t_cpp_dtor_atexit_order.cpp 
b/mingw-w64-crt/testcases/t_cpp_dtor_atexit_order.cpp
new file mode 100644
index 000000000000..dce764184ff6
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_cpp_dtor_atexit_order.cpp
@@ -0,0 +1,53 @@
+#include <cstdio>
+#include <cstdlib>
+
+/* mingw-w64 headers */
+#ifdef __MINGW64_VERSION_MAJOR
+#include "libtest.h"
+#endif
+
+template <void (*callback)()> struct dtor_callback { ~dtor_callback() { 
callback(); } };
+
+bool main_success = false;
+
+void cpp_tls_dtor_func() {
+    std::puts("C++ tls dtor func called");
+    if (!main_success) {
+        std::_Exit(1);
+    }
+    std::puts("TEST PASSED");
+    std::_Exit(0);
+}
+template struct dtor_callback<&cpp_tls_dtor_func>;
+thread_local dtor_callback<&cpp_tls_dtor_func> register_tls_dtor_callback;
+
+void atexit_func() {
+    std::puts("atexit func called");
+    std::puts("TEST FAILED");
+    main_success = false;
+    std::_Exit(1);
+}
+
+int main() {
+#ifdef __MINGW64_VERSION_MAJOR
+    mingw_test_init();
+#endif
+
+    /* For some unknown reasons, this test is passing under mingw-w64 clang 
and under linux gcc but is failing under mingw-w64 gcc */
+#if defined(__MINGW64_VERSION_MAJOR) && !defined(__clang__)
+    return 77;
+#endif
+
+    std::puts("registering atexit func");
+    std::atexit(atexit_func);
+
+    std::puts("referencing C++ TLS obj");
+    (void)register_tls_dtor_callback;
+
+    std::puts("registering atexit func");
+    std::atexit(atexit_func);
+
+    std::puts("returning from main");
+    main_success = true;
+    return 1;
+}
-- 
2.20.1

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to