src/runtime/cgo/gcc_libinit_windows.c | 28 +++++++++++++++++++++------- src/runtime/cgo/gcc_windows_386.c | 10 ++-------- src/runtime/cgo/gcc_windows_amd64.c | 8 +------- src/runtime/cgo/gcc_windows_arm64.c | 8 +------- src/runtime/cgo/libcgo_windows.h | 10 ++-------- diff --git a/src/runtime/cgo/gcc_libinit_windows.c b/src/runtime/cgo/gcc_libinit_windows.c index ad5038667a5b62458a147d1fa0a6f12d876aa431..a9b94c37139b446cb5d506c18f8d249d7a1ec122 100644 --- a/src/runtime/cgo/gcc_libinit_windows.c +++ b/src/runtime/cgo/gcc_libinit_windows.c @@ -13,6 +13,16 @@ #include #include #include "libcgo.h" +#include "libcgo_windows.h" + +// Ensure there's one symbol marked __declspec(dllexport). +// If there are no exported symbols, the unfortunate behavior of +// the binutils linker is to also strip the relocations table, +// resulting in non-PIE binary. The other option is the +// --export-all-symbols flag, but we don't need to export all symbols +// and this may overflow the export table (#40795). +// See https://sourceware.org/bugzilla/show_bug.cgi?id=19011 +__declspec(dllexport) int _cgo_dummy_export; static volatile LONG runtime_init_once_gate = 0; static volatile LONG runtime_init_once_done = 0; @@ -53,13 +63,7 @@ } void x_cgo_sys_thread_create(void (*func)(void*), void* arg) { - uintptr_t thandle; - - thandle = _beginthread(func, 0, arg); - if(thandle == -1) { - fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); - abort(); - } + _cgo_beginthread(func, arg); } int @@ -123,3 +127,13 @@ ret = cgo_context_function; LeaveCriticalSection(&runtime_init_cs); return ret; } + +void _cgo_beginthread(void (*func)(void*), void* arg) { + uintptr_t thandle; + + thandle = _beginthread(func, 0, arg); + if (thandle == -1) { + fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); + abort(); + } +} diff --git a/src/runtime/cgo/gcc_windows_386.c b/src/runtime/cgo/gcc_windows_386.c index 60cb011bf2457871a5d458385a6e907acddbf90e..56fbaac9b831fa27da76f145607e3a80b74fa5fe 100644 --- a/src/runtime/cgo/gcc_windows_386.c +++ b/src/runtime/cgo/gcc_windows_386.c @@ -22,13 +22,7 @@ void _cgo_sys_thread_start(ThreadStart *ts) { - uintptr_t thandle; - - thandle = _beginthread(threadentry, 0, ts); - if(thandle == -1) { - fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); - abort(); - } + _cgo_beginthread(threadentry, ts); } static void @@ -50,6 +44,6 @@ "movl %%fs:0x14, %%eax\n" // MOVL 0x14(FS), tmp "movl %1, 0(%%eax)\n" // MOVL g, 0(FS) :: "r"(ts.tls), "r"(ts.g) : "%eax" ); - + crosscall_386(ts.fn); } diff --git a/src/runtime/cgo/gcc_windows_amd64.c b/src/runtime/cgo/gcc_windows_amd64.c index 9df9b9b1e4a0312144fa28ef4ad9f9b0dffec00c..996947eccf4baa23e29fb1481859b575d514e119 100644 --- a/src/runtime/cgo/gcc_windows_amd64.c +++ b/src/runtime/cgo/gcc_windows_amd64.c @@ -24,13 +24,7 @@ void _cgo_sys_thread_start(ThreadStart *ts) { - uintptr_t thandle; - - thandle = _beginthread(threadentry, 0, ts); - if(thandle == -1) { - fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); - abort(); - } + _cgo_beginthread(threadentry, ts); } static void diff --git a/src/runtime/cgo/gcc_windows_arm64.c b/src/runtime/cgo/gcc_windows_arm64.c index 61ef094866a07e9c2517685b20a7ed958e5c5b5b..8f113cc3b16727290d12f86eda522860bb7fd547 100644 --- a/src/runtime/cgo/gcc_windows_arm64.c +++ b/src/runtime/cgo/gcc_windows_arm64.c @@ -23,13 +23,7 @@ void _cgo_sys_thread_start(ThreadStart *ts) { - uintptr_t thandle; - - thandle = _beginthread(threadentry, 0, ts); - if(thandle == -1) { - fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno); - abort(); - } + _cgo_beginthread(threadentry, ts); } extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g); diff --git a/src/runtime/cgo/libcgo_windows.h b/src/runtime/cgo/libcgo_windows.h index 0013f06baebd1b25683462bdde8dd2c416f3c53e..33d7637fece0ecb5e0c3667c560a7f7850d96a29 100644 --- a/src/runtime/cgo/libcgo_windows.h +++ b/src/runtime/cgo/libcgo_windows.h @@ -2,11 +2,5 @@ // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Ensure there's one symbol marked __declspec(dllexport). -// If there are no exported symbols, the unfortunate behavior of -// the binutils linker is to also strip the relocations table, -// resulting in non-PIE binary. The other option is the -// --export-all-symbols flag, but we don't need to export all symbols -// and this may overflow the export table (#40795). -// See https://sourceware.org/bugzilla/show_bug.cgi?id=19011 -__declspec(dllexport) int _cgo_dummy_export; +// Call _beginthread, aborting on failure. +void _cgo_beginthread(void (*func)(void*), void* arg);