cmd/link: make -w behavior consistent on Windows

On UNIX-like platforms, the -w flag disables DWARF, and the -s
flag implies -w (so it disables both the symbol table and DWARF).
The implied -w can be negated with -w=0, i.e. -s -w=0 disables the
symbol table but keeps the DWARF. Currently, this negation doesn't
work on Windows. This CL makes it so, so it is consistent on all
platforms (that support DWARF).

Change-Id: I19764a15768433afe333b37061cea16f06cb901b
Reviewed-on: https://go-review.googlesource.com/c/go/+/703998
Reviewed-by: Than McIntosh <thanm@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Cherry Mui 2025-09-15 22:50:51 -04:00
parent 9b2d39b75b
commit 22ac328856
2 changed files with 50 additions and 3 deletions

View file

@ -358,3 +358,53 @@ func TestDWARFLocationList(t *testing.T) {
}
}
}
func TestFlagW(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
tmpdir := t.TempDir()
src := filepath.Join(tmpdir, "a.go")
err := os.WriteFile(src, []byte(helloSrc), 0666)
if err != nil {
t.Fatal(err)
}
tests := []struct {
flag string
wantDWARF bool
}{
{"-w", false}, // -w flag disables DWARF
{"-s", false}, // -s implies -w
{"-s -w=0", true}, // -w=0 negates the implied -w
}
for _, test := range tests {
name := strings.ReplaceAll(test.flag, " ", "_")
t.Run(name, func(t *testing.T) {
ldflags := "-ldflags=" + test.flag
exe := filepath.Join(t.TempDir(), "a.exe")
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", ldflags, "-o", exe, src)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("build failed: %v\n%s", err, out)
}
f, err := objfile.Open(exe)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d, err := f.DWARF()
if test.wantDWARF {
if err != nil {
t.Errorf("want binary with DWARF, got error %v", err)
}
} else {
if d != nil {
t.Errorf("want binary with no DWARF, got DWARF")
}
}
})
}
}

View file

@ -487,9 +487,6 @@ func (f *peFile) addDWARFSection(name string, size int) *peSection {
// addDWARF adds DWARF information to the COFF file f.
func (f *peFile) addDWARF() {
if *FlagS { // disable symbol table
return
}
if *FlagW { // disable dwarf
return
}