]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/MarkLive.cpp
Merge lld trunk r338150, and resolve conflicts.
[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 "lld/Common/Timer.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include <vector>
15
16 namespace lld {
17 namespace coff {
18
19 static Timer GCTimer("GC", Timer::root());
20
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);
26
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;
31
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))
35       if (SC->isLive())
36         Worklist.push_back(SC);
37
38   auto Enqueue = [&](SectionChunk *C) {
39     if (C->isLive())
40       return;
41     C->markLive();
42     Worklist.push_back(C);
43   };
44
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;
52   };
53
54   // Add GC root chunks.
55   for (Symbol *B : Config->GCRoot)
56     AddSym(B);
57
58   while (!Worklist.empty()) {
59     SectionChunk *SC = Worklist.pop_back_val();
60     assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
61
62     // Mark all symbols listed in the relocation table for this section.
63     for (Symbol *B : SC->symbols())
64       if (B)
65         AddSym(B);
66
67     // Mark associative sections if any.
68     for (SectionChunk *C : SC->children())
69       Enqueue(C);
70   }
71 }
72
73 }
74 }