[all-commits] [llvm/llvm-project] 6d87ca: [ELF] Refactor DynamicReloc to fix incorrect reloc...

Alexander Richardson via All-commits all-commits at lists.llvm.org
Fri Jul 9 02:42:03 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 6d87ca08aed19fd4e975788ffd3d25592447ff8e
      https://github.com/llvm/llvm-project/commit/6d87ca08aed19fd4e975788ffd3d25592447ff8e
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Relocations.cpp
    M lld/ELF/SyntheticSections.cpp
    M lld/ELF/SyntheticSections.h
    M lld/ELF/Thunks.cpp
    M lld/ELF/Writer.cpp
    M lld/test/ELF/got32-i386-pie-rw.s
    M lld/test/ELF/i386-zrel-zrela.s
    M lld/test/ELF/ppc64-abs64-dyn.s
    M lld/test/ELF/ppc64-long-branch-pi.s

  Log Message:
  -----------
  [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


  Commit: 35c5e564e628666af7903f6525b0de1a5917f071
      https://github.com/llvm/llvm-project/commit/35c5e564e628666af7903f6525b0de1a5917f071
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/ARM.cpp
    M lld/ELF/Arch/Mips.cpp
    M lld/ELF/Arch/X86.cpp
    M lld/ELF/Config.h
    M lld/ELF/Driver.cpp
    M lld/ELF/Options.td
    M lld/ELF/OutputSections.cpp
    M lld/ELF/OutputSections.h
    M lld/ELF/SyntheticSections.h
    M lld/ELF/Target.cpp
    M lld/ELF/Writer.cpp
    M lld/test/ELF/invalid/broken-relaxation-x64.test
    M lld/test/ELF/reloc-sec-before-relocated.test

  Log Message:
  -----------
  [ELF] Check the Elf_Rel addends for dynamic relocations

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 (which use Elf_Rela), but I thought
it would be useful to upstream our local changes anyway.

This patch adds a (hidden) command line flag --check-dynamic-relocations
that can be used to enable these checks. It is also on by default in
assertions builds for targets that handle all dynamic relocations kinds
that LLD can emit in Target::getImplicitAddend(). Currently this is
enabled for ARM, MIPS, and I386.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101450


  Commit: f4b0c9abfb8fb142ad644756b9959bd34811bc4b
      https://github.com/llvm/llvm-project/commit/f4b0c9abfb8fb142ad644756b9959bd34811bc4b
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/X86_64.cpp
    M lld/ELF/Driver.cpp
    M lld/test/ELF/invalid/broken-relaxation-x64.test
    M lld/test/ELF/reloc-sec-before-relocated.test

  Log Message:
  -----------
  [ELF] Implement X86_64::getImplicitAddend()

This allows checking dynamic relocation addends for -z rel and
--apply-dynamic-relocs output.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101451


  Commit: 79332fb722024558e16c2ab22feae5816b7bb886
      https://github.com/llvm/llvm-project/commit/79332fb722024558e16c2ab22feae5816b7bb886
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/X86_64.cpp
    M lld/test/ELF/x86-64-gotpc-relax.s

  Log Message:
  -----------
  [ELF] Write R_X86_64_IRELATIVE addends with -z rel

I found this missing case with the new --check-dynamic-relocation flag
while running the lld tests with --apply-dynamic-relocs enabled by default.

This also fixes a broken CHECK in lld/test/ELF/x86-64-gotpc-relax.s:
The test wasn't using CHECK-NEXT, so it was passing despite the output
actually containing relocations. I am not sure when this changed, but I
think this behaviour is correct.

Found with D101450 + enabling --apply-dynamic-relocs by default.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101452


  Commit: e5649328428f107962bf82a43299ba126514aae9
      https://github.com/llvm/llvm-project/commit/e5649328428f107962bf82a43299ba126514aae9
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/RISCV.cpp
    M lld/test/ELF/riscv-ifunc-nonpreemptible.s

  Log Message:
  -----------
  [ELF] Write R_RISCV_IRELATIVE addends with -z rel

I found this missing case with the new --check-dynamic-relocation flag
while running the lld tests with --apply-dynamic-relocs enabled by default.

This is the same as D101452 just for RISC-V

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101454


  Commit: 97fe63753938680cbbb968737f85ff0caedf221c
      https://github.com/llvm/llvm-project/commit/97fe63753938680cbbb968737f85ff0caedf221c
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/RISCV.cpp
    M lld/ELF/Driver.cpp

  Log Message:
  -----------
  [ELF] Implement RISCV::getImplicitAddend()

This allows checking dynamic relocation addends for -z rel and
--apply-dynamic-relocs output.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101455


  Commit: cc7cb9523e6aafd8079ba2bf2185d051221e5451
      https://github.com/llvm/llvm-project/commit/cc7cb9523e6aafd8079ba2bf2185d051221e5451
  Author: Alex Richardson <Alexander.Richardson at cl.cam.ac.uk>
  Date:   2021-07-09 (Fri, 09 Jul 2021)

  Changed paths:
    M lld/ELF/Arch/AArch64.cpp
    M lld/ELF/Relocations.cpp
    M lld/ELF/SyntheticSections.cpp
    M lld/ELF/SyntheticSections.h
    A lld/test/ELF/aarch64-tlsdesc-zrel.s

  Log Message:
  -----------
  [ELF][AArch64] Write addends for TLSDESC relocations with -z rel

Since D100490 this case is diagnosed for -z rel. This commit implements
R_AARCH64_TLSDESC cases for AArch64::getImplicitAddend() and
AArch64::relocate(). However, there are probably further relocation types
that need to be handled for full support of -z rel.

Fixes https://bugs.llvm.org/show_bug.cgi?id=47009

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D100544


Compare: https://github.com/llvm/llvm-project/compare/0c39f82f0b3e...cc7cb9523e6a


More information about the All-commits mailing list