]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use the correct linker config in the buildID hash
authorAlex Opie <amtopie@gmail.com>
Thu, 17 Sep 2020 04:31:50 +0000 (04:31 +0000)
committerJay Conrod <jayconrod@google.com>
Thu, 17 Sep 2020 14:18:11 +0000 (14:18 +0000)
The linker config is hashed into the buildID; however,
the GOROOT_FINAL environment variable that is
actually used when -trimpath is specified was not
reflected in that hash. This change fixes that.

Fixes #38989

Change-Id: I418a21a9f6293ca63c101d22b501dfdba8e91ac6
GitHub-Last-Rev: 4cf82920e4a76173c5cb5359b059e87ee7fc7f51
GitHub-Pull-Request: golang/go#40296
Reviewed-on: https://go-review.googlesource.com/c/go/+/243557
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Trust: Jay Conrod <jayconrod@google.com>
Trust: Bryan C. Mills <bcmills@google.com>

src/cmd/go/internal/work/exec.go
src/cmd/go/internal/work/gc.go
src/cmd/go/testdata/script/link_matching_actionid.txt [new file with mode: 0644]

index d975c363060128acc114b9ecfe9c5d12cbbff417..9da5a44e17fdddc6dd305a7ae77c2ecb342291bf 100644 (file)
@@ -1178,8 +1178,13 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
                key, val := cfg.GetArchEnv()
                fmt.Fprintf(h, "%s=%s\n", key, val)
 
-               // The linker writes source file paths that say GOROOT_FINAL.
-               fmt.Fprintf(h, "GOROOT=%s\n", cfg.GOROOT_FINAL)
+               // The linker writes source file paths that say GOROOT_FINAL, but
+               // only if -trimpath is not specified (see ld() in gc.go).
+               gorootFinal := cfg.GOROOT_FINAL
+               if cfg.BuildTrimpath {
+                       gorootFinal = trimPathGoRootFinal
+               }
+               fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal)
 
                // GO_EXTLINK_ENABLED controls whether the external linker is used.
                fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED"))
index 6031897f88f2154c9b446ad71b6601cd7fc0dbe9..d76574932ec93b1e8ccaf95fcbf2b0d5438c913c 100644 (file)
@@ -25,6 +25,9 @@ import (
        "crypto/sha1"
 )
 
+// The 'path' used for GOROOT_FINAL when -trimpath is specified
+const trimPathGoRootFinal = "go"
+
 // The Go toolchain.
 
 type gcToolchain struct{}
@@ -569,7 +572,7 @@ func (gcToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string)
 
        env := []string{}
        if cfg.BuildTrimpath {
-               env = append(env, "GOROOT_FINAL=go")
+               env = append(env, "GOROOT_FINAL="+trimPathGoRootFinal)
        }
        return b.run(root, dir, root.Package.ImportPath, env, cfg.BuildToolexec, base.Tool("link"), "-o", out, "-importcfg", importcfg, ldflags, mainpkg)
 }
diff --git a/src/cmd/go/testdata/script/link_matching_actionid.txt b/src/cmd/go/testdata/script/link_matching_actionid.txt
new file mode 100644 (file)
index 0000000..b8d423d
--- /dev/null
@@ -0,0 +1,38 @@
+# Checks that an identical binary is built with -trimpath from the same
+# source files, with GOROOT in two different locations.
+# Verifies golang.org/issue/38989
+
+[short] skip
+[!symlink] skip
+
+# Symlink the compiler to a local path
+env GOROOT=$WORK/goroot1
+symlink $GOROOT -> $TESTGO_GOROOT
+
+# Set up fresh GOCACHE
+env GOCACHE=$WORK/gocache1
+mkdir $GOCACHE
+
+# Build a simple binary
+go build -o binary1 -trimpath -x main.go
+
+# Now repeat the same process with the compiler at a different local path
+env GOROOT=$WORK/goroot2
+symlink $GOROOT -> $TESTGO_GOROOT
+
+env GOCACHE=$WORK/gocache2
+mkdir $GOCACHE
+
+go build -o binary2 -trimpath -x main.go
+
+# Check that the binaries match exactly
+go tool buildid binary1
+cp stdout buildid1
+go tool buildid binary2
+cp stdout buildid2
+cmp buildid1 buildid2
+
+
+-- main.go --
+package main
+func main() {}