在 2026-10-19 05:34, Peter Damianov 写道:
On other x86 targets, -mpc32, -mpc64 and -mpc80 link crtprec32.o, crtprec64.o or crtprec80.o, whose constructor sets the x87 precision control. The mingw-w64 targets accept the options, but libgcc does not build those objects for them and the driver does not link them, so the options have no effect.Build the objects for mingw targets and link them for mingw-w64. Unlike on Linux, a new thread on Windows does not inherit the x87 control word of the thread that created it. Instead, every thread starts with 53-bit precision. On 32-bit targets crtprec.c therefore also registers a TLS callback that sets the precision on each new thread. It is placed in .CRT$XLB so that it runs before the CRT's callback that runs dynamic initializers of thread-local variables. This is not possible on x86_64, where the thread's initial context, including its floating-point state, is loaded after TLS callbacks run. There is nothing crtprec.c can do about threads created with CreateThread or _beginthreadex there. But winpthreads passes the creating thread's floating-point environment to new threads (mingw-w64 patch "winpthreads: Inherit the floating-point environment in new threads"), so on x86_64 the setting still reaches the main thread and threads created with pthread_create. The TLS callback refers to the CRT's TLS directory, _tls_used, so the objects are linked from STARTFILE_SPEC, ahead of the CRT libraries, rather than from ENDFILE_SPEC as on other targets. gcc/ChangeLog: * config/i386/mingw-w64.h (STARTFILE_SPEC): Link crtprec32.o, crtprec64.o or crtprec80.o for -mpc32, -mpc64 or -mpc80. libgcc/ChangeLog: * config.host (i[34567]86-*-mingw*, x86_64-*-mingw*): Add crtprec32.o, crtprec64.o and crtprec80.o to extra_parts and i386/t-crtpc to tmake_file. * config/i386/crtprec.c [_WIN32 && __i386__] (set_precision_tls): New function. (crtprec_tls_used, crtprec_tls_callback): New variables. Signed-off-by: Peter Damianov <[email protected]> --- Testing: I built an i686-w64-mingw32 cross compiler (C only) from this commit and ran a small test program on Windows 11 (32-bit process under WOW64) and on native 32-bit Windows 2000 (in the v86 emulator). With -mpc32, -mpc64 and -mpc80, the x87 control word has the requested precision on the main thread and on threads created with CreateThread, _beginthreadex and pthread_create, linked with and without -pthread, statically and against the winpthreads DLL. A TLS callback placed in .CRT$XLC, where the CRT constructs thread-local objects, also sees the requested precision. I have not built or tested an x86_64-w64-mingw32 compiler with this change, and have not run the GCC testsuite. Like on Linux, crtprec*.o is also linked into DLLs built with -shared, so such a DLL changes the precision of the process that loads it. I can restrict it to executables with %{!shared:%{!mdll:...}} if preferred. The winpthreads change mentioned above was posted to mingw-w64-public as Message-ID <[email protected]>. gcc/config/i386/mingw-w64.h | 6 ++++++ libgcc/config.host | 8 ++++---- libgcc/config/i386/crtprec.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/gcc/config/i386/mingw-w64.h b/gcc/config/i386/mingw-w64.h index 5d71aa5358f..14abdee494c 100644 --- a/gcc/config/i386/mingw-w64.h +++ b/gcc/config/i386/mingw-w64.h @@ -49,12 +49,18 @@ along with GCC; see the file COPYING3. If not see -lmingwex %{!mcrtdll=*:-lmsvcrt} %{mcrtdll=*:-l%*} \ -lkernel32 " MCFGTHREAD_SPEC+/* crtprec*.o refers to the CRT's TLS directory (_tls_used) to register a+ TLS callback, so unlike on other targets it is linked before the + libraries rather than from ENDFILE_SPEC. */ #undef STARTFILE_SPEC #define STARTFILE_SPEC "%{shared|mdll:dllcrt2%O%s} \ %{!shared:%{!mdll:%{!municode:crt2%O%s}}} \ %{!shared:%{!mdll:%{municode:crt2u%O%s}}} \ %{pg:gcrt2%O%s} \ crtbegin.o%s \ + %{mpc32:crtprec32.o%s} \ + %{mpc64:crtprec64.o%s} \ + %{mpc80:crtprec80.o%s} \ %{fvtable-verify=none:%s; \ fvtable-verify=preinit:vtv_start.o%s; \ fvtable-verify=std:vtv_start.o%s}" diff --git a/libgcc/config.host b/libgcc/config.host index 319d6cfe0ef..4ac663c5983 100644 --- a/libgcc/config.host +++ b/libgcc/config.host @@ -882,7 +882,7 @@ x86_64-*-cygwin*) tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file} i386/t-slibgcc-cygming i386/t-cygming i386/t-cygwin t-crtfm t-dfprules i386/t-chkstk" ;; i[34567]86-*-mingw*) - extra_parts="crtbegin.o crtend.o crtfastmath.o" + extra_parts="crtbegin.o crtend.o crtprec32.o crtprec64.o crtprec80.o crtfastmath.o" if test x$enable_vtable_verify = xyes; then extra_parts="$extra_parts vtv_start.o vtv_end.o vtv_start_preinit.o vtv_end_preinit.o" fi @@ -910,7 +910,7 @@ i[34567]86-*-mingw*) else tmake_dlldir_file="i386/t-dlldir-x" fi - tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_thr_file} ${tmake_dlldir_file} i386/t-slibgcc-cygming i386/t-slibgcc-mingw i386/t-cygming i386/t-mingw32 t-crtfm i386/t-chkstk t-dfprules" + tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_thr_file} ${tmake_dlldir_file} i386/t-slibgcc-cygming i386/t-slibgcc-mingw i386/t-cygming i386/t-mingw32 i386/t-crtpc t-crtfm i386/t-chkstk t-dfprules" ;; x86_64-*-mingw*) case ${target_thread_file} in @@ -940,8 +940,8 @@ x86_64-*-mingw*) else tmake_dlldir_file="i386/t-dlldir-x" fi - tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_thr_file} ${tmake_dlldir_file} i386/t-slibgcc-cygming i386/t-slibgcc-mingw i386/t-cygming i386/t-mingw32 t-dfprules t-crtfm i386/t-chkstk" - extra_parts="$extra_parts crtbegin.o crtend.o crtfastmath.o" + tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_thr_file} ${tmake_dlldir_file} i386/t-slibgcc-cygming i386/t-slibgcc-mingw i386/t-cygming i386/t-mingw32 t-dfprules i386/t-crtpc t-crtfm i386/t-chkstk" + extra_parts="$extra_parts crtbegin.o crtend.o crtprec32.o crtprec64.o crtprec80.o crtfastmath.o" if test x$enable_vtable_verify = xyes; then extra_parts="$extra_parts vtv_start.o vtv_end.o vtv_start_preinit.o vtv_end_preinit.o" fi diff --git a/libgcc/config/i386/crtprec.c b/libgcc/config/i386/crtprec.c index a2f97bfd5c0..a4d429b3ac6 100644 --- a/libgcc/config/i386/crtprec.c +++ b/libgcc/config/i386/crtprec.c @@ -46,4 +46,35 @@ set_precision (void)asm volatile ("fldcw\t%0" : : "m" (cwd));} + +#if defined (_WIN32) && defined (__i386__) +/* Windows does not let a new thread inherit the x87 control word of the + thread that created it; every thread starts with 53-bit precision. Set + the precision again from a TLS callback, which runs on each new thread + before its start routine. This cannot work on x86_64, where the thread's + initial context, including its floating-point state, is loaded after the + TLS callbacks have run. */ +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +static void NTAPI +set_precision_tls (PVOID handle, DWORD reason, PVOID reserved) +{ + (void) handle; + (void) reserved; + if (reason == DLL_THREAD_ATTACH) + set_precision (); +} + +/* Pull in the CRT's TLS directory, so that the callback is run. */ +extern const IMAGE_TLS_DIRECTORY _tls_used; +static const void *const crtprec_tls_used __attribute__((used)) = &_tls_used;
This is not sufficient to retain `_tls_used`; `crtprec_tls_used` may be subject to `-Wl,--gc-sections`. The variable should be defined in a section that is KEEP'd in the linker script.
In mingw-w64-crt/crt/tlsdtor.c, it's referenced like this:
extern const IMAGE_TLS_DIRECTORY _tls_used;
static __attribute__((destructor)) void _include_tls_used(void)
{ asm volatile ("" :: "r" (&_tls_used)); }
+
+/* The CRT's TLS callbacks run in the order of their .CRT$XL? section names.
+ Use .CRT$XLB so that the precision is already set when the CRT's
+ callback in .CRT$XLC runs dynamic initializers of thread-local
+ variables. */
+static const PIMAGE_TLS_CALLBACK crtprec_tls_callback
+ __attribute__((section (".CRT$XLB"), used)) = set_precision_tls;
+#endif
#endif
Other than that, this change looks fine. You probably want to document the limitation that these options do not always work on x86-64, as described in the commit message.
-- Best regards, LIU Hao
OpenPGP_signature.asc
Description: OpenPGP digital signature
