binutils Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
risc-v

RISC-V: Update static TLS handling in elfnn-riscv.c

Refactors RISC-V TLS type recording for better code organization.

The handling of STATIC_TLS in the RISC-V ELF backend has been moved to a dedicated function, riscv_elf_record_tls_type. This change improves code clarity and organization within bfd/elfnn-riscv.c. Additionally, an early return was added for the R_RISCV_TPREL_HI20 case, and logic was introduced to set the DF_STATIC_TLS flag when GOT_TLS_IE is encountered in a dynamically linked executable.

In Details

The riscv_elf_record_tls_type function in bfd/elfnn-riscv.c now centralizes the logic for determining and recording Thread-Local Storage (TLS) types for RISC-V ELF objects. Previously, some of this logic was interleaved with relocation checks. This refactoring aims to streamline the process and ensure correct setting of the DF_STATIC_TLS flag when TLS Initial Exec (IE) model is used in dynamically linked executables, aligning with ELF specifications.

For Context
STATIC_TLS
Static Thread-Local Storage refers to variables with storage duration that is static but local to each thread. The compiler and linker must generate code to ensure each thread accesses its own copy of these variables.
TLS Initial Exec (IE)
A model for accessing Thread-Local Storage where the address of the TLS block is determined at runtime. This is generally efficient but requires runtime support.
DF_STATIC_TLS
A flag in the ELF header's e_flags field that indicates the presence of static TLS in the executable, signaling to the runtime linker that it needs to handle TLS setup.
GOT_TLS_IE
A flag indicating that a symbol reference uses the TLS Initial Exec model, typically involving a Global Offset Table (GOT) entry to resolve the TLS address at runtime.
Filed Under: risc-vtlslinkerelf