Skip to content

Commit

Permalink
internal/platform: pass race mode to DefaultPIE
Browse files Browse the repository at this point in the history
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 <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Mar 20, 2023
1 parent b98c1b2 commit c7ea996
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/cmd/go/internal/work/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/dwarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/nm/nm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 9 additions & 3 deletions src/internal/platform/supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit c7ea996

Please sign in to comment.