[go] cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

121 views
Skip to first unread message

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 7:28:37 PM1/7/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Michael Hudson-Doyle uploaded a change:
https://go-review.googlesource.com/18359

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Go fails to build on a system which has PIE enabled by default like this:

/usr/bin/ld: -r and -pie may not be used together
collect2: error: ld returned 1 exit status

The only system I know that has this property right now is Ubuntu Xenial
running on s390x, which is hardly the most accessible system, but it's
planned
to enable this on amd64 soon too. The fix is to pass -no-pie along with
-Wl,-r
to the compiler, but unfortunately that flag is very new as well. So this
does
a test compile of a trivial file to see if the flag is supported.

Change-Id: I1345571142b7c3a96212e43297d19e84ec4a3d41
---
M src/cmd/go/build.go
1 file changed, 27 insertions(+), 0 deletions(-)



diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go
index 6c6d551..1559e43 100644
--- a/src/cmd/go/build.go
+++ b/src/cmd/go/build.go
@@ -2902,6 +2902,29 @@
return a
}

+// On systems with PIE (position independent executables) enabled by
default,
+// -no-pie must be passed when doing a partial link with -Wl,-r. But
-no-pie is
+// not supported by all compilers.
+func (b *builder) gccSupportsNoPie() bool {
+ src := filepath.Join(b.work, "trivial.c")
+ if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
+ return false
+ }
+ cmdArgs := b.gccCmd(b.work)
+ cmdArgs = append(cmdArgs, "-no-pie", "-c", "trivial.c")
+ if buildN || buildX {
+ b.showcmd(b.work, "%s", joinUnambiguously(cmdArgs))
+ if buildN {
+ return false
+ }
+ }
+ cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
+ cmd.Dir = b.work
+ cmd.Env = envForDir(cmd.Dir, os.Environ())
+ err := cmd.Run()
+ return err == nil
+}
+
// gccArchArgs returns arguments to pass to gcc based on the architecture.
func (b *builder) gccArchArgs() []string {
switch goarch {
@@ -3158,6 +3181,10 @@
}
ldflags := stringList(bareLDFLAGS, "-Wl,-r", "-nostdlib", staticLibs)

+ if b.gccSupportsNoPie() {
+ ldflags = append(ldflags, "-no-pie")
+ }
+
// We are creating an object file, so we don't want a build ID.
ldflags = b.disableBuildID(ldflags)


--
https://go-review.googlesource.com/18359

Ian Lance Taylor (Gerrit)

unread,
Jan 7, 2016, 8:37:34 PM1/7/16
to Michael Hudson-Doyle, golang-co...@googlegroups.com
Ian Lance Taylor has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 1: Code-Review+2

(1 comment)

The annoying thing is that if we passed -r, this wouldn't be a problem,
because -r disables -pie. But at least currently we have to pass -Wl,-r
because clang doesn't recognize -r.

https://go-review.googlesource.com/#/c/18359/1/src/cmd/go/build.go
File src/cmd/go/build.go:

Line 3185: ldflags = append(ldflags, "-no-pie")
You may as well also do this in the c-archive case of gccgoToolchain.ld.


--
https://go-review.googlesource.com/18359
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-HasComments: Yes

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 8:54:50 PM1/7/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Reviewers: Ian Lance Taylor

Michael Hudson-Doyle uploaded a new patch set:
https://go-review.googlesource.com/18359

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Go fails to build on a system which has PIE enabled by default like this:

/usr/bin/ld: -r and -pie may not be used together
collect2: error: ld returned 1 exit status

The only system I know that has this property right now is Ubuntu Xenial
running on s390x, which is hardly the most accessible system, but it's
planned
to enable this on amd64 soon too. The fix is to pass -no-pie along with
-Wl,-r
to the compiler, but unfortunately that flag is very new as well. So this
does
a test compile of a trivial file to see if the flag is supported.

Change-Id: I1345571142b7c3a96212e43297d19e84ec4a3d41
---
M src/cmd/go/build.go
1 file changed, 31 insertions(+), 0 deletions(-)

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 8:55:41 PM1/7/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Michael Hudson-Doyle has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 1:

(1 comment)

> (1 comment)
>
> The annoying thing is that if we passed -r, this wouldn't be a
> problem, because -r disables -pie. But at least currently we have
> to pass -Wl,-r because clang doesn't recognize -r.

So we could in theory do the same sort of thing to detect if $CC supports
-r instead? (not saying that is what should be done).

https://go-review.googlesource.com/#/c/18359/1/src/cmd/go/build.go
File src/cmd/go/build.go:

Line 3185: ldflags = append(ldflags, "-no-pie")
> You may as well also do this in the c-archive case of gccgoToolchain.ld.
Done


--
https://go-review.googlesource.com/18359
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Michael Hudson-Doyle <michael...@canonical.com>
Gerrit-HasComments: Yes

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 8:55:49 PM1/7/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Michael Hudson-Doyle has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 2: Run-TryBot+1

--
https://go-review.googlesource.com/18359
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Michael Hudson-Doyle <michael...@canonical.com>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Jan 7, 2016, 8:56:39 PM1/7/16
to Michael Hudson-Doyle, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 2:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=0fcd1437

--
https://go-review.googlesource.com/18359
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>

Gobot Gobot (Gerrit)

unread,
Jan 7, 2016, 9:02:51 PM1/7/16
to Michael Hudson-Doyle, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 2:

Build is still in progress...
This change failed on freebsd-amd64-gce101:
See
https://storage.googleapis.com/go-build-log/0fcd1437/freebsd-amd64-gce101_055dcda3.log

Consult https://build.golang.org/ to see whether it's a new failure. Other
builds still in progress; subsequent failure notices suppressed until final
report.

Gobot Gobot (Gerrit)

unread,
Jan 7, 2016, 9:13:51 PM1/7/16
to Michael Hudson-Doyle, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 2: TryBot-Result-1

3 of 15 TryBots failed:
Failed on freebsd-amd64-gce101:
https://storage.googleapis.com/go-build-log/0fcd1437/freebsd-amd64-gce101_055dcda3.log
Failed on openbsd-amd64-gce56:
https://storage.googleapis.com/go-build-log/0fcd1437/openbsd-amd64-gce56_93a9d859.log
Failed on openbsd-386-gce56:
https://storage.googleapis.com/go-build-log/0fcd1437/openbsd-386-gce56_51a9839f.log

Consult https://build.golang.org/ to see whether they are new failures.

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 9:24:44 PM1/7/16
to Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com
Michael Hudson-Doyle has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported

Patch Set 2: -Run-TryBot

Well, that's confusing: it seems that even with Command.Std* set to nil,
the error message is still being printed on bsd systems. What's going on?

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 11:44:46 PM1/7/16
to Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com
Reviewers: Ian Lance Taylor, Gobot Gobot

Michael Hudson-Doyle uploaded a new patch set:
https://go-review.googlesource.com/18359

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported on linux

Go fails to build on a system which has PIE enabled by default like this:

/usr/bin/ld: -r and -pie may not be used together
collect2: error: ld returned 1 exit status

The only system I know that has this property right now is Ubuntu Xenial
running on s390x, which is hardly the most accessible system, but it's
planned
to enable this on amd64 soon too. The fix is to pass -no-pie along with
-Wl,-r
to the compiler, but unfortunately that flag is very new as well. So this
does
a test compile of a trivial file to see if the flag is supported.

Change-Id: I1345571142b7c3a96212e43297d19e84ec4a3d41
---
M src/cmd/go/build.go
1 file changed, 38 insertions(+), 0 deletions(-)

Michael Hudson-Doyle (Gerrit)

unread,
Jan 7, 2016, 11:45:02 PM1/7/16
to Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com
Michael Hudson-Doyle has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported on linux

Patch Set 3: Run-TryBot+1

--
https://go-review.googlesource.com/18359
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Michael Hudson-Doyle <michael...@canonical.com>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Jan 7, 2016, 11:45:41 PM1/7/16
to Michael Hudson-Doyle, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported on linux

Patch Set 3:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=0bf606a9

Gobot Gobot (Gerrit)

unread,
Jan 8, 2016, 12:03:38 AM1/8/16
to Michael Hudson-Doyle, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported on linux

Patch Set 3: TryBot-Result+1

TryBots are happy.

Michael Hudson-Doyle (Gerrit)

unread,
Jan 11, 2016, 4:20:14 PM1/11/16
to golang-...@googlegroups.com, Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com
Michael Hudson-Doyle has submitted this change and it was merged.

cmd/go: pass -no-pie along with -Wl,-r to gcc when supported on linux

Go fails to build on a system which has PIE enabled by default like this:

/usr/bin/ld: -r and -pie may not be used together
collect2: error: ld returned 1 exit status

The only system I know that has this property right now is Ubuntu Xenial
running on s390x, which is hardly the most accessible system, but it's
planned
to enable this on amd64 soon too. The fix is to pass -no-pie along with
-Wl,-r
to the compiler, but unfortunately that flag is very new as well. So this
does
a test compile of a trivial file to see if the flag is supported.

Change-Id: I1345571142b7c3a96212e43297d19e84ec4a3d41
Reviewed-on: https://go-review.googlesource.com/18359
Reviewed-by: Ian Lance Taylor <ia...@golang.org>
Run-TryBot: Michael Hudson-Doyle <michael...@canonical.com>
TryBot-Result: Gobot Gobot <go...@golang.org>
---
M src/cmd/go/build.go
1 file changed, 38 insertions(+), 0 deletions(-)

Approvals:
Michael Hudson-Doyle: Run TryBots
Ian Lance Taylor: Looks good to me, approved
Gobot Gobot: TryBots succeeded
Reply all
Reply to author
Forward
0 new messages