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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Alex Brainman 2020-04-26 10:40:38 +10:00
parent 5c9a8c0761
commit c76befe0f4
5 changed files with 44 additions and 7 deletions

View File

@ -31,6 +31,12 @@ TODO
<h2 id="ports">Ports</h2>
<p> <!-- CL 214397 and CL 230217 -->
Go 1.15 now generates Windows ASLR executables when -buildmode=pie
cmd/link flag is provided. Go command uses -buildmode=pie by default
on Windows.
</p>
<p>
TODO
</p>

View File

@ -2082,19 +2082,38 @@ func TestBuildmodePIE(t *testing.T) {
t.Skipf("skipping test because buildmode=pie is not supported on %s", platform)
}
t.Run("non-cgo", func(t *testing.T) {
testBuildmodePIE(t, false)
testBuildmodePIE(t, false, true)
})
if canCgo {
switch runtime.GOOS {
case "darwin", "freebsd", "linux", "windows":
t.Run("cgo", func(t *testing.T) {
testBuildmodePIE(t, true)
testBuildmodePIE(t, true, true)
})
}
}
}
func testBuildmodePIE(t *testing.T, useCgo bool) {
func TestWindowsDefaultBuildmodIsPIE(t *testing.T) {
if testing.Short() && testenv.Builder() == "" {
t.Skipf("skipping in -short mode on non-builder")
}
if runtime.GOOS != "windows" {
t.Skip("skipping windows only test")
}
t.Run("non-cgo", func(t *testing.T) {
testBuildmodePIE(t, false, false)
})
if canCgo {
t.Run("cgo", func(t *testing.T) {
testBuildmodePIE(t, true, false)
})
}
}
func testBuildmodePIE(t *testing.T, useCgo, setBuildmodeToPIE bool) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()
@ -2106,7 +2125,12 @@ func testBuildmodePIE(t *testing.T, useCgo bool) {
tg.tempFile("main.go", fmt.Sprintf(`package main;%s func main() { print("hello") }`, s))
src := tg.path("main.go")
obj := tg.path("main.exe")
tg.run("build", "-buildmode=pie", "-o", obj, src)
args := []string{"build"}
if setBuildmodeToPIE {
args = append(args, "-buildmode=pie")
}
args = append(args, "-o", obj, src)
tg.run(args...)
switch runtime.GOOS {
case "linux", "android", "freebsd":

View File

@ -155,6 +155,8 @@ func buildModeInit() {
case "android":
codegenArg = "-shared"
ldBuildmode = "pie"
case "windows":
ldBuildmode = "pie"
case "darwin":
switch cfg.Goarch {
case "arm64":

View File

@ -924,8 +924,8 @@ func TestRuntimeTypeAttrInternal(t *testing.T) {
t.Skip("skipping on plan9; no DWARF symbol table in executables")
}
if runtime.GOOS == "windows" && runtime.GOARCH == "arm" {
t.Skip("skipping on windows/arm; test is incompatible with relocatable binaries")
if runtime.GOOS == "windows" {
t.Skip("skipping on windows; test is incompatible with relocatable binaries")
}
testRuntimeTypeAttr(t, "-ldflags=-linkmode=internal")
@ -944,6 +944,11 @@ func TestRuntimeTypeAttrExternal(t *testing.T) {
if runtime.GOARCH == "ppc64" {
t.Skip("-linkmode=external not supported on ppc64")
}
if runtime.GOOS == "windows" {
t.Skip("skipping on windows; test is incompatible with relocatable binaries")
}
testRuntimeTypeAttr(t, "-ldflags=-linkmode=external")
}

View File

@ -170,7 +170,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
return true
}
}
if runtime.GOOS == "windows" && runtime.GOARCH == "arm" {
if runtime.GOOS == "windows" {
return true
}
return false