LoongArch: Adjust addends of relocations with STT_SECTION symbol
For ld -r, when an input STT_SECTION symbol is redirected to the output
section symbol, elf_link_input_bfd() folds sec->output_offset into the
RELA addend. LoongArch relaxation does not update these relocation
addends, leaving the relocated target incorrect.
a.s:
.text
call .text
b.s:
.text
call .text
Before the fix, with:
as a.s -o a.o
as b.s -o b.o
ld -r a.o b.o -o ab.o
readelf -rW ab.o reported:
Relocation section .rela.text:
0000000000000000 R_LARCH_CALL36 .text + 0
0000000000000008 R_LARCH_CALL36 .text + 8
During relaxation, shrinking the first call36 sequence from 8 bytes to 4
bytes moves the second call PC from 0x8 to 0x4. Its addend must be
remapped from 0x8 to 0x4 as well. Otherwise the relocation still
targets offset 0x8, adding an extra 0x4 displacement to the second call.
The fix adds loongarch_relax_adjust_reloc_addends() to adjust relocation
addends according to the instructions deleted during relaxation. sec_flg1
marks sections that the STT_SECTION symbol of this section is referenced by
a relocation with a nonzero addend. Only flagged sections call this helper,
and it changes addends of relocations with STT_SECTION symbols.
bfd/
* elfnn-loongarch.c (loongarch_sec_sym_has_reloc_ref): Define.
(loongarch_elf_check_relocs): Mark sections symbol is referenced by
a relocation with a nonzero addend.
(loongarch_relax_adjust_reloc_addends): New function.
(loongarch_relax_perform_deletes): Return bool and adjust addends
of relocations with an STT_SECTION symbol.
(loongarch_elf_relax_section): Propagate failures from
loongarch_relax_perform_deletes.
ld/
* testsuite/ld-loongarch-elf/relax.exp: Run new tests.
* testsuite/ld-loongarch-elf/relax-stt-section-a.s: New file.
* testsuite/ld-loongarch-elf/relax-stt-section-b.s: New file.
* testsuite/ld-loongarch-elf/relax-stt-section-ld-r.d: New file.
* testsuite/ld-loongarch-elf/relax-stt-section-ld.d: New file.
* testsuite/ld-loongarch-elf/relax-stt-section.ld: New file.
7 files changed