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 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 56 // If this section was discarded, there are relocations referring to 57 // discarded sections. Ignore these sections to avoid crashing. They will be 58 // diagnosed during relocation processing. 59 if (SC->isDiscarded()) 60 continue; 61 62 assert(SC->isLive() && "We mark as live when pushing onto the worklist!"); 63 64 // Mark all symbols listed in the relocation table for this section. 65 for (Symbol *B : SC->symbols()) 66 AddSym(B); 67 68 // Mark associative sections if any. 69 for (SectionChunk *C : SC->children()) 70 Enqueue(C); 71 } 72 } 73 74 } 75 } 76