From ca8c361d867d62bd46013c5abbaaad0b2ca6077f Mon Sep 17 00:00:00 2001 From: Lynn Boger Date: Thu, 28 Sep 2017 10:26:39 -0400 Subject: [PATCH] cmd/dist: use -buildmode=pie for pie testing Some tests in misc/cgo/test are run with various options including '-linkmode=external "-extldflags=-pie"'. On ppc64x passing -pie to the external linker with code that was not compiled as position independent is incorrect. This works by luck in many cases but is not guaranteed to work. I suspect it is an issue on other targets as well. This will now run the tests using -buildmode=pie for the platforms that support that buildmode option. Fixes #21954 Change-Id: I25fc7573f2d3cb5b0d1c691a0ac91aef7715404f Reviewed-on: https://go-review.googlesource.com/66870 Run-TryBot: Lynn Boger TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/dist/test.go | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index ae7f25cad9..41fc84e3ed 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -837,6 +837,16 @@ func (t *tester) supportedBuildmode(mode string) bool { return true } return false + case "pie": + switch pair { + case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-s390x", + "android-amd64", "android-arm", "android-arm64", "android-386": + return true + case "darwin-amd64": + return true + } + return false + default: log.Fatalf("internal error: unknown buildmode %s", mode) return false @@ -902,33 +912,10 @@ func (t *tester) cgoTest(dt *distTest) error { // static linking on FreeBSD/ARM with clang. (cgo depends on // -fPIC fundamentally.) default: - cmd := t.dirCmd("misc/cgo/test", - defaultcc, "-xc", "-o", "/dev/null", "-static", "-") - cmd.Stdin = strings.NewReader("int main() {}") - if err := cmd.Run(); err != nil { - fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.") - } else { - if goos != "android" { - t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`) - } - t.addCmd(dt, "misc/cgo/nocgo", "go", "test") - t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external`) - if goos != "android" { - t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`) - } - } - - if pair != "freebsd-amd64" { // clang -pie fails to link misc/cgo/test - cmd := t.dirCmd("misc/cgo/test", - defaultcc, "-xc", "-o", "/dev/null", "-pie", "-") - cmd.Stdin = strings.NewReader("int main() {}") - if err := cmd.Run(); err != nil { - fmt.Println("No support for -pie found, skip cgo PIE test.") - } else { - t.addCmd(dt, "misc/cgo/test", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`) - t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`) - t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`) - } + if t.supportedBuildmode("pie") { + t.addCmd(dt, "misc/cgo/test", "go", "test", "-buildmode=pie") + t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-buildmode=pie") + t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-buildmode=pie") } } } -- 2.48.1