Skip to content

Commit

Permalink
cmd/go: don't always link in cgo for PIE
Browse files Browse the repository at this point in the history
Internal linking for PIE is now supported and enabled by default
on some platforms, for which cgo is not needed. Don't always
bring in cgo.

Change-Id: I043ed436f0e6a3acbcc53ec543f06e193d614b36
Reviewed-on: https://go-review.googlesource.com/c/go/+/261498
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
cherrymui committed Oct 12, 2020
1 parent 027367a commit 6c0135d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"cmd/go/internal/search"
"cmd/go/internal/str"
"cmd/go/internal/trace"
"cmd/internal/sys"
)

var IgnoreImports bool // control whether we ignore imports in packages
Expand Down Expand Up @@ -1968,7 +1969,7 @@ func externalLinkingForced(p *Package) bool {
// external linking mode, as of course does
// -ldflags=-linkmode=external. External linking mode forces
// an import of runtime/cgo.
pieCgo := cfg.BuildBuildmode == "pie"
pieCgo := cfg.BuildBuildmode == "pie" && !sys.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH)
linkmodeExternal := false
if p != nil {
ldflags := BuildLdflags.For(p)
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/internal/sys/supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,13 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
return false
}
}

func InternalLinkPIESupported(goos, goarch string) bool {
switch goos + "/" + goarch {
case "linux/amd64", "linux/arm64",
"android/arm64",
"windows-amd64", "windows-386", "windows-arm":
return true
}
return false
}

0 comments on commit 6c0135d

Please sign in to comment.