From: Cherry Mui Date: Tue, 16 Sep 2025 02:50:51 +0000 (-0400) Subject: cmd/link: make -w behavior consistent on Windows X-Git-Tag: go1.26rc1~815 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=22ac328856ae4c0dcd3d770f50aac5a2df498989;p=gostls13.git 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase --- diff --git a/src/cmd/link/dwarf_test.go b/src/cmd/link/dwarf_test.go index d269aa70c6..5a464fccf3 100644 --- a/src/cmd/link/dwarf_test.go +++ b/src/cmd/link/dwarf_test.go @@ -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") + } + } + }) + } +} diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index c290410b0e..5219a98dd4 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -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 }