Skip to content

Commit

Permalink
cmd/link: test the -s flag
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Cherry Mui <[email protected]>
  • Loading branch information
cherrymui committed Jul 21, 2023
1 parent 5bf8f53 commit f29c4fa
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/cmd/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"bufio"
"bytes"
"debug/macho"
"errors"
"internal/platform"
"internal/testenv"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand Down Expand Up @@ -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)
}
}
}
}

0 comments on commit f29c4fa

Please sign in to comment.