Greetings Hao,

Ok, so I understand better now how the attempt to map seh to cfi_macros
was misguided, and I went for the approach you suggested, with .seh
directives
directly within cygwin.S.

Part of the initial motivation was to handle resms*.h headers
as well, but these are intended for "-mcall-ms2sysv-xlogues", explicitly
advertised as
incompatible with SEH, hence irrelevant. And it is clear that the
prologue/epilogue/seh
rules don't map well to the set of cfi macros anyway.

Keeping the code in ___chkstk_ms as it is and merely removing the
.seh_pushreg
operations for rax and rcx, leaving only startproc and endproc,
reintroduced backtrace
oddities.

I think the reason why the .seh_pushreg operations helped, albeit invalid,
is not the
info about rax and rcx per se, but the sp offsetting induced by the two
push operations.

Conveying that with a simple stack offset, as in

  push rax
  push rbx
  .seh_stackalloc 16

works on my couple of testcases; The pushes indeed result in a sp move,
which just happen to store register values along the way.

The doc is pretty clear and strict about the "supported" patterns, though,
and adjusting the code to perform a plain sp move for the prologue, as you
suggested, wasn't difficult; So the attached patch does that.

I'm not sure if/how __alloca/chkstk should be changed. It's not called
in the few tests I looked at so I'd rather not touch it beyond the most
basic
cfi/seh directives switch.

Calls to __builtin_alloca result in a call to chkstk_ms and sp adjustment
at the point of the call.

The revised patch "builds" and "tests" as intended in our setups.

Does that look good to you?

Thanks a lot for your feedback!

Cheers,

Olivier

--

 libgcc/
        * config/i386/cygwin.S (__alloca, __x86_64__): Remove cfi_ macro
        calls and add .seh start/endproc directives.
        (___chkstk_ms): Likewise. Also rework the code to match the
        expected patterns for prologue/epilogue sequences and mark the
        stack space allocation.



On Fri, 6 Nov 2026 at 18:18, Olivier Hainque <[email protected]> wrote:

> Hello Hao,
>
> Thanks for your suggestions. Making progress; Still experimenting and
> getting inconsistent results ...
>
> Best Regards,
> Olivier
>
> On Sat, 31 Oct 2026 at 14:38, LIU Hao <[email protected]> wrote:
>
>> 在 2026-10-29 02:54, Olivier Hainque 写道:
>> > I suppose it doesn't hurt to have seh directives regardless, so I'm
>> happy to simplify
>> > the change accordingly.
>> >
>> > I'll experiment with a patch v2 and follow up as soon as I have the
>> first testing results.
>>
>> I just took a look at the function and I think there's an issue in the
>> epilogue too. The function
>> currently does
>>
>>     push   rcx
>>     push   rax
>>     # end of prologue
>>
>>     # stack probing loop ...
>>
>>     # start of epilogue
>>     pop    rax
>>     pop    rcx
>>     ret
>>
>> Even if we do
>>
>>     push   rcx
>>     .seh_allocstack 8
>>     push   rax
>>     .seh_allocstack 8
>>     .seh_endprologue
>>
>>     # stack probing loop ...
>>
>>     # start of epilogue
>>     pop    rax
>>     pop    rcx
>>     ret
>>
>> The Microsoft documentation has strict rules about how an epilogue should
>> be implemented:
>>
>> https://learn.microsoft.com/en-us/cpp/build/prolog-and-epilog?view=msvc-170#epilog-code
>>
>> Specifically, two questions arise here (otherwise the unwinder won't be
>> able to find an epilogue):
>>
>> 1. What is the 'fixed-allocation-size' of this function? Is it required
>> to match
>>     the prologue? Is it 16? Since we aren't using a frame pointer, does
>> this mean
>>     that RSP can only be restored with `add rsp, 16`?
>>
>> 2. Is popping volatile registers allowed in an epilogue? (the doc seems
>> to say 'no'.)
>>
>>
>> As we are not allowed to clobber any registers in `__chkstk_ms`, these
>> might have to be changed to
>>
>>     sub    rsp, 16
>>     .seh_allocstack 16
>>     .seh_endprologue
>>     mov    [rsp], rax
>>     mov    [rsp + 8], rcx
>>
>>     # stack probing loop ...
>>
>>     mov    rax, [rsp]
>>     mov    rcx, [rsp + 8]
>>     # start of epilogue
>>     add    rsp, 16
>>     ret
>>
>>
>> Please test whether it has any impacts on your issue. Thanks.
>>
>>
>>
>>
>> --
>> Best regards,
>> LIU Hao
>>
>

Attachment: 0001-cygwin-chkstk-functions-for-x86_64-more-amenable-to-seh.patch
Description: Binary data

Reply via email to