On Wednesday 30 September 2026 19:28:03 LIU Hao wrote:
> 在 2026-9-30 18:01, Pali Rohár 写道:
> > What do you think that is a bug in clang?
> > The non-existence of __builtin_sponentry() for amd64?
> > Or that __builtin_dwarf_cfa() does not return SP at the time when
> > function was called?
> > 
> > I just have not found clang documentation for __builtin_dwarf_cfa() so
> > I'm really not sure what this builtin should do and whether it is buggy
> > or not.
> > 
> > Maybe Martin could help us?
> > 
> > Here is example also with __builtin_sponentry() and on arm64 it returns
> > different value than __builtin_dwarf_cfa():
> > 
> > https://godbolt.org/z/9r4aPWYWb
> 
> I meant `__builtin_dwarf_cfa()`: 
> https://github.com/llvm/llvm-project/issues/227606

I see, now I understand what you mean, thanks for detailed description in bug 
report.

> For x86-64, and maybe only for Clang, maybe one can take the address of the
> first parameter? If it does yield the home slot for the first argument, then
> it will be the CFA.

This looks like a good idea. First parameter would not work (as it is
passed via register), but fifth parameter could be usable (as this one
is passed via stack).

After the function was called, on the stack should be return address
(8 bytes) and before that should be shadow space of 32 bytes. And before
that should be 5th parameter.

I extended the example:

https://godbolt.org/z/xh4oYE5ov

and seems with -O2 it is working:

        pushq   %rbp
        pushq   %rsi
        movl    $5192, %eax
        callq   ___chkstk_ms
        subq    %rax, %rsp
        leaq    128(%rsp), %rbp
        ...
        leaq    5120(%rbp), %rcx
        callq   process_arg5_ptr

  5120+rbp = 5120+128-5192-8-8+rsp = rsp + 40

-O0 generates different code:

        pushq   %rbp
        movl    $5184, %eax
        callq   ___chkstk_ms
        subq    %rax, %rsp
        leaq    128(%rsp), %rbp
        ...
        leaq    5104(%rbp), %rcx
        callq   process_arg5_ptr

  5104+rbp = 5104+128-5184-8+rsp = rsp + 40


Function _invoke_watson() has 5 arguments, the 5th is "reserved".
So "(uintptr_t)&reserved - 32" should calculate the original rsp
before the return address was pushed on the stack.


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

Reply via email to