[go] cmd/go: -ldflags=-linkmode=external requires runtime/cgo

545 views
Skip to first unread message

Ian Lance Taylor (Gerrit)

unread,
Nov 10, 2016, 1:02:26 PM11/10/16
to golang-co...@googlegroups.com

Ian Lance Taylor uploaded cmd/go: -ldflags=-linkmode=external requires runtime/cgo for review.

View Change

cmd/go: -ldflags=-linkmode=external requires runtime/cgo

We add runtime/cgo to the list of import paths for various cases that
imply external linking mode, but before this change we did not add for
an explicit request of external linking mode. This fixes the case where
you are using a non-default buildmode that implies a different
compilation option (for example, -buildmode=pie implies -shared) and the
runtime/cgo package for that option is stale.

No test, as I'm not sure how to write one. It would require forcing a
stale runtime/cgo.

Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
---
M src/cmd/go/pkg.go
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go
index 079412c..0c251dd 100644
--- a/src/cmd/go/pkg.go
+++ b/src/cmd/go/pkg.go
@@ -914,10 +914,19 @@
 	}
 
 	// Currently build modes c-shared, pie, plugin, and -linkshared force
-	// external linking mode, and external linking mode forces an
-	// import of runtime/cgo.
+	// external linking mode, as of course does -ldflags=-linkmode=external,
+	// and external linking mode forces an import of runtime/cgo.
 	pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64")
-	if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared) {
+	linkmodeExternal := false
+	for i, a := range buildLdflags {
+		if a == "-linkmode=external" {
+			linkmodeExternal = true
+		}
+		if a == "-linkmode" && i+1 < len(buildLdflags) && buildLdflags[i+1] == "external" {
+			linkmodeExternal = true
+		}
+	}
+	if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared || linkmodeExternal) {
 		importPaths = append(importPaths, "runtime/cgo")
 	}
 

To view, visit this change. To unsubscribe, visit settings.

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
Gerrit-PatchSet: 1
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>

Gobot Gobot (Gerrit)

unread,
Nov 10, 2016, 1:03:13 PM11/10/16
to Ian Lance Taylor, golang-co...@googlegroups.com

Gobot Gobot posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

View Change

