[lld] 67a212a - [ELF] Make subsequent opens to auxiliary files append

Alex Brachet via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 11 04:09:06 PDT 2023


Author: Alex Brachet
Date: 2023-07-11T11:08:57Z
New Revision: 67a212af4c24426de6e436e9b82590d41faa665c

URL: https://github.com/llvm/llvm-project/commit/67a212af4c24426de6e436e9b82590d41faa665c
DIFF: https://github.com/llvm/llvm-project/commit/67a212af4c24426de6e436e9b82590d41faa665c.diff

LOG: [ELF] Make subsequent opens to auxiliary files append

Previously, the same file could be used across diagnostic options but
the file would be silently overwritten by the whichever option gets
handled last.

Differential Revision: https://reviews.llvm.org/D153873

Added: 
    lld/test/ELF/diagnostic-options.test

Modified: 
    lld/ELF/Config.h
    lld/ELF/Driver.cpp
    lld/ELF/MapFile.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 2577397023e8be..797b7720a38fe5 100644
--- 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 <atomic>
@@ -458,6 +460,7 @@ struct Ctx {
   llvm::DenseMap<const Symbol *,
                  std::pair<const InputFile *, const InputFile *>>
       backwardReferences;
+  llvm::SmallSet<llvm::StringRef, 0> auxiliaryFiles;
   // True if SHT_LLVM_SYMPART is used.
   std::atomic<bool> hasSympart{false};
   // True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared.
@@ -466,6 +469,8 @@ struct Ctx {
   std::atomic<bool> 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
index 1252e343cd42e4..26283e1fac0969 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -107,6 +107,14 @@ void Ctx::reset() {
   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<const char *> args, llvm::raw_ostream &stdoutOS,
@@ -1982,7 +1990,7 @@ static void writeArchiveStats() {
     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 @@ static void writeWhyExtract() {
     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 @@ static void reportBackrefs() {
 // 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
index 9bda8f11467a63..1b6dfcc57176b5 100644
--- a/lld/ELF/MapFile.cpp
+++ b/lld/ELF/MapFile.cpp
@@ -263,7 +263,7 @@ void elf::writeMapAndCref() {
   // 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
index 00000000000000..b158a79c9d456d
--- /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


        


More information about the llvm-commits mailing list