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 <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Cherry Mui 2025-09-16 13:34:19 -04:00
parent 76d088eb74
commit 6dceff8bad
2 changed files with 16 additions and 2 deletions

View file

@ -370,14 +370,26 @@ func TestFlagW(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
tests := []struct { type testCase struct {
flag string flag string
wantDWARF bool wantDWARF bool
}{ }
tests := []testCase{
{"-w", false}, // -w flag disables DWARF {"-w", false}, // -w flag disables DWARF
{"-s", false}, // -s implies -w {"-s", false}, // -s implies -w
{"-s -w=0", true}, // -w=0 negates the implied -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 { for _, test := range tests {
name := strings.ReplaceAll(test.flag, " ", "_") name := strings.ReplaceAll(test.flag, " ", "_")
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {

View file

@ -1451,6 +1451,8 @@ func (ctxt *Link) hostlink() {
} else { } else {
argv = append(argv, "-s") argv = append(argv, "-s")
} }
} else if *FlagW {
argv = append(argv, "-Wl,-S") // suppress debugging symbols
} }
// On darwin, whether to combine DWARF into executable. // On darwin, whether to combine DWARF into executable.