Issue created by Anthony Cole: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5828



**Summary**

libdl registers a loaded object file's `.eh_frame` section by passing the start 
of the section to `__register_frame()` (`rtems_rtl_elf_unwind_dw2_register()`, 
`cpukit/libdl/rtl-unwind-dw2.c`). That is what GCC's libgcc expects. LLVM's 
libunwind also provides `__register_frame()`, but there it registers a single 
FDE (`__unw_add_dynamic_fde()`); a section starts with a CIE, so nothing is 
registered, and a C++ exception thrown from loaded code finds no handler 
(`std::terminate()`). `__deregister_frame()` has the same mismatch.

LLVM's libunwind has interfaces that take a whole `.eh_frame` section, 
`__unw_add_dynamic_eh_frame_section()` and 
`__unw_remove_dynamic_eh_frame_section()` (declared in its 
`src/libunwind_ext.h`, not in its public header).

**How it shows**

`dl05` (a C++ exception thrown from loaded code) fails when RTEMS is built with 
clang and linked against LLVM's libunwind: "exception_dl: throwing..." and then 
exit code 1. It passes with GCC.

Not the same as #4978 ("dl05 fails when memory is not zeroed out"): that 
failure depends on the memory contents; this one happens every time with LLVM's 
libunwind, and not with GCC.

**Fix**

When RTEMS is linked against LLVM's libunwind, register and deregister the 
section with `__unw_add_dynamic_eh_frame_section()` and 
`__unw_remove_dynamic_eh_frame_section()` instead; with libgcc, nothing 
changes. With that, `dl05` passes; GCC builds are unaffected (checked on 
`riscv/rv64imafdc` with the RSB 7 toolchain).

The open question is how libdl should know which unwinder it is linked against. 
We tested `defined(__clang__) && __has_include(<libunwind.h>)`, which works for 
our toolchain but may not suit every clang-based one; a build option, or a 
configure-time check for `__unw_add_dynamic_eh_frame_section`, would be 
alternatives. Guidance on the preferred approach would be welcome; a merge 
request will follow.

This will matter for RTEMS's own LLVM toolchains, once the RSB builds them 
(rtems/tools/rtems-source-builder!308, "Support for building LLVM toolchains 
for RISC-V and SPARC"), if they use LLVM's libunwind.

**Environment**

RTEMS `main` as of 17bb6e147c (2026-11-19). Found with the Codasip SDK (clang 
19, LLVM libunwind) on RISC-V (RV64 and RV32), under QEMU and on FPGA; with the 
change, `dl05` passes on both.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5828
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-7y90j9q5as385u8nbqp993620-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to