[ELF][MIPS] Apply mask while reading implicit addend and writing result of R_MIPS_26...
authorSimon Atanasyan <simon@atanasyan.com>
Fri, 22 Jul 2016 05:56:43 +0000 (05:56 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Fri, 22 Jul 2016 05:56:43 +0000 (05:56 +0000)
llvm-svn: 276395

lld/ELF/Target.cpp
lld/test/ELF/mips-26-mask.s [new file with mode: 0644]

index 3b11c50..dfbed13 100644 (file)
@@ -2040,7 +2040,7 @@ uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
     // FIXME (simon): If the relocation target symbol is not a PLT entry
     // we should use another expression for calculation:
     // ((A << 2) | (P & 0xf0000000)) >> 2
-    return SignExtend64<28>(read32<E>(Buf) << 2);
+    return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
   case R_MIPS_GPREL16:
   case R_MIPS_LO16:
   case R_MIPS_PCLO16:
@@ -2109,7 +2109,7 @@ void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
     write64<E>(Loc, Val);
     break;
   case R_MIPS_26:
-    write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
+    write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | ((Val >> 2) & 0x3ffffff));
     break;
   case R_MIPS_GOT_DISP:
   case R_MIPS_GOT_PAGE:
diff --git a/lld/test/ELF/mips-26-mask.s b/lld/test/ELF/mips-26-mask.s
new file mode 100644 (file)
index 0000000..4cf56cf
--- /dev/null
@@ -0,0 +1,16 @@
+# Check reading/writing implicit addend for R_MIPS_26 relocation.
+
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t.exe
+# RUN: llvm-objdump -d %t.exe | FileCheck %s
+
+# REQUIRES: mips
+
+# CHECK:      Disassembly of section .text:
+# CHECK:      __start:
+# CHECK-NEXT:   20000:       0e 00 80 00     jal     134348800
+
+  .text
+  .global __start
+__start:
+  jal __start+0x8000000