binutils Newspaper
OCTOBER 6, 2026
loongarch — Proposed

LoongArch: Optimize TC_FORCE_RELOCATION_SUB_LOCAL

Avoid ADD/SUB relocations by using PC-relative fixups when possible.

This patch optimizes relocation handling for LoongArch by changing fixups to be PC-relative when no linker-relaxable instructions are present between a subtraction symbol and the program counter. This aims to reduce the emission of ADD/SUB relocations, improving code generation efficiency.

In the Thread 1 participant
  1. mengqinggang <mengqinggang@loongson.cn> proposer

    Proposes to change fixups to PC-relative to avoid ADD/SUB relocations when linker-relaxable instructions are absent between a subtraction symbol and PC.

In Details

This patch modifies gas/config/tc-loongarch.c to optimize relocation generation for LoongArch. It refactors the loongarch_force_relocation_sub_local logic to conditionally use PC-relative fixups instead of ADD/SUB relocations when certain conditions related to linker relaxation and instruction segments are met. This change affects how the assembler handles symbolic arithmetic expressions and aims to reduce the overhead of specific relocation types.

For Context
relocation
Information embedded in an object file that describes how to modify symbolic addresses and constants when linking. It tells the linker which parts of the code or data need to be adjusted based on the final memory layout.
linker relaxation
A linker optimization that allows it to substitute shorter, faster instructions for longer ones if the target address is known to be close enough. This can involve changing instruction opcodes and relocation types.
PC-relative
A type of address calculation where the offset is relative to the current program counter (PC). This is common for position-independent code.
ADD/SUB relocations
Specific relocation types used by the assembler/linker to represent the result of an addition or subtraction between symbols or constants. These can sometimes be optimized away.
Filed Under: loongarchassemblerrelocationsoptimization