According to AAPCS64, when an argument requires 16-byte alignment and is passed on the stack, the stack address must be rounded up to the next 16-byte boundary. TCC previously failed to perform this alignment check in gen_va_arg, leading to data corruption when a 16-byte aligned argument followed an 8-byte argument on the stack. Note: This issue is most prominent on Mach-O/Apple Silicon where variadic arguments are passed entirely on the stack. Testing shows this fix resolves the failure on macOS while remaining compatible with Linux/ARM64 (e.g., Raspberry Pi). Modified gen_va_arg to perform a round-up (addr + 15) & ~15 when align == 16.
Unified · Split
| @@ -1355,6 +1355,10 @@ ST_FUNC void gen_va_arg(CType *t) · 1354 unmodified lines | |||
| 1355 | o(0x540000ad); // b.le .+20 | 1355 | o(0x540000ad); // b.le .+20 |
| 1356 | #endif | 1356 | #endif |
| 1357 | o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack | 1357 | o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack |
| 1358 | if (align == 16) { | ||
| 1359 | o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15 | ||
| 1360 | o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16 | ||
| 1361 | } | ||
| 1358 | o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n) | 1362 | o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n) |
| 1359 | o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack | 1363 | o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack |
| 1360 | #if !defined(TCC_TARGET_MACHO) | 1364 | #if !defined(TCC_TARGET_MACHO) |