github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/linkbig_test.go (about)

     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  	"github.com/gagliardetto/golang-go/cmd/internal/objabi"
    14  	"fmt"
    15  	"github.com/gagliardetto/golang-go/not-internal/testenv"
    16  	"io/ioutil"
    17  	"os"
    18  	"os/exec"
    19  	"testing"
    20  )
    21  
    22  func TestLargeText(t *testing.T) {
    23  	if testing.Short() || (objabi.GOARCH != "ppc64le" && objabi.GOARCH != "ppc64" && objabi.GOARCH != "arm") {
    24  		t.Skipf("Skipping large text section test in short mode or on %s", objabi.GOARCH)
    25  	}
    26  	testenv.MustHaveGoBuild(t)
    27  
    28  	var w bytes.Buffer
    29  	const FN = 4
    30  	tmpdir, err := ioutil.TempDir("", "bigtext")
    31  	if err != nil {
    32  		t.Fatalf("can't create temp directory: %v\n", err)
    33  	}
    34  
    35  	defer os.RemoveAll(tmpdir)
    36  
    37  	// Generate the scenario where the total amount of text exceeds the
    38  	// limit for the jmp/call instruction, on RISC architectures like ppc64le,
    39  	// which is 2^26.  When that happens the call requires special trampolines or
    40  	// long branches inserted by the linker where supported.
    41  	// Multiple .s files are generated instead of one.
    42  	instOnArch := map[string]string{
    43  		"ppc64":   "\tMOVD\tR0,R3\n",
    44  		"ppc64le": "\tMOVD\tR0,R3\n",
    45  		"arm":     "\tMOVW\tR0,R1\n",
    46  	}
    47  	inst := instOnArch[objabi.GOARCH]
    48  	for j := 0; j < FN; j++ {
    49  		testname := fmt.Sprintf("bigfn%d", j)
    50  		fmt.Fprintf(&w, "TEXT ยท%s(SB),$0\n", testname)
    51  		for i := 0; i < 2200000; i++ {
    52  			fmt.Fprintf(&w, inst)
    53  		}
    54  		fmt.Fprintf(&w, "\tRET\n")
    55  		err := ioutil.WriteFile(tmpdir+"/"+testname+".s", w.Bytes(), 0666)
    56  		if err != nil {
    57  			t.Fatalf("can't write output: %v\n", err)
    58  		}
    59  		w.Reset()
    60  	}
    61  	fmt.Fprintf(&w, "package main\n")
    62  	fmt.Fprintf(&w, "\nimport (\n")
    63  	fmt.Fprintf(&w, "\t\"os\"\n")
    64  	fmt.Fprintf(&w, "\t\"fmt\"\n")
    65  	fmt.Fprintf(&w, ")\n\n")
    66  
    67  	for i := 0; i < FN; i++ {
    68  		fmt.Fprintf(&w, "func bigfn%d()\n", i)
    69  	}
    70  	fmt.Fprintf(&w, "\nfunc main() {\n")
    71  
    72  	// There are lots of dummy code generated in the .s files just to generate a lot
    73  	// of text. Link them in but guard their call so their code is not executed but
    74  	// the main part of the program can be run.
    75  	fmt.Fprintf(&w, "\tif os.Getenv(\"LINKTESTARG\") != \"\" {\n")
    76  	for i := 0; i < FN; i++ {
    77  		fmt.Fprintf(&w, "\t\tbigfn%d()\n", i)
    78  	}
    79  	fmt.Fprintf(&w, "\t}\n")
    80  	fmt.Fprintf(&w, "\tfmt.Printf(\"PASS\\n\")\n")
    81  	fmt.Fprintf(&w, "}")
    82  	err = ioutil.WriteFile(tmpdir+"/bigfn.go", w.Bytes(), 0666)
    83  	if err != nil {
    84  		t.Fatalf("can't write output: %v\n", err)
    85  	}
    86  
    87  	// Build and run with internal linking.
    88  	os.Chdir(tmpdir)
    89  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext")
    90  	out, err := cmd.CombinedOutput()
    91  	if err != nil {
    92  		t.Fatalf("Build failed for big text program with internal linking: %v, output: %s", err, out)
    93  	}
    94  	cmd = exec.Command(tmpdir + "/bigtext")
    95  	out, err = cmd.CombinedOutput()
    96  	if err != nil {
    97  		t.Fatalf("Program built with internal linking failed to run with err %v, output: %s", err, out)
    98  	}
    99  
   100  	// Build and run with external linking
   101  	os.Chdir(tmpdir)
   102  	cmd = exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "'-linkmode=external'")
   103  	out, err = cmd.CombinedOutput()
   104  	if err != nil {
   105  		t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out)
   106  	}
   107  	cmd = exec.Command(tmpdir + "/bigtext")
   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  }