binutils Newspaper
OCTOBER 6, 2026
loongarch — Proposed

LoongArch: Fix md_pcrel_from

Correctly calculates PC-relative offsets for LoongArch instructions.

This patch corrects the md_pcrel_from function for the LoongArch architecture. Previously, it incorrectly returned 0, which is not the correct base address for PC-relative calculations. The function is updated to return fixP->fx_frag->fr_address + fixP->fx_where, which accurately represents the start of the current instruction, ensuring correct PC-relative offset computation.

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

    Changes `md_pcrel_from` to return `fixP->fx_frag->fr_address + fixP->fx_where` to correctly calculate PC-relative offsets on LoongArch.

In Details

This patch modifies the LoongArch assembler configuration in gas/config/tc-loongarch.c by correcting the md_pcrel_from function. On LoongArch, PC-relative addressing is calculated from the start of the instruction itself. The previous implementation incorrectly returned 0, leading to miscalculated offsets. This change ensures that md_pcrel_from now returns the correct base address (fixP->fx_frag->fr_address + fixP->fx_where), which is essential for generating accurate PC-relative relocations and ensuring correct program behavior, particularly in position-independent code.

For Context
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.
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.
assembler
A program that translates assembly language into machine code. It also generates relocation information for the linker.
LoongArch
A RISC ISA developed by China's Loongson Technology.
fixP
A pointer to a 'fixup' structure, which holds information about a relocation to be applied.
fr_address
Likely refers to the base address of a fragment, a contiguous chunk of code or data within an object file.
fx_where
Likely indicates the offset within a fragment where the relocation needs to be applied.
Filed Under: loongarchassemblerrelocationspc-relative