[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

llvm-svn: 283544
This commit is contained in:
George Rimar 2016-10-07 12:27:45 +00:00
parent 00b7444dc1
commit 7c213fd17e
4 changed files with 13 additions and 4 deletions

View File

@ -48,10 +48,14 @@ InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
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 {

View File

@ -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

View File

@ -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