Index: lld/Common/ErrorHandler.cpp =================================================================== --- lld/Common/ErrorHandler.cpp +++ lld/Common/ErrorHandler.cpp @@ -48,7 +48,8 @@ void lld::exitLld(int Val) { // Delete the output buffer so that any tempory file is deleted. - errorHandler().OutputBuffer.reset(); + if (errorHandler().OutputBuffer) + errorHandler().OutputBuffer->discard(); // Dealloc/destroy ManagedStatic variables before calling // _exit(). In a non-LTO build, this is a nop. In an LTO Index: llvm/include/llvm/Support/FileOutputBuffer.h =================================================================== --- llvm/include/llvm/Support/FileOutputBuffer.h +++ llvm/include/llvm/Support/FileOutputBuffer.h @@ -76,6 +76,10 @@ /// deallocates the buffer and the target file is never written. virtual ~FileOutputBuffer() {} + /// This removes the temporary file (unless it already was committed) + /// but keeps the memory mapping alive. + virtual void discard() {} + protected: FileOutputBuffer(StringRef Path) : FinalPath(Path) {} Index: llvm/lib/Support/FileOutputBuffer.cpp =================================================================== --- llvm/lib/Support/FileOutputBuffer.cpp +++ llvm/lib/Support/FileOutputBuffer.cpp @@ -61,6 +61,13 @@ consumeError(Temp.discard()); } + void discard() override { + // Delete the temp file if it still was open, but keeping the mapping + // active. (Closing the FD while keeping the mapping alive seems to work + // on windows as well.) + consumeError(Temp.discard()); + } + private: std::unique_ptr Buffer; fs::TempFile Temp;