]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: make -w behavior consistent on Windows
authorCherry Mui <cherryyz@google.com>
Tue, 16 Sep 2025 02:50:51 +0000 (22:50 -0400)
committerCherry Mui <cherryyz@google.com>
Tue, 23 Sep 2025 14:58:08 +0000 (07:58 -0700)
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>
src/cmd/link/dwarf_test.go
src/cmd/link/internal/ld/pe.go

index d269aa70c61ac9ebfb3f6e53505370d57a64e52e..5a464fccf3089cafbb6b6db44719a3a2473df5a3 100644 (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")
+                               }
+                       }
+               })
+       }
+}
index c290410b0ecb7423a8a432febc926a339b0098b3..5219a98dd47cf4bd2f449d150a131c08f9c4a416 100644 (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
        }