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: -buildmode=pie requires external (cgo) linking, but cgo is not enabled #64875

Open
andreas-schwab opened this issue Dec 27, 2023 · 8 comments
Labels
arch-riscv Issues solely affecting the riscv64 architecture. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@andreas-schwab
Copy link

andreas-schwab commented Dec 27, 2023

Go version

go version go1.22rc1 linux/riscv64

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

GO111MODULE=''
GOARCH='riscv64'
GOBIN=''
GOCACHE='/home/andreas/.cache/go-build'
GOENV='/home/andreas/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='riscv64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/andreas/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/andreas/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib64/go/1.22'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/lib64/go/1.22/pkg/tool/linux_riscv64'
GOVCS=''
GOVERSION='go1.22rc1'
GCCGO='gccgo'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1658771557=/tmp/go-build -gno-record-gcc-switches'

What did you do?

$ CGO_ENABLED=0 go build -buildmode=pie hellogo.go
-buildmode=pie requires external (cgo) linking, but cgo is not enabled

What did you expect to see?

$ CGO_ENABLED=0 /usr/lib64/go/1.21/bin/go build -buildmode=pie hellogo.go
# command-line-arguments
loadinternal: cannot find runtime/cgo

What did you see instead?

$ CGO_ENABLED=0 go build -buildmode=pie hellogo.go
-buildmode=pie requires external (cgo) linking, but cgo is not enabled
@mauri870
Copy link
Member

I think that is expected, riscv does not support internal linking with buildmode=pie. Can you point out to the source that says PIE works without cgo on riscv64?

@mauri870 mauri870 added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. arch-riscv Issues solely affecting the riscv64 architecture. labels Dec 27, 2023
@andreas-schwab
Copy link
Author

Why does it work with 1.21?

@mauri870
Copy link
Member

mauri870 commented Dec 28, 2023

Thanks for the heads-up! There was some logic shifted around in CL 472515 and CL 522239 for #31544 that causes -buildmode=pie to explicitly require CGO_ENABLED=1 with riscv64, this was not a requirement in go 1.21 and earlier. I imagine this also affects linux/s390x the same way.

My understanding is that for riscv64, s390x buildmode=pie do require cgo because of the external linking, so the current behavior is correct, while in 1.21 and earlier we were compiling with cgo anyway, regardless of CGO_ENABLED. But I might be wrong here.

cc @bcmills

@mauri870 mauri870 removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 28, 2023
@bcmills bcmills added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 2, 2024
@bcmills
Copy link
Member

bcmills commented Jan 2, 2024

linux/riscv64 is not currently marked as supporting internal linking for PIE binaries. If that is incorrect — that is, if the Go linker actually does support internal linking in this configuration — then internal/platform.InternalLinkPIESupported should be updated (along with any affected tests) to reflect that.

Otherwise, the fact that CGO_ENABLED=0 go build -buildmode=pie hellogo.go previously exited without error is a bug, and the new error here is working as intended.

(attn @golang/riscv64)

@bcmills bcmills added this to the Backlog milestone Jan 2, 2024
@mknyszek
Copy link
Contributor

mknyszek commented Jan 3, 2024

In triage, are we understanding correctly that, given that -buildmode=pie doesn't work with internal linking on linux/riscv64, that this issue is now basically a feature request for this behavior?

Even though the build didn't fail for Go 1.21, did we really support -buildmode=pie with internal linking? It looks to us like in Go 1.21 the toolchain would just silently build a non-PIE binary, which seems undesirable.

@mknyszek mknyszek added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 3, 2024
@andreas-schwab
Copy link
Author

$ readelf -h usr/bin/velero-plugin-for-aws
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Position-Independent Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x3165e0
Start of program headers: 64 (bytes into file)
Start of section headers: 35051632 (bytes into file)
Flags: 0x5, RVC, double-float ABI
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 11
Size of section headers: 64 (bytes)
Number of section headers: 33
Section header string table index: 32

@bcmills
Copy link
Member

bcmills commented Jan 3, 2024

I wonder if in practice it used the C linker (despite CGO_ENABLED being false) and produced a C-linked binary missing the runtime's usual C support code.

@bcmills bcmills removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-riscv Issues solely affecting the riscv64 architecture. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

4 participants