From 6dceff8bad6213bca76361462c99b0d06fd0a8f9 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Tue, 16 Sep 2025 13:34:19 -0400 Subject: [PATCH] cmd/link: handle -w flag in external linking mode Currently, when the -w flag is set, it doesn't actually disable the debug info generation with in external linking mode. (It does make the Go object have no debug info, but C objects may still have.) Pass "-Wl,-S" to let the external linker disable debug info generation. Change-Id: I0fce56b9f23a45546b69b9e6dd027c5527b1bc87 Reviewed-on: https://go-review.googlesource.com/c/go/+/705857 Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor --- src/cmd/link/dwarf_test.go | 16 ++++++++++++++-- src/cmd/link/internal/ld/lib.go | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cmd/link/dwarf_test.go b/src/cmd/link/dwarf_test.go index 5a464fccf30..4ca578498d7 100644 --- a/src/cmd/link/dwarf_test.go +++ b/src/cmd/link/dwarf_test.go @@ -370,14 +370,26 @@ func TestFlagW(t *testing.T) { t.Fatal(err) } - tests := []struct { + type testCase struct { flag string wantDWARF bool - }{ + } + tests := []testCase{ {"-w", false}, // -w flag disables DWARF {"-s", false}, // -s implies -w {"-s -w=0", true}, // -w=0 negates the implied -w } + if testenv.HasCGO() { + tests = append(tests, + testCase{"-w -linkmode=external", false}, + testCase{"-s -linkmode=external", false}, + // Some external linkers don't have a way to preserve DWARF + // without emitting the symbol table. Skip this case for now. + // I suppose we can post- process, e.g. with objcopy. + //testCase{"-s -w=0 -linkmode=external", true}, + ) + } + for _, test := range tests { name := strings.ReplaceAll(test.flag, " ", "_") t.Run(name, func(t *testing.T) { diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 1a1bc186755..623acc1ad48 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1451,6 +1451,8 @@ func (ctxt *Link) hostlink() { } else { argv = append(argv, "-s") } + } else if *FlagW { + argv = append(argv, "-Wl,-S") // suppress debugging symbols } // On darwin, whether to combine DWARF into executable.