]> Cypherpunks repositories - gostls13.git/commitdiff
internal/platform: add a function to report whether default builds are PIE
authorBryan C. Mills <bcmills@google.com>
Fri, 10 Mar 2023 20:37:17 +0000 (15:37 -0500)
committerGopher Robot <gobot@golang.org>
Wed, 15 Mar 2023 13:31:05 +0000 (13:31 +0000)
This consolidates a condition that was previously repeated (in
different approximations) in several different places in the code.

For #58807.

Change-Id: Idd308759f6262b1f5c61f79022965612319edf94
Reviewed-on: https://go-review.googlesource.com/c/go/+/475457
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/go/internal/work/init.go
src/cmd/link/internal/ld/dwarf_test.go
src/cmd/nm/nm_test.go
src/cmd/pprof/pprof_test.go
src/internal/platform/supported.go

index 93c068c528a40ef4dcaa162f183dd2a2282b8f12..35ea2311c706c1c70547030e4e4d95e27f5bebd3 100644 (file)
@@ -229,30 +229,16 @@ func buildModeInit() {
                }
                ldBuildmode = "c-shared"
        case "default":
-               switch cfg.Goos {
-               case "android":
-                       codegenArg = "-shared"
-                       ldBuildmode = "pie"
-               case "windows":
-                       if cfg.BuildRace {
-                               ldBuildmode = "exe"
+               ldBuildmode = "exe"
+               if platform.DefaultPIE(cfg.Goos, cfg.Goarch) {
+                       if cfg.Goos == "windows" && cfg.BuildRace {
+                               // PIE is not supported with -race on windows; see https://go.dev/cl/416174.
                        } else {
                                ldBuildmode = "pie"
+                               if cfg.Goos != "windows" && !gccgo {
+                                       codegenArg = "-shared"
+                               }
                        }
-               case "ios":
-                       codegenArg = "-shared"
-                       ldBuildmode = "pie"
-               case "darwin":
-                       switch cfg.Goarch {
-                       case "arm64":
-                               codegenArg = "-shared"
-                       }
-                       fallthrough
-               default:
-                       ldBuildmode = "exe"
-               }
-               if gccgo {
-                       codegenArg = ""
                }
        case "exe":
                pkgsFilter = pkgsMain
index abbfec0c412e52314ba27ae627d09bad605614b2..103ca5a4ab6641b40d16d3a08f8604340bc97eae 100644 (file)
@@ -8,6 +8,7 @@ import (
        "debug/dwarf"
        "debug/pe"
        "fmt"
+       "internal/platform"
        "internal/testenv"
        "io"
        "os"
@@ -891,12 +892,6 @@ func TestRuntimeTypeAttrInternal(t *testing.T) {
                t.Skip("skipping on plan9; no DWARF symbol table in executables")
        }
 
-       // TODO(#58807): factor this condition out into a function in
-       // internal/platform so that it won't get out of sync with cmd/link.
-       if runtime.GOOS == "android" || runtime.GOOS == "windows" {
-               t.Skipf("skipping on %s; test is incompatible with relocatable binaries", runtime.GOOS)
-       }
-
        testRuntimeTypeAttr(t, "-ldflags=-linkmode=internal")
 }
 
@@ -914,10 +909,6 @@ func TestRuntimeTypeAttrExternal(t *testing.T) {
                t.Skip("-linkmode=external not supported on ppc64")
        }
 
-       if runtime.GOOS == "windows" {
-               t.Skip("skipping on windows; test is incompatible with relocatable binaries")
-       }
-
        testRuntimeTypeAttr(t, "-ldflags=-linkmode=external")
 }
 
@@ -985,9 +976,7 @@ func main() {
                t.Fatalf("*main.X DIE had no runtime type attr. DIE: %v", dies[0])
        }
 
-       // TODO(#58807): factor this condition out into a function in
-       // internal/platform so that it won't get out of sync with cmd/link.
-       if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") || runtime.GOOS == "android" {
+       if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
                return // everything is PIE, addresses are relocated
        }
        if rtAttr.(uint64)+types.Addr != addr {
index 014a5d2bd75f12f4b618f35572ade50823d256b5..8c23d73d6daa2107caf70843ecd59b12ead970c7 100644 (file)
@@ -6,6 +6,7 @@ package main
 
 import (
        "internal/obscuretestdata"
+       "internal/platform"
        "internal/testenv"
        "os"
        "path/filepath"
@@ -165,20 +166,10 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
                                return true
                        }
                }
-               // Code is always relocated if the default buildmode is PIE.
-               //
-               // TODO(#58807): factor this condition out into a function in
-               // internal/platform so that it won't get out of sync with cmd/go and
-               // cmd/link.
-               if runtime.GOOS == "android" {
+               if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
+                       // Code is always relocated if the default buildmode is PIE.
                        return true
                }
-               if runtime.GOOS == "windows" {
-                       return true
-               }
-               if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
-                       return true // On darwin/arm64 everything is PIE
-               }
                return false
        }
 
index d6ca0e28da79fc9721c3e5e7b7abb2d6f5cc356a..2a651dda5ff4a48ad5c47ceef2059efcc386f361 100644 (file)
@@ -74,12 +74,10 @@ func mustHaveDisasm(t *testing.T) {
                t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
        }
 
-       // Skip PIE platforms, pprof can't disassemble PIE.
-       //
-       // TODO(#58807): factor this condition out into a function in
-       // internal/platform so that it won't get out of sync with cmd/go and
-       // cmd/link.
-       if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") || runtime.GOOS == "android" {
+       // pprof can only disassemble PIE on some platforms.
+       // Skip the ones it can't handle yet.
+       if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") ||
+               (runtime.GOOS == "android" && runtime.GOARCH == "arm") {
                t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH)
        }
 }
index 8bf68a6d5844f860f3089fb39d9fa25507a1e7bd..ea89ff19e37b4875687f53cb5b036b8430c30b28 100644 (file)
@@ -217,3 +217,17 @@ func InternalLinkPIESupported(goos, goarch string) bool {
        }
        return false
 }
+
+// DefaultPIE reports whether goos/goarch produces a PIE binary when using the
+// "default" buildmode.
+func DefaultPIE(goos, goarch string) bool {
+       switch goos {
+       case "android", "ios":
+               return true
+       case "windows":
+               return true // but switches back to "exe" if -race is enabled
+       case "darwin":
+               return goarch == "arm64"
+       }
+       return false
+}