From 50b287a65051b38a37d5cb2d647a4a25aa9c1f3b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 24 Nov 2026 13:06:31 +0900 Subject: [PATCH] internal/fakecgo: align the stack for C calls on ppc64le ELFv2 requires R1 to be 16-byte aligned at a call, but Go frames on ppc64le are only 8-byte aligned and call5 only subtracted a constant 32 from R1. The C function could therefore run on a misaligned stack, where stvx and lvx silently drop the low address bits of vector stack slots. Align R1 down to 16 bytes below the Go caller's frame, and keep the original R1 in R22, which C preserves. R14 is not used for this because rt0_go keeps its stack pointer there across _cgo_init. Store the back chain before moving R1 so that 0(R1) is valid at any point, and keep the 32-byte adjustment visible to the assembler so that the nosplit stack accounting does not change. Co-Authored-By: Claude Opus 5.5 --- internal/fakecgo/trampolines_ppc64le.s | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/fakecgo/trampolines_ppc64le.s b/internal/fakecgo/trampolines_ppc64le.s index aae2363b..2a9546bf 100644 --- a/internal/fakecgo/trampolines_ppc64le.s +++ b/internal/fakecgo/trampolines_ppc64le.s @@ -112,17 +112,22 @@ TEXT ·call5(SB), NOSPLIT|NOFRAME, $0-56 MOVD 64(R1), R6 // a4 → fourth C arg MOVD 72(R1), R7 // a5 → fifth C arg - MOVDU R1, -32(R1) + // Go frames are only 8-byte aligned, but ELFv2 requires R1 to be 16-byte + // aligned at a call. Keep the original R1 in R22, a non-volatile register. + // The back chain is stored before R1 moves so that 0(R1) is always valid. + MOVD R1, R22 + RLDCR $0, R1, $~15, R10 + MOVD R22, -32(R10) + SUB $32, R10, R1 MOVD R12, CTR CALL CTR - // Store return value - // After MOVDU -32, original 80(R1) is now at 80+32=112(R1) - MOVD R3, (80+32)(R1) - // Deallocate frame - ADD $32, R1 + MOVD R22, R1 + + // Store return value + MOVD R3, 80(R1) // Clear R0 before returning to Go code. // Go uses R0 as a constant 0 register for things like "std r0,X(r1)"