Skip to content

Commit 8bab1c8

Browse files
committed
cmd/cgo: don't generate random numbers for symbols
If cgo is built with -flto the symbols in runtime/cgo is going to include random numbers which would make builds unreproducible. Settings -frandom-seeds ensures this is consistent across builds, and to ensure we always use a reproducible seed across builds we use the actionID as the seed string. runtime/cgo built with "-frandom-seed=OFEc9OKoUMJwh3-5yFCH" would output the following: $ strings --all --bytes=8 $WORK/b055/_pkg_.a | grep "gnu.lto_.profile" .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797 .gnu.lto_.profile.8403a797
1 parent 2e773a3 commit 8bab1c8

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"cmd/go/internal/modload"
3737
"cmd/go/internal/str"
3838
"cmd/go/internal/trace"
39+
"cmd/internal/buildid"
3940
"cmd/internal/quoted"
4041
"cmd/internal/sys"
4142
)
@@ -2368,6 +2369,12 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
23682369
}
23692370
}
23702371

2372+
// Tell gcc to not insert truly random numbers into the build process
2373+
// this ensures LTO won't create random symbols.
2374+
if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
2375+
flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
2376+
}
2377+
23712378
overlayPath := file
23722379
if p, ok := a.nonGoOverlay[overlayPath]; ok {
23732380
overlayPath = p

0 commit comments

Comments
 (0)