[PATCH] D95262: [LLD][PowerPC] Fix bug in PC-Relative initial exec

Stefan Pintilie via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 22 12:56:43 PST 2021


stefanp created this revision.
stefanp added reviewers: nemanjai, lei, MaskRay.
Herald added subscribers: shchenz, kbarton, arichardson, emaste.
stefanp requested review of this revision.
Herald added a project: LLVM.

There is a bug when initial exec is relaxed to local exec.
In the following situation:

  pld 3, x at got@tprel at pcrel(0), 1
  add 4, 3, x at tls@pcrel

the result of the add is not placed in r3 like the result of the pld.
Therefore it is not correct to relax the above instructions by replacing
the add instruction with a nop.
Instead the add instruction should be replaced with a copy (mr) instruction.
If the add uses the same resgiter as input and as ouput then it is safe to
continue to replace the add with a nop.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95262

Files:
  lld/ELF/Arch/PPC64.cpp
  lld/test/ELF/ppc64-tls-pcrel-ie.s


Index: lld/test/ELF/ppc64-tls-pcrel-ie.s
===================================================================
--- lld/test/ELF/ppc64-tls-pcrel-ie.s
+++ lld/test/ELF/ppc64-tls-pcrel-ie.s
@@ -54,9 +54,9 @@
 
 # LE-RELOC: There are no relocations in this file.
 
-# LE-SYM: Symbol table '.symtab' contains 7 entries:
-# LE-SYM: 5: 0000000000000000     0 TLS     GLOBAL DEFAULT     6 x
-# LE-SYM: 6: 0000000000000004     0 TLS     GLOBAL DEFAULT     6 y
+# LE-SYM: Symbol table '.symtab' contains 8 entries:
+# LE-SYM: 6: 0000000000000000     0 TLS     GLOBAL DEFAULT     6 x
+# LE-SYM: 7: 0000000000000004     0 TLS     GLOBAL DEFAULT     6 y
 
 # LE-GOT: could not find section '.got'
 
@@ -74,6 +74,20 @@
 	add 3, 3, x at tls@pcrel
 	blr
 
+# IE-LABEL: <IEAddrCopy>:
+# IE-NEXT:    pld 3, 12488(0), 1
+# IE-NEXT:    add 4, 3, 13
+# IE-NEXT:    blr
+# LE-LABEL: <IEAddrCopy>:
+# LE-NEXT:    paddi 3, 13, -28672, 0
+# LE-NEXT:    mr 4, 3
+# LE-NEXT:    blr
+.section .text_addr, "ax", %progbits
+IEAddrCopy:
+	pld 3, x at got@tprel at pcrel(0), 1
+	add 4, 3, x at tls@pcrel
+	blr
+
 # IE-LABEL: <IEVal>:
 # IE-NEXT:    pld 3, 8408(0), 1
 # IE-NEXT:    lwzx 3, 3, 13
Index: lld/ELF/Arch/PPC64.cpp
===================================================================
--- lld/ELF/Arch/PPC64.cpp
+++ lld/ELF/Arch/PPC64.cpp
@@ -920,7 +920,15 @@
       // that comes before it will already have computed the address of the
       // symbol.
       if (secondaryOp == 266) {
-        write32(loc - 1, NOP);
+        // Check if the add uses the same result register as the input register.
+        uint32_t RT = (tlsInstr & 0x03E00000) >> 21; // bits 6-10
+        uint32_t RA = (tlsInstr & 0x001F0000) >> 16; // bits 11-15
+        if (RA == RT)
+          write32(loc - 1, NOP);
+        else {
+          // mr RT, RA
+          write32(loc - 1, 0x7C000378 | (RT << 16) | (RA << 21) | (RA << 11));
+        }
       } else {
         uint32_t dFormOp = getPPCDFormOp(secondaryOp);
         if (dFormOp == 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95262.318631.patch
Type: text/x-patch
Size: 1987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210122/1ad5cebd/attachment.bin>


More information about the llvm-commits mailing list