[lld] r368057 - [ELF][PPC] Don't relax ifunc toc-indirect accesses to toc-relative

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 6 09:57:55 PDT 2019


Author: maskray
Date: Tue Aug  6 09:57:54 2019
New Revision: 368057

URL: http://llvm.org/viewvc/llvm-project?rev=368057&view=rev
Log:
[ELF][PPC] Don't relax ifunc toc-indirect accesses to toc-relative

Fixes PR42759.

```
// If ifunc is taken address in -fPIC code, it may have a toc entry
.section .toc,"aw", at progbits
  .quad ifunc

// ifunc may be defined as STT_GNU_IFUNC in another object file
.type ifunc, %gnu_indirect_function
```

If ifunc is non-preemptable (e.g. when linking an executable), the toc
entry will be relocated by R_PPC64_IRELATIVE.

R_*_IRELATIVE represents the symbolic value of a
non-preemptable ifunc (not associated with a canonical PLT) in a writable location. It has an unknown value at
link time, so we cannot apply toc-indirect to toc-relative relaxation.

Reviewed By: luporl, sfertile

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

Added:
    lld/trunk/test/ELF/ppc64-toc-relax-ifunc.s
Modified:
    lld/trunk/ELF/Arch/PPC64.cpp

Modified: lld/trunk/ELF/Arch/PPC64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Arch/PPC64.cpp?rev=368057&r1=368056&r2=368057&view=diff
==============================================================================
--- lld/trunk/ELF/Arch/PPC64.cpp (original)
+++ lld/trunk/ELF/Arch/PPC64.cpp Tue Aug  6 09:57:54 2019
@@ -172,7 +172,11 @@ bool elf::tryRelaxPPC64TocIndirection(Re
                    : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend);
 
   // Only non-preemptable defined symbols can be relaxed.
-  if (!d || d->isPreemptible)
+  //
+  // The toc entry of a non-preemptable ifunc is relocated by R_PPC64_IRELATIVE,
+  // which will run at load time to determine the relocated value. It is not
+  // known until load time, so the access cannot be relaxed.
+  if (!d || d->isPreemptible || d->isGnuIFunc())
     return false;
 
   // Two instructions can materialize a 32-bit signed offset from the toc base.

Added: lld/trunk/test/ELF/ppc64-toc-relax-ifunc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/ppc64-toc-relax-ifunc.s?rev=368057&view=auto
==============================================================================
--- lld/trunk/test/ELF/ppc64-toc-relax-ifunc.s (added)
+++ lld/trunk/test/ELF/ppc64-toc-relax-ifunc.s Tue Aug  6 09:57:54 2019
@@ -0,0 +1,20 @@
+# REQUIRES: ppc
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o
+# RUN: echo '.globl ifunc; .type ifunc, %gnu_indirect_function; ifunc:' | \
+# RUN:   llvm-mc -filetype=obj -triple=powerpc64le - -o %t1.o
+# RUN: ld.lld %t.o %t1.o -o %t
+# RUN: llvm-objdump -d %t | FileCheck %s
+
+## ifunc is a non-preemptable STT_GNU_IFUNC. Its toc entry will be
+## relocated by R_PPC64_IRELATIVE, not representable by a toc-relative value.
+## Check the toc-indirect access is not relaxed.
+
+# CHECK:      nop
+# CHECK-NEXT: ld 3, -32768(2)
+
+addis 3, 2, .toc at toc@ha
+ld 3, .toc at toc@l(3)
+
+.section .toc,"aw", at progbits
+  .quad ifunc




More information about the llvm-commits mailing list