Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -48,10 +48,14 @@ 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(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(Header->sh_addralign, 1); + Alignment = V; } template size_t InputSectionBase::getSize() const { Index: test/ELF/invalid/section-alignment.test =================================================================== --- test/ELF/invalid/section-alignment.test +++ test/ELF/invalid/section-alignment.test @@ -13,7 +13,7 @@ - Name: .text Type: SHT_PROGBITS Flags: [ SHF_ALLOC, SHF_EXECINSTR ] - AddressAlign: 0x1000000000000001 + AddressAlign: 0x1000000000000000 Content: "00000000" # CHECK: section sh_addralign is too large 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 not a power of 2