[lld] r295159 - [ELF][MIPS] Fix writing updated addend for R_MIPS_GOT16 relocation

Simon Atanasyan via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 15 00:33:03 PST 2017


Author: atanasyan
Date: Wed Feb 15 02:33:03 2017
New Revision: 295159

URL: http://llvm.org/viewvc/llvm-project?rev=295159&view=rev
Log:
[ELF][MIPS] Fix writing updated addend for R_MIPS_GOT16 relocation

If target of R_MIPS_GOT16 relocation is a local symbol its addend
is high 16 bits of complete addend. To calculate a final value, the addend
of this relocation is read, shifted to the left and combined with addend
of paired R_MIPS_LO16 relocation. To save updated addend when the linker
produces a relocatable output, we need to store high 16 bits of the
addend's value. It is different from the case of writing the relocation
result when the linker saves a 16-bit GOT index as-is.

Added:
    lld/trunk/test/ELF/mips-got16-relocatable.s
Modified:
    lld/trunk/ELF/Target.cpp

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=295159&r1=295158&r2=295159&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Wed Feb 15 02:33:03 2017
@@ -2345,9 +2345,19 @@ void MipsTargetInfo<ELFT>::relocateOne(u
   case R_MIPS_26:
     write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | ((Val >> 2) & 0x3ffffff));
     break;
+  case R_MIPS_GOT16:
+    // The R_MIPS_GOT16 relocation's value in "relocatable" linking mode
+    // is updated addend (not a GOT index). In that case write high 16 bits
+    // to store a correct addend value.
+    if (Config->Relocatable)
+      writeMipsHi16<E>(Loc, Val);
+    else {
+      checkInt<16>(Loc, Val, Type);
+      writeMipsLo16<E>(Loc, Val);
+    }
+    break;
   case R_MIPS_GOT_DISP:
   case R_MIPS_GOT_PAGE:
-  case R_MIPS_GOT16:
   case R_MIPS_GPREL16:
   case R_MIPS_TLS_GD:
   case R_MIPS_TLS_LDM:

Added: lld/trunk/test/ELF/mips-got16-relocatable.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/mips-got16-relocatable.s?rev=295159&view=auto
==============================================================================
--- lld/trunk/test/ELF/mips-got16-relocatable.s (added)
+++ lld/trunk/test/ELF/mips-got16-relocatable.s Wed Feb 15 02:33:03 2017
@@ -0,0 +1,40 @@
+# Check writing updated addend for R_MIPS_GOT16 relocation,
+# when produce a relocatable output.
+
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux -o %t.o %s
+# RUN: ld.lld -r -o %t %t.o %t.o
+# RUN: llvm-objdump -d -r %t | FileCheck -check-prefix=OBJ %s
+# RUN: ld.lld -shared -o %t.so %t
+# RUN: llvm-objdump -d %t.so | FileCheck -check-prefix=SO %s
+
+# REQUIRES: mips
+
+# OBJ:      Disassembly of section .text:
+# OBJ-NEXT: .text:
+# OBJ-NEXT:        0:       8f 99 00 00     lw      $25, 0($gp)
+# OBJ-NEXT:                         00000000:  R_MIPS_GOT16 .data
+# OBJ-NEXT:        4:       27 24 00 00     addiu   $4, $25, 0
+# OBJ-NEXT:                         00000004:  R_MIPS_LO16  .data
+# OBJ-NEXT:        8:       00 00 00 00     nop
+# OBJ-NEXT:        c:       00 00 00 00     nop
+# OBJ-NEXT:       10:       8f 99 00 00     lw      $25, 0($gp)
+# OBJ-NEXT:                         00000010:  R_MIPS_GOT16 .data
+# OBJ-NEXT:       14:       27 24 00 10     addiu   $4, $25, 16
+# OBJ-NEXT:                         00000014:  R_MIPS_LO16  .data
+
+# SO:      Disassembly of section .text:
+# SO-NEXT: .text:
+# SO-NEXT:    10000:       8f 99 80 18     lw      $25, -32744($gp)
+# SO-NEXT:    10004:       27 24 00 00     addiu   $4, $25, 0
+# SO-NEXT:    10008:       00 00 00 00     nop
+# SO-NEXT:    1000c:       00 00 00 00     nop
+# SO-NEXT:    10010:       8f 99 80 18     lw      $25, -32744($gp)
+# SO-NEXT:    10014:       27 24 00 10     addiu   $4, $25, 16
+
+  .text
+  lw     $t9, %got(.data)($gp)
+  addiu  $a0, $t9, %lo(.data)
+
+  .data
+data:
+  .word 0




More information about the llvm-commits mailing list