在 2026-11-10 22:37, Olivier Hainque 写道:
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?

From 112ebdfb1a65a66c7722af142509703ebb012da2 Mon Sep 17 00:00:00 2001
From: Olivier Hainque <[email protected]>
Date: Sat, 7 Nov 2026 11:40:37 +0000
Subject: [PATCH] Make cygwin.S chkstk functions for x86_64 more amenable to
 SEH

The purpose is to help backtraces on traps from stack checking
probes from chkstk_ms, in configurations doing SEH eh.

The 64bit Windows ports are either SEH or sjlj-eh, not dwarf2, and
seh directives are harmless for sjlj-eh, so this change removes the
cfi_ macro calls within cygwin.S for x86_64 and adds .seh directives
instead.

For __chkstk_ms, the code is slightly reworked
to use code sequences matching the prologue/epilogue patterns
documented as expected by the MS runtime.

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.
---
 libgcc/config/i386/cygwin.S | 41 +++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/libgcc/config/i386/cygwin.S b/libgcc/config/i386/cygwin.S
index d96578bdc45..528981d8858 100644
--- a/libgcc/config/i386/cygwin.S
+++ b/libgcc/config/i386/cygwin.S
@@ -29,6 +29,14 @@
        .cfi_sections   .debug_frame
 #endif
+/* Regarding call-frame info notes: 32bits Windows port are either DWARF2 or

                                                      ^^^ port[s]


+   sjlj-eh, with .cfi notes emitted or not, respectively, through calls to
+   the cfi_ macros exposed by i386-asm.h.  64bits Windows ports are either SEH
+   or sjlj-eh, with .seh directives emitted directly here, as they are harmless
+   for sjlj-eh and here is the only file where they are of potential use, for
+   example to help backtraces on memory faults triggered by the stack checking
+   probes.  */
+
 #ifdef L_chkstk
 /* Function prologue calls __chkstk to probe the stack when allocating more
    than CHECK_STACK_LIMIT bytes in one go.  Touching the stack at 4K
@@ -39,7 +47,7 @@
        .global __alloca
 #ifdef __x86_64__
 /* __alloca is a normal function call, which uses %rcx as the argument.  */
-       cfi_startproc()
+       .seh_proc __alloca
 __alloca:

Missing `.seh_endprologue` here; the prologue is empty.


        movq    %rcx, %rax
        /* FALLTHRU */
@@ -50,8 +58,6 @@ __alloca:
        .align  4
 ___chkstk:
        popq    %r11                    /* pop return address */
-       cfi_adjust_cfa_offset(-8)       /* indicate return address in r11 */
-       cfi_register(%rip, %r11)
        movq    %rsp, %r10
        cmpq    $0x1000, %rax           /* > 4k ?*/
        jb      2f
@@ -64,7 +70,6 @@ ___chkstk:
2: subq %rax, %r10
        movq    %rsp, %rax              /* hold CFA until return */
-       cfi_def_cfa_register(%rax)
        orl     $0x0, (%r10)            /* less than 4k, just peek here */
        movq    %r10, %rsp              /* decrement stack */
@@ -73,7 +78,7 @@ ___chkstk:
           used by most modern processors.  */
        pushq   %r11
        ret
-       cfi_endproc()
+       .seh_endproc
 #else
        cfi_startproc()
 ___chkstk:
@@ -112,12 +117,19 @@ __alloca:
    stack and does no stack allocation.  */
        .global ___chkstk_ms
 #ifdef __x86_64__
-       cfi_startproc()
+       .seh_proc ___chkstk_ms
 ___chkstk_ms:
-       pushq   %rcx                    /* save temps */
-       cfi_push(%rcx)
-       pushq   %rax
-       cfi_push(%rax)
+       /* prologue, using a code sequence matching the MS documentation
+          of expected patterns to allow SEH through;  No non-volatile reg
+          to push in our case, allocate space (for volatile temps we want
+          to preserve) with a plain sp move.  */
+       subq   $16, %rsp
+       .seh_stackalloc 16
+       .seh_endprologue
+
+       movq   %rax, (%rsp)
+       movq   %rcx, 8(%rsp)
+
        cmpq    $0x1000, %rax           /* > 4k ?*/
        leaq    24(%rsp), %rcx          /* point past return addr */
        jb      2f
@@ -131,12 +143,11 @@ ___chkstk_ms:
 2:     subq    %rax, %rcx
        orq     $0x0, (%rcx)            /* less than 4k, just peek here */
- popq %rax
-       cfi_pop(%rax)
-       popq    %rcx
-       cfi_pop(%rcx)
+       movq   8(%rsp), %rcx
+       movq   (%rsp), %rax
+       addq   $16, %rsp
        ret
-       cfi_endproc()
+       .seh_endproc
 #else
        cfi_startproc()
 ___chkstk_ms:
--
2.43.0


The other parts look OK, but I'm no GCC maintainer. I have added Jonathan and 
Eric in CC.




--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to