From cbe392629b55999f2bd37e10048b0e00630a9d0d Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 2 Feb 2016 22:48:04 +0000 Subject: [PATCH] ELF: Do not exit if it cannot open an output file. It can fail to open an output file for various reasons, including lack of permission, too long filename, or the output file is not a mmap'able file. llvm-svn: 259596 --- lld/ELF/Writer.cpp | 14 +++++++++----- lld/test/ELF/driver.test | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 3a317b3..1e94b03 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -56,7 +56,7 @@ private: void assignAddresses(); void buildSectionMap(); void fixAbsoluteSymbols(); - void openFile(StringRef OutputPath); + bool openFile(); void writeHeader(); void writeSections(); bool isDiscarded(InputSectionBase *IS) const; @@ -172,7 +172,8 @@ template void Writer::run() { return; assignAddresses(); fixAbsoluteSymbols(); - openFile(Config->OutputFile); + if (!openFile()) + return; writeHeader(); writeSections(); if (HasError) @@ -1384,11 +1385,14 @@ template void Writer::writeHeader() { Sec->writeHeaderTo(++SHdrs); } -template void Writer::openFile(StringRef Path) { +template bool Writer::openFile() { ErrorOr> BufferOrErr = - FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); - fatal(BufferOrErr, "failed to open " + Path); + FileOutputBuffer::create(Config->OutputFile, FileSize, + FileOutputBuffer::F_executable); + if (error(BufferOrErr, "failed to open " + Config->OutputFile)) + return false; Buffer = std::move(*BufferOrErr); + return true; } // Write section contents to a mmap'ed file. diff --git a/lld/test/ELF/driver.test b/lld/test/ELF/driver.test index 13f040e..70fe7ba 100644 --- a/lld/test/ELF/driver.test +++ b/lld/test/ELF/driver.test @@ -1,3 +1,5 @@ +# REQUIRES: x86 + # RUN: not ld.lld -unknown1 -unknown2 -m foo /no/such/file -lnosuchlib \ # RUN: 2>&1 | FileCheck %s @@ -7,3 +9,12 @@ # CHECK: Unknown emulation: foo # CHECK: cannot open /no/such/file # CHECK: Unable to find library -lnosuchlib + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck -check-prefix=CHECK2 %s + +# CHECK2: failed to open + +.globl _start +_start: + nop -- 2.7.4