Commit 9e859d5e authored by Ian Lance Taylor's avatar Ian Lance Taylor

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 9311af79
...@@ -2948,8 +2948,8 @@ func (tools gccgoToolchain) link(b *Builder, root *Action, out string, allaction ...@@ -2948,8 +2948,8 @@ func (tools gccgoToolchain) link(b *Builder, root *Action, out string, allaction
// libffi. // libffi.
ldflags = append(ldflags, "-Wl,-r", "-nostdlib", "-Wl,--whole-archive", "-lgolibbegin", "-Wl,--no-whole-archive") ldflags = append(ldflags, "-Wl,-r", "-nostdlib", "-Wl,--whole-archive", "-lgolibbegin", "-Wl,--no-whole-archive")
if b.gccSupportsNoPie() { if nopie := b.gccNoPie(); nopie != "" {
ldflags = append(ldflags, "-no-pie") ldflags = append(ldflags, nopie)
} }
// We are creating an object file, so we don't want a build ID. // We are creating an object file, so we don't want a build ID.
...@@ -3196,11 +3196,18 @@ func (b *Builder) ccompilerCmd(envvar, defcmd, objdir string) []string { ...@@ -3196,11 +3196,18 @@ func (b *Builder) ccompilerCmd(envvar, defcmd, objdir string) []string {
return a return a
} }
// On systems with PIE (position independent executables) enabled by default, // gccNoPie returns the flag to use to request non-PIE. On systems
// -no-pie must be passed when doing a partial link with -Wl,-r. But -no-pie is // with PIE (position independent executables) enabled by default,
// not supported by all compilers. // -no-pie must be passed when doing a partial link with -Wl,-r.
func (b *Builder) gccSupportsNoPie() bool { // But -no-pie is not supported by all compilers, and clang spells it -nopie.
return b.gccSupportsFlag("-no-pie") func (b *Builder) gccNoPie() string {
if b.gccSupportsFlag("-no-pie") {
return "-no-pie"
}
if b.gccSupportsFlag("-nopie") {
return "-nopie"
}
return ""
} }
// gccSupportsFlag checks to see if the compiler supports a flag. // gccSupportsFlag checks to see if the compiler supports a flag.
...@@ -3531,8 +3538,8 @@ func (b *Builder) collect(p *load.Package, obj, ofile string, cgoLDFLAGS, outObj ...@@ -3531,8 +3538,8 @@ func (b *Builder) collect(p *load.Package, obj, ofile string, cgoLDFLAGS, outObj
ldflags = append(ldflags, "-Wl,-r", "-nostdlib") ldflags = append(ldflags, "-Wl,-r", "-nostdlib")
if b.gccSupportsNoPie() { if flag := b.gccNoPie(); flag != "" {
ldflags = append(ldflags, "-no-pie") ldflags = append(ldflags, flag)
} }
// We are creating an object file, so we don't want a build ID. // We are creating an object file, so we don't want a build ID.
......
...@@ -1255,13 +1255,20 @@ func (l *Link) hostlink() { ...@@ -1255,13 +1255,20 @@ func (l *Link) hostlink() {
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil { if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
Errorf(nil, "WriteFile trivial.c failed: %v", err) Errorf(nil, "WriteFile trivial.c failed: %v", err)
} }
cmd := exec.Command(argv[0], "-c", "-no-pie", "trivial.c")
cmd.Dir = *flagTmpdir // GCC uses -no-pie, clang uses -nopie.
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...) for _, nopie := range []string{"-no-pie", "-nopie"} {
out, err := cmd.CombinedOutput() cmd := exec.Command(argv[0], "-c", nopie, "trivial.c")
supported := err == nil && !bytes.Contains(out, []byte("unrecognized")) cmd.Dir = *flagTmpdir
if supported { cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
argv = append(argv, "-no-pie") 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
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment