Skip to content

Commit c76befe

Browse files
committed
cmd/go: use -buildmode=pie as default on window
This change adjusts go command to pass -buildmode=pie to cmd/link, if -buildmode is not explicitly provided. Fixes #35192 Change-Id: Iec020131e676eb3e9a2df9eea1929b2af2b6df04 Reviewed-on: https://go-review.googlesource.com/c/go/+/230217 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 5c9a8c0 commit c76befe

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

doc/go1.15.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ <h2 id="language">Changes to the language</h2>
3131

3232
<h2 id="ports">Ports</h2>
3333

34+
<p> <!-- CL 214397 and CL 230217 -->
35+
Go 1.15 now generates Windows ASLR executables when -buildmode=pie
36+
cmd/link flag is provided. Go command uses -buildmode=pie by default
37+
on Windows.
38+
</p>
39+
3440
<p>
3541
TODO
3642
</p>

src/cmd/go/go_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,19 +2082,38 @@ func TestBuildmodePIE(t *testing.T) {
20822082
t.Skipf("skipping test because buildmode=pie is not supported on %s", platform)
20832083
}
20842084
t.Run("non-cgo", func(t *testing.T) {
2085-
testBuildmodePIE(t, false)
2085+
testBuildmodePIE(t, false, true)
20862086
})
20872087
if canCgo {
20882088
switch runtime.GOOS {
20892089
case "darwin", "freebsd", "linux", "windows":
20902090
t.Run("cgo", func(t *testing.T) {
2091-
testBuildmodePIE(t, true)
2091+
testBuildmodePIE(t, true, true)
20922092
})
20932093
}
20942094
}
20952095
}
20962096

2097-
func testBuildmodePIE(t *testing.T, useCgo bool) {
2097+
func TestWindowsDefaultBuildmodIsPIE(t *testing.T) {
2098+
if testing.Short() && testenv.Builder() == "" {
2099+
t.Skipf("skipping in -short mode on non-builder")
2100+
}
2101+
2102+
if runtime.GOOS != "windows" {
2103+
t.Skip("skipping windows only test")
2104+
}
2105+
2106+
t.Run("non-cgo", func(t *testing.T) {
2107+
testBuildmodePIE(t, false, false)
2108+
})
2109+
if canCgo {
2110+
t.Run("cgo", func(t *testing.T) {
2111+
testBuildmodePIE(t, true, false)
2112+
})
2113+
}
2114+
}
2115+
2116+
func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
20982117
tg := testgo(t)
20992118
defer tg.cleanup()
21002119
tg.parallel()
@@ -2106,7 +2125,12 @@ func testBuildmodePIE(t *testing.T, useCgo bool) {
21062125
tg.tempFile("main.go", fmt.Sprintf(`package main;%s func main() { print("hello") }`, s))
21072126
src := tg.path("main.go")
21082127
obj := tg.path("main.exe")
2109-
tg.run("build", "-buildmode=pie", "-o", obj, src)
2128+
args := []string{"build"}
2129+
if setBuildmodeToPIE {
2130+
args = append(args, "-buildmode=pie")
2131+
}
2132+
args = append(args, "-o", obj, src)
2133+
tg.run(args...)
21102134

21112135
switch runtime.GOOS {
21122136
case "linux", "android", "freebsd":

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ func buildModeInit() {
155155
case "android":
156156
codegenArg = "-shared"
157157
ldBuildmode = "pie"
158+
case "windows":
159+
ldBuildmode = "pie"
158160
case "darwin":
159161
switch cfg.Goarch {
160162
case "arm64":

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,8 @@ func TestRuntimeTypeAttrInternal(t *testing.T) {
924924
t.Skip("skipping on plan9; no DWARF symbol table in executables")
925925
}
926926

927-
if runtime.GOOS == "windows" && runtime.GOARCH == "arm" {
928-
t.Skip("skipping on windows/arm; test is incompatible with relocatable binaries")
927+
if runtime.GOOS == "windows" {
928+
t.Skip("skipping on windows; test is incompatible with relocatable binaries")
929929
}
930930

931931
testRuntimeTypeAttr(t, "-ldflags=-linkmode=internal")
@@ -944,6 +944,11 @@ func TestRuntimeTypeAttrExternal(t *testing.T) {
944944
if runtime.GOARCH == "ppc64" {
945945
t.Skip("-linkmode=external not supported on ppc64")
946946
}
947+
948+
if runtime.GOOS == "windows" {
949+
t.Skip("skipping on windows; test is incompatible with relocatable binaries")
950+
}
951+
947952
testRuntimeTypeAttr(t, "-ldflags=-linkmode=external")
948953
}
949954

src/cmd/nm/nm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
170170
return true
171171
}
172172
}
173-
if runtime.GOOS == "windows" && runtime.GOARCH == "arm" {
173+
if runtime.GOOS == "windows" {
174174
return true
175175
}
176176
return false

0 commit comments

Comments
 (0)