Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 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(const std::vector<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 | // Add GC root chunks. |
| 41 | for (Undefined *U : Config->GCRoot) |
| 42 | if (auto *D = dyn_cast<DefinedRegular>(U->repl())) |
| 43 | Enqueue(D->getChunk()); |
| 44 | |
| 45 | while (!Worklist.empty()) { |
| 46 | SectionChunk *SC = Worklist.pop_back_val(); |
| 47 | assert(SC->isLive() && "We mark as live when pushing onto the worklist!"); |
| 48 | |
| 49 | // Mark all symbols listed in the relocation table for this section. |
| 50 | for (SymbolBody *S : SC->symbols()) |
| 51 | if (auto *D = dyn_cast<DefinedRegular>(S->repl())) |
| 52 | Enqueue(D->getChunk()); |
| 53 | |
| 54 | // Mark associative sections if any. |
| 55 | for (SectionChunk *C : SC->children()) |
| 56 | Enqueue(C); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | } |