[lld] r240264 - [Mips] Reject R_MIPS_GPREL32 against external symbols

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


Author: atanasyan
Date: Mon Jun 22 04:26:33 2015
New Revision: 240264

URL: http://llvm.org/viewvc/llvm-project?rev=240264&view=rev
Log:
[Mips] Reject R_MIPS_GPREL32 against external symbols

Added:
    lld/trunk/test/elf/Mips/validate-rel-02.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=240264&r1=240263&r2=240264&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp Mon Jun 22 04:26:33 2015
@@ -680,15 +680,29 @@ make_reject_for_shared_lib_reloc_error(c
                                  atom.file().path() + " with -fPIC");
 }
 
+static std::error_code
+make_external_gprel32_reloc_error(const ELFLinkingContext &ctx,
+                                  const DefinedAtom &atom,
+                                  const Reference &ref) {
+  return make_dynamic_error_code(
+      "R_MIPS_GPREL32 (12) relocation cannot be used "
+      "against external symbol " +
+      ref.target()->name() + " in file " + atom.file().path());
+}
+
 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();
 
+  if (ref.kindValue() == R_MIPS_GPREL32 && !isLocal(ref.target()))
+    return make_external_gprel32_reloc_error(this->_ctx, atom, ref);
+
+  if (this->_ctx.getOutputELFType() != ET_DYN)
+    return std::error_code();
+
   switch (ref.kindValue()) {
   case R_MIPS16_HI16:
   case R_MIPS_HI16:

Added: lld/trunk/test/elf/Mips/validate-rel-02.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/validate-rel-02.test?rev=240264&view=auto
==============================================================================
--- lld/trunk/test/elf/Mips/validate-rel-02.test (added)
+++ lld/trunk/test/elf/Mips/validate-rel-02.test Mon Jun 22 04:26:33 2015
@@ -0,0 +1,41 @@
+# Check that the linker does not accept R_MIPS_GPREL32 relocation
+# against external symbol.
+
+# RUN: yaml2obj -format=elf -docnum 1 %s > %t.o
+# RUN: not lld -flavor gnu -target mipsel -e T0 -o %t.exe %t.o 2>&1 \
+# RUN:       | FileCheck %s
+
+# CHECK: R_MIPS_GPREL32 (12) relocation cannot be used against external symbol T0 in file {{.*}}validate-rel-02.test.tmp.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
+    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_GPREL32
+
+Symbols:         
+  Global:          
+    - Name:     T0
+      Type:     STT_FUNC
+      Section:  .text
+      Size:     4
+...





More information about the llvm-commits mailing list