LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs
authorXi Ruoyao <xry111@xry111.site>
Tue, 20 Sep 2022 06:09:30 +0000 (14:09 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Tue, 20 Sep 2022 09:16:06 +0000 (17:16 +0800)
commitae2e4d4035f511543d12f74b3b7fdb1ba0daab16
treee4974a4c0504bcbc069b32a90f7bf814ce428552
parent6224a6c2ead26a04f0b2b8ccf4ff5b817afbb425
LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs

loongarch_elf_finish_dynamic_symbol is called after elf_link_sort_relocs
if -z combreloc.  elf_link_sort_relocs redistributes the contents of
.rela.* sections those would be merged into .rela.dyn, so the slot for
R_LARCH_IRELATIVE may be out of relplt->contents now.

To make things worse, the boundary check

    dyn < dyn + relplt->size / sizeof (*dyn)

is obviously wrong ("x + 10 < x"? :), causing the issue undetected
during the linking process and the resulted executable suddenly crashes
at runtime.

The issue was found during an attempt to add static-pie support to the
toolchain.

Fix it by iterating through the inputs of .rela.dyn to find the slot.
bfd/elfnn-loongarch.c
ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.s [new file with mode: 0644]