Source file
src/cmd/link/linkbig_test.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "bytes"
13 "fmt"
14 "internal/buildcfg"
15 "internal/testenv"
16 "os"
17 "testing"
18 )
19
20 func TestLargeText(t *testing.T) {
21 if testing.Short() || (buildcfg.GOARCH != "ppc64le" && buildcfg.GOARCH != "ppc64" && buildcfg.GOARCH != "arm") {
22 t.Skipf("Skipping large text section test in short mode or on %s", buildcfg.GOARCH)
23 }
24 testenv.MustHaveGoBuild(t)
25
26 var w bytes.Buffer
27 const FN = 4
28 tmpdir := t.TempDir()
29
30 if err := os.WriteFile(tmpdir+"/go.mod", []byte("module big_test\n"), 0666); err != nil {
31 t.Fatal(err)
32 }
33
34
35
36
37
38
39 instOnArch := map[string]string{
40 "ppc64": "\tMOVD\tR0,R3\n",
41 "ppc64le": "\tMOVD\tR0,R3\n",
42 "arm": "\tMOVW\tR0,R1\n",
43 }
44 inst := instOnArch[buildcfg.GOARCH]
45 for j := 0; j < FN; j++ {
46 testname := fmt.Sprintf("bigfn%d", j)
47 fmt.Fprintf(&w, "TEXT ยท%s(SB),$0\n", testname)
48 for i := 0; i < 2200000; i++ {
49 fmt.Fprintf(&w, inst)
50 }
51 fmt.Fprintf(&w, "\tRET\n")
52 err := os.WriteFile(tmpdir+"/"+testname+".s", w.Bytes(), 0666)
53 if err != nil {
54 t.Fatalf("can't write output: %v\n", err)
55 }
56 w.Reset()
57 }
58 fmt.Fprintf(&w, "package main\n")
59 fmt.Fprintf(&w, "\nimport (\n")
60 fmt.Fprintf(&w, "\t\"os\"\n")
61 fmt.Fprintf(&w, "\t\"fmt\"\n")
62 fmt.Fprintf(&w, ")\n\n")
63
64 for i := 0; i < FN; i++ {
65 fmt.Fprintf(&w, "func bigfn%d()\n", i)
66 }
67 fmt.Fprintf(&w, "\nfunc main() {\n")
68
69
70
71
72 fmt.Fprintf(&w, "\tif os.Getenv(\"LINKTESTARG\") != \"\" {\n")
73 for i := 0; i < FN; i++ {
74 fmt.Fprintf(&w, "\t\tbigfn%d()\n", i)
75 }
76 fmt.Fprintf(&w, "\t}\n")
77 fmt.Fprintf(&w, "\tfmt.Printf(\"PASS\\n\")\n")
78 fmt.Fprintf(&w, "}")
79 err := os.WriteFile(tmpdir+"/bigfn.go", w.Bytes(), 0666)
80 if err != nil {
81 t.Fatalf("can't write output: %v\n", err)
82 }
83
84
85 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "bigtext")
86 cmd.Dir = tmpdir
87 out, err := cmd.CombinedOutput()
88 if err != nil {
89 t.Fatalf("Build failed for big text program with internal linking: %v, output: %s", err, out)
90 }
91 cmd = testenv.Command(t, "./bigtext")
92 cmd.Dir = tmpdir
93 out, err = cmd.CombinedOutput()
94 if err != nil {
95 t.Fatalf("Program built with internal linking failed to run with err %v, output: %s", err, out)
96 }
97
98
99 cmd = testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "-linkmode=external")
100 cmd.Dir = tmpdir
101 out, err = cmd.CombinedOutput()
102 if err != nil {
103 t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out)
104 }
105 cmd = testenv.Command(t, "./bigtext")
106 cmd.Dir = tmpdir
107 out, err = cmd.CombinedOutput()
108 if err != nil {
109 t.Fatalf("Program built with external linking failed to run with err %v, output: %s", err, out)
110 }
111 }
112
View as plain text