[ELF] - Don't crash on multiple SHT_MIPS_REGINFO/SHT_MIPS_ABIFLAGS sections.
authorGeorge Rimar <grimar@accesssoftek.com>
Mon, 17 Oct 2016 11:31:46 +0000 (11:31 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Mon, 17 Oct 2016 11:31:46 +0000 (11:31 +0000)
In continue of D25555, this patch fixes possible crash when
we have multiple SHT_MIPS_REGINFO or SHT_MIPS_ABIFLAGS sections.
yaml2obj was used to produce such objects.

Differential revision: https://reviews.llvm.org/D25609

llvm-svn: 284376

lld/ELF/InputFiles.cpp
lld/test/ELF/invalid/mips-multiple-abiflags.test [new file with mode: 0644]
lld/test/ELF/invalid/mips-multiple-reginfo.test [new file with mode: 0644]

index 3aafdbb..a5f9b90 100644 (file)
@@ -321,6 +321,9 @@ elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
     // they can be used to reason about object compatibility.
     return &InputSection<ELFT>::Discarded;
   case SHT_MIPS_REGINFO:
+    if (MipsReginfo)
+      fatal(getFilename(this) +
+            ": multiple SHT_MIPS_REGINFO sections are not allowed");
     MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
     return MipsReginfo.get();
   case SHT_MIPS_OPTIONS:
@@ -330,6 +333,9 @@ elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
     MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
     return MipsOptions.get();
   case SHT_MIPS_ABIFLAGS:
+    if (MipsAbiFlags)
+      fatal(getFilename(this) +
+            ": multiple SHT_MIPS_ABIFLAGS sections are not allowed");
     MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
     return MipsAbiFlags.get();
   case SHT_RELA:
diff --git a/lld/test/ELF/invalid/mips-multiple-abiflags.test b/lld/test/ELF/invalid/mips-multiple-abiflags.test
new file mode 100644 (file)
index 0000000..797233e
--- /dev/null
@@ -0,0 +1,21 @@
+# RUN: yaml2obj %s -o %t
+# RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+
+--- !ELF
+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:          .foo1
+    Type:          SHT_MIPS_ABIFLAGS
+    ISA:           MIPS64
+
+  - Name:          .foo2
+    Type:          SHT_MIPS_ABIFLAGS
+    ISA:           MIPS64
+
+# CHECK: multiple SHT_MIPS_ABIFLAGS sections are not allowed
diff --git a/lld/test/ELF/invalid/mips-multiple-reginfo.test b/lld/test/ELF/invalid/mips-multiple-reginfo.test
new file mode 100644 (file)
index 0000000..59aa038
--- /dev/null
@@ -0,0 +1,25 @@
+# RUN: yaml2obj %s -o %t
+# RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+
+--- !ELF
+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:          .foo1
+    Type:          SHT_MIPS_REGINFO
+    Flags:         [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:  16
+    Content:       "000000000000000000000000000000000000000000000000"
+
+  - Name:          .foo2
+    Type:          SHT_MIPS_REGINFO
+    Flags:         [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:  16
+    Content:       "000000000000000000000000000000000000000000000000"
+
+# CHECK: multiple SHT_MIPS_REGINFO sections are not allowed