Do not use errs() or outs() directly. Instead use message(), log() or error()

LLD is a multi-threaded program. errs() or outs() are not guaranteed
to be thread-safe (they are actually not).

LLD's message(), log() or error() are thread-safe. We should use them.

llvm-svn: 295787
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index b8d648b..5663ede 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -299,7 +299,7 @@
   // in help to print the version information, GNU ld just normally exits,
   // while gold can continue linking. We are compatible with ld.bfd here.
   if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v))
-    outs() << getLLDVersion() << "\n";
+    message(getLLDVersion());
   if (Args.hasArg(OPT_version))
     return;
 
diff --git a/lld/ELF/Error.cpp b/lld/ELF/Error.cpp
index 5fbb59d..700ef70 100644
--- a/lld/ELF/Error.cpp
+++ b/lld/ELF/Error.cpp
@@ -45,9 +45,16 @@
 }
 
 void elf::log(const Twine &Msg) {
-  std::lock_guard<std::mutex> Lock(Mu);
-  if (Config->Verbose)
+  if (Config->Verbose) {
+    std::lock_guard<std::mutex> Lock(Mu);
     outs() << Argv0 << ": " << Msg << "\n";
+  }
+}
+
+void elf::message(const Twine &Msg) {
+  std::lock_guard<std::mutex> Lock(Mu);
+  outs() << Msg << "\n";
+  outs().flush();
 }
 
 void elf::warn(const Twine &Msg) {
diff --git a/lld/ELF/Error.h b/lld/ELF/Error.h
index 95fab7e..e26f2da 100644
--- a/lld/ELF/Error.h
+++ b/lld/ELF/Error.h
@@ -36,6 +36,7 @@
 extern llvm::StringRef Argv0;
 
 void log(const Twine &Msg);
+void message(const Twine &Msg);
 void warn(const Twine &Msg);
 void error(const Twine &Msg);
 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 8564b25..ffee291 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -56,14 +56,13 @@
 }
 
 Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
-  if (Config->Verbose)
-    outs() << Path << "\n";
-
+  log(Path);
   auto MBOrErr = MemoryBuffer::getFile(Path);
   if (auto EC = MBOrErr.getError()) {
     error("cannot open " + Path + ": " + EC.message());
     return None;
   }
+
   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
   MemoryBufferRef MBRef = MB->getMemBufferRef();
   make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 7998d89..9eff851 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -568,8 +568,8 @@
 template <class ELFT> static void reportDiscarded(InputSectionBase<ELFT> *IS) {
   if (!Config->PrintGcSections)
     return;
-  errs() << "removing unused section from '" << IS->Name << "' in file '"
-         << IS->getFile()->getName() << "'\n";
+  message("removing unused section from '" + IS->Name + "' in file '" +
+          IS->getFile()->getName());
 }
 
 template <class ELFT>
diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp
index 1f23f1f..76e6e0e 100644
--- a/lld/ELF/SymbolTable.cpp
+++ b/lld/ELF/SymbolTable.cpp
@@ -75,7 +75,7 @@
   }
 
   if (Config->Trace)
-    outs() << toString(File) << "\n";
+    message(toString(File));
 
   // .so file
   if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index fc99545..f3c2a0e 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -293,15 +293,15 @@
 // Print out a log message for --trace-symbol.
 void elf::printTraceSymbol(Symbol *Sym) {
   SymbolBody *B = Sym->body();
-  outs() << toString(B->File);
-
+  std::string S;
   if (B->isUndefined())
-    outs() << ": reference to ";
+    S = ": reference to ";
   else if (B->isCommon())
-    outs() << ": common definition of ";
+    S = ": common definition of ";
   else
-    outs() << ": definition of ";
-  outs() << B->getName() << "\n";
+    S = ": definition of ";
+
+  message(toString(B->File) + S + B->getName());
 }
 
 // Returns a symbol for an error message.