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