[ELF][MIPS] Set GP0 value to zero in case of relocatable object generation
authorSimon Atanasyan <simon@atanasyan.com>
Thu, 29 Sep 2016 12:58:48 +0000 (12:58 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Thu, 29 Sep 2016 12:58:48 +0000 (12:58 +0000)
LLD does not update relocations addends when generate a relocatable
object. That is why we should not write a non-zero GP0 value into
the .reginfo and .MIPS.options sections. And we should not accept input
object files with non-zero GP0 value because we cannot handle them
properly.

llvm-svn: 282716

lld/ELF/InputSection.cpp
lld/ELF/OutputSections.cpp
lld/test/ELF/Inputs/mips-gp0-non-zero.o [new file with mode: 0755]
lld/test/ELF/mips-gprel32-relocs-gp0.s

index 5d7312b..964bf39 100644 (file)
@@ -659,6 +659,8 @@ MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
     return;
   }
   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Data.data());
+  if (Reginfo->ri_gp_value)
+    error(getName(this) + ": unsupported non-zero ri_gp_value");
 }
 
 template <class ELFT>
@@ -682,6 +684,8 @@ MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F,
     auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data());
     if (O->kind == ODK_REGINFO) {
       Reginfo = &O->getRegInfo();
+      if (Reginfo->ri_gp_value)
+        error(getName(this) + ": unsupported non-zero ri_gp_value");
       break;
     }
     D = D.slice(O->size);
index feb7cad..9a7fb0f 100644 (file)
@@ -1740,7 +1740,10 @@ MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
 template <class ELFT>
 void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
   auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
-  R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
+  if (Config->Relocatable)
+    R->ri_gp_value = 0;
+  else
+    R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
   R->ri_gprmask = GprMask;
 }
 
@@ -1769,7 +1772,10 @@ void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
   Opt->section = 0;
   Opt->info = 0;
   auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt));
-  Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
+  if (Config->Relocatable)
+    Reg->ri_gp_value = 0;
+  else
+    Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
   Reg->ri_gprmask = GprMask;
 }
 
diff --git a/lld/test/ELF/Inputs/mips-gp0-non-zero.o b/lld/test/ELF/Inputs/mips-gp0-non-zero.o
new file mode 100755 (executable)
index 0000000..4c82343
Binary files /dev/null and b/lld/test/ELF/Inputs/mips-gp0-non-zero.o differ
index 027de73..49ee0bb 100644 (file)
@@ -1,29 +1,35 @@
-# Check R_MIPS_GPREL32 relocation calculation if input file conatins
-# non-zero GP0 value in the .reginfo section.
+# Check that relocatable object produced by LLD has zero gp0 value.
+# Also check an error message if input object file has non-zero gp0 value.
+# mips-gp0-non-zero.o is a relocatable object produced from the asm code
+# below and linked by GNU bfd linker.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld -r -o %t-r.o %t.o
 # RUN: ld.lld -shared -o %t.so %t-r.o
 # RUN: llvm-readobj -mips-reginfo %t-r.o %t.so | FileCheck %s
 # RUN: llvm-objdump -s -t %t.so | FileCheck --check-prefix=DUMP %s
+# RUN: not ld.lld -shared -o %t.so %S/Inputs/mips-gp0-non-zero.o 2>&1 \
+# RUN:   | FileCheck --check-prefix=ERR %s
 
 # REQUIRES: mips
 
 # CHECK: {{.*}}mips-gprel32-relocs-gp0.s.tmp-r.o
-# CHECK: GP: 0x7FF0
+# CHECK: GP: 0x0
 # CHECK: {{.*}}mips-gprel32-relocs-gp0.s.tmp.so
 # CHECK: GP: 0x27FF0
 
 # DUMP: Contents of section .rodata:
-# DUMP:  0114 ffff0004 ffff0008
-#             ^ 0x10004 + 0x7ff0 - 0x27ff0
-#                      ^ 0x10008 + 0x7ff0 - 0x27ff0
+# DUMP:  0114 fffe8014 fffe8018
+#             ^ 0x10004 + 0 - 0x27ff0
+#                      ^ 0x10008 + 0 - 0x27ff0
 
 # DUMP: SYMBOL TABLE:
 # DUMP: 00010008         .text          00000000 bar
 # DUMP: 00010004         .text          00000000 foo
 # DUMP: 00027ff0         .got           00000000 .hidden _gp
 
+# ERR: {{.*}}mips-gp0-non-zero.o(.reginfo): unsupported non-zero ri_gp_value
+
   .text
   .global  __start
 __start: