]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: only check PIE size difference when the linkmode is the same
authorJoel Sing <joel@sing.id.au>
Wed, 12 Mar 2025 13:39:27 +0000 (00:39 +1100)
committerJoel Sing <joel@sing.id.au>
Thu, 13 Mar 2025 11:22:49 +0000 (04:22 -0700)
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>
src/cmd/link/elf_test.go

index db6ef1bb51d9ba6cf4141dbdf8ee83a1bfc03aae..59a19a20d2a215228da8b91f8fd7580210916b49 100644 (file)
@@ -357,16 +357,14 @@ func TestPIESize(t *testing.T) {
                }
        }
 
-       for _, external := range []bool{false, true} {
-               external := external
-
-               name := "TestPieSize-"
-               if external {
-                       name += "external"
-               } else {
-                       name += "internal"
-               }
-               t.Run(name, func(t *testing.T) {
+       var linkmodes []string
+       if platform.InternalLinkPIESupported(runtime.GOOS, runtime.GOARCH) {
+               linkmodes = append(linkmodes, "internal")
+       }
+       linkmodes = append(linkmodes, "external")
+
+       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)