diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2782,12 +2782,15 @@ // 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) { + if (os->size == 0) + continue; + uint64_t lastByte = os->addr + (os->size - 1); + 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 diff --git a/lld/test/ELF/linkerscript/i386-sections-until-end-of-va.s b/lld/test/ELF/linkerscript/i386-sections-until-end-of-va.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/i386-sections-until-end-of-va.s @@ -0,0 +1,12 @@ +# 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: ld.lld -o /dev/null --script %t.script %t.o 2>&1 + +## .bar section has data in [0xfffffff0, 0xfffffff0 + 0x10] == +## [0xfffffff0, 0]. There is no overflow. + +.section .bar,"ax",@progbits +.zero 0x10 diff --git a/lld/test/ELF/linkerscript/sections-until-end-of-va.s b/lld/test/ELF/linkerscript/sections-until-end-of-va.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/sections-until-end-of-va.s @@ -0,0 +1,12 @@ +# 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, 0]. There is no overflow. + +.section .bar,"ax",@progbits +.zero 0x10