Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/link: linking fails with some PE imported libraries #38951

Closed
Neurostep opened this issue May 8, 2020 · 4 comments
Closed

cmd/link: linking fails with some PE imported libraries #38951

Neurostep opened this issue May 8, 2020 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@Neurostep
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.14 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build325877425=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created a repository https://github.com/Neurostep/go-link-test where I put everything to reproduce the issue.

If you clone the provided repo and try to run BUILDER_IMAGE=neurostep/go-linker-test:go114 ./run.sh, you should see the linker throws an error.

If you run BUILDER_IMAGE=neurostep/go-linker-test:go112 ./run.sh the build creates just fine.

What did you expect to see?

I expect to see PE imported libs ws2_32 and winmm are linked as it did before go version 1.13.

Here I put the host link output only

"x86_64-w64-mingw32-gcc" "-m64" "-mconsole" "-Wl,--tsaware" "-Wl,--nxcompat" "-Wl,--major-os-version=6" "-Wl,--minor-os-version=1" "-Wl,--major-subsystem-version=6" "-Wl,--minor-subsystem-version=1" "-o" "/tmp/go-build768320292/b001/exe/a.out.exe" "/tmp/go-link-058751748/go.o" "/tmp/go-link-058751748/000000.o" "/tmp/go-link-058751748/000001.o" "/tmp/go-link-058751748/000002.o" "/tmp/go-link-058751748/000003.o" "/tmp/go-link-058751748/000004.o" "/tmp/go-link-058751748/000005.o" "/tmp/go-link-058751748/000006.o" "/tmp/go-link-058751748/000007.o" "/tmp/go-link-058751748/000008.o" "-g" "-O2" "-L/opt/mingw64/lib" "-lcrypto" "-g" "-O2" "-no-pie" "-static" "-static-libgcc" "-static-libstdc++" "-Wl,-T,/tmp/go-link-058751748/fix_debug_gdb_scripts.ld" "-Wl,--start-group" "-lmingwex" "-lmingw32" "-Wl,--end-group" "-lwinmm" "-lws2_32" "-lkernel32"

What did you see instead?

Here I put the host link output only

"x86_64-w64-mingw32-gcc" "-m64" "-mconsole" "-Wl,--tsaware" "-Wl,--nxcompat" "-Wl,--major-os-version=6" "-Wl,--minor-os-version=1" "-Wl,--major-subsystem-version=6" "-Wl,--minor-subsystem-version=1" "-o" "/tmp/go-build768320292/b001/exe/a.out.exe" "/tmp/go-link-058751748/go.o" "/tmp/go-link-058751748/000000.o" "/tmp/go-link-058751748/000001.o" "/tmp/go-link-058751748/000002.o" "/tmp/go-link-058751748/000003.o" "/tmp/go-link-058751748/000004.o" "/tmp/go-link-058751748/000005.o" "/tmp/go-link-058751748/000006.o" "/tmp/go-link-058751748/000007.o" "/tmp/go-link-058751748/000008.o" "-g" "-O2" "-L/opt/mingw64/lib" "-lcrypto" "-g" "-O2" "-no-pie" "-static" "-static-libgcc" "-static-libstdc++" "-Wl,-T,/tmp/go-link-058751748/fix_debug_gdb_scripts.ld" "-Wl,--start-group" "-lmingwex" "-lmingw32" "-Wl,--end-group" "-lkernel32"
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 8, 2020
@thanm
Copy link
Contributor

thanm commented May 8, 2020

Thanks for submitting this issue.

the linker throws an error

Could you please attach the text of the error (including stack trace) to the bug? Thanks.

@Neurostep
Copy link
Author

@thanm I believe I was not super accurate in explaining the issue. The linker does not throw an error actually. Error produces by the x86_64-w64-mingw32-gcc complaining about undefined reference. Here what I got:

