Index: lld/ELF/Config.h =================================================================== --- lld/ELF/Config.h +++ lld/ELF/Config.h @@ -14,6 +14,7 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/BinaryFormat/ELF.h" @@ -23,6 +24,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/Compression.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/GlobPattern.h" #include "llvm/Support/PrettyStackTrace.h" #include @@ -453,6 +455,7 @@ llvm::DenseMap> backwardReferences; + llvm::SmallSet auxiliaryFiles; // True if SHT_LLVM_SYMPART is used. std::atomic hasSympart{false}; // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared. @@ -461,6 +464,8 @@ std::atomic needsTlsLd{false}; void reset(); + + llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &); }; LLVM_LIBRARY_VISIBILITY extern Ctx ctx; Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -107,6 +107,14 @@ needsTlsLd.store(false, std::memory_order_relaxed); } +llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename, + std::error_code &ec) { + using namespace llvm::sys::fs; + OpenFlags flags = + auxiliaryFiles.insert(filename).second ? OF_None : OF_Append; + return {filename, ec, flags}; +} + namespace lld { namespace elf { bool link(ArrayRef args, llvm::raw_ostream &stdoutOS, @@ -1957,7 +1965,7 @@ return; std::error_code ec; - raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None); + raw_fd_ostream os = ctx.openAuxiliaryFile(config->printArchiveStats, ec); if (ec) { error("--print-archive-stats=: cannot open " + config->printArchiveStats + ": " + ec.message()); @@ -1987,7 +1995,7 @@ return; std::error_code ec; - raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None); + raw_fd_ostream os = ctx.openAuxiliaryFile(config->whyExtract, ec); if (ec) { error("cannot open --why-extract= file " + config->whyExtract + ": " + ec.message()); @@ -2046,7 +2054,7 @@ // easily. static void writeDependencyFile() { std::error_code ec; - raw_fd_ostream os(config->dependencyFile, ec, sys::fs::OF_None); + raw_fd_ostream os = ctx.openAuxiliaryFile(config->dependencyFile, ec); if (ec) { error("cannot open " + config->dependencyFile + ": " + ec.message()); return; Index: lld/ELF/MapFile.cpp =================================================================== --- lld/ELF/MapFile.cpp +++ lld/ELF/MapFile.cpp @@ -263,7 +263,7 @@ // Open a map file for writing. std::error_code ec; StringRef mapFile = config->mapFile.empty() ? "-" : config->mapFile; - raw_fd_ostream os(mapFile, ec, sys::fs::OF_None); + raw_fd_ostream os = ctx.openAuxiliaryFile(mapFile, ec); if (ec) { error("cannot open " + mapFile + ": " + ec.message()); return; Index: lld/test/ELF/diagnostic-options.test =================================================================== --- /dev/null +++ lld/test/ELF/diagnostic-options.test @@ -0,0 +1,19 @@ +# RUN: ld.lld -o /dev/null -shared /dev/null -m elf_x86_64 \ +# RUN: --print-archive-stats=%t --why-extract=%t --dependency-file=%t -Map=%t +# RUN: FileCheck --input-file=%t %s + +# RUN: ld.lld -o /dev/null -shared /dev/null -m elf_x86_64 \ +# RUN: --print-archive-stats=- --why-extract=- --dependency-file=- -Map=- \ +# RUN: | FileCheck %s + +## From --print-archive-stats +# CHECK: members extracted archive + +## From --why-extract +# CHECK: reference extracted symbol + +## From --dependency-file +# CHECK: /dev/null: \ + +## From -Map +# CHECK: VMA LMA Size