[ELF][PPC64] Fix getRelExpr for R_PPC64_REL16_LO and R_PPC64_REL16_HA
authorZaara Syeda <syzaara@ca.ibm.com>
Wed, 21 Mar 2018 15:04:04 +0000 (15:04 +0000)
committerZaara Syeda <syzaara@ca.ibm.com>
Wed, 21 Mar 2018 15:04:04 +0000 (15:04 +0000)
The relocations R_PPC64_REL16_LO and R_PPC64_REL16_HA should return R_PC
for getRelExpr since they compute #lo(S + A – P) and #ha(S + A – P).

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

llvm-svn: 328103

lld/ELF/Arch/PPC64.cpp
lld/test/ELF/ppc64_entry_point.s [new file with mode: 0644]

index a8c305099d0ea4eb1c2dc9a414c9fed48c3b82fc..a8550ac2819746f60da93b39bc81cd2c55715929 100644 (file)
@@ -139,6 +139,9 @@ RelExpr PPC64::getRelExpr(RelType Type, const Symbol &S,
     return R_PPC_TOC;
   case R_PPC64_REL24:
     return R_PPC_PLT_OPD;
+  case R_PPC64_REL16_LO:
+  case R_PPC64_REL16_HA:
+    return R_PC;
   default:
     return R_ABS;
   }
@@ -232,10 +235,10 @@ void PPC64::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
     write16(Loc, applyPPCHighesta(Val));
     break;
   case R_PPC64_ADDR16_LO:
+  case R_PPC64_REL16_LO:
     write16(Loc, applyPPCLo(Val));
     break;
   case R_PPC64_ADDR16_LO_DS:
-  case R_PPC64_REL16_LO:
     write16(Loc, (read16(Loc) & 3) | (applyPPCLo(Val) & ~3));
     break;
   case R_PPC64_ADDR32:
diff --git a/lld/test/ELF/ppc64_entry_point.s b/lld/test/ELF/ppc64_entry_point.s
new file mode 100644 (file)
index 0000000..733c4cc
--- /dev/null
@@ -0,0 +1,33 @@
+# REQUIRES: ppc
+# RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t
+# RUN: ld.lld %t -o %t2
+# RUN: llvm-objdump -d %t2 | FileCheck %s
+
+.text
+.abiversion 2
+.globl  _start
+.p2align        4
+.type   _start,@function
+
+_start:
+.Lfunc_begin0:
+.Lfunc_gep0:
+  lis   4, .Lfunc_gep0@ha
+  addi  4, 4, .Lfunc_gep0@l
+  # now r4 should contain the address of _start
+
+  lis   5, .TOC.-.Lfunc_gep0@ha
+  addi  5, 5, .TOC.-.Lfunc_gep0@l
+  # now r5 should contain the offset s.t. r4 + r5 = TOC base
+
+  # exit 55
+  li    0, 1
+  li    3, 55
+  sc
+.Lfunc_end0:
+    .size   _start, .Lfunc_end0-.Lfunc_begin0
+
+// CHECK: 10010000:       01 10 80 3c     lis 4, 4097
+// CHECK-NEXT: 10010004:       00 00 84 38     addi 4, 4, 0
+// CHECK-NEXT: 10010008:       02 00 a0 3c     lis 5, 2
+// CHECK-NEXT: 1001000c:       00 80 a5 38     addi 5, 5, -32768