]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: add a test for trampoline insertion
authorCherry Zhang <cherryyz@google.com>
Fri, 10 Apr 2020 18:58:54 +0000 (14:58 -0400)
committerCherry Zhang <cherryyz@google.com>
Thu, 16 Apr 2020 14:40:57 +0000 (14:40 +0000)
Now that we have converted pclntab pass to using the loader,
trampoline insertion should work again. Add a test.

Change-Id: Ia9a0485456ac75cc6e706218a359f109cd8fce43
Reviewed-on: https://go-review.googlesource.com/c/go/+/228141
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/link/link_test.go

index e665737cc38b4ae5f49030babffd5c4e2e5d2048..f374e9322fd4687e2973b741888f61748bbb1b32 100644 (file)
@@ -628,3 +628,51 @@ func TestFuncAlign(t *testing.T) {
                t.Errorf("unexpected output: %s\n", out)
        }
 }
+
+const helloSrc = `
+package main
+import "fmt"
+func main() { fmt.Println("hello") }
+`
+
+func TestTrampoline(t *testing.T) {
+       // Test that trampoline insertion works as expected.
+       // For stress test, we set -debugtramp=2 flag, which sets a very low
+       // threshold for trampoline generation, and essentially all cross-package
+       // calls will use trampolines.
+       switch runtime.GOARCH {
+       case "arm", "ppc64", "ppc64le":
+       default:
+               t.Skipf("trampoline insertion is not implemented on %s", runtime.GOARCH)
+       }
+
+       testenv.MustHaveGoBuild(t)
+
+       tmpdir, err := ioutil.TempDir("", "TestTrampoline")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(tmpdir)
+
+       src := filepath.Join(tmpdir, "hello.go")
+       err = ioutil.WriteFile(src, []byte(helloSrc), 0666)
+       if err != nil {
+               t.Fatal(err)
+       }
+       exe := filepath.Join(tmpdir, "hello.exe")
+
+       // Build and run with old object file format.
+       cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-debugtramp=2", "-o", exe, src)
+       out, err := cmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("build failed: %v\n%s", err, out)
+       }
+       cmd = exec.Command(exe)
+       out, err = cmd.CombinedOutput()
+       if err != nil {
+               t.Errorf("executable failed to run: %v\n%s", err, out)
+       }
+       if string(out) != "hello\n" {
+               t.Errorf("unexpected output:\n%s", out)
+       }
+}