[ELF][RISCV] Resolve branch relocations referencing undefined weak to current locatio...
authorFangrui Song <i@maskray.me>
Thu, 10 Jun 2021 20:25:16 +0000 (13:25 -0700)
committerFangrui Song <i@maskray.me>
Thu, 10 Jun 2021 20:25:16 +0000 (13:25 -0700)
commitc03b6305d8419fda84a67f4fe357b69a86e4b54f
tree638ffc93c437d642422c3eb8e3b6fd0cc1bc3ca0
parentaaaeb4b160fe94e0ad3bcd6073eea4807f84a33a
[ELF][RISCV] Resolve branch relocations referencing undefined weak to current location if not using PLT

In a -no-pie link we optimize R_PLT_PC to R_PC. Currently we resolve a branch
relocation to the link-time zero address. However such a choice tends to cause
relocation overflow possibility for RISC architectures.

* aarch64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the next instruction
* mips: GNU ld: branch to the start of the text segment (?); ld.lld: branch to zero
* ppc32: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the current instruction
* ppc64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the current instruction
* riscv: GNU ld: branch to the absolute zero address (with instruction rewriting)
* i386/x86_64: GNU ld/ld.lld: branch to the link-time zero address

I think that resolving to the same location is a good choice. The instruction,
if triggered, is clearly an undefined behavior. Resolving to the same location
can cause an infinite loop (making the user aware of the issue) while ensuring
no overflow.

Reviewed By: jrtc27

Differential Revision: https://reviews.llvm.org/D103001
lld/ELF/InputSection.cpp
lld/test/ELF/riscv-undefined-weak.s