Skip to content

Commit c03e25a

Browse files
cmd/link: always run current linker in tests
This ensures that the tests are testing the current linker sources, not the installed linker. Change-Id: I14a2ca9d413e1af57c7b5a00657c72023626a651 Reviewed-on: https://go-review.googlesource.com/c/go/+/721220 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent 9f5cd43 commit c03e25a

File tree

5 files changed

+151
-123
lines changed

5 files changed

+151
-123
lines changed

‎src/cmd/link/cgo_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func testCGOLTO(t *testing.T, cc, cgoCflags string, test int) {
115115
t.Fatalf("bad case %d", test)
116116
}
117117

118-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build")
118+
cmd := goCmd(t, "build")
119119
cmd.Dir = dir
120120
cgoCflags += " -flto"
121-
cmd.Env = append(cmd.Environ(), "CGO_CFLAGS="+cgoCflags)
121+
cmd.Env = append(cmd.Env, "CGO_CFLAGS="+cgoCflags)
122122

123123
t.Logf("CGO_CFLAGS=%q %v", cgoCflags, cmd)
124124
out, err := cmd.CombinedOutput()

‎src/cmd/link/dwarf_test.go‎

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,6 @@ import (
2121
"testing"
2222
)
2323

24-
// TestMain allows this test binary to run as a -toolexec wrapper for
25-
// the 'go' command. If LINK_TEST_TOOLEXEC is set, TestMain runs the
26-
// binary as if it were cmd/link, and otherwise runs the requested
27-
// tool as a subprocess.
28-
//
29-
// This allows the test to verify the behavior of the current contents of the
30-
// cmd/link package even if the installed cmd/link binary is stale.
31-
func TestMain(m *testing.M) {
32-
// Are we running as a toolexec wrapper? If so then run either
33-
// the correct tool or this executable itself (for the linker).
34-
// Running as toolexec wrapper.
35-
if os.Getenv("LINK_TEST_TOOLEXEC") != "" {
36-
if strings.TrimSuffix(filepath.Base(os.Args[1]), ".exe") == "link" {
37-
// Running as a -toolexec linker, and the tool is cmd/link.
38-
// Substitute this test binary for the linker.
39-
os.Args = os.Args[1:]
40-
main()
41-
os.Exit(0)
42-
}
43-
// Running some other tool.
44-
cmd := exec.Command(os.Args[1], os.Args[2:]...)
45-
cmd.Stdin = os.Stdin
46-
cmd.Stdout = os.Stdout
47-
cmd.Stderr = os.Stderr
48-
if err := cmd.Run(); err != nil {
49-
os.Exit(1)
50-
}
51-
os.Exit(0)
52-
}
53-
54-
// Are we being asked to run as the linker (without toolexec)?
55-
// If so then kick off main.
56-
if os.Getenv("LINK_TEST_EXEC_LINKER") != "" {
57-
main()
58-
os.Exit(0)
59-
}
60-
61-
if testExe, err := os.Executable(); err == nil {
62-
// on wasm, some phones, we expect an error from os.Executable()
63-
testLinker = testExe
64-
}
65-
66-
// Not running as a -toolexec wrapper or as a linker executable.
67-
// Just run the tests.
68-
os.Exit(m.Run())
69-
}
70-
71-
// Path of the test executable being run.
72-
var testLinker string
73-
7424
func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) {
7525
testenv.MustHaveCGO(t)
7626
testenv.MustHaveGoBuild(t)
@@ -106,14 +56,13 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string)
10656

