On Saturday 26 September 2026 18:23:48 LIU Hao wrote:
> 在 2026-9-25 20:59, Pali Rohár 写道:
> > This completely differs from gcc output. If I understood the code
> > correctly the stack layout is:
> > 
> >    8 bytes return address
> >    8 bytes rbp
> >    8 bytes rsi
> >    1544 bytes for local variables
> >    <--- here points original %rsp (new %rbp+128)
> > 
> > 
> > 
> > So those 3 lines (context.Rip, context.Rsp, context.Rbp) are highly
> > dependent on the compiler version. The only thing which seems to be
> > correct is "context.Rip".
> > 
> > What we could do with it? Just to note that even current version of the
> > _invoke_watson in mingw-w64 git is broken.
> 
> `context.Rbp` should always be filled by `RtlVirtualUnwind()`.

All 3 fields in that my change are called only when RtlVirtualUnwind()
was not called at all. So this is just a fallback code.

IIRC, RtlVirtualUnwind() will all of those 3 fields (Rbp, Rip, Rsp), so
in my change they are not touched after the RtlVirtualUnwind() call.

> `context.Rip` is `__builtin_return_address(0)`; I think there's no need to
> call `__builtin_extract_return_addr()` for x86 (32 or 64).

Yes, __builtin_extract_return_addr() is identity function on x86.
But I'm always using it in any code, so the builtin call usage would be
correct on any platform (just in case somebody copies my usage, so it
would be correct on any platform).

> `context.Rsp` is the stack top of the caller frame so this should be
> `__builtin_dwarf_cfa()`. Despite the name, it doesn't seem to have anything
> to do with DWARF.

I did not know about __builtin_dwarf_cfa existence. Now I'm testing it.
Each compiler generates the code (the result is in the %rax register):


clang 16 with -O0:

        pushq   %rbp
        pushq   %rsi
        subq    $1544, %rsp
        leaq    128(%rsp), %rbp

        leaq    -112(%rbp), %rax
        movq    %rax, 296(%rbp)


clang 16 with -O2:

        pushq   %rbp
        pushq   %rsi
        subq    $1496, %rsp
        leaq    128(%rsp), %rbp

        leaq    -112(%rbp), %rcx
        movq    %rcx, 88(%rbp)

        (in this case it stores result to %rcx, not %rax)


gcc 16 with -O0:

        pushq   %rbp
        movq    %rsp, %rbp
        subq    $1520, %rsp

        leaq    16(%rbp), %rax
        movq    %rax, -1096(%rbp)


gcc 16 with -O2:

        pushq   %rbp
        movq    %rsp, %rbp
        pushq   %rdi
        pushq   %rbx
        subq    $1504, %rsp

        leaq    16(%rbp), %rax
        movq    %rax, -1096(%rbp)


gcc 8 with -O0:

  18:   55                      push   %rbp
  19:   57                      push   %rdi
  1a:   48 89 e5                mov    %rsp,%rbp
  1d:   48 81 ec f8 05 00 00    sub    $0x5f8,%rsp

 129:   48 8d 45 18             lea    0x18(%rbp),%rax
 12d:   48 89 85 a0 fb ff ff    mov    %rax,-0x460(%rbp)


gcc 8 with -O2:

   0:   55                      push   %rbp
   1:   57                      push   %rdi
   2:   48 89 e5                mov    %rsp,%rbp
   5:   48 81 ec e8 05 00 00    sub    $0x5e8,%rsp

 11f:   48 8d 45 18             lea    0x18(%rbp),%rax
 123:   48 89 85 c0 fb ff ff    mov    %rax,-0x440(%rbp)



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.


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

Reply via email to