]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use -buildmode=pie as default on window
authorAlex Brainman <alex.brainman@gmail.com>
Sun, 26 Apr 2020 00:40:38 +0000 (10:40 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 30 Apr 2020 08:07:47 +0000 (08:07 +0000)
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>
doc/go1.15.html
src/cmd/go/go_test.go
src/cmd/go/internal/work/init.go
src/cmd/link/internal/ld/dwarf_test.go
src/cmd/nm/nm_test.go

index 97479d1537c13539dc7b85d8bee3bae34fe7073f..f5e72649fac1b18681667dcc0be1c466414643ca 100644 (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>
index e1cf1f8ff5952f020d8823f450ff18825566fa21..d7f6b47135208e9590690d14832baf6d830db21b 100644 (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":
index 921a54f6f06e5972eea78f985f4a76cef1c0bc96..dad3b10111d0d1d177799849561a0bfd68e94990 100644 (file)
@@ -155,6 +155,8 @@ func buildModeInit() {
                case "android":
                        codegenArg = "-shared"
                        ldBuildmode = "pie"
+               case "windows":
+                       ldBuildmode = "pie"
                case "darwin":
                        switch cfg.Goarch {
                        case "arm64":
index cf6bec80535b51360f91d2e0a2e7bfdb8fa3f267..a1c8496eeae6987e726de71b785e49ff5b13084e 100644 (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")
 }
 
index 7dfb482b1889036821f6a46043f8a13ed057d5f8..a49423b21255ddce21287690820c48223c8e5cb1 100644 (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