FreeBSD/powerpc64 ELFv2: Fix TOC restore during unwinding
Corrects stack unwinding on FreeBSD/powerpc64 ELFv2 by properly restoring the TOC pointer.
This change fixes a bug in libgcc’s unwinding mechanisms on FreeBSD for powerpc64 ELFv2 systems. The Table of Contents (TOC) pointer, essential for accessing global data and PLT entries, was not being restored correctly during stack unwinding when the unwind information didn’t explicitly detail the TOC save slot. The previous implementation incorrectly assumed an ELFv1 TOC save offset, leading to crashes or incorrect behavior in C++ exception handling and function calls across module boundaries. The fix introduces a macro to correctly determine the TOC save slot based on the ELF version.
In Details
On powerpc64, frame unwinders must restore the TOC pointer (r2). When unwind info lacks explicit TOC save details (common for PLT stubs), frob_update_context in libgcc/config/rs6000/freebsd-unwind.h scans the code stream. The FreeBSD implementation hardcoded the ELFv1 TOC save slot (offset 40) into its checks. However, FreeBSD/powerpc64 uses ELFv2, where the TOC is saved at offset 24. This commit defines TOC_SAVE_SLOT based on _CALL_ELF (24 for ELFv2, 40 otherwise) and uses it, preventing incorrect TOC restoration. It also guards ELFv1-specific code-reading logic with _CALL_ELF != 2.
- TOC (Table of Contents)
- A special register (r2 on powerpc64) that holds the base address for accessing global variables and dynamic symbols, particularly in Position-Independent Code (PIC).
- Stack Unwinding
- The process of walking up the call stack during exception handling or debugging to find a suitable handler or frame. This involves restoring register state from each stack frame.
- PLT (Procedure Linkage Table)
- A table used in dynamically linked executables to indirectly call functions in shared libraries. It resolves function addresses at runtime.
- ELFv1/ELFv2
- Different versions of the Executable and Linkable Format, an object file format. Powerpc64 systems can use either, with significant differences in ABI and conventions, like TOC layout.