Skip to content

cmd/go: -buildmode=pie requires external (cgo) linking, but cgo is not enabled #64875

Open

Description

@andreas-schwab

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

Activity

  1. mauri870 commented on Dec 27, 2023

    @mauri870
    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?

  2. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    arch-riscvIssues solely affecting the riscv64 architecture.
    on Dec 27, 2023
  3. andreas-schwab commented on Dec 27, 2023

    @andreas-schwab
    Author

    Why does it work with 1.21?

  4. mauri870 commented on Dec 28, 2023

    @mauri870
    Member

    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

  5. removed
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Dec 28, 2023
  6. bcmills commented on Jan 2, 2024

    @bcmills
    Contributor

    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)

  7. added this to the Backlog milestone on Jan 2, 2024
  8. mknyszek commented on Jan 3, 2024

    @mknyszek
    Contributor

    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.

  9. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Jan 3, 2024
  10. 3 remaining items

  11. nmeum commented on Mar 2, 2024

    @nmeum

    Hi, Alpine Linux maintainer here 👋 We use -buildmode=pie by default for all Go packages in our repository. Since Go 1.22 we are running into a lot of build failures as there seems to be a lot of software which sets CGO_ENABLED=0 in Makefiles and other go build-wrappers, for example fq. Just checking in here to make sure that this is intended. If so, I believe we have to patch all software which sets CGO_ENABLED=0?

    See: https://gitlab.alpinelinux.org/alpine/aports/-/issues/15809

  12. Xeonacid commented on Mar 2, 2024

    @Xeonacid

    Hi, Alpine Linux maintainer here 👋 We use -buildmode=pie by default for all Go packages in our repository. Since Go 1.22 we are running into a lot of build failures as there seems to be a lot of software which sets CGO_ENABLED=0 in Makefiles and other go build-wrappers, for example fq. Just checking in here to make sure that this is intended. If so, I believe we have to patch all software which sets CGO_ENABLED=0?

    We are from Arch Linux RISC-V and having the exact same problem.

  13. seankhliao commented on Mar 2, 2024

    @seankhliao
    Member
  14. bcmills commented on Mar 5, 2024

    @bcmills
    Contributor

    @nmeum, @Xeonacid: correct. If the Go linker requires an external (C) linker for -buildmode=pie on a given platform, then cgo must also be enabled in order to link PIE binaries on that platform.

  15. gopherbot commented on Mar 5, 2024

    @gopherbot
    Contributor

    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

  16. added a commit that references this issue on Apr 16, 2024
  17. ricardobranco777 commented on Feb 11, 2025

    @ricardobranco777

    I can confirm it also affects s390x.

  18. gopherbot commented on Feb 4, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/741860 mentions this issue: cmd/link: add R_RISCV_TLS_IE support for internal link

  19. gopherbot commented on Feb 5, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/742200 mentions this issue: cmd/link: enable PIE internal link for riscv64

  20. added a commit that references this issue on May 18, 2026
  21. added a commit that references this issue on May 20, 2026
  22. added
    cgoIssues affecting runtime/cgo and cmd/go
    on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.arch-riscvIssues solely affecting the riscv64 architecture.cgoIssues affecting runtime/cgo and cmd/gocompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions