[lld] r259596 - ELF: Do not exit if it cannot open an output file.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 14:48:04 PST 2016


Author: ruiu
Date: Tue Feb  2 16:48:04 2016
New Revision: 259596

URL: http://llvm.org/viewvc/llvm-project?rev=259596&view=rev
Log:
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.

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/driver.test

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=259596&r1=259595&r2=259596&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Feb  2 16:48:04 2016
@@ -56,7 +56,7 @@ private:
   void assignAddresses();
   void buildSectionMap();
   void fixAbsoluteSymbols();
-  void openFile(StringRef OutputPath);
+  bool openFile();
   void writeHeader();
   void writeSections();
   bool isDiscarded(InputSectionBase<ELFT> *IS) const;
@@ -172,7 +172,8 @@ template <class ELFT> void Writer<ELFT>:
     return;
   assignAddresses();
   fixAbsoluteSymbols();
-  openFile(Config->OutputFile);
+  if (!openFile())
+    return;
   writeHeader();
   writeSections();
   if (HasError)
@@ -1384,11 +1385,14 @@ template <class ELFT> void Writer<ELFT>:
     Sec->writeHeaderTo(++SHdrs);
 }
 
-template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
+template <class ELFT> bool Writer<ELFT>::openFile() {
   ErrorOr<std::unique_ptr<FileOutputBuffer>> 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.

Modified: lld/trunk/test/ELF/driver.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/driver.test?rev=259596&r1=259595&r2=259596&view=diff
==============================================================================
--- lld/trunk/test/ELF/driver.test (original)
+++ lld/trunk/test/ELF/driver.test Tue Feb  2 16:48:04 2016
@@ -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




More information about the llvm-commits mailing list