[lld] r240262 - [Mips] Reject position-dependent relocations in case of shared library linking

Simon Atanasyan simon at atanasyan.com
Mon Jun 22 02:26:20 PDT 2015


Author: atanasyan
Date: Mon Jun 22 04:26:20 2015
New Revision: 240262

URL: http://llvm.org/viewvc/llvm-project?rev=240262&view=rev
Log:
[Mips] Reject position-dependent relocations in case of shared library linking

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

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=240262&r1=240261&r2=240262&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp Mon Jun 22 04:26:20 2015
@@ -379,8 +379,12 @@ private:
 
   /// \brief Collect information about the reference to use it
   /// later in the handleReference() routine.
-  void collectReferenceInfo(const MipsELFDefinedAtom<ELFT> &atom,
-                            Reference &ref);
+  std::error_code collectReferenceInfo(const MipsELFDefinedAtom<ELFT> &atom,
+                                       Reference &ref);
+
+  /// \brief Check that the relocation is valid for the current linking mode.
+  std::error_code validateRelocation(const DefinedAtom &atom,
+                                     const Reference &ref) const;
 
   void handlePlain(const MipsELFDefinedAtom<ELFT> &atom, Reference &ref);
   void handle26(const MipsELFDefinedAtom<ELFT> &atom, Reference &ref);
