Blame SOURCES/ppc64le-internal-linker-fix.patch

25ed44
diff --git a/src/cmd/go/testdata/script/trampoline_reuse_test.txt b/src/cmd/go/testdata/script/trampoline_reuse_test.txt
25ed44
new file mode 100644
25ed44
index 0000000000000..bca897c16d054
25ed44
--- /dev/null
25ed44
+++ b/src/cmd/go/testdata/script/trampoline_reuse_test.txt
25ed44
@@ -0,0 +1,100 @@
25ed44
+# Verify PPC64 does not reuse a trampoline which is too far away.
25ed44
+# This tests an edge case where the direct call relocation addend should
25ed44
+# be ignored when computing the distance from the direct call to the
25ed44
+# already placed trampoline
25ed44
+[short] skip
25ed44
+[!ppc64] [!ppc64le] skip
25ed44
+[aix] skip
25ed44
+
25ed44
+# Note, this program does not run. Presumably, 'DWORD $0' is simpler to
25ed44
+# assembly 2^26 or so times.
25ed44
+#
25ed44
+# We build something which should be laid out as such:
25ed44
+#
25ed44
+# bar.Bar
25ed44
+# main.Func1
25ed44
+# bar.Bar+400-tramp0
25ed44
+# main.BigAsm
25ed44
+# main.Func2
25ed44
+# bar.Bar+400-tramp1
25ed44
+#
25ed44
+# bar.Bar needs to be placed far enough away to generate relocations
25ed44
+# from main package calls. and main.Func1 and main.Func2 are placed
25ed44
+# a bit more than the direct call limit apart, but not more than 0x400
25ed44
+# bytes beyond it (to verify the reloc calc).
25ed44
+
25ed44
+go build
25ed44
+
25ed44
+-- go.mod --
25ed44
+
25ed44
+module foo
25ed44
+
25ed44
+go 1.19
25ed44
+
25ed44
+-- main.go --
25ed44
+
25ed44
+package main
25ed44
+
25ed44
+import "foo/bar"
25ed44
+
25ed44
+func Func1()
25ed44
+
25ed44
+func main() {
25ed44
+        Func1()
25ed44
+        bar.Bar2()
25ed44
+}
25ed44
+
25ed44
+-- foo.s --
25ed44
+
25ed44
+TEXT main·Func1(SB),0,$0-0
25ed44
+        CALL bar·Bar+0x400(SB)
25ed44
+        CALL main·BigAsm(SB)
25ed44
+// A trampoline will be placed here to bar.Bar
25ed44
+
25ed44
+// This creates a gap sufficiently large to prevent trampoline reuse
25ed44
+#define NOP64 DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0;
25ed44
+#define NOP256 NOP64 NOP64 NOP64 NOP64
25ed44
+#define NOP2S10 NOP256 NOP256 NOP256 NOP256
25ed44
+#define NOP2S12 NOP2S10 NOP2S10 NOP2S10 NOP2S10
25ed44
+#define NOP2S14 NOP2S12 NOP2S12 NOP2S12 NOP2S12
25ed44
+#define NOP2S16 NOP2S14 NOP2S14 NOP2S14 NOP2S14
25ed44
+#define NOP2S18 NOP2S16 NOP2S16 NOP2S16 NOP2S16
25ed44
+#define NOP2S20 NOP2S18 NOP2S18 NOP2S18 NOP2S18
25ed44
+#define NOP2S22 NOP2S20 NOP2S20 NOP2S20 NOP2S20
25ed44
+#define NOP2S24 NOP2S22 NOP2S22 NOP2S22 NOP2S22
25ed44
+#define BIGNOP NOP2S24 NOP2S24
25ed44
+TEXT main·BigAsm(SB),0,$0-0
25ed44
+        // Fill to the direct call limit so Func2 must generate a new trampoline.
25ed44
+        // As the implicit trampoline above is just barely unreachable.
25ed44
+        BIGNOP
25ed44
+        MOVD $main·Func2(SB), R3
25ed44
+
25ed44
+TEXT main·Func2(SB),0,$0-0
25ed44
+        CALL bar·Bar+0x400(SB)
25ed44
+// Another trampoline should be placed here.
25ed44
+
25ed44
+-- bar/bar.s --
25ed44
+
25ed44
+#define NOP64 DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0; DWORD $0;
25ed44
+#define NOP256 NOP64 NOP64 NOP64 NOP64
25ed44
+#define NOP2S10 NOP256 NOP256 NOP256 NOP256
25ed44
+#define NOP2S12 NOP2S10 NOP2S10 NOP2S10 NOP2S10
25ed44
+#define NOP2S14 NOP2S12 NOP2S12 NOP2S12 NOP2S12
25ed44
+#define NOP2S16 NOP2S14 NOP2S14 NOP2S14 NOP2S14
25ed44
+#define NOP2S18 NOP2S16 NOP2S16 NOP2S16 NOP2S16
25ed44
+#define NOP2S20 NOP2S18 NOP2S18 NOP2S18 NOP2S18
25ed44
+#define NOP2S22 NOP2S20 NOP2S20 NOP2S20 NOP2S20
25ed44
+#define NOP2S24 NOP2S22 NOP2S22 NOP2S22 NOP2S22
25ed44
+#define BIGNOP NOP2S24 NOP2S24 NOP2S10
25ed44
+// A very big not very interesting function.
25ed44
+TEXT bar·Bar(SB),0,$0-0
25ed44
+        BIGNOP
25ed44
+
25ed44
+-- bar/bar.go --
25ed44
+
25ed44
+package bar
25ed44
+
25ed44
+func Bar()
25ed44
+
25ed44
+func Bar2() {
25ed44
+}
25ed44
diff --git a/src/cmd/link/internal/ppc64/asm.go b/src/cmd/link/internal/ppc64/asm.go
25ed44
index 5d5fbe2a97735..6313879da083c 100644
25ed44
--- a/src/cmd/link/internal/ppc64/asm.go
25ed44
+++ b/src/cmd/link/internal/ppc64/asm.go
25ed44
@@ -900,8 +900,9 @@ func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
25ed44
 				if ldr.SymValue(tramp) == 0 {
25ed44
 					break
25ed44
 				}
25ed44
-
25ed44
-				t = ldr.SymValue(tramp) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
25ed44
+				// Note, the trampoline is always called directly. The addend of the original relocation is accounted for in the
25ed44
+				// trampoline itself.
25ed44
+				t = ldr.SymValue(tramp) - (ldr.SymValue(s) + int64(r.Off()))
25ed44
 
25ed44
 				// With internal linking, the trampoline can be used if it is not too far.
25ed44
 				// With external linking, the trampoline must be in this section for it to be reused.