summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Atanasyan <simon@atanasyan.com>2017-02-15 08:33:03 +0000
committerSimon Atanasyan <simon@atanasyan.com>2017-02-15 08:33:03 +0000
commit91e30ae2052cf1a2f9ec46a5e9a726aaab970f47 (patch)
treeaf3bc2270aa180051cf7cb818144d13ca36bbc52
parent35801a2470e9fe5e4a296f12ff86b95ea411b875 (diff)
downloadbcm5719-llvm-91e30ae2052cf1a2f9ec46a5e9a726aaab970f47.tar.gz
bcm5719-llvm-91e30ae2052cf1a2f9ec46a5e9a726aaab970f47.zip
[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. llvm-svn: 295159
-rw-r--r--lld/ELF/Target.cpp12
-rw-r--r--lld/test/ELF/mips-got16-relocatable.s40
2 files changed, 51 insertions, 1 deletions
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index e2a64ddd3c9..2b0381f06b3 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -2345,9 +2345,19 @@ void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
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:
diff --git a/lld/test/ELF/mips-got16-relocatable.s b/lld/test/ELF/mips-got16-relocatable.s
new file mode 100644
index 00000000000..963efeb4686
--- /dev/null
+++ b/lld/test/ELF/mips-got16-relocatable.s
@@ -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
OpenPOWER on IntegriCloud