]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/cgo: do not explicitly link msvcrt.dll
authorAlex Brainman <alex.brainman@gmail.com>
Tue, 11 Oct 2016 03:24:59 +0000 (14:24 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Tue, 11 Oct 2016 05:45:06 +0000 (05:45 +0000)
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>
src/runtime/cgo/cgo.go
src/runtime/os_windows.go

index ce0e6a3c35a0a5067ca90a092d10b43ba8861be1..c94dd0f5bc0b8ffaef45ddffcbe58551dd0ba033 100644 (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
 
index 0f52d7d4703ff145b44f16fe2cffedf86726ed41..123ede9d148865fbae068c7a42c225350628dbb9 100644 (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