]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: -ldflags=-linkmode=external requires runtime/cgo
authorIan Lance Taylor <iant@golang.org>
Thu, 10 Nov 2016 17:59:42 +0000 (09:59 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 10 Nov 2016 18:46:00 +0000 (18:46 +0000)
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.hudson@canonical.com>
src/cmd/go/pkg.go

index 079412ce8aede0df707a875dbd7fea6fad370338..23d3114682bae2958ce929dc80de6f86e2d9f84b 100644 (file)
@@ -913,11 +913,22 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
                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")
        }