]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/work: make toolchain builds reproducible when buildmode=pie
authorElias Naur <mail@eliasnaur.com>
Sun, 7 Apr 2019 13:02:31 +0000 (15:02 +0200)
committerElias Naur <mail@eliasnaur.com>
Mon, 8 Apr 2019 20:38:29 +0000 (20:38 +0000)
When buildmode=pie, external linking is forced, and our toolchain build id
will be included in the external build id, resulting in the building of
a toolchain tool will never reach a fixed point id.

More importantly, this change will make make.bash converge on self-hosted
Android builds (Android refuses to run non-PIE executables).

Fixes #31320
Updates #18968

Change-Id: Icb5db9f4b1b688afe37f4dafe261ffda580fa4e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/170942
Run-TryBot: Elias Naur <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/work/gc.go
src/cmd/internal/sys/supported.go
src/cmd/link/internal/ld/config.go

index cdd0989a9363906f3877a004938965b9f6d3d392..1721ecbc4e8b8801877e3508e0e00a4df648c410 100644 (file)
@@ -21,6 +21,7 @@ import (
        "cmd/go/internal/load"
        "cmd/go/internal/str"
        "cmd/internal/objabi"
+       "cmd/internal/sys"
        "crypto/sha1"
 )
 
@@ -525,7 +526,13 @@ func (gcToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string)
        // Store BuildID inside toolchain binaries as a unique identifier of the
        // tool being run, for use by content-based staleness determination.
        if root.Package.Goroot && strings.HasPrefix(root.Package.ImportPath, "cmd/") {
-               ldflags = append(ldflags, "-X=cmd/internal/objabi.buildID="+root.buildID)
+               // When buildmode=pie, external linking will include our build
+               // id in the external linker's build id, which will cause our
+               // build id to not match the next time the tool is built.
+               // Rely on the external build id instead.
+               if ldBuildmode != "pie" || !sys.PIEDefaultsToExternalLink(cfg.Goos, cfg.Goarch) {
+                       ldflags = append(ldflags, "-X=cmd/internal/objabi.buildID="+root.buildID)
+               }
        }
 
        // If the user has not specified the -extld option, then specify the
index a53da6ed2cbebc993fc0704b9811fd82e59eedee..c963971f59ae048e1e9fcc2ddc011bcc7202e1e5 100644 (file)
@@ -27,3 +27,11 @@ func MSanSupported(goos, goarch string) bool {
                return false
        }
 }
+
+// PIEDefaultsToExternalLink reports whether goos/goarch defaults
+// to external linking for buildmode=pie.
+func PIEDefaultsToExternalLink(goos, goarch string) bool {
+       // Currently all systems external link PIE binaries.
+       // See https://golang.org/issue/18968.
+       return true
+}
index 85842f2fa24719a1a41e6f0350ca6be1d4e36558..b404f1897d17a355fb8ec07a610086ec044178e6 100644 (file)
@@ -256,8 +256,8 @@ func determineLinkMode(ctxt *Link) {
                                ctxt.LinkMode = LinkExternal
                        } else if iscgo && externalobj {
                                ctxt.LinkMode = LinkExternal
-                       } else if ctxt.BuildMode == BuildModePIE {
-                               ctxt.LinkMode = LinkExternal // https://golang.org/issue/18968
+                       } else if ctxt.BuildMode == BuildModePIE && sys.PIEDefaultsToExternalLink(objabi.GOOS, objabi.GOARCH) {
+                               ctxt.LinkMode = LinkExternal
                        } else {
                                ctxt.LinkMode = LinkInternal
                        }