Patch Set 1: TryBots beginning. Status page: http://farmer.golang.org/try?commit=4dbee27f

    To view, visit this change. To unsubscribe, visit settings.

    Gerrit-MessageType: comment


    Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
    Gerrit-PatchSet: 1
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>

    Gerrit-HasComments: No

    Gobot Gobot (Gerrit)

    unread,
    Nov 10, 2016, 1:08:52 PM11/10/16
    to Ian Lance Taylor, golang-co...@googlegroups.com

    Gobot Gobot posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

    View Change

    Patch Set 1: TryBot-Result+1 TryBots are happy.

      To view, visit this change. To unsubscribe, visit settings.

      Gerrit-MessageType: comment


      Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Gerrit-PatchSet: 1
      Gerrit-Project: go
      Gerrit-Branch: master

      Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>Gerrit-Reviewer: Gobot Gobot <go...@golang.org>

      Gerrit-HasComments: No

      Michael Hudson-Doyle (Gerrit)

      unread,
      Nov 10, 2016, 1:22:05 PM11/10/16
      to Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com

      Michael Hudson-Doyle posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

      View Change

      Patch Set 1: Code-Review+2 (1 comment) It feels a bit unpleasant to have to duplicate this information between here and the linker, but that's not what this change is about.

      To view, visit this change. To unsubscribe, visit settings.

      Gerrit-MessageType: comment


      Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Gerrit-PatchSet: 1
      Gerrit-Project: go
      Gerrit-Branch: master

      Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>Gerrit-Reviewer: Gobot Gobot <go...@golang.org>Gerrit-Reviewer: Michael Hudson-Doyle <michael...@canonical.com>

      Gerrit-HasComments: Yes

      Ian Lance Taylor (Gerrit)

      unread,
      Nov 10, 2016, 1:45:35 PM11/10/16
      to Michael Hudson-Doyle, Gobot Gobot, golang-co...@googlegroups.com

      Ian Lance Taylor uploaded patch set #2 to cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

      View Change

      cmd/go: -ldflags=-linkmode=external requires runtime/cgo
      
      We add runtime/cgo to the list of import paths for various cases that
      imply external linking mode, but before this change we did not add for
      an explicit request of external linking mode. This fixes the case where
      you are using a non-default buildmode that implies a different
      compilation option (for example, -buildmode=pie implies -shared) and the
      runtime/cgo package for that option is stale.
      
      No test, as I'm not sure how to write one. It would require forcing a
      stale runtime/cgo.
      
      Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      ---
      M src/cmd/go/pkg.go
      1 file changed, 15 insertions(+), 4 deletions(-)
      
      

      To view, visit this change. To unsubscribe, visit settings.

      Gerrit-MessageType: newpatchset
      Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Gerrit-PatchSet: 2
      Gerrit-Project: go
      Gerrit-Branch: master

      Ian Lance Taylor (Gerrit)

      unread,
      Nov 10, 2016, 1:45:45 PM11/10/16
      to Michael Hudson-Doyle, Gobot Gobot, golang-co...@googlegroups.com

      Ian Lance Taylor posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

      View Change

      Patch Set 2: (1 comment)
        • I guess you could change this to "pie (except on linux/amd64)"? Or maybe ch

        • Done

      To view, visit this change. To unsubscribe, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Gerrit-PatchSet: 2
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>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: Yes

      Ian Lance Taylor (Gerrit)

      unread,
      Nov 10, 2016, 1:46:04 PM11/10/16
      to golang-...@googlegroups.com, Michael Hudson-Doyle, Gobot Gobot, golang-co...@googlegroups.com

      Ian Lance Taylor merged cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

      View Change

      cmd/go: -ldflags=-linkmode=external requires runtime/cgo
      
      We add runtime/cgo to the list of import paths for various cases that
      imply external linking mode, but before this change we did not add for
      an explicit request of external linking mode. This fixes the case where
      you are using a non-default buildmode that implies a different
      compilation option (for example, -buildmode=pie implies -shared) and the
      runtime/cgo package for that option is stale.
      
      No test, as I'm not sure how to write one. It would require forcing a
      stale runtime/cgo.
      
      Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Reviewed-on: https://go-review.googlesource.com/33070
      Reviewed-by: Michael Hudson-Doyle <michael...@canonical.com>
      ---
      M src/cmd/go/pkg.go
      1 file changed, 15 insertions(+), 4 deletions(-)
      
      
      Approvals:
        Michael Hudson-Doyle: Looks good to me, approved
      
      
      diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go
      index 079412c..23d3114 100644
      --- a/src/cmd/go/pkg.go
      +++ b/src/cmd/go/pkg.go
      @@ -913,11 +913,22 @@
       		importPaths = append(importPaths, "syscall")
       	}
       
      -	// Currently build modes c-shared, pie, plugin, and -linkshared force
      -	// external linking mode, and external linking mode forces an
      -	// import of runtime/cgo.
      +	// Currently build modes c-shared, pie (on systems that do not
      +	// support PIE with internal linking mode), plugin, and
      +	// -linkshared force external linking mode, as of course does
      +	// -ldflags=-linkmode=external. External linking mode forces
      +	// an import of runtime/cgo.
       	pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64")
      -	if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared) {
      +	linkmodeExternal := false
      +	for i, a := range buildLdflags {
      +		if a == "-linkmode=external" {
      +			linkmodeExternal = true
      +		}
      +		if a == "-linkmode" && i+1 < len(buildLdflags) && buildLdflags[i+1] == "external" {
      +			linkmodeExternal = true
      +		}
      +	}
      +	if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared || linkmodeExternal) {
       		importPaths = append(importPaths, "runtime/cgo")
       	}
       
      

      To view, visit this change. To unsubscribe, visit settings.

      Gerrit-MessageType: merged
      Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
      Gerrit-PatchSet: 3
      Gerrit-Project: go
      Gerrit-Branch: master

      Brad Fitzpatrick (Gerrit)

      unread,
      Nov 10, 2016, 5:55:36 PM11/10/16
      to Ian Lance Taylor, Michael Hudson-Doyle, Gobot Gobot, golang-co...@googlegroups.com, Brad Fitzpatrick

      Brad Fitzpatrick posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

      View Change

      Patch Set 3: This CL broke the nocgo build.

        To view, visit this change. To unsubscribe, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
        Gerrit-PatchSet: 3
        Gerrit-Project: go
        Gerrit-Branch: master


        Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>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

        Ian Lance Taylor (Gerrit)

        unread,
        Nov 10, 2016, 6:20:33 PM11/10/16
        to Michael Hudson-Doyle, Gobot Gobot, golang-co...@googlegroups.com

        Ian Lance Taylor posted comments on cmd/go: -ldflags=-linkmode=external requires runtime/cgo.

        View Change

        Patch Set 3: > This CL broke the nocgo build. Whoops. The code was already broken in some cases, but this broke it more. https://golang.org/cl/33104

          To view, visit this change. To unsubscribe, visit settings.

          Gerrit-MessageType: comment
          Gerrit-Change-Id: Id0409c7274ce67fe15d910baf587d3220cb53d83
          Gerrit-PatchSet: 3
          Gerrit-Project: go
          Gerrit-Branch: master


          Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>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

          Reply all
          Reply to author
          Forward
          0 new messages