Skip to content

cmd/go: use the correct linker config in the buildID hash #40296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/cmd/go/internal/work/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/go/internal/work/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down
38 changes: 38 additions & 0 deletions src/cmd/go/testdata/script/link_matching_actionid.txt
Original file line number Diff line number Diff line change
@@ -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() {}