[go] cmd/go: force external linking when CGO_LDFLAGS contains -static

2 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
10:24 AM (13 hours ago) 10:24 AM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

cmd/go: force external linking when CGO_LDFLAGS contains -static

When CGO_LDFLAGS contains -static, the internal linker cannot
resolve libc symbols, producing errors like "relocation target
stderr not defined". Detect -static in CGO_LDFLAGS and emit the
preferlinkext token to fall back to external linking.

Fixes #77768
Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
GitHub-Last-Rev: 626cfac10c7e438ecc0a9e193d0ad96e848c655c
GitHub-Pull-Request: golang/go#79331

Change diff

diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
index 51fb442..d66cacf 100644
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -2985,6 +2985,14 @@
flagSources := []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_FFLAGS"}
flagLists := [][]string{cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS}
notCompatibleWithInternalLinking := flagsNotCompatibleWithInternalLinking(flagSources, flagLists)
+ if !notCompatibleWithInternalLinking {
+ for _, f := range cgoLDFLAGS {
+ if f == "-static" {
+ notCompatibleWithInternalLinking = true
+ break
+ }
+ }
+ }

if cfg.BuildMSan {
cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
diff --git a/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt b/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt
index b4b2d14..3e6e989 100644
--- a/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt
+++ b/src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt
@@ -91,6 +91,12 @@
! stderr preferlinkext
env CGO_CFLAGS=

+# CGO_LDFLAGS=-static is not compatible with internal linking.
+env CGO_LDFLAGS=-static
+go build -x -n -o dummy.exe ./usesInternalCgo
+stderr preferlinkext
+env CGO_LDFLAGS=
+
[short] skip

# In the remaining tests below we do actual builds (without -n) to

Change information

Files:
  • M src/cmd/go/internal/work/exec.go
  • M src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt
Change size: S
Delta: 2 files changed, 14 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
Gerrit-Change-Number: 776760
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Ian Lance Taylor (Gerrit)

unread,
2:08 PM (9 hours ago) 2:08 PM
to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com

Ian Lance Taylor added 1 comment

File src/cmd/go/internal/work/exec.go
Line 2988, Patchset 1 (Latest): if !notCompatibleWithInternalLinking {
Ian Lance Taylor . unresolved

We should probably check LDFLAGS the way we check the compiler flags. Also, for some toolchains -static can appear as a compiler flag. Also, for some toolchains -static is actually not supported and people use -Wl,-static. Or -Wl,--static is something accepted. And there is also -Wl,-Bstatic.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
    Gerrit-Change-Number: 776760
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Mon, 11 May 2026 18:08:04 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    3:22 PM (8 hours ago) 3:22 PM
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
    Gerrit-Change-Number: 776760
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Sebastien Tardif (Gerrit)

    unread,
    3:49 PM (7 hours ago) 3:49 PM
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com
    Attention needed from Ian Lance Taylor

    Sebastien Tardif added 1 comment

    File src/cmd/go/internal/work/exec.go
    File-level comment, Patchset 2 (Latest):
    Sebastien Tardif . resolved

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Ian Lance Taylor
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
      Gerrit-Change-Number: 776760
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Sebastien Tardif <star...@teachx.ai>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Comment-Date: Mon, 11 May 2026 19:49:20 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Ian Lance Taylor (Gerrit)

      unread,
      4:00 PM (7 hours ago) 4:00 PM
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Sebastien Tardif, golang-co...@googlegroups.com

      Ian Lance Taylor voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
      Gerrit-Change-Number: 776760
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Sebastien Tardif <star...@teachx.ai>
      Gerrit-Comment-Date: Mon, 11 May 2026 20:00:46 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Ian Lance Taylor (Gerrit)

      unread,
      5:20 PM (6 hours ago) 5:20 PM
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang...@luci-project-accounts.iam.gserviceaccount.com, Sebastien Tardif, golang-co...@googlegroups.com

      Ian Lance Taylor voted and added 1 comment

      Votes added by Ian Lance Taylor

      Auto-Submit+1
      Code-Review+2

      1 comment

      Patchset-level comments
      Ian Lance Taylor . resolved

      Thanks.

      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I5cbbc3cd30880ccf49dd575dd65188d9935d9488
      Gerrit-Change-Number: 776760
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Sebastien Tardif <star...@teachx.ai>
      Gerrit-Comment-Date: Mon, 11 May 2026 21:20:38 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages