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.