cmd/go, cmd/link: if -no-pie doesn't work, try -nopie

GCC says -no-pie, clang says -nopie.

Fixes #21042

Change-Id: Iadc83ea7a48ea0debc5064c1ee8da4ebff752044
Reviewed-on: https://go-review.googlesource.com/49710
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ian Lance Taylor 2017-07-18 13:15:05 -07:00
parent 9311af79d5
commit 9e859d5e9c
2 changed files with 30 additions and 16 deletions

View file

@ -1255,13 +1255,20 @@ func (l *Link) hostlink() {
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
Errorf(nil, "WriteFile trivial.c failed: %v", err)
}
cmd := exec.Command(argv[0], "-c", "-no-pie", "trivial.c")
cmd.Dir = *flagTmpdir
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
out, err := cmd.CombinedOutput()
supported := err == nil && !bytes.Contains(out, []byte("unrecognized"))
if supported {
argv = append(argv, "-no-pie")
// GCC uses -no-pie, clang uses -nopie.
for _, nopie := range []string{"-no-pie", "-nopie"} {
cmd := exec.Command(argv[0], "-c", nopie, "trivial.c")
cmd.Dir = *flagTmpdir
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
out, _ := cmd.CombinedOutput()
// GCC says "unrecognized command line option -no-pie"
// clang says "unknown argument: '-no-pie'"
supported := !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
if supported {
argv = append(argv, nopie)
break
}
}
}