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

Gaier, Bjoern via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 10 22:28:45 PST 2020

Hello Tim,

Thank you! Both source files gave me a good idea about what is happening! That is really interesting!

Kind greetings

-----Original Message-----
From: Tim Northover <t.p.northover at gmail.com>
Sent: 10 February 2020 19:27
To: Gaier, Bjoern <Bjoern.Gaier at horiba.com>
Cc: Clang Dev <cfe-dev at lists.llvm.org>; llvm-dev at lists.llvm.org
Subject: Re: [cfe-dev] How do SJLJ-Exceptions works?

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.


Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika

More information about the cfe-dev mailing list