mirror of
https://github.com/golang/go.git
synced 2025-05-08 00:53:07 +00:00
[release-branch.go1.4] cmd/go: if -no-pie doesn't work, try -nopie
This is a backport of https://golang.org/cl/49710 to the Go 1.4 branch. Original CL description: GCC says -no-pie, clang says -nopie. Updates #21042 Change-Id: I67b755ccb0149324098b79e07ee4bbc49429c44c Reviewed-on: https://go-review.googlesource.com/49711 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
d6bb5454a7
commit
a3310f9981
@ -2121,34 +2121,40 @@ 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.
|
||||||
|
func (b *builder) gccNoPie() string {
|
||||||
if goos != "linux" {
|
if goos != "linux" {
|
||||||
// On some BSD platforms, error messages from the
|
// On some BSD platforms, error messages from the
|
||||||
// compiler make it to the console despite cmd.Std*
|
// compiler make it to the console despite cmd.Std*
|
||||||
// all being nil. As -no-pie is only required on linux
|
// all being nil. As -no-pie is only required on linux
|
||||||
// systems so far, we only test there.
|
// systems so far, we only test there.
|
||||||
return false
|
return ""
|
||||||
}
|
}
|
||||||
src := filepath.Join(b.work, "trivial.c")
|
src := filepath.Join(b.work, "trivial.c")
|
||||||
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
|
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
|
||||||
return false
|
return ""
|
||||||
}
|
}
|
||||||
|
for _, nopie := range []string{"-no-pie", "-nopie"} {
|
||||||
cmdArgs := b.gccCmd(b.work)
|
cmdArgs := b.gccCmd(b.work)
|
||||||
cmdArgs = append(cmdArgs, "-no-pie", "-c", "trivial.c")
|
cmdArgs = append(cmdArgs, nopie, "-c", "trivial.c")
|
||||||
if buildN || buildX {
|
if buildN || buildX {
|
||||||
b.showcmd(b.work, "%s", joinUnambiguously(cmdArgs))
|
b.showcmd(b.work, "%s", joinUnambiguously(cmdArgs))
|
||||||
if buildN {
|
if buildN {
|
||||||
return false
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
||||||
cmd.Dir = b.work
|
cmd.Dir = b.work
|
||||||
cmd.Env = envForDir(cmd.Dir)
|
cmd.Env = envForDir(cmd.Dir)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
return err == nil
|
if err == nil {
|
||||||
|
return nopie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// gccArchArgs returns arguments to pass to gcc based on the architecture.
|
// gccArchArgs returns arguments to pass to gcc based on the architecture.
|
||||||
@ -2399,8 +2405,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, gccfi
|
|||||||
}
|
}
|
||||||
ldflags := stringList(bareLDFLAGS, "-Wl,-r", "-nostdlib", staticLibs)
|
ldflags := stringList(bareLDFLAGS, "-Wl,-r", "-nostdlib", staticLibs)
|
||||||
|
|
||||||
if b.gccSupportsNoPie() {
|
if flag := b.gccNoPie(); flag != "" {
|
||||||
ldflags = append(ldflags, "-no-pie")
|
ldflags = append(ldflags, flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some systems, such as Ubuntu, always add --build-id to
|
// Some systems, such as Ubuntu, always add --build-id to
|
||||||
|
Loading…
x
Reference in New Issue
Block a user