Index: Common/ErrorHandler.cpp
===================================================================
--- Common/ErrorHandler.cpp
+++ Common/ErrorHandler.cpp
@@ -46,6 +46,9 @@
 }
 
 void lld::exitLld(int Val) {
+  // Delete the output buffer so that any tempory file is deleted.
+  errorHandler().OutputBuffer.reset();
+
   // Dealloc/destroy ManagedStatic variables before calling
   // _exit(). In a non-LTO build, this is a nop. In an LTO
   // build allows us to get the output of -time-passes.
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -22,7 +22,6 @@
 #include "lld/Common/Threads.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSwitch.h"
-#include "llvm/Support/FileOutputBuffer.h"
 #include <climits>
 
 using namespace llvm;
@@ -38,6 +37,7 @@
 // The writer writes a SymbolTable result to a file.
 template <class ELFT> class Writer {
 public:
+  Writer() : Buffer(errorHandler().OutputBuffer) {}
   typedef typename ELFT::Shdr Elf_Shdr;
   typedef typename ELFT::Ehdr Elf_Ehdr;
   typedef typename ELFT::Phdr Elf_Phdr;
@@ -70,7 +70,7 @@
   void writeSectionsBinary();
   void writeBuildId();
 
-  std::unique_ptr<FileOutputBuffer> Buffer;
+  std::unique_ptr<FileOutputBuffer> &Buffer;
 
   void addRelIpltSymbols();
   void addStartEndSymbols();
Index: include/lld/Common/ErrorHandler.h
===================================================================
--- include/lld/Common/ErrorHandler.h
+++ include/lld/Common/ErrorHandler.h
@@ -31,6 +31,7 @@
 #include "lld/Common/LLVM.h"
 
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileOutputBuffer.h"
 
 namespace lld {
 
@@ -52,6 +53,8 @@
   void message(const Twine &Msg);
   void warn(const Twine &Msg);
 
+  std::unique_ptr<llvm::FileOutputBuffer> OutputBuffer;
+
 private:
   void print(StringRef S, raw_ostream::Colors C);
 };