1 //===- MarkLive.cpp -------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "lld/Common/Timer.h"
12 #include "llvm/ADT/STLExtras.h"
18 static Timer gctimer("GC", Timer::root());
20 // Set live bit on for each reachable chunk. Unmarked (unreachable)
21 // COMDAT chunks will be ignored by Writer, so they will be excluded
22 // from the final output.
23 void markLive(ArrayRef<Chunk *> chunks) {
24 ScopedTimer t(gctimer);
26 // We build up a worklist of sections which have been marked as live. We only
27 // push into the worklist when we discover an unmarked section, and we mark
28 // as we push, so sections never appear twice in the list.
29 SmallVector<SectionChunk *, 256> worklist;
31 // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
32 for (Chunk *c : chunks)
33 if (auto *sc = dyn_cast<SectionChunk>(c))
35 worklist.push_back(sc);
37 auto enqueue = [&](SectionChunk *c) {
41 worklist.push_back(c);
44 auto addSym = [&](Symbol *b) {
45 if (auto *sym = dyn_cast<DefinedRegular>(b))
46 enqueue(sym->getChunk());
47 else if (auto *sym = dyn_cast<DefinedImportData>(b))
48 sym->file->live = true;
49 else if (auto *sym = dyn_cast<DefinedImportThunk>(b))
50 sym->wrappedSym->file->live = sym->wrappedSym->file->thunkLive = true;
53 // Add GC root chunks.
54 for (Symbol *b : config->gcroot)
57 while (!worklist.empty()) {
58 SectionChunk *sc = worklist.pop_back_val();
59 assert(sc->live && "We mark as live when pushing onto the worklist!");
61 // Mark all symbols listed in the relocation table for this section.
62 for (Symbol *b : sc->symbols())
66 // Mark associative sections if any.
67 for (SectionChunk &c : sc->children())