COFF: Add names for logging/debugging to COMDAT chunks.

Chunks are basically unnamed chunks of bytes, and we don't like
to give them names. However, for logging or debugging, we want to
know symbols names of functions for COMDAT chunks. (For example,
we want to print out "we have removed unreferenced COMDAT section
which contains a function FOOBAR.")

This patch is to do that.

llvm-svn: 240484
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 18963f2..b931d11 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -145,22 +145,8 @@
   return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
 }
 
-// Prints "Discarded <symbol>" for all external function symbols.
 void SectionChunk::printDiscardedMessage() {
-  uint32_t E = File->getCOFFObj()->getNumberOfSymbols();
-  for (uint32_t I = 0; I < E; ++I) {
-    auto SrefOrErr = File->getCOFFObj()->getSymbol(I);
-    COFFSymbolRef Sym = SrefOrErr.get();
-    if (uint32_t(Sym.getSectionNumber()) != SectionIndex)
-      continue;
-    if (!Sym.isFunctionDefinition())
-      continue;
-    StringRef SymbolName;
-    File->getCOFFObj()->getSymbolName(Sym, SymbolName);
-    llvm::outs() << "Discarded " << SymbolName << " from "
-                 << File->getShortName() << "\n";
-    I += Sym.getNumberOfAuxSymbols();
-  }
+  llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
 }
 
 SectionRef SectionChunk::getSectionRef() {
@@ -169,6 +155,10 @@
   return SectionRef(Ref, File->getCOFFObj());
 }
 
+StringRef SectionChunk::getDebugName() {
+  return Sym->getName();
+}
+
 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
   // Common symbols are aligned on natural boundaries up to 32 bytes.
   // This is what MSVC link.exe does.
diff --git a/lld/COFF/Chunks.h b/lld/COFF/Chunks.h
index 88d944c..41cad18 100644
--- a/lld/COFF/Chunks.h
+++ b/lld/COFF/Chunks.h
@@ -27,6 +27,7 @@
 using llvm::sys::fs::file_magic;
 
 class Defined;
+class DefinedCOMDAT;
 class DefinedImportData;
 class ObjectFile;
 class OutputSection;
@@ -96,6 +97,10 @@
   // Collect all locations that contain absolute addresses for base relocations.
   virtual void getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {}
 
+  // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
+  // bytes, so this is used only for logging or debugging.
+  virtual StringRef getDebugName() { return ""; }
+
 protected:
   // The RVA of this chunk in the output. The writer sets a value.
   uint64_t RVA = 0;
@@ -133,6 +138,9 @@
   // and its children are treated as a group by the garbage collector.
   void addAssociative(SectionChunk *Child);
 
+  StringRef getDebugName() override;
+  void setSymbol(DefinedCOMDAT *S) { if (!Sym) Sym = S; }
+
 private:
   void mark() override;
   SectionRef getSectionRef();
@@ -145,6 +153,10 @@
   uint32_t SectionIndex;
   StringRef SectionName;
   std::vector<Chunk *> AssocChildren;
+
+  // Chunks are basically unnamed chunks of bytes.
+  // Symbols are associated for debugging and logging purposs only.
+  DefinedCOMDAT *Sym = nullptr;
 };
 
 // A chunk for common symbols. Common chunks don't have actual data.
diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index f2734ea..0c751e9 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -212,9 +212,14 @@
     }
   }
   if (Chunk *C = SparseChunks[Sym.getSectionNumber()]) {
-    if (C->isCOMDAT())
-      return new (Alloc) DefinedCOMDAT(COFFObj.get(), Sym, C);
-    return new (Alloc) DefinedRegular(COFFObj.get(), Sym, C);
+    if (!C->isCOMDAT())
+      return new (Alloc) DefinedRegular(COFFObj.get(), Sym, C);
+    auto *B = new (Alloc) DefinedCOMDAT(COFFObj.get(), Sym, C);
+    if (Sym.getValue() == 0 && !AuxP) {
+      auto *SC = reinterpret_cast<SectionChunk *>(C);
+      SC->setSymbol(B);
+    }
+    return B;
   }
   return nullptr;
 }