[lld] r234724 - [Mips] Support R_MIPS_GOT_HI16 / R_MIPS_GOT_LO16 relocations handling

Simon Atanasyan simon at atanasyan.com
Mon Apr 13 01:20:35 PDT 2015


Author: atanasyan
Date: Mon Apr 13 03:20:34 2015
New Revision: 234724

URL: http://llvm.org/viewvc/llvm-project?rev=234724&view=rev
Log:
[Mips] Support R_MIPS_GOT_HI16 / R_MIPS_GOT_LO16 relocations handling

Added:
    lld/trunk/test/elf/Mips/rel-got-hilo-01.test
Modified:
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp?rev=234724&r1=234723&r2=234724&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp Mon Apr 13 03:20:34 2015
@@ -82,6 +82,8 @@ static MipsRelocationParams getRelocatio
   case R_MIPS_GOT_DISP:
   case R_MIPS_GOT_PAGE:
   case R_MIPS_GOT_OFST:
+  case R_MIPS_GOT_HI16:
+  case R_MIPS_GOT_LO16:
   case R_MIPS_TLS_DTPREL_HI16:
   case R_MIPS_TLS_DTPREL_LO16:
   case R_MIPS_TLS_TPREL_HI16:
@@ -221,6 +223,18 @@ static uint64_t relocGOT(uint64_t S, uin
   return G;
 }
 
+/// \brief R_MIPS_GOT_LO16
+/// rel16 G (truncate)
+static uint64_t relocGOTLo16(uint64_t S, uint64_t GP) {
+  return S - GP;
+}
+
+/// \brief R_MIPS_GOT_HI16
+/// rel16 %high(G) (truncate)
+static uint64_t relocGOTHi16(uint64_t S, uint64_t GP) {
+  return (S - GP + 0x8000) >> 16;
+}
+
 /// R_MIPS_GOT_OFST
 /// rel16 offset of (S+A) from the page pointer (verify)
 static uint32_t relocGOTOfst(uint64_t S, int64_t A) {
@@ -402,6 +416,10 @@ static ErrorOr<uint64_t> calculateReloca
     return relocPcLo16(relAddr, tgtAddr, addend);
   case R_MICROMIPS_LO16:
     return relocLo16(relAddr, tgtAddr, addend, isGP, true);
+  case R_MIPS_GOT_LO16:
+    return relocGOTLo16(tgtAddr, gpAddr);
+  case R_MIPS_GOT_HI16:
+    return relocGOTHi16(tgtAddr, gpAddr);
   case R_MIPS_EH:
   case R_MIPS_GOT16:
   case R_MIPS_CALL16:

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp?rev=234724&r1=234723&r2=234724&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp Mon Apr 13 03:20:34 2015
@@ -530,6 +530,8 @@ void RelocationPass<ELFT>::handleReferen
   case R_MIPS_EH:
   case R_MIPS_GOT16:
   case R_MIPS_CALL16:
+  case R_MIPS_GOT_HI16:
+  case R_MIPS_GOT_LO16:
   case R_MICROMIPS_GOT16:
   case R_MICROMIPS_CALL16:
   case R_MIPS_GOT_DISP:
@@ -612,6 +614,7 @@ void RelocationPass<ELFT>::collectRefere
 
   if (refKind != R_MIPS_CALL16 && refKind != R_MICROMIPS_CALL16 &&
       refKind != R_MIPS_26 && refKind != R_MICROMIPS_26_S1 &&
+      refKind != R_MIPS_GOT_HI16 && refKind != R_MIPS_GOT_LO16 &&
       refKind != R_MIPS_EH)
     _requiresPtrEquality.insert(ref.target());
 }
@@ -641,7 +644,8 @@ template <typename ELFT>
 bool RelocationPass<ELFT>::mightBeDynamic(const MipsELFDefinedAtom<ELFT> &atom,
                                           Reference::KindValue refKind) const {
   if (refKind == R_MIPS_CALL16 || refKind == R_MIPS_GOT16 ||
-      refKind == R_MICROMIPS_CALL16 || refKind == R_MICROMIPS_GOT16)
+      refKind == R_MICROMIPS_CALL16 || refKind == R_MICROMIPS_GOT16 ||
+      refKind == R_MIPS_GOT_HI16 || refKind == R_MIPS_GOT_LO16)
     return true;
 
   if (refKind != R_MIPS_32 && refKind != R_MIPS_64)
@@ -792,7 +796,9 @@ template <typename ELFT> void Relocation
 
   if (ref.kindValue() == R_MIPS_GOT_PAGE)
     ref.setTarget(getLocalGOTPageEntry(ref));
