diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/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 @@ -458,6 +460,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. @@ -466,6 +469,8 @@ std::atomic needsTlsLd{false}; void reset(); + + llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &); }; LLVM_LIBRARY_VISIBILITY extern Ctx ctx; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/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, @@ -1982,7 +1990,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()); @@ -2012,7 +2020,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()); @@ -2071,7 +2079,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; diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp --- a/lld/ELF/MapFile.cpp +++ b/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; diff --git a/lld/test/ELF/diagnostic-options.test b/lld/test/ELF/diagnostic-options.test new file mode 100644 --- /dev/null +++ b/lld/test/ELF/diagnostic-options.test @@ -0,0 +1,19 @@ +# RUN: ld.lld -o %t.out -shared /dev/null -m elf_x86_64 \ +# RUN: --print-archive-stats=%t --why-extract=%t --dependency-file=%t -Map=%t +# RUN: FileCheck -DOUT_FILE=%t.out --input-file=%t %s + +# RUN: ld.lld -o %t.out -shared /dev/null -m elf_x86_64 \ +# RUN: --print-archive-stats=- --why-extract=- --dependency-file=- -Map=- \ +# RUN: | FileCheck -DOUT_FILE=%t.out %s + +## From --print-archive-stats +# CHECK: members extracted archive + +## From --why-extract +# CHECK: reference extracted symbol + +## From --dependency-file +# CHECK: [[OUT_FILE]]: \ + +## From -Map +# CHECK: VMA LMA Size