binutils Newspaper
NOVEMBER 30, 2026
risc-v — Proposed

RISC-V: Don't partially relax TLS LE address sequences

Fixes RISC-V linker bug where incorrect relaxation of TLS LE address sequences could lead to incorrect program behavior.

This patch addresses a bug in the RISC-V linker (ld) related to Thread-Local Storage (TLS) LE address sequences. When certain relocation types (R_RISCV_TPREL_HI20/ADD) are marked for relaxation, but their corresponding R_RISCV_TPREL_LO12_I/S consumers are not, the linker incorrectly deletes instructions. This can revive a dead incoming value, leading to program errors. The fix ensures that such producer instructions are only deleted if all their LO12 consumers are also relaxable.

In the Thread 1 participant
  1. Shivam Gupta proposer

    Submits a patch to prevent the linker from incorrectly relaxing TLS LE address sequences on RISC-V architectures, fixing a bug where instruction deletion could lead to incorrect program behavior.

    “When R_RISCV_TPREL_HI20 / R_RISCV_TPREL_ADD are marked with R_RISCV_RELAX but a R_RISCV_TPREL_LO12_I/S for the same symbol and addend is not, ld deleted the lui/add while leaving the consumer using its original base register, reviving a dead incoming value. Only delete the producers when every TPREL_LO12 consumer of the same symbol+addend in the section is itself relaxable.”

In Details

This patch for the RISC-V GNU ld linker corrects a bug in TLS LE (Local Exec) relaxation. The relaxation process can remove lui and add instructions that establish a base register for accessing TLS variables. The bug occurs when a R_RISCV_TPREL_HI20 or R_RISCV_TPREL_ADD relocation is marked relaxable, but its corresponding R_RISCV_TPREL_LO12_I or R_RISCV_TPREL_LO12_S consumer is not. The linker would incorrectly remove the producer instructions, leaving the consumer to use a stale register value, thus breaking TLS access. The fix adds a check to ensure all consumers of the base re…

Filed Under: risc-vlinkertlsrelocation