Skip to content

Commit 441b382

Browse files
committed
[lld] check disk space before writing to disk if using mmap (#5)
1 parent 25cf784 commit 441b382

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lld/ELF/Writer.cpp

+14
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"
@@ -2762,6 +2764,18 @@ template <class ELFT> void Writer<ELFT>::openFile() {
27622764
flags |= FileOutputBuffer::F_executable;
27632765
if (!config->mmapOutputFile)
27642766
flags |= FileOutputBuffer::F_no_mmap;
2767+
if (config->mmapOutputFile) {
2768+
// LLD relies on [fallocate] to mmap the output.
2769+
// In case there's no space left on the device
2770+
// it will error with SIGBUS, which is confusing
2771+
// for users
2772+
auto ErrOrSpaceInfo = sys::fs::disk_space(sys::path::parent_path(config->outputFile));
2773+
if (!ErrOrSpaceInfo)
2774+
error("Can't get remaining size on disk");
2775+
if (ErrOrSpaceInfo.get().free < fileSize)
2776+
error("failed to open " + config->outputFile + ": " +
2777+
"No Space Left on Device");
2778+
}
27652779
Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
27662780
FileOutputBuffer::create(config->outputFile, fileSize, flags);
27672781

0 commit comments

Comments
 (0)