[PATCH] D25324: [ELF] - Check that section alignment is a power of 2.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 07:21:38 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, davide.
grimar added subscribers: llvm-commits, grimar, evgeny777.

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.


https://reviews.llvm.org/D25324

Files:
  ELF/InputSection.cpp
  test/ELF/invalid/Inputs/section-alignment-notpow2.elf
  test/ELF/invalid/section-alignment.test
  test/ELF/invalid/section-alignment2.s


Index: test/ELF/invalid/section-alignment2.s
===================================================================
--- test/ELF/invalid/section-alignment2.s
+++ test/ELF/invalid/section-alignment2.s
@@ -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 invalid
Index: test/ELF/invalid/section-alignment.test
===================================================================
--- test/ELF/invalid/section-alignment.test
+++ test/ELF/invalid/section-alignment.test
@@ -16,4 +16,4 @@
     AddressAlign:    0x1000000000000001
     Content:         "00000000"
 
-# CHECK: section sh_addralign is too large
+# CHECK: section sh_addralign is invalid
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -45,8 +45,9 @@
       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");
+  if (Header->sh_addralign > UINT32_MAX ||
+      (Header->sh_addralign && !isPowerOf2_32(Header->sh_addralign)))
+    fatal(getFilename(File) + ": section sh_addralign is invalid");
   Alignment = std::max<uintX_t>(Header->sh_addralign, 1);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25324.73793.patch
Type: text/x-patch
Size: 1523 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/89f9230a/attachment.bin>


More information about the llvm-commits mailing list