[lld] r233532 - [Mips] Fix writing R_MIPS_REL32 relocation addendum

Simon Atanasyan simon at atanasyan.com
Mon Mar 30 04:39:02 PDT 2015


Author: atanasyan
Date: Mon Mar 30 06:39:02 2015
New Revision: 233532

URL: http://llvm.org/viewvc/llvm-project?rev=233532&view=rev
Log:
[Mips] Fix writing R_MIPS_REL32 relocation addendum

If input relocation records have RELA format while output dynamic
relocations have REL format the only way to transfer a dynamic
relocation addendum is to save it into the location modified by
the dynamic relocation.

Added:
    lld/trunk/test/elf/Mips/rel-dynamic-13.test
    lld/trunk/test/elf/Mips/rel-dynamic-14.test
Modified:
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp?rev=233532&r1=233531&r2=233532&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp Mon Mar 30 06:39:02 2015
@@ -113,3 +113,17 @@ bool MipsLinkingContext::isPLTRelocation
     return false;
   }
 }
+
+bool MipsLinkingContext::isRelativeReloc(const Reference &r) const {
+  if (r.kindNamespace() != Reference::KindNamespace::ELF)
+    return false;
+  assert(r.kindArch() == Reference::KindArch::Mips);
+  switch (r.kindValue()) {
+  case llvm::ELF::R_MIPS_REL32:
+  case llvm::ELF::R_MIPS_GPREL16:
+  case llvm::ELF::R_MIPS_GPREL32:
+    return true;
+  default:
+    return false;
+  }
+}

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h?rev=233532&r1=233531&r2=233532&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h Mon Mar 30 06:39:02 2015
@@ -57,6 +57,7 @@ public:
   bool isDynamicRelocation(const Reference &r) const override;
   bool isCopyRelocation(const Reference &r) const override;
   bool isPLTRelocation(const Reference &r) const override;
+  bool isRelativeReloc(const Reference &r) const override;
 
 private:
   MipsELFFlagsMerger _flagsMerger;

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=233532&r1=233531&r2=233532&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp Mon Mar 30 06:39:02 2015
@@ -124,6 +124,7 @@ static MipsRelocationParams getRelocatio
   case R_MICROMIPS_JALR:
     return {4, 0x0, 0, true};
   case R_MIPS_REL32:
+    return {4, 0xffffffff, 0, false};
   case R_MIPS_JUMP_SLOT:
   case R_MIPS_COPY:
   case R_MIPS_TLS_DTPMOD32:
@@ -554,6 +555,13 @@ std::error_code RelocationHandler<ELFT>:
   if (auto ec = res.getError())
     return ec;
 
+  // If output relocation format is REL and the input one is RELA, the only
+  // method to transfer the relocation addend from the input relocation
+  // to the output dynamic relocation is to save this addend to the location
+  // modified by R_MIPS_REL32.
+  if (ref.kindValue() == R_MIPS_REL32 && !_ctx.isRelaOutputFormat())
+    res = ref.addend();
+
   Reference::KindValue op = ref.kindValue();
 
   // FIXME (simon): Handle r_ssym value.

Added: lld/trunk/test/elf/Mips/rel-dynamic-13.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/rel-dynamic-13.test?rev=233532&view=auto
==============================================================================
--- lld/trunk/test/elf/Mips/rel-dynamic-13.test (added)
+++ lld/trunk/test/elf/Mips/rel-dynamic-13.test Mon Mar 30 06:39:02 2015
@@ -0,0 +1,94 @@
+# Conditions:
+#   a) Linking a non-shared 32-bit executable file.
+#   b) Relocations' targets are symbols defined in the shared object.
+# Check:
+#   a) Emitting R_MIPS_REL32 relocation.
+#   b) Applying addendum from the original relocation.
+#
+# RUN: yaml2obj -format=elf -docnum 1 %s > %t-so.o
+# RUN: lld -flavor gnu -target mipsel -shared -o %t.so %t-so.o
+# RUN: yaml2obj -format=elf -docnum 2 %s > %t-o.o
+# RUN: lld -flavor gnu -target mipsel -e T0 -o %t.exe %t-o.o %t.so
+# RUN: llvm-objdump -s %t.exe | FileCheck -check-prefix=RAW %s
+# RUN: llvm-readobj -r %t.exe | FileCheck -check-prefix=REL %s
+
+# RAW:      Contents of section .data:
+# RAW-NEXT:  402000 01000000
+
+# REL:      Relocations [
+# REL-NEXT:   Section (5) .rel.dyn {
+# REL-NEXT:     0x402000 R_MIPS_REL32 T1 0x0
+# REL-NEXT:   }
+# REL-NEXT: ]
+
+# 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_EXECINSTR, SHF_ALLOC]
+
+Symbols:
+  Global:
+    - Name:    T1
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    4
+
+# o.o
+---
+FileHeader:
+  Class:   ELFCLASS32
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+  Flags:   [EF_MIPS_CPIC, EF_MIPS_ABI_O32, EF_MIPS_ARCH_32]
+
+Sections:
+- Name:         .text
+  Type:         SHT_PROGBITS
+  Size:         4
+  AddressAlign: 16
+  Flags:        [SHF_EXECINSTR, SHF_ALLOC]
+
+- Name:         .data
+  Type:         SHT_PROGBITS
+  Size:         4
+  AddressAlign: 16
+  Flags:        [SHF_WRITE, SHF_ALLOC]
+
+- Name:         .rel.data
+  Type:         SHT_RELA
+  Info:         .data
+  AddressAlign: 4
+  Relocations:
+    - Offset: 0
+      Symbol: T1
+      Type:   R_MIPS_32
+      Addend: 1
+
+Symbols:
+  Global:
+    - Name:    T0
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    4
+    - Name:    T1
+      Type:    STT_FUNC
+    - Name:    D0
+      Section: .data
+      Type:    STT_OBJECT
+      Value:   0
+      Size:    4
+...

Added: lld/trunk/test/elf/Mips/rel-dynamic-14.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/rel-dynamic-14.test?rev=233532&view=auto
==============================================================================
--- lld/trunk/test/elf/Mips/rel-dynamic-14.test (added)
+++ lld/trunk/test/elf/Mips/rel-dynamic-14.test Mon Mar 30 06:39:02 2015
@@ -0,0 +1,94 @@
+# Conditions:
+#   a) Linking a non-shared 64-bit executable file.
+#   b) Relocations' targets are symbols defined in the shared object.
+# Check:
+#   a) Emitting R_MIPS_REL32/R_MIPS_64 relocation.
+#   b) Applying addendum from the original relocation.
+#
+# RUN: yaml2obj -format=elf -docnum 1 %s > %t-so.o
+# RUN: lld -flavor gnu -target mips64el -shared -o %t.so %t-so.o
+# RUN: yaml2obj -format=elf -docnum 2 %s > %t-o.o
+# RUN: lld -flavor gnu -target mips64el -e T0 -o %t.exe %t-o.o %t.so
+# RUN: llvm-objdump -s %t.exe | FileCheck -check-prefix=RAW %s
+# RUN: llvm-readobj -r %t.exe | FileCheck -check-prefix=REL %s
+
+# RAW:      Contents of section .data:
+# RAW-NEXT:  120002000 04000000 00000000
+
+# REL:      Relocations [
+# REL-NEXT:   Section (5) .rel.dyn {
+# REL-NEXT:     0x120002000 R_MIPS_REL32/R_MIPS_64/R_MIPS_NONE T1 0x0
+# REL-NEXT:   }
+# REL-NEXT: ]
+
+# so.o
+---
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+  Flags:   [EF_MIPS_PIC, EF_MIPS_CPIC, EF_MIPS_ARCH_64]
+
+Sections:
+- Name:         .text
+  Type:         SHT_PROGBITS
+  Size:         4
+  AddressAlign: 16
+  Flags:        [SHF_EXECINSTR, SHF_ALLOC]
+
+Symbols:
+  Global:
+    - Name:    T1
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    4
+
+# o.o
+---
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+  Flags:   [EF_MIPS_PIC, EF_MIPS_CPIC, EF_MIPS_ARCH_64]
+
+Sections:
+- Name:         .text
+  Type:         SHT_PROGBITS
+  Size:         4
+  AddressAlign: 16
+  Flags:        [SHF_EXECINSTR, SHF_ALLOC]
+
+- Name:         .data
+  Type:         SHT_PROGBITS
+  Size:         8
+  AddressAlign: 16
+  Flags:        [SHF_WRITE, SHF_ALLOC]
+
+- Name:         .rel.data
+  Type:         SHT_RELA
+  Info:         .data
+  AddressAlign: 4
+  Relocations:
+    - Offset: 0
+      Symbol: T1
+      Type:   R_MIPS_64
+      Addend: 4
+
+Symbols:
+  Global:
+    - Name:    T0
+      Section: .text
+      Type:    STT_FUNC
+      Value:   0
+      Size:    4
+    - Name:    T1
+      Type:    STT_FUNC
+    - Name:    D0
+      Section: .data
+      Type:    STT_OBJECT
+      Value:   0
+      Size:    4
+...





More information about the llvm-commits mailing list