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 "llvm/ADT/STLExtras.h"
18 // Set live bit on for each reachable chunk. Unmarked (unreachable)
19 // COMDAT chunks will be ignored by Writer, so they will be excluded
20 // from the final output.
21 void markLive(ArrayRef<Chunk *> Chunks) {
22 // We build up a worklist of sections which have been marked as live. We only
23 // push into the worklist when we discover an unmarked section, and we mark
24 // as we push, so sections never appear twice in the list.
25 SmallVector<SectionChunk *, 256> Worklist;
27 // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
28 for (Chunk *C : Chunks)
29 if (auto *SC = dyn_cast<SectionChunk>(C))
31 Worklist.push_back(SC);
33 auto Enqueue = [&](SectionChunk *C) {
37 Worklist.push_back(C);
40 auto AddSym = [&](Symbol *B) {
41 if (auto *Sym = dyn_cast<DefinedRegular>(B))
42 Enqueue(Sym->getChunk());
43 else if (auto *Sym = dyn_cast<DefinedImportData>(B))
44 Sym->File->Live = true;
45 else if (auto *Sym = dyn_cast<DefinedImportThunk>(B))
46 Sym->WrappedSym->File->Live = true;
49 // Add GC root chunks.
50 for (Symbol *B : Config->GCRoot)
53 while (!Worklist.empty()) {
54 SectionChunk *SC = Worklist.pop_back_val();
55 assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
57 // Mark all symbols listed in the relocation table for this section.
58 for (Symbol *B : SC->symbols())
62 // Mark associative sections if any.
63 for (SectionChunk *C : SC->children())