-  else if (ref.kindValue() == R_MIPS_GOT_DISP || ref.kindValue() == R_MIPS_EH)
+  else if (ref.kindValue() == R_MIPS_GOT_DISP ||
+           ref.kindValue() == R_MIPS_GOT_HI16 ||
+           ref.kindValue() == R_MIPS_GOT_LO16 || ref.kindValue() == R_MIPS_EH)
     ref.setTarget(getLocalGOTEntry(ref));
   else if (isLocal(ref.target()))
     ref.setTarget(getLocalGOTPageEntry(ref));

Added: lld/trunk/test/elf/Mips/rel-got-hilo-01.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/rel-got-hilo-01.test?rev=234724&view=auto
==============================================================================
--- lld/trunk/test/elf/Mips/rel-got-hilo-01.test (added)
+++ lld/trunk/test/elf/Mips/rel-got-hilo-01.test Mon Apr 13 03:20:34 2015
@@ -0,0 +1,109 @@
+# Check handling of R_MIPS_GOT_HI16 / R_MIPS_GOT_LO16 relocations.
+
+# RUN: yaml2obj -format=elf -docnum 1 %s > %t1.so.o
+# RUN: lld -flavor gnu -target mipsel -shared -o %t1.so %t1.so.o
+# RUN: yaml2obj -format=elf -docnum 2 %s > %t2.so.o
+# RUN: lld -flavor gnu -target mipsel -shared -o %t2.so %t2.so.o %t1.so
+# RUN: llvm-objdump -s -t %t2.so | FileCheck -check-prefix=RAW %s
+# RUN: llvm-readobj -mips-plt-got %t2.so | FileCheck -check-prefix=GOT %s
+
+# RAW:      Contents of section .text:
+# RAW-NEXT:  0110 00000000 18800000 00000000 1c800000
+#                          ^ -32744          ^ -32740
+# RAW-NEXT:  0120 00000000
+
+# RAW: SYMBOL TABLE:
+# RAW: 00000120 l  F .text  00000004 T1
+
+# GOT:      Local entries [
+# GOT-NEXT:   Entry {
+# GOT-NEXT:     Address: 0x1008
+# GOT-NEXT:     Access: -32744
+# GOT-NEXT:     Initial: 0x120
+# GOT-NEXT:   }
+# GOT-NEXT: ]
+# GOT-NEXT: Global entries [
+# GOT-NEXT:   Entry {
+# GOT-NEXT:     Address: 0x100C
+# GOT-NEXT:     Access: -32740
+# GOT-NEXT:     Initial: 0x0
+# GOT-NEXT:     Value: 0x0
+# GOT-NEXT:     Type: Function (0x2)
+# GOT-NEXT:     Section: Undefined (0x0)
+# GOT-NEXT:     Name: T2@ (4)
+# GOT-NEXT:   }
+# GOT-NEXT: ]
+
+# t1.so.o
+---
+FileHeader:
+  Class:   ELFCLASS32
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+  Flags:   [EF_MIPS_PIC, EF_MIPS_CPIC, EF_MIPS_ABI_O32, EF_MIPS_ARCH_32]
+
+Sections:
+- Name:         .text
+  Type:         SHT_PROGBITS
+  Size:         4
+  AddressAlign: 16
+  Flags:        [SHF_ALLOC, SHF_EXECINSTR]
+
+Symbols:
+  Global:
+    - Name:    T2
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    4
+
+# t2.so.o
+---
+FileHeader:
+  Class:   ELFCLASS32
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+  Flags:   [EF_MIPS_PIC, EF_MIPS_CPIC, EF_MIPS_ABI_O32, EF_MIPS_ARCH_32]
+
+Sections:
+- Name:         .text
+  Type:         SHT_PROGBITS
+  Size:         20
+  AddressAlign: 16
+  Flags:        [SHF_ALLOC, SHF_EXECINSTR]
+
+- Name:         .rel.text
+  Type:         SHT_REL
+  Info:         .text
+  AddressAlign: 4
+  Relocations:
+    - Offset: 0
+      Symbol: T1
+      Type:   R_MIPS_GOT_HI16
+    - Offset: 4
+      Symbol: T1
+      Type:   R_MIPS_GOT_LO16
+    - Offset: 8
+      Symbol: T2
+      Type:   R_MIPS_GOT_HI16
+    - Offset: 12
+      Symbol: T2
+      Type:   R_MIPS_GOT_LO16
+
+Symbols:
+  Local:
+    - Name:    T1
+      Section: .text
+      Type:    STT_FUNC
+      Value:   16
+      Size:    4
+  Global:
+    - Name:    T0
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    16
+    - Name:    T2
+...





More information about the llvm-commits mailing list