...

Source file src/cmd/link/linkbig_test.go

Documentation: cmd/link

     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  	"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  	// Generate the scenario where the total amount of text exceeds the
    35  	// limit for the jmp/call instruction, on RISC architectures like ppc64le,
    36  	// which is 2^26.  When that happens the call requires special trampolines or
    37  	// long branches inserted by the linker where supported.
    38  	// Multiple .s files are generated instead of one.
    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  	// There are lots of dummy code generated in the .s files just to generate a lot
    70  	// of text. Link them in but guard their call so their code is not executed but
    71  	// the main part of the program can be run.
    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  	// Build and run with internal linking.
    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  	// Build and run with external linking
    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