From: Alex Brainman Date: Tue, 11 Oct 2016 03:24:59 +0000 (+1100) Subject: runtime/cgo: do not explicitly link msvcrt.dll X-Git-Tag: go1.8beta1~957 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dd307da10c3582c195928d9bf073d1b0b01f2135;p=gostls13.git runtime/cgo: do not explicitly link msvcrt.dll CL 14472 solved issue #12030 by explicitly linking msvcrt.dll to every cgo executable we build. This CL achieves the same by manually loading ntdll.dll during startup. Updates #12030 Change-Id: I5d9cd925ef65cc34c5d4031c750f0f97794529b2 Reviewed-on: https://go-review.googlesource.com/30737 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor Reviewed-by: Minux Ma --- diff --git a/src/runtime/cgo/cgo.go b/src/runtime/cgo/cgo.go index ce0e6a3c35..c94dd0f5bc 100644 --- a/src/runtime/cgo/cgo.go +++ b/src/runtime/cgo/cgo.go @@ -20,9 +20,7 @@ package cgo #cgo !android,linux LDFLAGS: -lpthread #cgo netbsd LDFLAGS: -lpthread #cgo openbsd LDFLAGS: -lpthread -// we must explicitly link msvcrt, because runtime needs ntdll, and ntdll -// exports some incompatible libc functions. See golang.org/issue/12030. -#cgo windows LDFLAGS: -lmsvcrt -lm -mthreads +#cgo windows LDFLAGS: -lm -mthreads #cgo CFLAGS: -Wall -Werror diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go index 0f52d7d470..123ede9d14 100644 --- a/src/runtime/os_windows.go +++ b/src/runtime/os_windows.go @@ -33,7 +33,6 @@ const ( //go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll" //go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll" //go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll" -//go:cgo_import_dynamic runtime._NtWaitForSingleObject NtWaitForSingleObject%3 "ntdll.dll" //go:cgo_import_dynamic runtime._ResumeThread ResumeThread%1 "kernel32.dll" //go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler%2 "kernel32.dll" //go:cgo_import_dynamic runtime._SetErrorMode SetErrorMode%1 "kernel32.dll" @@ -77,7 +76,6 @@ var ( _GetThreadContext, _LoadLibraryW, _LoadLibraryA, - _NtWaitForSingleObject, _ResumeThread, _SetConsoleCtrlHandler, _SetErrorMode, @@ -114,6 +112,11 @@ var ( // when building executable as Cgo. So load SystemFunction036 // manually during runtime startup. _RtlGenRandom stdFunction + + // Load ntdll.dll manually during startup, otherwise Mingw + // links wrong printf function to cgo executable (see issue + // 12030 for details). + _NtWaitForSingleObject stdFunction ) // Function to be called by windows CreateThread @@ -178,6 +181,13 @@ func loadOptionalSyscalls() { throw("advapi32.dll not found") } _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000")) + + var ntdll = []byte("ntdll.dll\000") + n32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&ntdll[0]))) + if n32 == 0 { + throw("ntdll.dll not found") + } + _NtWaitForSingleObject = windowsFindfunc(n32, []byte("NtWaitForSingleObject\000")) } //go:nosplit