Skip to content

Commit ca8c361

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 92ba9b5 commit ca8c361

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

src/cmd/dist/test.go

+14-27
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,16 @@ func (t *tester) supportedBuildmode(mode string) bool {
837837
return true
838838
}
839839
return false
840+
case "pie":
841+
switch pair {
842+
case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-s390x",
843+
"android-amd64", "android-arm", "android-arm64", "android-386":
844+
return true
845+
case "darwin-amd64":
846+
return true
847+
}
848+
return false
849+
840850
default:
841851
log.Fatalf("internal error: unknown buildmode %s", mode)
842852
return false
@@ -902,33 +912,10 @@ func (t *tester) cgoTest(dt *distTest) error {
902912
// static linking on FreeBSD/ARM with clang. (cgo depends on
903913
// -fPIC fundamentally.)
904914
default:
905-
cmd := t.dirCmd("misc/cgo/test",
906-
defaultcc, "-xc", "-o", "/dev/null", "-static", "-")
907-
cmd.Stdin = strings.NewReader("int main() {}")
908-
if err := cmd.Run(); err != nil {
909-
fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
910-
} else {
911-
if goos != "android" {
912-
t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
913-
}
914-
t.addCmd(dt, "misc/cgo/nocgo", "go", "test")
915-
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external`)
916-
if goos != "android" {
917-
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
918-
}
919-
}
920-
921-
if pair != "freebsd-amd64" { // clang -pie fails to link misc/cgo/test
922-
cmd := t.dirCmd("misc/cgo/test",
923-
defaultcc, "-xc", "-o", "/dev/null", "-pie", "-")
924-
cmd.Stdin = strings.NewReader("int main() {}")
925-
if err := cmd.Run(); err != nil {
926-
fmt.Println("No support for -pie found, skip cgo PIE test.")
927-
} else {
928-
t.addCmd(dt, "misc/cgo/test", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
929-
t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
930-
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
931-
}
915+
if t.supportedBuildmode("pie") {
916+
t.addCmd(dt, "misc/cgo/test", "go", "test", "-buildmode=pie")
917+
t.addCmd(dt, "misc/cgo/testtls", "go", "test", "-buildmode=pie")
918+
t.addCmd(dt, "misc/cgo/nocgo", "go", "test", "-buildmode=pie")
932919
}
933920
}
934921
}

0 commit comments

Comments
 (0)