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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Minux Ma <minux@golang.org>
This commit is contained in:
Alex Brainman 2016-10-11 14:24:59 +11:00
parent 8f65379f94
commit dd307da10c
2 changed files with 13 additions and 5 deletions

View file

@ -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

View file

@ -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