binutils Newspaper
OCTOBER 6, 2026
risc-v — Proposed

RISC-V: Unify TLS handling in check_relocs.

Consolidate TLS relocation checks in RISC-V ELF object files.

This patch unifies the handling of various Thread-Local Storage (TLS) relocation types within the check_relocs function for RISC-V ELF files. By merging switch cases for different TLS relocations (GD, GOT, TLSDESC, TPREL), the code becomes more maintainable and reduces redundancy. This change simplifies the logic for validating TLS references and ensures consistent behavior across different TLS access models.

In Details

This patch refactors bfd/elfnn-riscv.c by consolidating multiple case statements within riscv_elf_check_relocs that previously handled different TLS relocation types (R_RISCV_TLS_GD_HI20, R_RISCV_TLS_GOT_HI20, etc.). A new helper function, riscv_elf_tls_type_from_hi_reloc, is introduced to map these relocation types to internal TLS type constants (e.g., GOT_TLS_GD). The unified code path simplifies the logic and allows for common checks and actions related to TLS relocations, improving code clarity and maintainability. It also ensures that the DF_STATIC_TLS flag is correctly set w…

For Context
TLS
Thread-Local Storage. A memory model where each thread in a process gets its own instance of a variable.
GOT
Global Offset Table. A table used by dynamically linked executables to store addresses of global symbols, allowing indirect access.
Relocation
Information in an object file that specifies how to update addresses or other values when the code or data is loaded into memory or linked.
R_RISCV_TLS_GD_HI20
A RISC-V relocation type for the high 20 bits of a Global Dynamic TLS access.
R_RISCV_TLS_GOT_HI20
A RISC-V relocation type for the high 20 bits of a GOT-relative TLS access.
R_RISCV_TLSDESC_HI20
A RISC-V relocation type for the high 20 bits of a TLSDESC access.
R_RISCV_TPREL_HI20
A RISC-V relocation type for the high 20 bits of a Thread Pointer Relative TLS access.
GOT_TLS_GD
Global Dynamic TLS: A TLS access mode where the GOT is used to access TLS variables that are globally visible.
GOT_TLS_IE
Global Initial Exec TLS: A TLS access mode where the GOT is used for TLS variables accessed from the initial executable.
GOT_TLSDESC
TLSDESC TLS: A TLS access mode using a descriptor table for accessing TLS variables.
Filed Under: risc-vtlslinkerrelocation