864f2d
diff -up go/misc/cgo/testcarchive/carchive_test.go.orig go/misc/cgo/testcarchive/carchive_test.go
864f2d
--- go/misc/cgo/testcarchive/carchive_test.go.orig	2017-10-25 20:30:21.000000000 +0200
864f2d
+++ go/misc/cgo/testcarchive/carchive_test.go	2017-11-08 10:31:42.982462249 +0100
864f2d
@@ -6,6 +6,7 @@ package carchive_test
864f2d
 
864f2d
 import (
864f2d
 	"bufio"
864f2d
+	"bytes"
864f2d
 	"debug/elf"
864f2d
 	"fmt"
864f2d
 	"io/ioutil"
864f2d
@@ -605,9 +606,26 @@ func TestCompileWithoutShared(t *testing
864f2d
 	}
864f2d
 
864f2d
 	exe := "./testnoshared" + exeSuffix
864f2d
-	ccArgs := append(cc, "-o", exe, "main5.c", "libgo2.a")
864f2d
+
864f2d
+	// In some cases, -no-pie is needed here, but not accepted everywhere. First try
864f2d
+	// if -no-pie is accepted. See #22126.
864f2d
+	ccArgs := append(cc, "-o", exe, "-no-pie", "main5.c", "libgo2.a")
864f2d
 	t.Log(ccArgs)
864f2d
 	out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
864f2d
+
864f2d
+	// If -no-pie unrecognized, try -nopie if this is possibly clang
864f2d
+	if err != nil && bytes.Contains(out, []byte("unknown")) && !strings.Contains(cc[0], "gcc") {
864f2d
+		ccArgs = append(cc, "-o", exe, "-nopie", "main5.c", "libgo2.a")
864f2d
+		t.Log(ccArgs)
864f2d
+		out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
864f2d
+	}
864f2d
+
864f2d
+	// Don't use either -no-pie or -nopie
864f2d
+	if err != nil && bytes.Contains(out, []byte("unrecognized")) {
864f2d
+		ccArgs := append(cc, "-o", exe, "main5.c", "libgo2.a")
864f2d
+		t.Log(ccArgs)
864f2d
+		out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
864f2d
+	}
864f2d
 	t.Logf("%s", out)
864f2d
 	if err != nil {
864f2d
 		t.Fatal(err)