binutils Newspaper
NOVEMBER 30, 2026
risc-v — Proposed

RISC-V: free PC-relative relocation state on IFUNC errors

Fixes a memory leak in RISC-V relocation handling by ensuring PC-relative state is freed on IFUNC error paths.

This patch corrects a memory leak in the RISC-V relocation function riscv_elf_relocate_section. Previously, on error paths related to STT_GNU_IFUNC symbols, the function would exit without freeing allocated PC-relative relocation state. The fix introduces a goto out; statement to ensure this state is properly deallocated before returning, preventing memory leaks.

In the Thread 1 participant
  1. Harshit Kumar <harshit7kr@gmail.com> proposer

    Corrects a memory leak in `riscv_elf_relocate_section` by adding a `goto out;` to free PC-relative relocation state on IFUNC error paths.

    “The RISC-V relocation function allocates PC-relative relocation state at entry. On error paths for STT_GNU_IFUNC symbols, the function previously returned false directly without freeing this state. Branch to the existing out label to free the PC-relative relocation state before exiting.”

In Details

This patch addresses a resource management issue in the RISC-V relocation code within bfd/elfnn-riscv.c. When handling STT_GNU_IFUNC symbols, certain error conditions caused the function riscv_elf_relocate_section to return prematurely without releasing dynamically allocated PC-relative relocation state. The fix ensures that control flow now correctly jumps to the out label, which contains the deallocation logic, thus preventing memory leaks.

For Context
STT_GNU_IFUNC
A symbol type used in ELF files to represent indirect functions, allowing for runtime selection of function implementations. Binutils needs to handle these specially during linking.
PC-relative relocation
A type of relocation where the target address is calculated relative to the current Program Counter (PC). This is common for function calls and global data access in position-independent code.
memory leak
A situation where a program allocates memory but fails to release it when it's no longer needed, leading to a gradual depletion of available memory over time.
Filed Under: risc-vmemory leakrelocationifunc