crt: test: New test which checks that native TLS callback inside linked DLL for DLL_PROCESS_DETACH is called at least once

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/testcases/Makefile.am b/mingw-w64-crt/testcases/Makefile.am
index 38116a3..c9930ce 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -23,6 +23,11 @@
 libprocdetach.dll: libprocdetach.o
 	$(LINK) -shared libprocdetach.o -o libprocdetach.dll
 
+# Used by t_tls_dll_native_process_detach
+libtlsnatprocdet.dll: libtlsnatprocdet.o
+	$(LINK) -shared -o $@ $^
+t_tls_dll_native_process_detach.exe: libtlsnatprocdet.dll
+
 # Used by t_nontls_dll
 libnontls.dll: libnontls.o
 	$(LINK) -shared -o $@ $^
@@ -121,6 +126,7 @@
   t_nontls_dll \
   t_nontls_exe \
   t_tls_dll \
+  t_tls_dll_native_process_detach \
   t_tls_exe_dtor \
   t_tls_exe_dyn \
   t_tls_exe_native \
@@ -237,6 +243,9 @@
 # Compile t_tls_c11 in C11 mode
 t_tls_c11_CFLAGS = -std=c11 $(AM_CFLAGS)
 
+# Link t_tls_dll_native_process_detach against libtlsnatprocdet.dll
+t_tls_dll_native_process_detach_LDADD = libtlsnatprocdet.dll $(LDADD)
+
 # t_winmain is a GUI App; use -mwindows to properly link it and set subsystem
 t_winmain_LDFLAGS = $(AM_LDFLAGS) -mwindows
 
@@ -302,4 +311,5 @@
   libtls08.dll \
   libtls09.dll \
   libtls10.dll \
-  libtls11.dll
+  libtls11.dll \
+  libtlsnatprocdet.dll
diff --git a/mingw-w64-crt/testcases/libtlsnatprocdet.c b/mingw-w64-crt/testcases/libtlsnatprocdet.c
new file mode 100644
index 0000000..3d29c8b
--- /dev/null
+++ b/mingw-w64-crt/testcases/libtlsnatprocdet.c
@@ -0,0 +1,27 @@
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
+#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 WINAPI pe_tls_callback(HANDLE handle __attribute__((unused)), DWORD reason, LPVOID reserved __attribute__((unused)))
+{
+  /* When process is exiting, change its exit code from 1 to 0 */
+  if (reason == DLL_PROCESS_DETACH) {
+    /* exit, _exit, or ExitProcess calls TLS callbacks, so use TerminateProcess() which is not calling them */
+    TerminateProcess(GetCurrentProcess(), 0);
+  }
+}
+
+/* Register pe_tls_callback as PE TLS callback with the highest priority (=B) */
+static __attribute__((section(".CRT$XLB"), used)) const PIMAGE_TLS_CALLBACK register_pe_tls_callback = pe_tls_callback;
+
+/* 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)); }
+
+/* Export some symbol which is specific to this library, test file is importing it */
+__attribute__((dllexport)) const int libtlsnatprocdet_library = 1;
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
new file mode 100644
index 0000000..fd86e6d
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_tls_dll_native_process_detach.c
@@ -0,0 +1,29 @@
+#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"
+
+__attribute__((dllimport)) extern const int libtlsnatprocdet_library;
+
+int main(void)
+{
+  mingw_test_init();
+
+  if (_osplatform == VER_PLATFORM_WIN32_WINDOWS) {
+    printf("PE TLS callbacks are not supported on Win9x\n");
+    return 77;
+  }
+
+  /* Force linking libtlsnatprocdet.dll library */
+  const volatile int *const volatile libtlsnatprocdet_library_ref = &libtlsnatprocdet_library;
+  (void)libtlsnatprocdet_library_ref;
+
+  printf("Checking if the PE TLS callback inside libtlsnatprocdet.dll for DLL_PROCESS_DETACH would be called...\n");
+  return 1; /* TLS callback inside libtlsnatprocdet.dll changes return code to 0 */
+}