From: Ian Lance Taylor Date: Mon, 20 Mar 2023 21:20:22 +0000 (-0700) Subject: cmd/go: use DefaultPIE to see if external linking is forced X-Git-Tag: go1.21rc1~1217 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9279a9af8b878a1d516539ac882833fe3e7cc202;p=gostls13.git cmd/go: use DefaultPIE to see if external linking is forced Before this CL, the code checked whether external linking was required for -buildmode=pie. This CL changes it to also consider whether external linking is required if PIE is the default build mode. Change-Id: I5ac62fc027622576a152a8b7b5d97bc1d112adb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/477917 Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Bryan Mills Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 922dc99e69..240cbc1a21 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2636,14 +2636,26 @@ func externalLinkingForced(p *Package) bool { return true } - // Currently build modes c-shared, pie (on systems that do not - // support PIE with internal linking mode (currently all - // systems: issue #18968)), plugin, and -linkshared force + // Decide whether we are building a PIE, + // bearing in mind that some systems default to PIE. + isPIE := false + if cfg.BuildBuildmode == "pie" { + isPIE = true + } else if cfg.BuildBuildmode == "default" && platform.DefaultPIE(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH, cfg.BuildRace) { + isPIE = true + } + // If we are building a PIE, and we are on a system + // that does not support PIE with internal linking mode, + // then we must use external linking. + if isPIE && !platform.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH) { + return true + } + + // Currently build modes c-shared, plugin, and -linkshared force // external linking mode, as of course does // -ldflags=-linkmode=external. External linking mode forces // an import of runtime/cgo. // If there are multiple -linkmode options, the last one wins. - pieCgo := cfg.BuildBuildmode == "pie" && !platform.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH) linkmodeExternal := false if p != nil { ldflags := BuildLdflags.For(p) @@ -2660,7 +2672,7 @@ func externalLinkingForced(p *Package) bool { } } - return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal + return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || cfg.BuildLinkshared || linkmodeExternal } // mkAbs rewrites list, which must be paths relative to p.Dir,