Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -2782,12 +2782,14 @@ // ranges and the virtual address ranges don't overlap template void Writer::checkSections() { // First, check that section's VAs fit in available address space for target. - for (OutputSection *os : outputSections) - if ((os->addr + os->size < os->addr) || - (!ELFT::Is64Bits && os->addr + os->size > UINT32_MAX)) + for (OutputSection *os : outputSections) { + // For empty sections, we want the base VA to fit. + uint64_t lastByte = os->addr + (os->size ? os->size - 1 : 0); + if ((lastByte < os->addr) || (!ELFT::Is64Bits && lastByte > UINT32_MAX)) errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) + " of size 0x" + utohexstr(os->size) + " exceeds available address space"); + } // Check for overlapping file offsets. In this case we need to skip any // section marked as SHT_NOBITS. These sections don't actually occupy space in Index: lld/test/ELF/linkerscript/i386-sections-until-end-of-va.s =================================================================== --- /dev/null +++ lld/test/ELF/linkerscript/i386-sections-until-end-of-va.s @@ -0,0 +1,14 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=i386-unknown-linux %s -o %t.o + +# RUN: echo "SECTIONS { . = 0xfffffff0;" > %t.script +# RUN: echo " .bar : { *(.bar*) }" >> %t.script +# RUN: echo " /DISCARD/ : { *(.text) } }" >> %t.script +# RUN: ld.lld -o /dev/null --script %t.script %t.o 2>&1 + +## .bar section has data in [0xfffffff0, 0xfffffff0 + 0x10) == +## [0xfffffff0, 0x100000000). The address of the last byte of the section is +## 0xffffffff, so this is not considered overflow. + +.section .bar,"ax",@progbits +.zero 0x10 Index: lld/test/ELF/linkerscript/sections-until-end-of-va.s =================================================================== --- /dev/null +++ lld/test/ELF/linkerscript/sections-until-end-of-va.s @@ -0,0 +1,13 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o + +# RUN: echo "SECTIONS { . = 0xfffffffffffffff0;" > %t.script +# RUN: echo " .bar : { *(.bar*) } }" >> %t.script +# RUN: ld.lld -o /dev/null --script %t.script %t.o 2>&1 + +## .bar section has data in [0xfffffffffffffff0, 0xfffffffffffffff0 + 0x10) == +## [0xfffffffffffffff0, 0x10000000000000000). The address of the last byte of +## the section is 0xffffffffffffffff, so this is not considered overflow. + +.section .bar,"ax",@progbits +.zero 0x10