From 119213566a8e0da729a8bccc7d7f7d525f0c1cf9 Mon Sep 17 00:00:00 2001 From: jochen weber Date: Fri, 14 May 2021 12:45:13 +0000 Subject: cmd/cgo: remove hardcoded '-pie' ldflag for linux/arm a minimally invasive fix proposal for #45940. which keeps the fix for #26197. an alternative for (#26197) could be to fail if we have both flags. adding/removing a flag without an message to the user is inconvenient. Change-Id: I6ac2524d81ff57202fbe3032a53afd5106270a9e GitHub-Last-Rev: edaf02fa455329b5d794a139f99874b5e8cc12d1 GitHub-Pull-Request: golang/go#45989 Reviewed-on: https://go-review.googlesource.com/c/go/+/317569 Reviewed-by: Cherry Mui Run-TryBot: Cherry Mui TryBot-Result: Go Bot Trust: Bryan C. Mills --- src/cmd/go/internal/work/exec.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index f7fae9fdd9..f82028aef6 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -2963,18 +2963,24 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe linkobj := str.StringList(ofile, outObj, mkAbsFiles(p.Dir, p.SysoFiles)) dynobj := objdir + "_cgo_.o" - // we need to use -pie for Linux/ARM to get accurate imported sym ldflags := cgoLDFLAGS if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" { - // -static -pie doesn't make sense, and causes link errors. - // Issue 26197. - n := make([]string, 0, len(ldflags)) - for _, flag := range ldflags { - if flag != "-static" { - n = append(n, flag) + if !str.Contains(ldflags, "-no-pie") { + // we need to use -pie for Linux/ARM to get accurate imported sym (added in https://golang.org/cl/5989058) + // this seems to be outdated, but we don't want to break existing builds depending on this (Issue 45940) + ldflags = append(ldflags, "-pie") + } + if str.Contains(ldflags, "-pie") && str.Contains(ldflags, "-static") { + // -static -pie doesn't make sense, and causes link errors. + // Issue 26197. + n := make([]string, 0, len(ldflags)-1) + for _, flag := range ldflags { + if flag != "-static" { + n = append(n, flag) + } } + ldflags = n } - ldflags = append(n, "-pie") } if err := b.gccld(a, p, objdir, dynobj, ldflags, linkobj); err != nil { return err -- cgit v1.2.3-54-g00ecf