[cfe-dev] How do SJLJ-Exceptions works?

Tim Northover via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 10 10:26:47 PST 2020


Hi Bjoern,

On Mon, 10 Feb 2020 at 07:30, Gaier, Bjoern via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
> When I execute a function with a try-catch segment, as soon as I hit the try statement (Pretending I'm a Debugger or so), I would save the registerts (RSP, RIP of the catch block) on the stack I guess and keep doing what I do. However, when I encounter an exception now - how do I know where to find my stored registers on the stack?

It's a bit more dependent on the runtime than that. You can use
-fsjlj-exceptions with Clang to see what it would actually do to code
even on platforms that normally use DWARF based exceptions (obviously
just a tool to inspect assembly, you shouldn't try to run the result).

Roughly speaking, try blocks result in a call to some function like
_Unwind_SjLj_Register that stashes the information needed to find the
right catch block and/or call needed destructors in a reasonably
generic fashion. It looks like libunwind's implementation makes those
frame-contexts into a linked-list accessed (essentially) via a global
variable.

The key files are lib/CodeGen/SjLjEHPrepare.cpp in LLVM and
src/Unwind-sjlj.c in libunwind.

> Also is that the mysterious 'stack unwinding' I often heard about?

More or less. The process libunwind goes through to look through all
these registered frames and restore needed state is called unwinding.

Cheers.

Tim.


More information about the cfe-dev mailing list