binutils Newspaper
NOVEMBER 30, 2026
loongarch — Proposed

LoongArch: Fix GOT allocation for TLS PCREL20 relocations

Fixes an issue in the LoongArch linker where certain TLS relocations were not allocated GOT entries, leading to link-time crashes.

This patch addresses a bug in the LoongArch ELF linker where specific Thread Local Storage (TLS) relocations (R_LARCH_TLS_LD/GD/DESC_PCREL20_S2) were not being allocated entries in the Global Offset Table (GOT). This omission causes the linker to crash during the final relocation phase due to unallocated GOT entries. The fix involves updating the loongarch_elf_check_relocs function to correctly record these references and includes updated test cases.

In Details

This patch targets the LoongArch architecture's linker script and relocation handling for Thread Local Storage (TLS). It specifically addresses PC-relative relocations using a 20-bit signed offset with a scale of 2 (PCREL20_S2) that involve TLS variables. The issue arises because the linker was not properly accounting for GOT (Global Offset Table) entries needed for these TLS accesses, leading to crashes when the GOT table became full or entries were unexpectedly missing during final linking. The fix ensures that these specific TLS relocations are recognized and allocated their necessary GOT…

For Context
LoongArch
A RISC-ISA architecture developed in China.
GOT
Global Offset Table. A table used by position-independent executable code to locate global variables and functions at runtime.
TLS
Thread Local Storage. A mechanism that allows each thread in a multi-threaded program to have its own copy of a variable.
PCREL20_S2
A type of relocation that uses a 20-bit signed PC-relative offset, scaled by 2. This is common for accessing data like TLS variables efficiently.
Relocation
The process of updating symbolic references in an object file to absolute addresses when linking. Relocations contain information about which parts of the code need to be modified and how.
Filed Under: loongarchlinkertlsrelocationsgot