[ELF] Refactor DynamicReloc to fix incorrect relocation addends
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Fri, 9 Jul 2021 09:04:35 +0000 (10:04 +0100)
committerAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Fri, 9 Jul 2021 09:41:40 +0000 (10:41 +0100)
commit6d87ca08aed19fd4e975788ffd3d25592447ff8e
tree53ee6ba4efd67653da5c1007c4c9507d3ea4cadd
parent0c39f82f0b3e9268f7699e62f59f960a1bff89fa
[ELF] Refactor DynamicReloc to fix incorrect relocation addends

This patch changes the DynamicReloc class to store an enum instead
of the overloaded useSymVA member to make it easier to understand
and fix incorrect addends being written in some corner cases. The
change is motivated by a follow-up review that checks the value of
implicit Elf_Rel addends written to the output file.

This patch fixes an incorrect output when using `-z rela` for i386 files
with R_386_GOT32 relocations (not that this really matters since it's an
unsupported configuration).
Storing the relocation expression kind also addresses an incorrect addend
FIXME in ppc64-abs64-dyn.s introduced in D63383.

DynamicReloc now also has a special case for the MIPS TLS relocations
(DynamicReloc::AgainstSymbolWithTargetVA) since the
R_MIPS_TLS_TPREL{32/64} the symbol VA to the GOT for preemptible
symbols. I'm not sure if the symbol value actually should be written
for R_MIPS_TLS_TPREL32, but this patch does not attempt to change
that behaviour.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D100490
lld/ELF/Relocations.cpp
lld/ELF/SyntheticSections.cpp
lld/ELF/SyntheticSections.h
lld/ELF/Thunks.cpp
lld/ELF/Writer.cpp
lld/test/ELF/got32-i386-pie-rw.s
lld/test/ELF/i386-zrel-zrela.s
lld/test/ELF/ppc64-abs64-dyn.s
lld/test/ELF/ppc64-long-branch-pi.s