Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Add check to see if gcc supports -no-pie. (#607)
Browse files Browse the repository at this point in the history
Basically a copy of golang/go@b80b098.
  • Loading branch information
rdwilliamson committed Nov 5, 2016
1 parent 65d8604 commit 3f7c62c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -278,6 +279,9 @@ func rungcc3(pkg *Package, dir string, ofile string, ofiles []string) error {
args = append(args, "-o", ofile)
args = append(args, ofiles...)
args = append(args, "-Wl,-r", "-nostdlib")
if gccSupportsNoPie(pkg) {
args = append(args, "-no-pie")
}
var cmd []string
if len(pkg.CXXFiles) > 0 || len(pkg.SwigCXXFiles) > 0 {
cmd = gxxCmd(pkg, dir)
Expand Down Expand Up @@ -313,6 +317,20 @@ func libgcc(ctx *Context) (string, error) {
return strings.TrimSpace(buf.String()), err
}

// gccSupportsNoPie check if the -no-pie option is supported. On systems with
// PIE (position independent executables) enabled by default, -no-pie must be
// passed when doing a partial link with -Wl,-r.
func gccSupportsNoPie(pkg *Package) bool {
cmd := gccCmd(pkg, "")
args := []string{
"-no-pie",
"-fsyntax-only",
"-xc",
"-",
}
return exec.Command(cmd[0], append(cmd[1:], args...)...).Run() == nil
}

func cgotool(ctx *Context) string {
return filepath.Join(runtime.GOROOT(), "pkg", "tool", ctx.gohostos+"_"+ctx.gohostarch, "cgo")
}
Expand Down

0 comments on commit 3f7c62c

Please sign in to comment.