Skip to content

Commit 6c0135d

Browse files
committed
cmd/go: don't always link in cgo for PIE
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>
1 parent 027367a commit 6c0135d

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/cmd/go/internal/load/pkg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"cmd/go/internal/search"
3434
"cmd/go/internal/str"
3535
"cmd/go/internal/trace"
36+
"cmd/internal/sys"
3637
)
3738

3839
var IgnoreImports bool // control whether we ignore imports in packages
@@ -1968,7 +1969,7 @@ func externalLinkingForced(p *Package) bool {
19681969
// external linking mode, as of course does
19691970
// -ldflags=-linkmode=external. External linking mode forces
19701971
// an import of runtime/cgo.
1971-
pieCgo := cfg.BuildBuildmode == "pie"
1972+
pieCgo := cfg.BuildBuildmode == "pie" && !sys.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH)
19721973
linkmodeExternal := false
19731974
if p != nil {
19741975
ldflags := BuildLdflags.For(p)

src/cmd/internal/sys/supported.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,13 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
115115
return false
116116
}
117117
}
118+
119+
func InternalLinkPIESupported(goos, goarch string) bool {
120+
switch goos + "/" + goarch {
121+
case "linux/amd64", "linux/arm64",
122+
"android/arm64",
123+
"windows-amd64", "windows-386", "windows-arm":
124+
return true
125+
}
126+
return false
127+
}

0 commit comments

Comments
 (0)