Skip to content

Commit c7ea996

Browse files
ianlancetaylorgopherbot
authored andcommitted
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 <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>
1 parent b98c1b2 commit c7ea996

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/cmd/go/internal/work/init.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,10 @@ func buildModeInit() {
230230
ldBuildmode = "c-shared"
231231
case "default":
232232
ldBuildmode = "exe"
233-
if platform.DefaultPIE(cfg.Goos, cfg.Goarch) {
234-
if cfg.Goos == "windows" && cfg.BuildRace {
235-
// PIE is not supported with -race on windows; see https://go.dev/cl/416174.
236-
} else {
237-
ldBuildmode = "pie"
238-
if cfg.Goos != "windows" && !gccgo {
239-
codegenArg = "-shared"
240-
}
233+
if platform.DefaultPIE(cfg.Goos, cfg.Goarch, cfg.BuildRace) {
234+
ldBuildmode = "pie"
235+
if cfg.Goos != "windows" && !gccgo {
236+
codegenArg = "-shared"
241237
}
242238
}
243239
case "exe":

src/cmd/link/internal/ld/dwarf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ func main() {
976976
t.Fatalf("*main.X DIE had no runtime type attr. DIE: %v", dies[0])
977977
}
978978

979-
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
979+
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH, false) {
980980
return // everything is PIE, addresses are relocated
981981
}
982982
if rtAttr.(uint64)+types.Addr != addr {

src/cmd/nm/nm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
166166
return true
167167
}
168168
}
169-
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
169+
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH, false) {
170170
// Code is always relocated if the default buildmode is PIE.
171171
return true
172172
}

src/internal/platform/supported.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,19 @@ func InternalLinkPIESupported(goos, goarch string) bool {
219219
}
220220

221221
// DefaultPIE reports whether goos/goarch produces a PIE binary when using the
222-
// "default" buildmode.
223-
func DefaultPIE(goos, goarch string) bool {
222+
// "default" buildmode. On Windows this is affected by -race,
223+
// so force the caller to pass that in to centralize that choice.
224+
func DefaultPIE(goos, goarch string, isRace bool) bool {
224225
switch goos {
225226
case "android", "ios":
226227
return true
227228
case "windows":
228-
return true // but switches back to "exe" if -race is enabled
229+
if isRace {
230+
// PIE is not supported with -race on windows;
231+
// see https://go.dev/cl/416174.
232+
return false
233+
}
234+
return true
229235
case "darwin":
230236
return goarch == "arm64"
231237
}

0 commit comments

Comments
 (0)