]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/link: rationalize -s and -w flags with Mach-O external linking
authorCherry Mui <cherryyz@google.com>
Fri, 5 May 2023 22:52:39 +0000 (18:52 -0400)
committerCherry Mui <cherryyz@google.com>
Fri, 21 Jul 2023 15:44:11 +0000 (15:44 +0000)
Currently, on Mach-O in external linking mode, the handling of -s
and -w flags are a bit mixed: neither flag disables the symbol
table, and both flags disable DWARF.

This CL makes it do what is documented: -s disables symbol table,
and -w disables DWARF. For the Darwin system linker, the -s flag
(strip symbol table) is obsolete. So we strip it afterwards. We
already use the strip command to strip the debug STAB symbols if
we need to combine DWARF. With this CL we'll use an additional
flag to strip more symbols. And we now also use strip if -s is
specified and we don't need to combine DWARF.

Change-Id: I9bed24fd388f2bd5b0ffa4ec2db46a4a2f6b1016
Reviewed-on: https://go-review.googlesource.com/c/go/+/493136
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/macho.go

index 6c03072160945e6b2d061eaf20c6ad3e053270f7..595e656e5b16cb1b1a6cb2be883d7b90e8e91898 100644 (file)
@@ -1390,7 +1390,7 @@ func (ctxt *Link) hostlink() {
                if ctxt.HeadType == objabi.Hdarwin {
                        // Recent versions of macOS print
                        //      ld: warning: option -s is obsolete and being ignored
-                       // so do not pass any arguments.
+                       // so do not pass any arguments (but we strip symbols below).
                } else {
                        argv = append(argv, "-s")
                }
@@ -1398,7 +1398,7 @@ func (ctxt *Link) hostlink() {
 
        // On darwin, whether to combine DWARF into executable.
        // Only macOS supports unmapped segments such as our __DWARF segment.
-       combineDwarf := ctxt.IsDarwin() && !*FlagS && !*FlagW && !debug_s && machoPlatform == PLATFORM_MACOS
+       combineDwarf := ctxt.IsDarwin() && !*FlagW && machoPlatform == PLATFORM_MACOS
 
        switch ctxt.HeadType {
        case objabi.Hdarwin:
@@ -1417,6 +1417,12 @@ func (ctxt *Link) hostlink() {
                }
                if !combineDwarf {
                        argv = append(argv, "-Wl,-S") // suppress STAB (symbolic debugging) symbols
+                       if debug_s {
+                               // We are generating a binary with symbol table suppressed.
+                               // Suppress local symbols. We need to keep dynamically exported
+                               // and referenced symbols so the dynamic linker can resolve them.
+                               argv = append(argv, "-Wl,-x")
+                       }
                }
        case objabi.Hopenbsd:
                argv = append(argv, "-Wl,-nopie")
@@ -1929,7 +1935,15 @@ func (ctxt *Link) hostlink() {
                }
                // Remove STAB (symbolic debugging) symbols after we are done with them (by dsymutil).
                // They contain temporary file paths and make the build not reproducible.
-               if out, err := exec.Command(stripCmd, "-S", *flagOutfile).CombinedOutput(); err != nil {
+               var stripArgs = []string{"-S"}
+               if debug_s {
+                       // We are generating a binary with symbol table suppressed.
+                       // Suppress local symbols. We need to keep dynamically exported
+                       // and referenced symbols so the dynamic linker can resolve them.
+                       stripArgs = append(stripArgs, "-x")
+               }
+               stripArgs = append(stripArgs, *flagOutfile)
+               if out, err := exec.Command(stripCmd, stripArgs...).CombinedOutput(); err != nil {
                        Exitf("%s: running strip failed: %v\n%s", os.Args[0], err, out)
                }
                // Skip combining if `dsymutil` didn't generate a file. See #11994.
index 81ebfb6c7ac8b5cd05de1aacd35cc0f76e790e6f..52ff85ddef51b12b2cae50cc57d035627711bcde 100644 (file)
@@ -665,7 +665,7 @@ func machoshbits(ctxt *Link, mseg *MachoSeg, sect *sym.Section, segname string)
 
 func asmbMacho(ctxt *Link) {
        machlink := doMachoLink(ctxt)
-       if !*FlagS && ctxt.IsExternal() {
+       if ctxt.IsExternal() {
                symo := int64(Segdwarf.Fileoff + uint64(Rnd(int64(Segdwarf.Filelen), int64(*FlagRound))) + uint64(machlink))
                ctxt.Out.SeekSet(symo)
                machoEmitReloc(ctxt)