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/go: link should stop when cgo is required but disabled #46330

Closed
perillo opened this issue May 23, 2021 · 8 comments
Closed

cmd/go: link should stop when cgo is required but disabled #46330

perillo opened this issue May 23, 2021 · 8 comments
Assignees
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@perillo
Copy link
Contributor

perillo commented May 23, 2021

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

$ go version
go version go1.16.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.
It also reproduces with go1.17 devel.

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/manlio/.local/bin"
GOCACHE="/home/manlio/.cache/go-build"
GOENV="/home/manlio/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE="*.local"
GOMODCACHE="/home/manlio/.local/lib/go/pkg/mod"
GONOPROXY=""
GONOSUMDB="*.local"
GOOS="linux"
GOPATH="/home/manlio/.local/lib/go:/home/manlio/src/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build2831720649=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.16.4 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.16.4
uname -sr: Linux 5.12.5-arch1-1
/usr/lib/libc.so.6: GNU C Library (GNU libc) release release version 2.33.
gdb --version: GNU gdb (GDB) 10.2

What did you do?

Using https://play.golang.org/p/LqHgq34kmVq:

$ CGO_ENABLED=0 GOOS=android GOARCH=386 go build

What I'm doing is to check if my package is buildable for the specified platform.

What did you expect to see?

An error, reporting that cgo is required for the platform but explicitly disabled by the user. The program should exit, instead of continuing.

What did you see instead?

loadinternal: cannot find runtime/cgo
/usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: skipping incompatible /usr/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: cannot find libgcc_s.so.1
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/../../../libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: skipping incompatible /usr/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
collect2: error: ld returned 1 exit status
  • android/arm reports:

    loadinternal: cannot find runtime/cgo
    /usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
    gcc: error: unrecognized command-line option ‘-marm’; did you mean ‘-mabm’?
    
  • android/amd64 and android/arm64 works

  • ios/amd64 and ios/arm64 reports:

    loadinternal: cannot find runtime/cgo
    /usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
    /usr/bin/ld: cannot find 1144: No such file or directory
    /tmp/go-link-897956511/go.o: file not recognized: file format not recognized
    collect2: error: ld returned 1 exit status
    
@perillo perillo changed the title cmd/go: build should stop when cgo is required but disabled by the user cmd/go: link should stop when cgo is required but disabled by the user May 23, 2021
@perillo
Copy link
Contributor Author

perillo commented May 23, 2021

cc @cherrymui @rsc @mdempsky @ianlancetaylor as per owners.

@perillo perillo changed the title cmd/go: link should stop when cgo is required but disabled by the user cmd/link: link should stop when cgo is required but disabled by the user May 23, 2021
@gopherbot
Copy link

Change https://golang.org/cl/321935 mentions this issue: cmd/link: exit if cgo is required for external linking but is disabled

@perillo perillo changed the title cmd/link: link should stop when cgo is required but disabled by the user cmd/link: link should stop when cgo is required but disabled May 23, 2021
@perillo
Copy link
Contributor Author

perillo commented May 23, 2021

The go tool disables cgo automatically when cross compiling, so it is not necessary to explicitly disable it.

@mknyszek mknyszek added this to the Backlog milestone May 24, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 24, 2021
@cherrymui cherrymui modified the milestones: Backlog, Unplanned May 24, 2021
@cherrymui cherrymui changed the title cmd/link: link should stop when cgo is required but disabled cmd/go: link should stop when cgo is required but disabled May 24, 2021
@bcmills bcmills modified the milestones: Unplanned, Backlog Aug 18, 2023
@bcmills bcmills added the GoCommand cmd/go label Aug 21, 2023
@bcmills bcmills self-assigned this Aug 23, 2023
@bcmills bcmills modified the milestones: Backlog, Go1.22 Aug 23, 2023
@gopherbot
Copy link

Change https://go.dev/cl/522239 mentions this issue: cmd/go: error out of linking package main if cgo is required but not enabled

cellularmitosis pushed a commit to cellularmitosis/go that referenced this issue Aug 24, 2023
…enabled

Fixes golang#46330.
Fixes golang#62123.
Updates golang#31544.

Change-Id: I023aa2bdb5a24e126a0de5192a077e8cf1a0a67c
Reviewed-on: https://go-review.googlesource.com/c/go/+/522239
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

Change https://go.dev/cl/522182 mentions this issue: syscall: skip TestUnshareMountNameSpaceChroot on platforms that require external linking

@gopherbot
Copy link

Change https://go.dev/cl/522495 mentions this issue: test/fixedbugs: require cgo for issue10607.go

gopherbot pushed a commit that referenced this issue Aug 24, 2023
This test passes "-linkmode=external" to 'go run' to link the binary
using the system C linker.

CGO_ENABLED=0 explicitly tells cmd/go not to use the C toolchain,
so the test should not be run in that configuration.

Updates #46330.

Change-Id: I16ac66aac91178045f9decaeb28134061e9711f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/522495
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
gopherbot pushed a commit that referenced this issue Aug 24, 2023
…re external linking

TestUnshareMountNameSpaceChroot attempts to build a statically-linked
binary to run in a chroot, and sets CGO_ENABLED=0 in order to do so.
Rather than trying to figure out some other way to coax the linker
into building a static binary, let's just skip the test on Linux
platforms that require external linking (namely android/arm).

This should fix the build failure reported in
https://build.golang.org/log/1ea245a9c2e916c81043db177be76778bab00058.

While we're here, let's also fix the failure logging to make the text
readable!

Updates #46330.

Change-Id: I4fa07640ce012ac141bf4698bc3215a7f146062c
Reviewed-on: https://go-review.googlesource.com/c/go/+/522182
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

Change https://go.dev/cl/522795 mentions this issue: internal/testenv: simplify 'go build' support check

gopherbot pushed a commit to golang/tools that referenced this issue Aug 25, 2023
We have tried various approaches in testenv.hasTool to detect whether
'go build' works without actually building a binary. However, they all
turn out to be fairly fragile. If cgo is enabled then we need a C
linker, but if the platform requires external linking then cgo must be
enabled too.

Instead of trying to second-guess whether 'go build' will actually work,
let's just ask it to build a binary and see whether it succeeds.
Hopefully this appreach will be simple enough in practice.

This parallels the approach taken in CL 492979.

Fixes golang/go#62268.
Updates golang/go#46330.

Change-Id: I9fd85fb73d291306e7bfb2f3ef6cf75a09cb6bed
Reviewed-on: https://go-review.googlesource.com/c/tools/+/522795
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 6, 2023
@gopherbot
Copy link

Change https://go.dev/cl/569296 mentions this issue: _content/doc: mention cgo requirements for external linking in release notes for Go 1.21 and 1.22

gopherbot pushed a commit to golang/website that referenced this issue Mar 11, 2024
…e notes for Go 1.21 and 1.22

Fixes golang/go#65887.
Updates golang/go#31544.
Updates golang/go#46330.
Updates golang/go#64875.

Change-Id: Ibb035e2287ad0efdbe875c5dd16ffd938ec7a956
Reviewed-on: https://go-review.googlesource.com/c/website/+/569296
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants