Source file src/cmd/link/linkbig_test.go

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This program generates a test to verify that a program can be
     6  // successfully linked even when there are very large text
     7  // sections present.
     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  	// Generate the scenario where the total amount of text exceeds the
    36  	// limit for the jmp/call instruction, on RISC architectures like ppc64le,
    37  	// which is 2^26.  When that happens the call requires special trampolines or
    38  	// long branches inserted by the linker where supported.
    39  	// Multiple .s files are generated instead of one.
    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  	// There are lots of dummy code generated in the .s files just to generate a lot
    71  	// of text. Link them in but guard their call so their code is not executed but
    72  	// the main part of the program can be run.
    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  	// Build and run with internal linking.
    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  	// Build and run with external linking
   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