COFF: Devirtualize mark(), markLive() and isCOMDAT().
Only SectionChunk can be dead-stripped. Previously,
all types of chunks implemented these functions,
but their functions were blank.
Likewise, only DefinedRegular and DefinedCOMDAT symbols
can be dead-stripped. markLive() function was implemented
for other symbol types, but they were blank.
I started thinking that the change I made in r240319 was
a mistake. I separated DefinedCOMDAT from DefinedRegular
because I thought that would make the code cleaner, but now
we want to handle them as the same type here. Maybe we
should roll it back.
This change should improve readability a bit as this removes
some dubious uses of reinterpret_cast. Previously, we
assumed that all COMDAT chunks are actually SectionChunks,
which was not very obvious.
llvm-svn: 240675
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 2acc722..1369bdf 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -28,7 +28,7 @@
namespace coff {
SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H)
- : File(F), Ptr(this), Header(H),
+ : Chunk(SectionKind), File(F), Ptr(this), Header(H),
Relocs(File->getCOFFObj()->getRelocations(Header)),
NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
// Initialize SectionName.
@@ -39,16 +39,9 @@
if (Shift > 0)
Align = uint32_t(1) << (Shift - 1);
- // When a new chunk is created, we don't if if it's going to make it
- // to the final output. Initially all sections are unmarked in terms
- // of garbage collection. The writer will call markLive() to mark
- // all reachable section chunks.
- Live = false;
-
// COMDAT sections are not GC root. Non-text sections are not
// subject of garbage collection (thus they are root).
- if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE))
- Root = true;
+ Root = !isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
}
static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
@@ -93,13 +86,17 @@
// Mark all symbols listed in the relocation table for this section.
for (const coff_relocation &Rel : Relocs) {
SymbolBody *B = File->getSymbolBody(Rel.SymbolTableIndex);
- if (auto *Def = dyn_cast<Defined>(B))
- Def->markLive();
+ if (auto *D = dyn_cast<DefinedRegular>(B)) {
+ D->markLive();
+ } else if (auto *D = dyn_cast<DefinedCOMDAT>(B)) {
+ D->markLive();
+ }
}
// Mark associative sections if any.
for (Chunk *C : AssocChildren)
- C->markLive();
+ if (auto *SC = dyn_cast<SectionChunk>(C))
+ SC->markLive();
}
void SectionChunk::addAssociative(SectionChunk *Child) {
@@ -139,7 +136,7 @@
return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
}
-void SectionChunk::printDiscardedMessage() {
+void SectionChunk::printDiscardedMessage() const {
if (this == Ptr) {
// Removed by dead-stripping.
llvm::dbgs() << "Discarded " << Sym->getName() << "\n";