1 //===- MarkLive.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lld/Common/Timer.h"
13 #include "llvm/ADT/STLExtras.h"
19 static Timer GCTimer("GC", Timer::root());
21 // Set live bit on for each reachable chunk. Unmarked (unreachable)
22 // COMDAT chunks will be ignored by Writer, so they will be excluded
23 // from the final output.
24 void markLive(ArrayRef<Chunk *> Chunks) {
25 ScopedTimer T(GCTimer);
27 // We build up a worklist of sections which have been marked as live. We only
28 // push into the worklist when we discover an unmarked section, and we mark
29 // as we push, so sections never appear twice in the list.
30 SmallVector<SectionChunk *, 256> Worklist;
32 // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
33 for (Chunk *C : Chunks)
34 if (auto *SC = dyn_cast<SectionChunk>(C))
36 Worklist.push_back(SC);
38 auto Enqueue = [&](SectionChunk *C) {
42 Worklist.push_back(C);
45 auto AddSym = [&](Symbol *B) {
46 if (auto *Sym = dyn_cast<DefinedRegular>(B))
47 Enqueue(Sym->getChunk());
48 else if (auto *Sym = dyn_cast<DefinedImportData>(B))
49 Sym->File->Live = true;
50 else if (auto *Sym = dyn_cast<DefinedImportThunk>(B))
51 Sym->WrappedSym->File->Live = Sym->WrappedSym->File->ThunkLive = true;
54 // Add GC root chunks.
55 for (Symbol *B : Config->GCRoot)
58 while (!Worklist.empty()) {
59 SectionChunk *SC = Worklist.pop_back_val();
60 assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
62 // Mark all symbols listed in the relocation table for this section.
63 for (Symbol *B : SC->symbols())
67 // Mark associative sections if any.
68 for (SectionChunk *C : SC->children())