$ BUILDER_IMAGE=neurostep/go-linker-test:go114 ./run.sh
+ exec docker run --rm --user=root -v /Users/maksim-terekhin/my-projects/go-link-test:/srv/src/github.com/Neurostep/go-link-test --entrypoint=/bin/sh neurostep/go-linker-test:go114 -c 'export GOPATH=/srv && cd '\''/srv/src/github.com/Neurostep/go-link-test'\'' && GOARCH=amd64 GOOS=windows CXX="x86_64-w64-mingw32-g++" CC="x86_64-w64-mingw32-gcc -fno-stack-protector -lssp" CGO_ENABLED=1 go install -ldflags "-v -linkmode=external -extldflags '\'' -static -static-libgcc -static-libstdc++'\''  " ./cmd/test '
# github.com/Neurostep/go-link-test/cmd/test
HEADER = -H10 -T0xffffffffffffffff -R0xffffffff
deadcode
symsize = 0
pclntab=305413 bytes, funcdata total 68666 bytes
symsize = 0
host link: "x86_64-w64-mingw32-gcc" "-m64" "-mconsole" "-Wl,--tsaware" "-Wl,--nxcompat" "-Wl,--major-os-version=6" "-Wl,--minor-os-version=1" "-Wl,--major-subsystem-version=6" "-Wl,--minor-subsystem-version=1" "-o" "/tmp/go-build711421856/b001/exe/a.out.exe" "/tmp/go-link-132502817/go.o" "/tmp/go-link-132502817/000000.o" "/tmp/go-link-132502817/000001.o" "/tmp/go-link-132502817/000002.o" "/tmp/go-link-132502817/000003.o" "/tmp/go-link-132502817/000004.o" "/tmp/go-link-132502817/000005.o" "/tmp/go-link-132502817/000006.o" "/tmp/go-link-132502817/000007.o" "/tmp/go-link-132502817/000008.o" "-g" "-O2" "-L/opt/mingw64/lib" "-lcrypto" "-g" "-O2" "-no-pie" "-static" "-static-libgcc" "-static-libstdc++" "-Wl,-T,/tmp/go-link-132502817/fix_debug_gdb_scripts.ld" "-Wl,--start-group" "-lmingwex" "-lmingw32" "-Wl,--end-group" "-lkernel32"
/usr/local/go/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32-gcc failed: exit status 1
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xa1): undefined reference to `__imp_getnameinfo'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xd2): undefined reference to `__imp_ntohs'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0x1b9): undefined reference to `gai_strerrorW'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0x758): undefined reference to `__imp_freeaddrinfo'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xb58): undefined reference to `__imp_getaddrinfo'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xb95): undefined reference to `gai_strerrorW'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xc93): undefined reference to `__imp_getaddrinfo'
/opt/mingw64/lib/libcrypto.a(b_addr.o):b_addr.c:(.text+0xcd0): undefined reference to `gai_strerrorW'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0xc5): undefined reference to `__imp_WSAStartup'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0xd3): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x266): undefined reference to `__imp_WSAStartup'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x274): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x32d): undefined reference to `__imp_ntohs'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x373): undefined reference to `__imp_getsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x392): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x3a3): undefined reference to `__imp_gethostbyname'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x3f5): undefined reference to `__imp_WSAStartup'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x3ff): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x47d): undefined reference to `__imp_WSACleanup'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x49a): undefined reference to `__imp_ioctlsocket'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x4b2): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x5ca): undefined reference to `__imp_WSAStartup'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x5d8): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x84d): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x8d5): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x908): undefined reference to `__imp_ioctlsocket'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x922): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0x988): undefined reference to `__imp_getsockname'
/opt/mingw64/lib/libcrypto.a(b_sock.o):b_sock.c:(.text+0xa02): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x27): undefined reference to `__imp_socket'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x3f): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0xf1): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x11a): undefined reference to `__imp_connect'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x19d): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x1ab): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x202): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x254): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x2e2): undefined reference to `__imp_bind'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x302): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x3db): undefined reference to `__imp_getsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x3f0): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x4c3): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x4cd): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x54e): undefined reference to `__imp_bind'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x569): undefined reference to `__imp_listen'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x582): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x5f8): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x606): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x67d): undefined reference to `__imp_setsockopt'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x68b): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x6dd): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x765): undefined reference to `__imp_accept'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x79d): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x7fa): undefined reference to `__imp_closesocket'
/opt/mingw64/lib/libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x809): undefined reference to `__imp_closesocket'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x1df): undefined reference to `__imp_WSASetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x1f2): undefined reference to `__imp_send'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x22a): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x295): undefined reference to `__imp_WSASetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x2aa): undefined reference to `__imp_send'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x2da): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x34d): undefined reference to `__imp_WSASetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x362): undefined reference to `__imp_recv'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x38d): undefined reference to `__imp_WSAGetLastError'
/opt/mingw64/lib/libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x445): undefined reference to `__imp_WSAGetLastError'
collect2: error: ld returned 1 exit status

@odeke-em odeke-em changed the title Go tool link stopped linking some PE imported libraries cmd/link: linking fails with some PE imported libraries May 10, 2020
@Neurostep
Copy link
Author

@thanm I did a bit of investigation and found out that as a part of fixing security issues in go1.13 we stopped implicitly loading exactly those .dll files that I expect to be linked by the linking process. Here are changes 9b6e9f0

So, I believe it is fine and we can close the issue as there is no regression, or at least it is a part of mitigating security issues.

@thanm If you feel the same please close the issue

@thanm
Copy link
Contributor

thanm commented May 13, 2020

Thanks, makes sense. Closing out the issue.

@thanm thanm closed this as completed May 13, 2020
@golang golang locked and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants