winpthreads: Unify setup of winpthreads_init callback between gcc and msvc builds For all builds register callback via .CRT$XCT section (same what is already done for msvc builds). For gcc builds, the .CRT$XC* callbacks are executed before all other __attribute__((constructor(0))) callbacks. So the change should be safe. To prevent garbage collecting the __xc_t variable, it is enough to mark it with __attribute__((used)). It does not have to be exported, so mark it as static. Co-authored-by: LIU Hao <lh_mouse@126.com> Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c index 95f4033..7954769 100644 --- a/mingw-w64-libraries/winpthreads/src/misc.c +++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -161,13 +161,6 @@ } WinpthreadsImpl; #endif -#if defined(__GNUC__) || defined(__clang__) -#if __GNUC__ >= 9 && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wprio-ctor-dtor" -#endif -__attribute__ ((constructor (0))) -#endif static void winpthreads_init (void) { #ifdef RUNTIME_CHECKS @@ -225,25 +218,23 @@ winpthreads_affinity_init (); winpthreads_time_init (); } -#if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__) -#pragma GCC diagnostic pop -#endif +/* Prevent whole program optimization from discarding the __xc_t variable. */ #if defined(_MSC_VER) && !defined(__clang__) -/* Force a reference to __xc_t to prevent whole program optimization - * from discarding the variable. */ - +/* msvc: Tell linker to always include the public symbol for __xc_t variable. */ /* On x86, symbols are prefixed with an underscore. */ #if defined(_M_IX86) #pragma comment(linker, "/include:___xc_t") #else #pragma comment(linker, "/include:__xc_t") #endif - +/* .CRT$XCB to .CRT$XCY is an array of _PVFV pointers. + * Pick an arbitrary location for our callback. */ #pragma section(".CRT$XCT", long, read) -__declspec (allocate (".CRT$XCT")) -extern const _PVFV __xc_t; -const _PVFV __xc_t = winpthreads_init; +__declspec(allocate(".CRT$XCT")) const _PVFV __xc_t = winpthreads_init; +#else +/* gcc: Mark __xc_t variable with used attribute. */ +__attribute__ ((used, section(".CRT$XCT"))) static const _PVFV __xc_t = winpthreads_init; #endif #if WINPTHREADS_WINNT > WINPTHREADS_WINNT_WIN10