LoongArch: Fix relaxation alignment with ld -r (PR 33236)
LoongArch linker now handles alignment correctly when performing relocations with `ld -r`.
This commit fixes a relaxation alignment issue on LoongArch when using ld -r, addressing PR 33236. The linker previously failed to adjust section alignments for objects generated with ld -r, leading to potential misalignments if preceding sections were relaxed. The solution involves adding align sections and relocations before each section and setting section alignment to 4 to disable default start address calculation.
In Details
The LoongArch linker's behavior with ld -r is corrected to match the fix for RISC-V in PR 33236. The changes in bfd/elfnn-loongarch.c introduce loongarch_build_align_nops to insert nop padding for alignment. ld/emultempl/loongarchelf.em and related test files are updated to ensure that when ld -r is used, an alignment section and an align relocation are inserted before each section. Additionally, the default section alignment is set to 4, preventing the linker from making assumptions about section start addresses that could lead to misalignment after relaxation.
- LoongArch
- A RISC-based instruction set architecture developed in China.
- linker relaxation
- A linker optimization technique where the linker adjusts instruction offsets for branches and calls after the initial pass, potentially improving code density or enabling features like Position-Independent Code (PIC).
- ld -r
- A linker option that produces a relocatable output file, merging multiple input object files into a single one while preserving relocation information.
- section alignment
- The requirement that a section in an object file or executable must start at a memory address that is a multiple of a specific value (e.g., 4 or 8 bytes) for performance or hardware reasons.
- PR 33236
- Problem Report 33236, a bug filed against Binutils concerning linker relaxation alignment issues on RISC-V and now LoongArch.