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: inconsistent argument order in hostLink() and linkerFlagSupported() #69954

Closed
voxeljorge opened this issue Oct 20, 2024 · 2 comments
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@voxeljorge
Copy link

voxeljorge commented Oct 20, 2024

Go version

1.23.2

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jorge/.cache/go-build'
GOENV='/home/jorge/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jorge/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jorge/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/jorge/.cache/bazel/_bazel_jorge/576ee54cf2522471c5c073f98825487b/external/go_sdk'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/jorge/.cache/bazel/_bazel_jorge/576ee54cf2522471c5c073f98825487b/external/go_sdk/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jorge/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/jorge/Projects/voxel/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build758936887=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Attempted to link programs using a hermetic llvm toolchain and sysroot with go tool link.

Reproducing this is a little difficult as the conditions for this bug surfaced through a Bazel build using the rules_go ruleset: bazel-contrib/rules_go#3886

What did you see happen?

Calls to go tool link detected all linker flags as unsupported, causing go tool link to add flags like -rdynamic when static linking: https://cs.opensource.google/go/go/+/master:src/cmd/link/internal/ld/lib.go;l=1732?q=hostlink

In my case this caused hostLink() to fail with PIE errors when static linking, although I'm sure other errors related to flag ordering are possible.

What did you expect to see?

I expected to see programs link correctly. They in fact did link correctly when no sysroot was used but failed to do so when using a sysroot. Triggering and analyzing this bug was challenging due to the fact that there seems to be no logging that can be turned on for linkerFlagSupported(). This was encountered while building Go code with Bazel using rules_go and fix for the rules_go side of this issue has been proposed here: bazel-contrib/rules_go#4144

The cause of the rules_go bug appears to be an inconsistency in how go tool link calls linkers from linkerFlagSupported() and hostLink() which could affect users more generally than just in rules_go

Calls to linkerFlagSupported() put any -extldflags that were specified before the cgo flags that have been extracted from archives, while hostLink() seems to put them after. In cases where the archives were originally built with a --sysroot parameter which needs to be adjusted (because maybe the archives were built in a different sandbox than the final program for example) this means that linkerFlagSupported() is likely to detect all linker flags as "unsupported". The sysroot parameter specified in -extldflags will basically be ignored for linkerFlagSupported() calls but not for hostLink() calls.

I suspect this is just an oversight and not intended behavior. A simple fix would just be to adjust linkerFlagSupported() to always put -extldflags at the end.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 20, 2024
@voxeljorge
Copy link
Author

voxeljorge commented Oct 20, 2024

ah I see my search failed, as this is is a duplicate of #69350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants