]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/MarkLive.cpp
MFV r337744:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / MarkLive.cpp
1 //===- MarkLive.cpp -------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Chunks.h"
11 #include "Symbols.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include <vector>
14
15 namespace lld {
16 namespace coff {
17
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;
26
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))
30       if (SC->isLive())
31         Worklist.push_back(SC);
32
33   auto Enqueue = [&](SectionChunk *C) {
34     if (C->isLive())
35       return;
36     C->markLive();
37     Worklist.push_back(C);
38   };
39
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;
47   };
48
49   // Add GC root chunks.
50   for (Symbol *B : Config->GCRoot)
51     AddSym(B);
52
53   while (!Worklist.empty()) {
54     SectionChunk *SC = Worklist.pop_back_val();
55     assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
56
57     // Mark all symbols listed in the relocation table for this section.
58     for (Symbol *B : SC->symbols())
59       if (B)
60         AddSym(B);
61
62     // Mark associative sections if any.
63     for (SectionChunk *C : SC->children())
64       Enqueue(C);
65   }
66 }
67
68 }
69 }