cmd/link: only check PIE size difference when the linkmode is the same

Currently we check the size difference between non-PIE and PIE binaries
without specifying a linkmode (and that is presumed to be internal).
However, on some platforms (like openbsd/arm64), the use of
-buildmode=pie results in external linking. Ensure that we only test
internally linked non-PIE against internally linked PIE and externally
linked non-PIE against externally linked PIE, avoiding unexpected
differences.

Fixes #72818

Change-Id: I7e1da0976a4b5de387a59d0d6c04f58498a8eca0
Reviewed-on: https://go-review.googlesource.com/c/go/+/657035
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@golang.org>
This commit is contained in:
Joel Sing 2025-03-13 00:39:27 +11:00
parent b143c98169
commit 8cdef129fb
1 changed files with 10 additions and 17 deletions

View File

@ -357,16 +357,14 @@ func TestPIESize(t *testing.T) {
}
}
for _, external := range []bool{false, true} {
external := external
var linkmodes []string
if platform.InternalLinkPIESupported(runtime.GOOS, runtime.GOARCH) {
linkmodes = append(linkmodes, "internal")
}
linkmodes = append(linkmodes, "external")
name := "TestPieSize-"
if external {
name += "external"
} else {
name += "internal"
}
t.Run(name, func(t *testing.T) {
for _, linkmode := range linkmodes {
t.Run(fmt.Sprintf("TestPieSize-%v", linkmode), func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
@ -375,16 +373,11 @@ func TestPIESize(t *testing.T) {
binexe := filepath.Join(dir, "exe")
binpie := filepath.Join(dir, "pie")
if external {
binexe += "external"
binpie += "external"
}
binexe += linkmode
binpie += linkmode
build := func(bin, mode string) error {
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", bin, "-buildmode="+mode)
if external {
cmd.Args = append(cmd.Args, "-ldflags=-linkmode=external")
}
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", bin, "-buildmode="+mode, "-ldflags=-linkmode="+linkmode)
cmd.Args = append(cmd.Args, "pie.go")
cmd.Dir = dir
t.Logf("%v", cmd.Args)