]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: test the -s flag
authorCherry Mui <cherryyz@google.com>
Wed, 28 Jun 2023 17:27:17 +0000 (13:27 -0400)
committerCherry Mui <cherryyz@google.com>
Fri, 21 Jul 2023 15:48:45 +0000 (15:48 +0000)
Add a test checking the -s flag actually suppresses the symbol
table.

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

src/cmd/link/link_test.go

index 346dde05eb0e589b1d56e0012bc345e0671b306d..522e5a4c166778781a1554b9af73b9f83dd7bb4f 100644 (file)
@@ -8,9 +8,11 @@ import (
        "bufio"
        "bytes"
        "debug/macho"
+       "errors"
        "internal/platform"
        "internal/testenv"
        "os"
+       "os/exec"
        "path/filepath"
        "regexp"
        "runtime"
@@ -1319,3 +1321,54 @@ func TestDynimportVar(t *testing.T) {
                }
        }
 }
+
+const helloSrc = `
+package main
+var X = 42
+var Y int
+func main() { println("hello", X, Y) }
+`
+
+func TestFlagS(t *testing.T) {
+       // Test that the -s flag strips the symbol table.
+       testenv.MustHaveGoBuild(t)
+
+       t.Parallel()
+
+       tmpdir := t.TempDir()
+       exe := filepath.Join(tmpdir, "a.exe")
+       src := filepath.Join(tmpdir, "a.go")
+       err := os.WriteFile(src, []byte(helloSrc), 0666)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       modes := []string{"auto"}
+       if testenv.HasCGO() {
+               modes = append(modes, "external")
+       }
+
+       // check a text symbol, a data symbol, and a BSS symbol
+       syms := []string{"main.main", "main.X", "main.Y"}
+
+       for _, mode := range modes {
+               cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-s -linkmode="+mode, "-o", exe, src)
+               out, err := cmd.CombinedOutput()
+               if err != nil {
+                       t.Fatalf("build (linkmode=%s) failed: %v\n%s", mode, err, out)
+               }
+               cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe)
+               out, err = cmd.CombinedOutput()
+               if err != nil && !errors.As(err, new(*exec.ExitError)) {
+                       // Error exit is fine as it may have no symbols.
+                       // On darwin we need to emit dynamic symbol references so it
+                       // actually has some symbols, and nm succeeds.
+                       t.Errorf("(mode=%s) go tool nm failed: %v\n%s", mode, err, out)
+               }
+               for _, s := range syms {
+                       if bytes.Contains(out, []byte(s)) {
+                               t.Errorf("(mode=%s): unexpected symbol %s", mode, s)
+                       }
+               }
+       }
+}