[lld] r283544 - [ELF] - Check that section alignment is a power of 2.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 05:27:46 PDT 2016


Author: grimar
Date: Fri Oct  7 07:27:45 2016
New Revision: 283544

URL: http://llvm.org/viewvc/llvm-project?rev=283544&view=rev
Log:
[ELF] - Check that section alignment is a power of 2.

I found that this check still may be useful in some cases.
At fact since we use uint32_t alignment, then maximum value
that is valid for us is 0x80000000. But some broken files,
for example file from testcase may have greater value.
Because of that offset calculation overflow and crash happens.

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

Added:
    lld/trunk/test/ELF/invalid/Inputs/section-alignment-notpow2.elf   (with props)
    lld/trunk/test/ELF/invalid/section-alignment2.s
Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/test/ELF/invalid/section-alignment.test

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=283544&r1=283543&r2=283544&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Fri Oct  7 07:27:45 2016
@@ -48,10 +48,14 @@ InputSectionBase<ELFT>::InputSectionBase
                        Hdr->sh_flags & SHF_COMPRESSED, !Config->GcSections),
       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)
+  // no alignment constraits. Also we reject object files having insanely large
+  // alignment requirements and may want to relax this limitation in the future.
+  uintX_t V = std::max<uintX_t>(Header->sh_addralign, 1);
+  if (!isPowerOf2_64(V))
+    fatal(getFilename(File) + ": section sh_addralign is not a power of 2");
+  if (V > UINT32_MAX)
     fatal(getFilename(File) + ": section sh_addralign is too large");
-  Alignment = std::max<uintX_t>(Header->sh_addralign, 1);
+  Alignment = V;
 }
 
 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {

Added: lld/trunk/test/ELF/invalid/Inputs/section-alignment-notpow2.elf
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/Inputs/section-alignment-notpow2.elf?rev=283544&view=auto
==============================================================================
Binary file - no diff available.

Propchange: lld/trunk/test/ELF/invalid/Inputs/section-alignment-notpow2.elf
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: lld/trunk/test/ELF/invalid/section-alignment.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/section-alignment.test?rev=283544&r1=283543&r2=283544&view=diff
==============================================================================
--- lld/trunk/test/ELF/invalid/section-alignment.test (original)
+++ lld/trunk/test/ELF/invalid/section-alignment.test Fri Oct  7 07:27:45 2016
@@ -13,7 +13,7 @@ Sections:
   - Name:            .text
     Type:            SHT_PROGBITS
     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
-    AddressAlign:    0x1000000000000001
+    AddressAlign:    0x1000000000000000
     Content:         "00000000"
 
 # CHECK: section sh_addralign is too large

Added: lld/trunk/test/ELF/invalid/section-alignment2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/section-alignment2.s?rev=283544&view=auto
==============================================================================
--- lld/trunk/test/ELF/invalid/section-alignment2.s (added)
+++ lld/trunk/test/ELF/invalid/section-alignment2.s Fri Oct  7 07:27:45 2016
@@ -0,0 +1,5 @@
+## section-alignment-notpow2.elf has section alignment 
+## 0xFFFFFFFF which is not a power of 2.
+# RUN: not ld.lld %p/Inputs/section-alignment-notpow2.elf -o %t2 2>&1 | \
+# RUN:   FileCheck %s
+# CHECK: section sh_addralign is not a power of 2




More information about the llvm-commits mailing list