Skip to content

Commit bbf795e

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
internal/platform: add a function to report whether default builds are PIE
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 <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent a8cd10f commit bbf795e

File tree

5 files changed

+30
-52
lines changed

5 files changed

+30
-52
lines changed

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,16 @@ func buildModeInit() {
229229
}
230230
ldBuildmode = "c-shared"
231231
case "default":
232-
switch cfg.Goos {
233-
case "android":
234-
codegenArg = "-shared"
235-
ldBuildmode = "pie"
236-
case "windows":
237-
if cfg.BuildRace {
238-
ldBuildmode = "exe"
232+
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.
239236
} else {
240237
ldBuildmode = "pie"
238+
if cfg.Goos != "windows" && !gccgo {
239+
codegenArg = "-shared"
240+
}
241241
}
242-
case "ios":
243-
codegenArg = "-shared"
244-
ldBuildmode = "pie"
245-
case "darwin":
246-
switch cfg.Goarch {
247-
case "arm64":
248-
codegenArg = "-shared"
249-
}
250-
fallthrough
251-
default:
252-
ldBuildmode = "exe"
253-
}
254-
if gccgo {
255-
codegenArg = ""
256242
}
257243
case "exe":
258244
pkgsFilter = pkgsMain

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"debug/dwarf"
99
"debug/pe"
1010
"fmt"
11+
"internal/platform"
1112
"internal/testenv"
1213
"io"
1314
"os"
@@ -891,12 +892,6 @@ func TestRuntimeTypeAttrInternal(t *testing.T) {
891892
t.Skip("skipping on plan9; no DWARF symbol table in executables")
892893
}
893894

894-
// TODO(#58807): factor this condition out into a function in
895-
// internal/platform so that it won't get out of sync with cmd/link.
896-
if runtime.GOOS == "android" || runtime.GOOS == "windows" {
897-
t.Skipf("skipping on %s; test is incompatible with relocatable binaries", runtime.GOOS)
898-
}
899-
900895
testRuntimeTypeAttr(t, "-ldflags=-linkmode=internal")
901896
}
902897

@@ -914,10 +909,6 @@ func TestRuntimeTypeAttrExternal(t *testing.T) {
914909
t.Skip("-linkmode=external not supported on ppc64")
915910
}
916911

917-
if runtime.GOOS == "windows" {
918-
t.Skip("skipping on windows; test is incompatible with relocatable binaries")
919-
}
920-
921912
testRuntimeTypeAttr(t, "-ldflags=-linkmode=external")
922913
}
923914

@@ -985,9 +976,7 @@ func main() {
985976
t.Fatalf("*main.X DIE had no runtime type attr. DIE: %v", dies[0])
986977
}
987978

988-
// TODO(#58807): factor this condition out into a function in
989-
// internal/platform so that it won't get out of sync with cmd/link.
990-
if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") || runtime.GOOS == "android" {
979+
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
991980
return // everything is PIE, addresses are relocated
992981
}
993982
if rtAttr.(uint64)+types.Addr != addr {

src/cmd/nm/nm_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"internal/obscuretestdata"
9+
"internal/platform"
910
"internal/testenv"
1011
"os"
1112
"path/filepath"
@@ -165,20 +166,10 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
165166
return true
166167
}
167168
}
168-
// Code is always relocated if the default buildmode is PIE.
169-
//
170-
// TODO(#58807): factor this condition out into a function in
171-
// internal/platform so that it won't get out of sync with cmd/go and
172-
// cmd/link.
173-
if runtime.GOOS == "android" {
169+
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH) {
170+
// Code is always relocated if the default buildmode is PIE.
174171
return true
175172
}
176-
if runtime.GOOS == "windows" {
177-
return true
178-
}
179-
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
180-
return true // On darwin/arm64 everything is PIE
181-
}
182173
return false
183174
}
184175

src/cmd/pprof/pprof_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ func mustHaveDisasm(t *testing.T) {
7474
t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
7575
}
7676

77-
// Skip PIE platforms, pprof can't disassemble PIE.
78-
//
79-
// TODO(#58807): factor this condition out into a function in
80-
// internal/platform so that it won't get out of sync with cmd/go and
81-
// cmd/link.
82-
if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") || runtime.GOOS == "android" {
77+
// pprof can only disassemble PIE on some platforms.
78+
// Skip the ones it can't handle yet.
79+
if (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64") ||
80+
(runtime.GOOS == "android" && runtime.GOARCH == "arm") {
8381
t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH)
8482
}
8583
}

src/internal/platform/supported.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,17 @@ func InternalLinkPIESupported(goos, goarch string) bool {
217217
}
218218
return false
219219
}
220+
221+
// DefaultPIE reports whether goos/goarch produces a PIE binary when using the
222+
// "default" buildmode.
223+
func DefaultPIE(goos, goarch string) bool {
224+
switch goos {
225+
case "android", "ios":
226+
return true
227+
case "windows":
228+
return true // but switches back to "exe" if -race is enabled
229+
case "darwin":
230+
return goarch == "arm64"
231+
}
232+
return false
233+
}

0 commit comments

Comments
 (0)