1
0
mirror of https://github.com/golang/go synced 2024-11-07 15:36:23 -07:00

cmd/cgo: include seed string with -frandom-seed for lto

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
This commit is contained in:
Morten Linderud 2022-06-24 09:56:05 +02:00
parent 518889b35c
commit cec5162316
No known key found for this signature in database
GPG Key ID: 9C02FF419FECBE16

View File

@ -39,6 +39,7 @@ import (
"cmd/go/internal/slices" "cmd/go/internal/slices"
"cmd/go/internal/str" "cmd/go/internal/str"
"cmd/go/internal/trace" "cmd/go/internal/trace"
"cmd/internal/buildid"
"cmd/internal/quoted" "cmd/internal/quoted"
"cmd/internal/sys" "cmd/internal/sys"
) )
@ -2544,6 +2545,12 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
} }
} }
// Tell gcc to not insert truly random numbers into the build process
// this ensures LTO won't create random numbers for symbols.
if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
}
overlayPath := file overlayPath := file
if p, ok := a.nonGoOverlay[overlayPath]; ok { if p, ok := a.nonGoOverlay[overlayPath]; ok {
overlayPath = p overlayPath = p