Skip to content

cmd/go: non reproducible builds since cgo_ldflag that contains build path #77086

Open

Description

@sandy-lcq

Go version

1.25.3

Output of go env in your module/workspace:

GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build/.cache'
GOCACHEPROG=''
GODEBUG='gocachehash=1'
GOENV=''
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot -I . -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/go-build-tmp/go-build3678307125=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot-native/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTMPDIR='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/go-build-tmp'
GOTOOLCHAIN='auto'
GOTOOLDIR='/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot-native/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''

What did you do?

I am compiling buildah with yocto project, under build path ra and build path rb-extend, here is the build command:

x86_64-wrs-linux-go install -v -ldflags="-r /usr/lib64/go/pkg/linux_amd64_dynlink -I /usr/lib64/ld-linux-x86-64.so.2 -extldflags ' -m64 -march=x86-64-v3 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot -Wl,-rpath-link=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot/usr/lib64/go/pkg/linux_amd64_dynlink -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot= -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot-native= -Wl,-z,relro,-z,now'" -trimpath -mod vendor -buildmode=pie ./cmd/buildah

With above command, the generated buildah is not reproducible,
BUT, after adding "-buildid=", the generated buildah is reproducible.

go buildid make the binary buildah not reproducible.

maybe this is related to: https://github.com/golang/go/blob/master/src/cmd/go/internal/modindex/read.go#L70

the buildid include hash of module root directory, in my case, module root directory is different, so maybe the TODO in comments,
use module path instead of module root directory can resolve this problem.

Here is the log under build path ra:

HASH[moduleIndex]: "go1.25.3"
HASH[moduleIndex]: "modroot /ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah\n"
HASH[moduleIndex]: "package go1.25.3 go index v2 /ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah/cmd/buildah\n"
...

Here is the log under build path rb-extend:

HASH[moduleIndex]: "go1.25.3"
HASH[moduleIndex]: "modroot /rb-extend/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah\n"
HASH[moduleIndex]: "package go1.25.3 go index v2 /rb-extend/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah/cmd/buildah\n"
...

What did you see happen?

I am compiling buildah with yocto project, under build path ra and build path rb-extend, here is the build command:

x86_64-wrs-linux-go install -v -ldflags="-r /usr/lib64/go/pkg/linux_amd64_dynlink -I /usr/lib64/ld-linux-x86-64.so.2 -extldflags ' -m64 -march=x86-64-v3 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot -Wl,-rpath-link=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot/usr/lib64/go/pkg/linux_amd64_dynlink -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot= -ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot-native= -Wl,-z,relro,-z,now'" -trimpath -mod vendor -buildmode=pie ./cmd/buildah

With above command, the generated buildah is not reproducible, I tried to add "-buildvcs=false ", not work.
BUT, after adding "-buildid=", the generated buildah is reproducible. Seems there is still something make buildid not reproducile.

I checked the compile log and output of "go tool buildid ./buildah", I can see some hash difference, but I cannot get what make this happen? Could you provide some guide, so that I can collect more info for debuging this? Thanks.``

What did you expect to see?

the binary buildah compiled out by go is reproducible

Activity

  1. changed the title [-]go buildid make the compiled binary not reproducible[/-] [+]go BuildID make the compiled binary not reproducible[/+] on Jan 6, 2026
  2. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Jan 6, 2026
  3. changed the title [-]go BuildID make the compiled binary not reproducible[/-] [+]cmd/go: non reproducible builds from buildid using modroot directory[/+] on Jan 6, 2026
  4. JunyangShao commented on Jan 6, 2026

    @JunyangShao
    Contributor

    @golang/command-line

  5. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jan 6, 2026
  6. sandy-lcq commented on Jan 22, 2026

    @sandy-lcq
    Author

    Hi,

    After do more investigation, it turns out that the not reproducible is not related to the moduleIndex. But related to the cgo_ldflag in _go_.o, and this _go_.o is one of file for ar archive file _pkg_.a. This cgo_ldflag contains build path, so cause not reproducible.

    Take example:
    In my env, build runtime/cgo will generate _pkg_.a, and _go_.o in _pkg_.a contains contents like below:

    ["cgo_ldflag","-ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5=/usr/src/debug/buildah/1.41.5"],["cgo_ldflag","-ffile-prefix-map=/ra/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build=/usr/src/debug/buildah/1.41.5"]

    So when call buildid.FindAndHash, the content hash of _pkg_.a will be calculated, and the build path make it not reproducible. And all the package deps on it will not reproducible.

    Besides build path, in _go_.o and __.PKGDEF, 'build id "actionid/contentid" also different since above build path diff

    I find that there is function like excludeHostBuildID, try to remove content that influence reproducible before calculate content hash:
    https://github.com/golang/go/blob/master/src/cmd/internal/buildid/rewrite.go#L46

    So I tried to exclude the buildpath and buildid before calculate content hash, but seems this content hash cannot be changed like this:

    It will cause errors like "go: internal cache error: cache verify failed" or "Stale: xxx build id mismatch", I have no idea how the content hash is handle in the whole process, so I am stucked and have no idea how this should be fixed.

    Could experts in this please help to review and give your comments, Thanks.

  7. changed the title [-]cmd/go: non reproducible builds from buildid using modroot directory[/-] [+]cmd/go: non reproducible builds since cgo_ldflag that contains build path[/+] on Jan 23, 2026
  8. StaticRocket commented on Jan 27, 2026

    @StaticRocket

    Just a heads up, build paths being included in CGO_LDFLAGS was a requirement when using LTO with GCC versions < 12. Tools like Yocto didn't make this conditional based on LTO enablement and just always passed macro prefixes to work around the issue reproducible builds using LTO.

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101473

    If/when #43505 is addressed we may need to add some form of disclaimer.

  9. added this to the Go1.27 milestone on Mar 17, 2026
  10. cherrymui commented on Apr 15, 2026

    @cherrymui
    Member

    Thanks for the report and the investigation! It seems the go command adds -ffile-prefix-map to C compilations when -trimpath is used. I think the buildID computation should be more robust with that. One tricky part is that when the build cache is used, it should pick the right object with the right path. If one object contains cgo_ldflag -ffile-prefix-map=/ra/build/... and one contains cgo_ldflag -ffile-prefix-map=/rb/build/..., they should have different cache keys, so simply ignoring that part from the buildID is probably not going to work. Maybe we need a different mechanism to pass these flags to the linker without affecting the buildID?

    cc @matloob

  11. modified the milestones: Go1.27, Backlog on Apr 15, 2026
  12. modified the milestones: Go1.27, Go1.28 on Aug 19, 2026
  13. 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

Labels

BugReportIssues describing a possible bug in the Go implementation.GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.cgoIssues affecting runtime/cgo and cmd/go

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions