Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1216,9 +1216,13 @@ return; } + uintX_t T = Off; Off = getFileAlignment(Off, Sec); Sec->setFileOffset(Off); Off += Sec->getSize(); + if (T > Off) + fatal("attemp to set offset for " + Sec->getName() + + " failed: output is too large"); } template void Writer::assignFileOffsetsBinary() { @@ -1381,6 +1385,8 @@ } template void Writer::openFile() { + if (FileSize > SIZE_MAX) + fatal("output file size is too large for this configuration"); ErrorOr> BufferOrErr = FileOutputBuffer::create(Config->OutputFile, FileSize, FileOutputBuffer::F_executable); Index: test/ELF/invalid/too-large-output-i386.s =================================================================== --- test/ELF/invalid/too-large-output-i386.s +++ test/ELF/invalid/too-large-output-i386.s @@ -0,0 +1,7 @@ +# REQUIRES: x86 + +## too-large-output-i386.elf contains section with address align. +## of 0xFFFFFFFF. Total file size calculation overflows because of that. +# RUN: not ld.lld -shared %S/Inputs/too-large-output-i386.elf -o %t 2>&1 \ +# RUN: | FileCheck %s +# CHECK: attemp to set offset for .text1 failed: output is too large