aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/FileOutputBuffer.cpp
diff options
context:
space:
mode:
authorFangrui Song <maskray@google.com>2020-05-04 09:45:41 -0700
committerFangrui Song <maskray@google.com>2020-05-05 08:11:58 -0700
commit9d53db2aa098ca5136c352c38710ea48bf078e7a (patch)
treec4385325c7e8cb2914f553a112d0f29ec7ec6941 /llvm/lib/Support/FileOutputBuffer.cpp
parent47f5066553240b1559eed06706688ed8cce56fe7 (diff)
downloadllvm-9d53db2aa098ca5136c352c38710ea48bf078e7a.zip
llvm-9d53db2aa098ca5136c352c38710ea48bf078e7a.tar.gz
llvm-9d53db2aa098ca5136c352c38710ea48bf078e7a.tar.bz2
[Support] Allow FileOutputBuffer::create to create an empty file
Size==0 triggers `assert(Size != 0)` in mapped_file_region::init. I plan to use an empty file in D79339 (llvm-objcopy --dump-section). According to POSIX, "If len is zero, mmap() shall fail and no mapping shall be established." Just specialize case Size=0 to use createInMemoryBuffer. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D79338
Diffstat (limited to 'llvm/lib/Support/FileOutputBuffer.cpp')
-rw-r--r--llvm/lib/Support/FileOutputBuffer.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/llvm/lib/Support/FileOutputBuffer.cpp b/llvm/lib/Support/FileOutputBuffer.cpp
index 0a5306f..ec12820 100644
--- a/llvm/lib/Support/FileOutputBuffer.cpp
+++ b/llvm/lib/Support/FileOutputBuffer.cpp
@@ -172,6 +172,10 @@ FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
if (Flags & F_executable)
Mode |= fs::all_exe;
+ // If Size is zero, don't use mmap which will fail with EINVAL.
+ if (Size == 0)
+ return createInMemoryBuffer(Path, Size, Mode);
+
fs::file_status Stat;
fs::status(Path, Stat);