@@ -430,9 +434,11 @@ RelocationPass<ELFT>::RelocationPass(Mip
 template <typename ELFT>
 std::error_code RelocationPass<ELFT>::perform(SimpleFile &mf) {
   for (const auto &atom : mf.defined())
-    for (const auto &ref : *atom)
-      collectReferenceInfo(*cast<MipsELFDefinedAtom<ELFT>>(atom),
-                           const_cast<Reference &>(*ref));
+    for (const auto &ref : *atom) {
+      const auto &da = *cast<MipsELFDefinedAtom<ELFT>>(atom);
+      if (auto ec = collectReferenceInfo(da, const_cast<Reference &>(*ref)))
+        return ec;
+    }
 
   // Process all references.
   for (const auto &atom : mf.defined())
@@ -625,19 +631,23 @@ static bool isConstrainSym(const MipsELF
 }
 
 template <typename ELFT>
-void RelocationPass<ELFT>::collectReferenceInfo(
-    const MipsELFDefinedAtom<ELFT> &atom, Reference &ref) {
+std::error_code
+RelocationPass<ELFT>::collectReferenceInfo(const MipsELFDefinedAtom<ELFT> &atom,
+                                           Reference &ref) {
   if (!ref.target())
-    return;
+    return std::error_code();
   if (ref.kindNamespace() != Reference::KindNamespace::ELF)
-    return;
+    return std::error_code();
 
   auto refKind = ref.kindValue();
   if (refKind == R_MIPS_EH && this->_ctx.mipsPcRelEhRel())
     ref.setKindValue(R_MIPS_PC32);
 
+  if (auto ec = validateRelocation(atom, ref))
+    return ec;
+
   if (!isConstrainSym(atom, refKind))
-    return;
+    return std::error_code();
 
   if (mightBeDynamic(atom, refKind))
     _rel32Candidates.push_back(&ref);
@@ -652,6 +662,56 @@ void RelocationPass<ELFT>::collectRefere
       refKind != R_MICROMIPS_CALL_HI16 && refKind != R_MICROMIPS_CALL_LO16 &&
       refKind != R_MIPS_EH)
     _requiresPtrEquality.insert(ref.target());
+  return std::error_code();
+}
+
+static std::error_code
+make_reject_for_shared_lib_reloc_error(const ELFLinkingContext &ctx,
+                                       const DefinedAtom &atom,
+                                       const Reference &ref) {
+  StringRef kindValStr = "unknown";
+  ctx.registry().referenceKindToString(ref.kindNamespace(), ref.kindArch(),
+                                       ref.kindValue(), kindValStr);
+
+  return make_dynamic_error_code(Twine(kindValStr) + " (" +
+                                 Twine(ref.kindValue()) +
+                                 ") relocation cannot be used "
+                                 "when making a shared object, recompile " +
+                                 atom.file().path() + " with -fPIC");
+}
+
+template <typename ELFT>
+std::error_code
+RelocationPass<ELFT>::validateRelocation(const DefinedAtom &atom,
+                                         const Reference &ref) const {
+  if (this->_ctx.getOutputELFType() != ET_DYN)
+    return std::error_code();
+  if (!ref.target())
+    return std::error_code();
+
+  switch (ref.kindValue()) {
+  case R_MIPS16_HI16:
+  case R_MIPS_HI16:
+  case R_MIPS_HIGHER:
+  case R_MIPS_HIGHEST:
+  case R_MICROMIPS_HI16:
+  case R_MICROMIPS_HIGHER:
+  case R_MICROMIPS_HIGHEST:
+    // For shared object we accepts "high" relocations
+    // against the "_gp_disp" symbol only.
+    if (ref.target()->name() != "_gp_disp")
+      return make_reject_for_shared_lib_reloc_error(this->_ctx, atom, ref);
+    break;
+  case R_MIPS16_26:
+  case R_MIPS_26:
+  case R_MICROMIPS_26_S1:
+    // These relocations are position dependent
+    // and not acceptable in a shared object.
+    return make_reject_for_shared_lib_reloc_error(this->_ctx, atom, ref);
+  default:
+    break;
+  }
+  return std::error_code();
 }
 
 template <typename ELFT>

Added: lld/trunk/test/elf/Mips/validate-rel-01.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/validate-rel-01.test?rev=240262&view=auto
==============================================================================
--- lld/trunk/test/elf/Mips/validate-rel-01.test (added)
+++ lld/trunk/test/elf/Mips/validate-rel-01.test Mon Jun 22 04:26:20 2015
@@ -0,0 +1,82 @@
+# Check that the linker does not accept position-dependent relocations
+# in case of shared library linking.
+
+# RUN: yaml2obj -format=elf -docnum 1 %s > %t-hi.o
+# RUN: not lld -flavor gnu -target mipsel -shared -o %t.so %t-hi.o 2>&1 \
+# RUN:       | FileCheck -check-prefix=RHI %s
+
+# RHI: R_MIPS_HI16 (5) relocation cannot be used when making a shared object, recompile {{.*}}validate-rel-01.test.tmp-hi.o with -fPIC
+
+# RUN: yaml2obj -format=elf -docnum 2 %s > %t-26.o
+# RUN: not lld -flavor gnu -target mipsel -shared -o %t.so %t-26.o 2>&1 \
+# RUN:       | FileCheck -check-prefix=R26 %s
+
+# R26: R_MIPS_26 (4) relocation cannot be used when making a shared object, recompile {{.*}}validate-rel-01.test.tmp-26.o with -fPIC
+
+---
+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
+    Flags:         [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:  16
+    Size:          4
+
+  - Name:          .rel.text
+    Type:          SHT_REL
+    Link:          .symtab
+    AddressAlign:  4
+    Info:          .text
+    Relocations:   
+      - Offset:  0
+        Symbol:  T0
+        Type:    R_MIPS_HI16
+      - Offset:  0
+        Symbol:  T0
+        Type:    R_MIPS_LO16
+
+Symbols:         
+  Global:          
+    - Name:     T0
+      Type:     STT_FUNC
+      Section:  .text
+      Size:     4
+
+---
+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
+    Flags:         [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:  16
+    Size:          4
+
+  - Name:          .rel.text
+    Type:          SHT_REL
+    Link:          .symtab
+    AddressAlign:  4
+    Info:          .text
+    Relocations:   
+      - Offset:  0
+        Symbol:  T1
+        Type:    R_MIPS_26
+
+Symbols:         
+  Global:          
+    - Name:     T1
+      Type:     STT_FUNC
+      Section:  .text
+      Size:     4
+...





More information about the llvm-commits mailing list