[PATCH] D100490: [ELF] Check the Elf_Rel addends for dynamic relocations

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 14 09:08:30 PDT 2021


arichardson created this revision.
arichardson added reviewers: MaskRay, grimar, atanasyan, peter.smith.
Herald added subscribers: jrtc27, luismarques, s.egerton, PkmX, simoncook, kbarton, kristof.beyls, nemanjai, sdardis, emaste.
arichardson requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

There used to be many cases where addends for Elf_Rel were not emitted in
the final object file (mostly when building for MIPS64 since the input .o
files use RELA but the output uses REL). These cases have been fixed since,
but this patch adds a check to ensure that the written values are correct.
It is based on a previous patch that I added to the CHERI fork of LLD since
we were using MIPS64 as a baseline. The work has now almost entirely
shifted to RISC-V and Arm Morello, but I thought it would be useful to
upstream our local changes anyway.

In order to check that the relocation addend was written correctly, we have
to store the RelExpr in the DynamicReloc class to ensure that R_GOT
expressions are evaluated as the GOT entry for the symbol rather than the
actual symbol address. Without this change the got32-i386-pie-rw.s test
case complains about an incorrect addend since computeAddend()
currently returns the address of foo instead of the GOT slot.
This resulted in 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 <https://reviews.llvm.org/D63383>.

Finally, this patch adds a useSymVA+mustRetainSymbol workaround for
R_MIPS_TLS_TPREL{32/64} which currently writes the symbol VA to the GOT
for preemptible symbols. We can't set useSymVA since that would change
the relocation to no longer be against the symbol. I'm not sure if the symbol
value actually should be written for R_MIPS_TLS_TPREL32?
It seems like that might cause it to be added twice when relocating?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100490

Files:
  lld/ELF/Arch/ARM.cpp
  lld/ELF/Arch/Mips.cpp
  lld/ELF/Arch/X86.cpp
  lld/ELF/Arch/X86_64.cpp
  lld/ELF/OutputSections.cpp
  lld/ELF/OutputSections.h
  lld/ELF/Relocations.cpp
  lld/ELF/SyntheticSections.cpp
  lld/ELF/SyntheticSections.h
  lld/ELF/Target.cpp
  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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100490.337477.patch
Type: text/x-patch
Size: 25030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210414/a0cdc3e4/attachment-0001.bin>


More information about the llvm-commits mailing list