4.3 The unwinder: how _Unwind_RaiseException finds a catch
Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).
You port a C++ program to an aarch64-none-elf board. A C file, filter.c,
calls back into C++, and the callback throws. The catch around the call into
filter.c never runs, and the program stops in std::terminate. On x86-64
GNU/Linux the same code catches the exception. Compile filter.c with
-fexceptions and the board catches it too. The C file has no try and no
throw, and you did not change it.
A throw is two walks up the stack. libgcc does both walks, from one table per
frame that the compiler wrote. A frame with no table ends the walk, as if the
stack ended there. The x86-64 compiler writes a table for every C function by
default. The aarch64-none-elf compiler does not.
This chapter assumes Chapter 1.7, which named the
stack unwinder as part of libgcc, and Chapter 4.1,
which showed why the compiler emits calls into libgcc.
Chapter 4.2 explains the libgcc_eh.a
and libgcc_s.so artifacts that hold the unwinder.
Three parties to a throw: __cxa_throw, _Unwind_* and the personality routine
The C++ front end compiles throw into two library calls:
__cxa_allocate_exception for the object, then __cxa_throw
(gcc/cp/except.cc:568, :669). Both are in libsupc++,
the language-support part of libstdc++. __cxa_throw fills in an exception
header and gives it to libgcc:
#ifdef __USING_SJLJ_EXCEPTIONS__
_Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
#else
_Unwind_RaiseException (&header->exc.unwindHeader);
#endif
// Some sort of unwinding error. Note that terminate is a handler.
__cxa_begin_catch (&header->exc.unwindHeader);
std::terminate ();
Listing 4.3-1: the end of __cxa_throw
(libstdc++-v3/libsupc++/eh_throw.cc:90-98).
_Unwind_RaiseException returns only when it fails. A successful throw
continues in a landing pad in some other frame. Every failure ends in
std::terminate.
Three parties do the work, in two libraries:
| Party | What it knows | Where it is |
|---|---|---|
__cxa_throw, __cxa_begin_catch | the C++ exception object | libsupc++, in libstdc++ |
_Unwind_RaiseException, _Unwind_Resume | how to find a caller's frame from a table | libgcc |
| the personality routine | which catch clauses and cleanups a frame has | the language's runtime |
GCC did not invent this split. The Itanium C++ ABI exception-handling
specification defines it: "the method of locating and restoring
previous stack frames is language independent", and "the validity of a given
handler" is language dependent. libgcc's driver "is derived from the C++ ABI for
IA-64" (libgcc/unwind.inc:25-28). That document and the
Arm EHABI are the real specifications. The GCC manuals do not
restate them.
What the unwinder reads: .eh_frame, the CIE and the LSDA
For each function, the compiler emits .cfi_startproc, .cfi_endproc and CFI
directives between them. The assembler turns them into a frame description entry
(FDE) in .eh_frame. An FDE tells the unwinder, for any PC in the function, where
the caller's frame is and where the caller's registers were saved. This chapter
does not decode those rules.
Each FDE belongs to a common information entry (CIE). The CIE's augmentation
string can contain P and L. P names a personality routine. L says that
each FDE points to a language-specific data area (LSDA)
(libgcc/unwind-dw2.c:472-496, :1044-1050). The
compiler writes these as .cfi_personality and .cfi_lsda, only for functions
that need them (gcc/dwarf2out.cc:977-1039). The LSDA goes
into .gcc_except_table (gcc/except.cc:2983-2990). It is a
table of call sites. Each entry gives a range of code addresses, the landing pad
for an exception that passes through that range, and the catch types.
Now the failure in the opening. For each frame, the unwinder looks up the FDE that covers the return address:
fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
&context->bases);
if (fde == NULL)
{
#ifdef MD_FALLBACK_FRAME_STATE_FOR
/* Couldn't find frame unwind info for this function. Try a
target-specific fallback mechanism. This will necessarily
not provide a personality routine or LSDA. */
return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
#else
return _URC_END_OF_STACK;
#endif
}
Listing 4.3-2: a frame with no FDE, in uw_frame_state_for
(libgcc/unwind-dw2.c:1008-1020).
The fallback does not rescue an ordinary function. On x86-64 GNU/Linux it
recognises the signal-return trampoline and returns _URC_END_OF_STACK for
anything else (libgcc/config/i386/linux-unwind.h:41-67).
A frame without an FDE looks to the unwinder like the end of the stack. The
search stops, _Unwind_RaiseException returns, and Listing 4.3-1 calls
std::terminate.
The libstdc++ manual documents this result in the "With C" part of its Exceptions page. It does not say when the compiler writes the tables.
When the compiler writes .eh_frame: -fexceptions, -funwind-tables, -fasynchronous-unwind-tables
The GCC manual documents the three options under Code Gen Options.
-fexceptions generates the code and data that propagate exceptions.
-funwind-tables generates only the static data. -fasynchronous-unwind-tables
makes the table exact at every instruction, not only at calls. The manual also
says that -fexceptions is on by default for C++ and off for C. The front end
does that in one line, opts->x_flag_exceptions = c_dialect_cxx ();
(gcc/c-family/c-opts.cc:225).
One function decides whether a translation unit gets .eh_frame:
bool
dwarf2out_do_eh_frame (void)
{
return
(flag_unwind_tables || flag_exceptions)
&& targetm_common.except_unwind_info (&global_options) == UI_DWARF2;
}
Listing 4.3-3: the .eh_frame decision
(gcc/dwarf2cfi.cc:3703-3709).
-fasynchronous-unwind-tables works through flag_unwind_tables, because
toplev.cc sets that flag whenever the asynchronous one is on. The same lines
turn the asynchronous flag on for -fnon-call-exceptions
(gcc/toplev.cc:1376-1379).
So a C function gets an FDE only if the target sets one of those flags. The manual does not say which targets do. The source does:
- x86. The option starts at a "not set" value of 2
(
gcc/common/config/i386/i386-common.cc:2075). For 64-bit code, the option-override hook turns 2 into both flags (gcc/config/i386/i386-options.cc:1913-1915). For 32-bit code, it depends on the frame-pointer default (:1924-1925). - AArch64 and RISC-V. Both flags are on when
TARGET_DEFAULT_ASYNC_UNWIND_TABLESis 1 (aarch64-common.cc:63-66,riscv-common.cc:2530-2533).gcc/config.gccdefines it for AArch64 FreeBSD, GNU/Linux and Hurd (:1231,:1243,:1272) and for RISC-V GNU/Linux (:2530). The bare-metal-elftargets do not get it. - 32-bit Arm EABI. There are no tables unless you give
-fexceptionsor-funwind-tables(gcc/common/config/arm/arm-common.cc:49-77).
-g does not help. With debug information and no unwind tables, GCC emits
.cfi_sections .debug_frame (gcc/dwarf2out.cc:29852-29855), so
the CFI goes to .debug_frame. A debugger reads that section. The unwinder does
not: crtbegin.o registers the section named by __LIBGCC_EH_FRAME_SECTION_NAME__
(gcc/c-family/c-cppbuiltin.cc:1515-1517), which is .eh_frame
by default (gcc/defaults.h:365-366).
You can watch the decision on any installed compiler. Use C, and no -g:
$ gcc -S -o - frame.c | grep '\.cfi_'
$ gcc -S -fno-asynchronous-unwind-tables -o - frame.c | grep '\.cfi_'
$ gcc -c frame.c && readelf --debug-dump=frames frame.o
$ gcc -S -fexceptions -o - cleanup.c | grep -E '\.cfi_(personality|lsda)'
On x86-64 the source predicts .cfi_startproc lines from the first command and
none from the second. The third shows the CIE and FDEs that the assembler built.
For the fourth, give cleanup.c a variable with the cleanup attribute and a
call in its scope. The source predicts a .cfi_personality that names
__gcc_personality_v0. These commands were not run for this book.
Finding the table at run time: _Unwind_Find_FDE and __register_frame_info
The unwinder must also find .eh_frame in memory. libgcc has two ways.
With glibc and a linker that builds .eh_frame_hdr
(libgcc/unwind-dw2-fde-dip.c:49-52), libgcc asks the C library
where each loaded object's PT_GNU_EH_FRAME segment is. It tries registered
objects first, then glibc's _dl_find_object if it exists, then
dl_iterate_phdr (:535-574). That is the interface with binutils
and the C library, and the book stops there. libgcc/config.host selects this
file for *-*-linux* through t-eh-dw2-dip (config.host:314,
t-eh-dw2-dip).
Elsewhere, crtbegin.o registers the program's .eh_frame at startup. A
-static link on GNU/Linux takes this path too. The spec passes
--eh-frame-hdr only without -static (gcc/config/gnu-user.h:109),
and the static start file, crtbeginT.o, is built with CRTSTUFFT_O
(libgcc/Makefile.in:1054), which
excludes it from every USE_PT_GNU_EH_FRAME block
(crtstuff.c:88-132). So it registers the frames itself.
crtstuff.c defines USE_EH_FRAME_REGISTRY when it cannot use
PT_GNU_EH_FRAME (crtstuff.c:139-141). crtbegin.o puts a
label at the start of .eh_frame (:260-267), crtend.o
puts a zero terminator at the end (:653-669), and a constructor
gives the label to libgcc:
static void __attribute__((used))
frame_dummy (void)
{
#ifdef USE_EH_FRAME_REGISTRY
static struct object object;
...
if (__register_frame_info)
__register_frame_info (__EH_FRAME_BEGIN__, &object);
...
#endif /* USE_EH_FRAME_REGISTRY */
Listing 4.3-4: frame_dummy, abridged (libgcc/crtstuff.c:473-488).
The omitted branch passes two base addresses to __register_frame_info_bases.
Two details matter on a board. The references to __register_frame_info are
weak (crtstuff.c:180-189), so crtbegin.o does not pull the
unwinder into a C program. And the receiving function names its caller:
"Called from crtbegin.o to register the unwind info for an object"
(unwind-dw2-fde.c:123-154). An aarch64-none-elf libgcc has
no t-eh-dw2-dip (config.host:405-414), so registration is
its only path. -nostartfiles drops crtbegin.o
(Chapter 1.10). Unless your startup code
then calls __register_frame_info itself, every throw ends in
std::terminate.
Two walks up the stack: phase 1 and phase 2 in unwind.inc
_Unwind_RaiseException is in libgcc/unwind.inc, which unwind-dw2.c
includes (unwind-dw2.c:1483). Its first loop is the search phase:
/* Phase 1: Search. Unwind the stack, calling the personality routine
with the _UA_SEARCH_PHASE flag set. Do not modify the stack yet. */
while (1)
{
_Unwind_FrameState fs;
/* Set up fs to describe the FDE for the caller of cur_context. The
first time through the loop, that means __cxa_throw. */
code = uw_frame_state_for (&cur_context, &fs);
if (code == _URC_END_OF_STACK)
/* Hit end of stack with no handler found. */
return _URC_END_OF_STACK;
if (code != _URC_NO_REASON)
/* Some error encountered. Usually the unwinder doesn't
diagnose these and merely crashes. */
return _URC_FATAL_PHASE1_ERROR;
/* Unwind successful. Run the personality routine, if any. */
if (fs.personality)
{
code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class,
exc, &cur_context);
if (code == _URC_HANDLER_FOUND)
break;
else if (code != _URC_CONTINUE_UNWIND)
return _URC_FATAL_PHASE1_ERROR;
}
/* Update cur_context to describe the same frame as fs. */
uw_update_context (&cur_context, &fs);
}
Listing 4.3-5: phase 1 (libgcc/unwind.inc:96-128).
Each turn finds the caller's FDE through Listing 4.3-2, calls the CIE's
personality routine with _UA_SEARCH_PHASE, and moves cur_context one frame
up. cur_context is a copy, so no register changes and no destructor runs. A
frame with no personality routine, such as a C function with tables but without
-fexceptions, is walked through without a call.
When a personality routine answers _URC_HANDLER_FOUND, _Unwind_RaiseException
records that frame's canonical frame address in private_2, starts again from
the throw point, and runs _Unwind_RaiseException_Phase2
(unwind.inc:130-140). Phase 2 walks the same frames with
_UA_CLEANUP_PHASE, and adds _UA_HANDLER_FRAME at the frame that phase 1
found (:52-65). When a personality routine returns
_URC_INSTALL_CONTEXT, the loop stops (:66-67), and
uw_install_context loads that frame's registers and jumps to its landing pad.
The Itanium ABI states the reason for two walks: if the search phase reports
failure, the framework calls terminate() rather than start phase 2. libgcc
follows it. Phase 1 returns _URC_END_OF_STACK before phase 2 has touched a
frame. An exception that nothing catches runs no destructor. std::terminate
runs with the throwing frame still on the stack, so a debugger stopped in it shows
__cxa_throw and the function that threw.
The personality routine: __gxx_personality_v0
The unwinder knows nothing about C++ types. The compiler names the routine that
does, as __<lang>_personality plus a suffix for the unwinding model: _v0 for
tables, _sj0 for setjmp/longjmp and _seh0 for Windows SEH
(gcc/expr.cc:14122-14149). The C++ front end passes gxx
(gcc/cp/cp-lang.cc:156-165). C keeps the default hook
(gcc/langhooks-def.h:147), which passes gcc
(gcc/tree.cc:12379-12386). The suffix is deliberate: "Using a
different personality function name causes link failures when trying to mix code
using different exception handling models"
(eh_personality.cc:336-345).
The routine reads the frame's LSDA and looks up the PC in its call-site table. Then it answers according to the phase:
do_something:
if (found_type == found_nothing)
CONTINUE_UNWINDING;
if (actions & _UA_SEARCH_PHASE)
{
if (found_type == found_cleanup)
CONTINUE_UNWINDING;
// For domestic exceptions, we cache data from phase 1 for phase 2.
if (!foreign_exception)
{
save_caught_exception(ue_header, context, thrown_ptr,
handler_switch_value, language_specific_data,
landing_pad, action_record);
}
return _URC_HANDLER_FOUND;
}
Listing 4.3-6: the phase test in __gxx_personality_v0
(libstdc++-v3/libsupc++/eh_personality.cc:649-665).
In phase 1 a frame with only cleanups (destructors) answers "continue". Only a
matching catch finds a handler. In phase 2 the routine loads the
exception pointer and the clause's selector into two registers, sets the PC to
the landing pad, and returns _URC_INSTALL_CONTEXT (:726-737).
A frame with no LSDA is passed through (:449-451). A frame
whose PC is not in its call-site table gets std::terminate, "for a destructor
inside a cleanup, or a library routine the compiler was not expecting to throw"
(:522-526). The compiler makes that case on purpose: a
must-not-throw region gets no call-site entry
(gcc/except.cc:176).
Cleanups end in _Unwind_Resume
A landing pad for a cleanup runs the destructors and must then give the exception
back to the unwinder. The compiler adds that call when it lowers the end of the
cleanup region: "When we don't have a destination region, this exception escapes
up the call chain. We resolve this by generating a call to the _Unwind_Resume
library function" (gcc/tree-eh.cc:3485-3522). The built-in is
named _Unwind_Resume, or _Unwind_SjLj_Resume for setjmp/longjmp
unwinding (gcc/tree.cc:10166-10171). The callee is in
unwind.inc:
/* Choose between continuing to process _Unwind_RaiseException
or _Unwind_ForcedUnwind. */
if (exc->private_1 == 0)
code = _Unwind_RaiseException_Phase2 (exc, &cur_context, &frames);
else
code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
Listing 4.3-7: the core of _Unwind_Resume
(libgcc/unwind.inc:239-244). Its comment says it is "not to
implement rethrowing" (:226-227).
It does not search again. Phase 1 already found the handler, so _Unwind_Resume
continues phase 2 from the cleanup's frame toward the frame in private_2. So
on a target with .eh_frame, a C++ object file with a destructor on an
exception path refers to _Unwind_Resume even if it never throws.
Why the unwinder is in libgcc: __gcc_personality_v0 and libgcc_eh.a
Only the personality routine knows C++. Everything in Listings 4.3-2, 4.3-5 and 4.3-7 is language independent, so it can live below every language runtime, and C can use it too. libgcc has its own small personality routine for C:
/* Currently we only support cleanups for C. */
if ((actions & _UA_CLEANUP_PHASE) == 0)
CONTINUE_UNWINDING;
Listing 4.3-8: __gcc_personality_v0 ignores phase 1
(libgcc/unwind-c.c:148-150).
C has no catch, so in phase 1 every C frame says "continue". In phase 2 the
routine runs the landing pads that the cleanup variable attribute creates. The
GCC manual documents that attribute under Common Variable Attributes:
with -fexceptions, the cleanup function runs during unwinding, and the
attribute cannot catch the exception. A PC that is not in a C function's table
means "no cleanup", not std::terminate. A GCC comment marks that difference
from C++ (unwind-c.c:218-221).
The build puts all of this in one group of sources:
# Additional sources to handle exceptions; overridden by targets as needed.
LIB2ADDEH = $(srcdir)/unwind-dw2.c $(srcdir)/unwind-dw2-fde.c \
$(srcdir)/unwind-sjlj.c $(srcdir)/unwind-c.c
LIB2ADDEHSTATIC = $(LIB2ADDEH)
LIB2ADDEHSHARED = $(LIB2ADDEH)
Listing 4.3-9: the unwinder sources (libgcc/Makefile.in:346-350).
They are compiled with -fexceptions (Makefile.in:871). With a
shared libgcc they go into libgcc_eh.a and libgcc_s.so
(:873-879). Without one, they go into libgcc.a
(:882-887), and a target with no shared-library link command has
no shared libgcc (:392-397). Chapter 4.2 explains how the driver
picks between them.
The Arm EHABI: .ARM.exidx, .cantunwind and __aeabi_unwind_cpp_pr0
32-bit Arm EABI targets do not use .eh_frame for exceptions. bpabi.h sets
ARM_UNWIND_INFO to 1 (gcc/config/arm/bpabi.h:32-34). The
target hook then returns UI_TARGET, or UI_NONE when neither -fexceptions
nor -funwind-tables is on (arm-common.cc:58-67).
UI_TARGET is not UI_DWARF2, so Listing 4.3-3 is false. The compiler emits
.fnstart and .fnend around each function instead. It adds .cantunwind for
a function that cannot throw, unless -funwind-tables is on or the function has
an LSDA (gcc/config/arm/arm.cc:30464-30483). The assembler builds
the index table .ARM.exidx from these directives:
$ arm-none-eabi-gcc -S -o - frame.c | grep -E '\.(fnstart|fnend|cantunwind)'
$ arm-none-eabi-gcc -S -fexceptions -o - frame.c | grep -E '\.(fnstart|fnend|cantunwind)'
The source predicts no lines from the first command, because the hook returns
UI_NONE. These commands were not run for this book.
The model differs from the Itanium one in three places.
The index is found through two linker symbols. libgcc declares
__gnu_Unwind_Find_exidx weak and calls it if the link defines it
(libgcc/unwind-arm-common.inc:69-70, :272-275).
Otherwise it searches from __exidx_start to __exidx_end
(:302-307). The file declares both as extern
(:128-130), so something in the link, such as the linker script,
must define them. An entry marked EXIDX_CANTUNWIND ends the search with
_URC_END_OF_STACK (:339-363).
The personality routine unwinds its own frame. The unwinder loop calls the
personality routine with _US_VIRTUAL_UNWIND_FRAME until the routine stops
answering "continue" (unwind-arm-common.inc:585-633). There is no
uw_update_context step. The personality routine unwinds the frame itself, so in
libstdc++ "continue" means "unwind this frame, then continue":
#define CONTINUE_UNWINDING \
do \
{ \
if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
return _URC_FAILURE; \
return _URC_CONTINUE_UNWIND; \
} \
while (0)
Listing 4.3-10: CONTINUE_UNWINDING for the EHABI
(libstdc++-v3/libsupc++/eh_personality.cc:193-200). The
generic version is one return (:317).
The EHABI also defines three personality routines that a table entry can select
by index. libgcc provides them as __aeabi_unwind_cpp_pr0, pr1 and pr2
(libgcc/config/arm/unwind-arm.c:497-523), and the index lookup is at
unwind-arm-common.inc:384-418. They reach the C++ runtime only
through weak references such as __cxa_type_match (:63-67).
A C++ function that needs a full LSDA names __gxx_personality_v0 instead.
A C++ cleanup ends in __cxa_end_cleanup. With the EHABI unwinder, the
compiler declares a __cxa_end_cleanup built-in
(gcc/tree.cc:10155-10163) and calls it, with no argument,
at the end of a C++ cleanup (gcc/tree-eh.cc:3489-3495). The EHABI
requires it: "A cleanup must return control to the unwinding code by tail
calling __cxa_end_cleanup". libsupc++'s version does its housekeeping and then
calls _Unwind_Resume (eh_arm.cc:215-223), which on Arm is an
assembly wrapper around __gnu_Unwind_Resume
(libgcc/config/arm/libunwind.S:314-360).
libgcc/config/arm/t-bpabi replaces LIB2ADDEH with these Arm files and
unwind-c.c (t-bpabi:10-12). arm*-*-eabi* uses that fragment
(libgcc/config.host:578-595).
Exceptions with no operating system: what -fexceptions costs
On a target with no operating system, a C++ program that throws carries five parts:
- Tables.
.eh_frame, or.ARM.exidxand its entries, for each function on a throw path, and.gcc_except_tablefor each function with a cleanup or acatch. The GCC manual warns, under-fexceptions, that this "can produce significant data size overhead". - The unwinder. The
LIB2ADDEHobjects, fromlibgcc.aon a target with no shared libgcc. - libsupc++.
__cxa_throw, the personality routine, type matching andstd::terminate. - An allocator.
__cxa_allocate_exceptioncallsmallocfirst, and uses an emergency pool only ifmallocfails (libstdc++-v3/libsupc++/eh_alloc.cc:393-403). The pool itself comes frommallocat startup, unless libstdc++ was configured with a static pool (eh_alloc.cc:45-47). The libstdc++ manual documents the pool and its two configure options on its Configure page. - A terminate handler. Its default depends on how libstdc++ was built:
/* We default to the talkative, informative handler in a normal hosted
library. This pulls in the demangler, the dyn-string utilities, and
elements of the I/O library. For a low-memory environment, you can return
to the earlier "silent death" handler by configuring GCC with
--disable-libstdcxx-verbose and rebuilding the library.
In a freestanding environment, we default to this latter approach. */
#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE && __cpp_exceptions
# define _GLIBCXX_DEFAULT_TERM_HANDLER __gnu_cxx::__verbose_terminate_handler
#else
# include <cstdlib>
# define _GLIBCXX_DEFAULT_TERM_HANDLER std::abort
#endif
Listing 4.3-11: the default terminate handler
(libstdc++-v3/libsupc++/eh_term_handler.h:27-39).
_GLIBCXX_HOSTED describes how libstdc++ was configured, not your board. Every
throw reads the handler (eh_throw.cc:70), so the first
throw links the default handler, and in a hosted libstdc++ that brings the
demangler.
So -fno-exceptions is standard practice in embedded C++. It removes items 1 to
5 from your own code. cc1plus rejects throw and catch with "exception
handling disabled, use -fexceptions to enable" (gcc/cp/except.cc:395).
Listing 4.3-3 is false for a C++ file too. The libstdc++ manual documents what
the flag does to the library's headers, under "Doing without" on its
Exceptions page.
-fno-exceptions changes your code, not the libstdc++ you link. The library
reports errors through functions such as __throw_bad_alloc
(libstdc++-v3/src/c++11/functexcept.cc:52-54), and whether
those throw or call abort was decided when libstdc++ itself was compiled:
#ifndef _GLIBCXX_THROW_OR_ABORT
# if __cpp_exceptions
# define _GLIBCXX_THROW_OR_ABORT(_EXC) (throw (_EXC))
# else
# define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort(), (void)(_EXC))
# endif
#endif
Listing 4.3-12: _GLIBCXX_THROW_OR_ABORT
(libstdc++-v3/include/bits/c++config:260-266).
A libstdc++ compiled with the C++ default of -fexceptions still throws from
inside the library. Your frames, compiled with -fno-exceptions, have no LSDA,
and on Arm they may have no table at all. Nothing catches the exception, and
Listing 4.3-1 calls std::terminate.
That is the whole path of a throw with no handler. std::terminate calls the
handler and then std::abort, and it calls std::abort if the handler throws
(eh_terminate.cc:43-59). The handler is the verbose one or
std::abort itself (Listing 4.3-11). abort belongs to the C library or to your
board support code, and what it does on your board is outside GCC. A handler
that you install with std::set_terminate is the only part of this path that
your code controls.
Three things to carry away
A throw walks the stack twice. Phase 1 searches without changing anything,
and phase 2 runs cleanups and jumps to the handler. No handler means
std::terminate before any destructor runs.
A frame without unwind tables is the end of the stack. Whether a C function has tables depends on the target's defaults, not on the language.
The unwinder is libgcc's and language independent, and the personality routine
is the language's. That split lets C code take part in C++ unwinding, and it is
why the unwinder ships in libgcc_eh.a and libgcc_s.so, not in libstdc++.
Things that surprise people
On a bare-metal DWARF target, crtbegin.o is what makes exceptions work. With
-nostartfiles, register .eh_frame yourself or every throw terminates.
On arm-none-eabi, 64-bit division can bring the unwinder into a C program.
libgcc compiles its division functions with -fexceptions
(libgcc/config/arm/t-bpabi:17-20). A GCC comment states the
result: "printf will depend on udivmoddi4, which will depend on the exception
unwind routines" (gcc/config/arm/unknown-elf.h:91-96).
Documentation coverage
The GCC manual documents -fexceptions (gcc/doc/invoke.texi:19714),
-fnon-call-exceptions (:19728), -funwind-tables (:19748) and
-fasynchronous-unwind-tables (:19755) under Code Gen Options,
and the cleanup attribute (gcc/doc/extend.texi:7217) under
Common Variable Attributes. The libstdc++ manual's
Exceptions page (libstdc++-v3/doc/xml/manual/using_exceptions.xml)
covers the emergency pool, -fno-exceptions and C code, and its
Configure page (configure.xml:462-478) covers the pool options.
The internals manual lists the unwinder's entry points under
Exception handling routines (gcc/doc/libgcc.texi:2305-2339),
and the target macros under Exception Handling
(gcc/doc/tm.texi:3500) and Exception Region Output
(tm.texi:10018), with TARGET_EXCEPT_UNWIND_INFO at :10067 and
TARGET_ARM_EABI_UNWINDER at :10176. The real specifications are the
Itanium C++ ABI and the Arm EHABI.
What is not documented:
- What the unwinder's entry points do. The internals manual lists their names under the text "document me!".
- Which targets turn on
-fasynchronous-unwind-tablesby default, and so which targets give C functions tables. - That CFI from
-galone goes to.debug_frameand does not help an exception. - That a frame without an FDE ends the search. The libstdc++ manual states the result for C, not the mechanism.
- The two ways libgcc finds
.eh_frame, and that bare-metal targets depend oncrtbegin.oto register it. - The personality routine names, and that C has its own, which runs cleanups only.
- That
-fno-exceptionsdoes not change the prebuilt libstdc++.
Next: why a program built against one libstdc++ runs against a newer one, and
what __cxx11 is doing in your link errors.
All source references in this chapter are to GCC 15.2.0
(releases/gcc-15.2.0). Line numbers in other releases will differ; the
surrounding code rarely does. Where behaviour itself changed across a major
version, it is flagged inline.