[ELF] - Do not crash on invalid section alignment.
authorGeorge Rimar <grimar@accesssoftek.com>
Mon, 3 Oct 2016 10:04:38 +0000 (10:04 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Mon, 3 Oct 2016 10:04:38 +0000 (10:04 +0000)
Case was revealed by id_000010,sig_08,src_000000,op_havoc,rep_4 from PR30540.

Out implementation uses uint32 for storing section alignment value,
what seems reasonable, though if value exceeds 32 bits bounds we have
truncation and final value of 0.

Patch fixes the issue.

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

llvm-svn: 283097

lld/ELF/InputSection.cpp
lld/test/ELF/invalid/section-alignment.test [new file with mode: 0644]

index 789b4e4..09e640e 100644 (file)
@@ -45,6 +45,8 @@ InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
       Header(Hdr), File(File), Repl(this) {
   // The ELF spec states that a value of 0 means the section has
   // no alignment constraits.
+  if (Header->sh_addralign > UINT32_MAX)
+    fatal(getFilename(File) + ": section sh_addralign is too large");
   Alignment = std::max<uintX_t>(Header->sh_addralign, 1);
 }
 
diff --git a/lld/test/ELF/invalid/section-alignment.test b/lld/test/ELF/invalid/section-alignment.test
new file mode 100644 (file)
index 0000000..1c7afa7
--- /dev/null
@@ -0,0 +1,19 @@
+# RUN: yaml2obj %s -o %t
+# RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+
+## In current lld implementation, we do not accept sh_addralign
+## larger than UINT32_MAX.
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x1000000000000001
+    Content:         "00000000"
+
+# CHECK: section sh_addralign is too large