From: Ian Lance Taylor Date: Mon, 20 Mar 2023 21:08:59 +0000 (-0700) Subject: internal/platform: pass race mode to DefaultPIE X-Git-Tag: go1.21rc1~1218 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c7ea9969f8112721edc0a128277f8e5943e21a49;p=gostls13.git internal/platform: pass race mode to DefaultPIE On Windows we default to PIE, except in race mode. Pass isRace to platform.DefaultPIE to centralize that decision. This is in preparation for adding another call to DefaultPIE. Change-Id: I91b75d307e7d4d260246a934f98734ddcbca372a Reviewed-on: https://go-review.googlesource.com/c/go/+/477916 TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor Reviewed-by: Bryan Mills Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor --- diff --git a/src/cmd/go/internal/work/init.go b/src/cmd/go/internal/work/init.go index 35ea2311c7..88a6328285 100644 --- a/src/cmd/go/internal/work/init.go +++ b/src/cmd/go/internal/work/init.go @@ -230,14 +230,10 @@ func buildModeInit() { ldBuildmode = "c-shared" case "default": 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" - } + if platform.DefaultPIE(cfg.Goos, cfg.Goarch, cfg.BuildRace) { + ldBuildmode = "pie" + if cfg.Goos != "windows" && !gccgo { + codegenArg = "-shared" } } case "exe": diff --git a/src/cmd/link/internal/ld/dwarf_test.go b/src/cmd/link/internal/ld/dwarf_test.go index 103ca5a4ab..5e9b74f7d6 100644 --- a/src/cmd/link/internal/ld/dwarf_test.go +++ b/src/cmd/link/internal/ld/dwarf_test.go @@ -976,7 +976,7 @@ func main() { t.Fatalf("*main.X DIE had no runtime type attr. DIE: %v", dies[0]) } - if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) { + if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH, false) { return // everything is PIE, addresses are relocated } if rtAttr.(uint64)+types.Addr != addr { diff --git a/src/cmd/nm/nm_test.go b/src/cmd/nm/nm_test.go index 8c23d73d6d..530a720f2b 100644 --- a/src/cmd/nm/nm_test.go +++ b/src/cmd/nm/nm_test.go @@ -166,7 +166,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) { return true } } - if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) { + if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH, false) { // Code is always relocated if the default buildmode is PIE. return true } diff --git a/src/internal/platform/supported.go b/src/internal/platform/supported.go index ea89ff19e3..896f0b5273 100644 --- a/src/internal/platform/supported.go +++ b/src/internal/platform/supported.go @@ -219,13 +219,19 @@ func InternalLinkPIESupported(goos, goarch string) bool { } // DefaultPIE reports whether goos/goarch produces a PIE binary when using the -// "default" buildmode. -func DefaultPIE(goos, goarch string) bool { +// "default" buildmode. On Windows this is affected by -race, +// so force the caller to pass that in to centralize that choice. +func DefaultPIE(goos, goarch string, isRace bool) bool { switch goos { case "android", "ios": return true case "windows": - return true // but switches back to "exe" if -race is enabled + if isRace { + // PIE is not supported with -race on windows; + // see https://go.dev/cl/416174. + return false + } + return true case "darwin": return goarch == "arm64" }