10757
exe := filepath.Join(tmpDir, prog+".exe")
10858
dir := "../../runtime/testdata/" + prog
109-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-o", exe)
59+
cmd := goCmd(t, "build", "-o", exe)
11060
if buildmode != "" {
11161
cmd.Args = append(cmd.Args, "-buildmode", buildmode)
11262
}
11363
cmd.Args = append(cmd.Args, dir)
114-
cmd.Env = append(os.Environ(), env...)
64+
cmd.Env = append(cmd.Env, env...)
11565
cmd.Env = append(cmd.Env, "CGO_CFLAGS=") // ensure CGO_CFLAGS does not contain any flags. Issue #35459
116-
cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1")
11766
out, err := cmd.CombinedOutput()
11867
if err != nil {
11968
t.Fatalf("go build -o %v %v: %v\n%s", exe, dir, err, out)
@@ -282,9 +231,8 @@ func TestDWARFLocationList(t *testing.T) {
282231
exe := filepath.Join(tmpDir, "issue65405.exe")
283232
dir := "./testdata/dwarf/issue65405"
284233

285-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-gcflags=all=-N -l", "-o", exe, dir)
286-
cmd.Env = append(os.Environ(), "CGO_CFLAGS=")
287-
cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1")
234+
cmd := goCmd(t, "build", "-gcflags=all=-N -l", "-o", exe, dir)
235+
cmd.Env = append(cmd.Env, "CGO_CFLAGS=")
288236
out, err := cmd.CombinedOutput()
289237
if err != nil {
290238
t.Fatalf("go build -o %v %v: %v\n%s", exe, dir, err, out)
@@ -402,7 +350,7 @@ func TestFlagW(t *testing.T) {
402350
t.Run(name, func(t *testing.T) {
403351
ldflags := "-ldflags=" + test.flag
404352
exe := filepath.Join(t.TempDir(), "a.exe")
405-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", ldflags, "-o", exe, src)
353+
cmd := goCmd(t, "build", ldflags, "-o", exe, src)
406354
out, err := cmd.CombinedOutput()
407355
if err != nil {
408356
t.Fatalf("build failed: %v\n%s", err, out)

‎src/cmd/link/elf_test.go‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func TestSectionsWithSameName(t *testing.T) {
8080
dir := t.TempDir()
8181

8282
gopath := filepath.Join(dir, "GOPATH")
83-
env := append(os.Environ(), "GOPATH="+gopath)
83+
gopathEnv := "GOPATH=" + gopath
84+
env := append(os.Environ(), gopathEnv)
8485

8586
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module elf_test\n"), 0666); err != nil {
8687
t.Fatal(err)
@@ -91,7 +92,6 @@ func TestSectionsWithSameName(t *testing.T) {
9192
t.Fatal(err)
9293
}
9394

94-
goTool := testenv.GoToolPath(t)
9595
cc, cflags := getCCAndCCFLAGS(t, env)
9696

9797
asmObj := filepath.Join(dir, "x.o")
@@ -119,10 +119,10 @@ func TestSectionsWithSameName(t *testing.T) {
119119
t.Fatal(err)
120120
}
121121

122-
cmd := testenv.Command(t, goTool, "build")
122+
cmd := goCmd(t, "build")
123123
cmd.Dir = dir
124-
cmd.Env = env
125-
t.Logf("%s build", goTool)
124+
cmd.Env = append(cmd.Env, gopathEnv)
125+
t.Logf("%s build", testenv.GoToolPath(t))
126126
if out, err := cmd.CombinedOutput(); err != nil {
127127
t.Logf("%s", out)
128128
t.Fatal(err)
@@ -150,13 +150,13 @@ func TestMinusRSymsWithSameName(t *testing.T) {
150150
dir := t.TempDir()
151151

152152
gopath := filepath.Join(dir, "GOPATH")
153-
env := append(os.Environ(), "GOPATH="+gopath)
153+
gopathEnv := "GOPATH=" + gopath
154+
env := append(os.Environ(), gopathEnv)
154155

155156
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module elf_test\n"), 0666); err != nil {
156157
t.Fatal(err)
157158
}
158159

159-
goTool := testenv.GoToolPath(t)
160160
cc, cflags := getCCAndCCFLAGS(t, env)
161161

162162
objs := []string{}
@@ -198,10 +198,10 @@ func TestMinusRSymsWithSameName(t *testing.T) {
198198
t.Fatal(err)
199199
}
200200

201-
t.Logf("%s build", goTool)
202-
cmd := testenv.Command(t, goTool, "build")
201+
t.Logf("%s build", testenv.GoToolPath(t))
202+
cmd := goCmd(t, "build")
203203
cmd.Dir = dir
204-
cmd.Env = env
204+
cmd.Env = append(cmd.Env, gopathEnv)
205205
if out, err := cmd.CombinedOutput(); err != nil {
206206
t.Logf("%s", out)
207207
t.Fatal(err)
@@ -243,7 +243,7 @@ func TestGNUBuildID(t *testing.T) {
243243
for _, test := range tests {
244244
t.Run(test.name, func(t *testing.T) {
245245
exe := filepath.Join(tmpdir, test.name)
246-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-buildid="+gobuildid+" "+test.ldflags, "-o", exe, goFile)
246+
cmd := goCmd(t, "build", "-ldflags=-buildid="+gobuildid+" "+test.ldflags, "-o", exe, goFile)
247247
if out, err := cmd.CombinedOutput(); err != nil {
248248
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
249249
}
@@ -277,11 +277,9 @@ func TestMergeNoteSections(t *testing.T) {
277277
t.Fatal(err)
278278
}
279279
outFile := filepath.Join(t.TempDir(), "notes.exe")
280-
goTool := testenv.GoToolPath(t)
281280
// sha1sum of "gopher"
282281
id := "0xf4e8cd51ce8bae2996dc3b74639cdeaa1f7fee5f"
283-
cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags",
284-
"-B "+id, goFile)
282+
cmd := goCmd(t, "build", "-o", outFile, "-ldflags", "-B "+id, goFile)
285283
cmd.Dir = t.TempDir()
286284
if out, err := cmd.CombinedOutput(); err != nil {
287285
t.Logf("%s", out)
@@ -383,7 +381,7 @@ func TestPIESize(t *testing.T) {
383381
binpie += linkmode
384382

385383
build := func(bin, mode string) error {
386-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", bin, "-buildmode="+mode, "-ldflags=-linkmode="+linkmode)
384+
cmd := goCmd(t, "build", "-o", bin, "-buildmode="+mode, "-ldflags=-linkmode="+linkmode)
387385
cmd.Args = append(cmd.Args, "pie.go")
388386
cmd.Dir = dir
389387
t.Logf("%v", cmd.Args)
@@ -532,8 +530,7 @@ func TestIssue51939(t *testing.T) {
532530
t.Fatal(err)
533531
}
534532
outFile := filepath.Join(td, "issue51939.exe")
535-
goTool := testenv.GoToolPath(t)
536-
cmd := testenv.Command(t, goTool, "build", "-o", outFile, goFile)
533+
cmd := goCmd(t, "build", "-o", outFile, goFile)
537534
if out, err := cmd.CombinedOutput(); err != nil {
538535
t.Logf("%s", out)
539536
t.Fatal(err)
@@ -565,7 +562,7 @@ func TestFlagR(t *testing.T) {
565562
}
566563
exe := filepath.Join(tmpdir, "x.exe")
567564

568-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-R=0x100000", "-o", exe, src)
565+
cmd := goCmd(t, "build", "-ldflags=-R=0x100000", "-o", exe, src)
569566
if out, err := cmd.CombinedOutput(); err != nil {
570567
t.Fatalf("build failed: %v, output:\n%s", err, out)
571568
}
@@ -610,7 +607,7 @@ func testFlagD(t *testing.T, dataAddr string, roundQuantum string, expectedAddr
610607
ldflags += " -R=" + roundQuantum
611608
}
612609

613-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags="+ldflags, "-o", exe, src)
610+
cmd := goCmd(t, "build", "-ldflags="+ldflags, "-o", exe, src)
614611
if out, err := cmd.CombinedOutput(); err != nil {
615612
t.Fatalf("build failed: %v, output:\n%s", err, out)
616613
}
@@ -669,7 +666,7 @@ func testFlagDError(t *testing.T, dataAddr string, roundQuantum string, expected
669666
ldflags += " -R=" + roundQuantum
670667
}
671668

672-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags="+ldflags, "-o", exe, src)
669+
cmd := goCmd(t, "build", "-ldflags="+ldflags, "-o", exe, src)
673670
out, err := cmd.CombinedOutput()
674671
if err == nil {
675672
t.Fatalf("expected build to fail with unaligned data address, but it succeeded")
@@ -696,9 +693,7 @@ func TestELFHeadersSorted(t *testing.T) {
696693
}
697694

698695
exe := filepath.Join(tmpdir, "x.exe")
699-
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-ldflags=-linkmode=internal", "-o", exe, src)
700-
cmd = testenv.CleanCmdEnv(cmd)
701-
cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1")
696+
cmd := goCmd(t, "build", "-ldflags=-linkmode=internal", "-o", exe, src)
702697
if out, err := cmd.CombinedOutput(); err != nil {
703698
t.Fatalf("build failed: %v, output:\n%s", err, out)
704699
}

0 commit comments

Comments
 (0)