Skip to content

Commit cf1b9ac

Browse files
authored
[lld] check disk space before writing to disk if using mmap (#5)
1 parent 8bded6c commit cf1b9ac

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lld/ELF/Writer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "Config.h"
1414
#include "InputFiles.h"
1515
#include "LinkerScript.h"
16+
#include "llvm/Support/FileSystem.h"
17+
#include "llvm/Support/Path.h"
1618
#include "MapFile.h"
1719
#include "OutputSections.h"
1820
#include "Relocations.h"
@@ -2818,6 +2820,18 @@ template <class ELFT> void Writer<ELFT>::openFile() {
28182820
flags |= FileOutputBuffer::F_executable;
28192821
if (!config->mmapOutputFile)
28202822
flags |= FileOutputBuffer::F_no_mmap;
2823+
if (config->mmapOutputFile) {
2824+
// LLD relies on [fallocate] to mmap the output.
2825+
// In case there's no space left on the device
2826+
// it will error with SIGBUS, which is confusing
2827+
// for users
2828+
auto ErrOrSpaceInfo = sys::fs::disk_space(sys::path::parent_path(config->outputFile));
2829+
if (!ErrOrSpaceInfo)
2830+
error("Can't get remaining size on disk");
2831+
if (ErrOrSpaceInfo.get().free < fileSize)
2832+
error("failed to open " + config->outputFile + ": " +
2833+
"No Space Left on Device");
2834+
}
28212835
Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
28222836
FileOutputBuffer::create(config->outputFile, fileSize, flags);
28232837

0 commit comments

Comments
 (0)