binutils Newspaper
JULY 29, 2026
loongarch Proposed

LoongArch: Emit alignment relocations after relaxed instructions

Ensures LoongArch alignment relocations are emitted only after relaxed instructions, mimicking LLVM behavior.

This patch modifies the LoongArch gas configuration to ensure alignment relocations are emitted only after relaxed instructions. It prevents the creation of alignment fragments if the current section does not already contain relaxed instructions, aligning the behavior with LLVM’s approach to similar issues.

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

    Submits a patch to LoongArch gas configuration to emit alignment relocations only after relaxed instructions, based on LLVM's handling.

    “Alignment cannot change before relaxed instructions. Emit alignment relocations only after them. Similar with https://github.com/llvm/llvm-project/pull/150816.”

In Details

This patch for LoongArch's gas (GNU Assembler) addresses the placement of alignment relocations in the context of linker relaxation. Previously, alignment padding might have been inserted or accounted for before any 'relaxed' instructions. This change modifies the loongarch_frag_align_code function to ensure that alignment relocations (R_LARCH_ALIGN*) are only generated after sequences of relaxed instructions have been emitted. This is done by checking a flag (sec_flg1) on the current section to see if it already contains relaxed instructions, preventing premature alignment adjustments…

For Context
LoongArch
A RISC-V-based instruction set architecture developed by Loongson Technology.
alignment relocations
Relocation entries generated by the assembler to ensure that code or data is aligned on specific memory boundaries, which is crucial for performance and correctness on some architectures.
relaxed instructions
Instructions whose final encoding or address in the object file can change after the initial assembly pass, often due to linker relaxation.
gas
The GNU Assembler, part of the binutils package, responsible for converting assembly code into machine code.
linker relaxation
A process where the linker adjusts addresses and offsets of code and data after initial assembly, often to optimize for space or speed, especially in position-independent code.
LLVM
A collection of modular and reusable compiler and toolchain technologies. It provides a front-end for reading in languages and optimizing code, and back-ends for generating machine code for various architectures.
Filed Under: loongarchbinutilsassemblerlinkeroptimization