[lld] r240484 - COFF: Add names for logging/debugging to COMDAT chunks.

Rui Ueyama ruiu at google.com
Tue Jun 23 17:00:52 PDT 2015


Author: ruiu
Date: Tue Jun 23 19:00:52 2015
New Revision: 240484

URL: http://llvm.org/viewvc/llvm-project?rev=240484&view=rev
Log:
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.

Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/COFF/Chunks.h
    lld/trunk/COFF/InputFiles.cpp

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=240484&r1=240483&r2=240484&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Tue Jun 23 19:00:52 2015
@@ -145,22 +145,8 @@ bool SectionChunk::isCOMDAT() const {
   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 @@ SectionRef SectionChunk::getSectionRef()
   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.

Modified: lld/trunk/COFF/Chunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.h?rev=240484&r1=240483&r2=240484&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.h (original)
+++ lld/trunk/COFF/Chunks.h Tue Jun 23 19:00:52 2015
@@ -27,6 +27,7 @@ using llvm::object::coff_section;
 using llvm::sys::fs::file_magic;
 
 class Defined;
+class DefinedCOMDAT;
 class DefinedImportData;
 class ObjectFile;
 class OutputSection;
@@ -96,6 +97,10 @@ public:
   // 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 @@ public:
   // 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 @@ private:
   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.

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=240484&r1=240483&r2=240484&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Tue Jun 23 19:00:52 2015
@@ -212,9 +212,14 @@ SymbolBody *ObjectFile::createSymbolBody
     }
   }
   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;
 }





More information about the llvm-commits mailing list