RISC-V: Fix that IRELATIVE relocs may be inserted to the wrong place.
authorNelson Chu <nelson.chu@sifive.com>
Wed, 7 Oct 2020 03:48:23 +0000 (20:48 -0700)
committerNelson Chu <nelson.chu@sifive.com>
Fri, 16 Oct 2020 02:11:23 +0000 (10:11 +0800)
commit51a8a7c2e3cc0730831963651a55d23d1fae624d
tree67583e907a2cadef4a983a2719a366e06bd2460a
parent02dd9d25682311f45e1bb9629cbe4f6727e245a8
RISC-V: Fix that IRELATIVE relocs may be inserted to the wrong place.

For the ifunc symbol, which is referenced by GOT rather than PLT relocs,
we should add the dynamic reloc (usually IRELATIVE) into the .rel.iplt
when generating the static executable.  But if we use riscv_elf_append_rela
to add the dynamic relocs into .rela.iplt, this may cause the overwrite
problem.

The reason is that we don't handle the `reloc_index` of .rela.iplt, but
the riscv_elf_append_rela adds the relocs to the place that are calculated
from the reloc_index (in seqential).  Therefore, we may overwrite the
dynamic relocs when the `reloc_index` of .rela.iplt isn't handled correctly.

One solution is that we can add these dynamic relocs (GOT ifunc) from
the last of .rela.iplt section.  But I'm not sure if it is the best way.

bfd/
* elfnn-riscv.c (riscv_elf_link_hash_table): Add last_iplt_index.
(riscv_elf_size_dynamic_sections): Initialize the last_iplt_index.
(riscv_elf_relocate_section): Use riscv_elf_append_rela.
(riscv_elf_finish_dynamic_symbol): If the use_elf_append_rela is
false, then we should add the dynamic relocs from the last of
the .rela.iplt, and don't use the riscv_elf_append_rela to add.

ld/
* testsuite/ld-riscv-elf/ifunc-plt-got-overwrite.s: New testcase.
* testsuite/ld-riscv-elf/ifunc-plt-got-overwrite.d: Likewise.
* testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-exe.rd: Likewise.
* testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-pic.rd: Likewise.
* testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-pie.rd: Likewise.
* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
bfd/ChangeLog
bfd/elfnn-riscv.c
ld/ChangeLog
ld/testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-exe.rd [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-pic.rd [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ifunc-plt-got-overwrite-pie.rd [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ifunc-plt-got-overwrite.d [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ifunc-plt-got-overwrite.s [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp