Home | History | Annotate | Download | only in 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 	"cmd/internal/objabi"
     14 	"fmt"
     15 	"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 
     32 	defer os.RemoveAll(tmpdir)
     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[objabi.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 := ioutil.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 = ioutil.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 	os.Chdir(tmpdir)
     86 	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext")
     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 = exec.Command(tmpdir + "/bigtext")
     92 	out, err = cmd.CombinedOutput()
     93 	if err != nil {
     94 		t.Fatalf("Program built with internal linking failed to run with err %v, output: %s", err, out)
     95 	}
     96 
     97 	// Build and run with external linking
     98 	os.Chdir(tmpdir)
     99 	cmd = exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "'-linkmode=external'")
    100 	out, err = cmd.CombinedOutput()
    101 	if err != nil {
    102 		t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out)
    103 	}
    104 	cmd = exec.Command(tmpdir + "/bigtext")
    105 	out, err = cmd.CombinedOutput()
    106 	if err != nil {
    107 		t.Fatalf("Program built with external linking failed to run with err %v, output: %s", err, out)
    108 	}
    109 }
    110