LoongArch: Adjust addends for relaxed section-symbol relocations
Fixes incorrect relocation targets after code relaxation on LoongArch by adjusting addends.
This patch introduces a fix for incorrect relocation targets on LoongArch when using ld -r with relaxed section-symbol relocations. Previously, the ELF linker would fold the section offset into the relocation addend, but LoongArch’s relaxation process did not update these addends, leading to incorrect targets after code shrinking. The fix adds a new function loongarch_relax_adjust_reloc_addends to correct these addends based on deleted instructions during relaxation, specifically for sections with local STT_SECTION relocations and non-zero addends.
In Details
This change addresses an issue in ld -r on LoongArch where elf_link_input_bfd incorrectly folds sec->output_offset into relocation addends for input STT_SECTION symbols. The relaxation pass in bfd/elfnn-loongarch.c does not adjust these addends, causing incorrect jump targets after code shrinking. The fix introduces loongarch_relax_adjust_reloc_addends to re-evaluate and correct these addends based on instruction deletions.
- ld -r
- A linker command-line option that performs a partial link, combining multiple object files into a single output file without resolving external symbols. This is often used for creating relocatable object files or libraries.
- STT_SECTION
- A symbol type in the ELF file format that refers to a section. In this context, it's used for relocations related to section symbols.
- Relocation Addend
- A value stored within a relocation entry that is added to the address of the symbol or the relocation itself to determine the final address. In this case, it's being incorrectly handled after relaxation.
- Relaxation
- A process in linkers and assemblers where instructions are optimized or modified after initial assembly, often to shrink code size or adapt to specific hardware features. This can involve changing instruction lengths and addresses.