.seh_endproc having stack semantics

Julian Waters tanksherman27@gmail.com
Tue Jun 13 17:13:06 GMT 2023


Hi all,

I have on my end, an almost working version of a Structured Exception
Handler that mimics the VC compiler:

#define WIN32_TRY \
        asm ("1:" "\n" \
             "\t" ".seh_handler __C_specific_handler, @except" "\n" \
    "\t" ".seh_handlerdata" "\n" \
    "\t" ".long 1" "\n" \
    "\t" ".rva 1b, 2f, 3f, 4f" "\n" \
    "\t" ".seh_code");

#define WIN32_EXCEPT(code, ...) \
        asm ("nop" "\n" \
             "\t" "2: nop" "\n" \
             "\t" "jmp 5f" "\n" \
             "\t" "3:" "\n" \
             "\t" "push rbp" "\n" \
             "\t" "mov rbp, rsp" "\n"); \
        volatile long filter = code; \
        asm ("\t" "mov eax, DWORD PTR -8[rbp]" "\n" \
             "\t" "pop rbp" "\n" \
             "\t" "ret" "\n" \
             "\t" "4:"); \
        __VA_ARGS__ \
asm ("5:");

However, it has a major flaw: It replaces the structured handler for the
function it is in, which means it can only be used once in any given
function, and wrecks any regular C++ exception handling that is also in the
same function. I believe this can be helped by allowing .seh_endproc to
have stack semantics, so an .seh_proc .seh_endproc pair can be nested
inside another, allowing the assembly programmer to create disposable
functions with labels for exception handlers, while right now doing so will
result in unmatched pair errors. Is there a way I could PR a change for
this somehow?

best regards.
Julian


More information about the Binutils mailing list