在 2026-9-27 03:40, Pali Rohár 写道:
So for both gcc versions, it seems that __builtin_dwarf_cfa() really
returns the original %rsp value (before the caller issued the "call"
instruction). It is 16 bytes (gcc 16; rbp + return address) or
0x18/24 bytes (gcc 8; rbp + rdi + return address).


But clang's __builtin_dwarf_cfa() returns pointer into some local
variable on the stack. It does not return the original %rsp value.
And the offset is not even constant, depends on -O<value>.
For -O0 it is 1512 bytes + 8 bytes (return address).
For -O2 it is 1464 bytes + 8 bytes (return address).


So for gcc, this __builtin_dwarf_cfa() seems to be usable. But for clang
we need something different.

I played a bit more with clang 23 on some smaller example.
Seems that clang does not provide any usable builtin for rsp.

https://godbolt.org/z/c5rff4bEx

On arm there is __builtin_sponentry() but it is not available on amd64.

This looks like a bug in Clang.

DeepSeek:
On **Windows CFI (SEH) targets** — which your `x86_64-w64-windows-gnu` is — LLVM expands 
`__builtin_dwarf_cfa()` into **`frameaddress(0) + 2*SlotSize`**; but the semantics it assigns to 
`frameaddress(0)` is **not "the frame pointer RBP", it is "the SP at the end of the 
prologue"**. The constant 16 is designed for *CFA = RBP + 16*; combined with a base that is 
actually RSP, the result is only correct by accident when the function allocates no stack frame 
(RSP == RBP). As soon as there is a `sub rsp, N`, the returned value is one frame allocation too 
low and points into the function's own stack frame.






--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to