crt: Make tls_atexit.c to use explicit Tls function calls instead of using automatic thread local storage

The automatic thread local storage worked just fine when using
native TLS (as clang does by default), but fails when using emutls
(which is the default in GCC).

The TLS callback function for DLL_THREAD_DETACH is called after emutls
has already cleaned up for the thread, so the tls_dtors were always NULL
at that time. Work around this by using Windows native TlsAlloc and
friends.

This didn't seem like an issue originally, as GCC/libstdc++ seemed to
keep using its own function. However that was an incorrect conclusion;
when building a cross compiler, libstdc++ always provides the
__cxa_thread_atexit function, but if building GCC/libstdc++ natively,
it tries to detect the presence of the __cxa_thread_atexit, and if found,
it omits providing it in libstdc++, using the one from mingw-w64-crt
instead.

Fixes https://sourceforge.net/p/mingw-w64/bugs/859/

Signed-off-by: Jeremy Drake <jeremyd2019@users.sourceforge.net>
Signed-off-by: Martin Storsjö <martin@martin.st>
1 file changed