blob: 8f255e25a378e0aeb90036cfe9ef4e655164d8d4 [file] [log] [blame]
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001//===- 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// This file implements --gc-sections, which is a feature to remove unused
11// sections from output. Unused sections are sections that are not reachable
12// from known GC-root symbols or sections. Naturally the feature is
13// implemented as a mark-sweep garbage collector.
14//
15// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
16// by default. Starting with GC-root symbols or sections, markLive function
17// defined in this file visits all reachable sections to set their Live
18// bits. Writer will then ignore sections whose Live bits are off, so that
19// such sections are removed from output.
20//
21//===----------------------------------------------------------------------===//
22
23#include "InputSection.h"
24#include "OutputSections.h"
25#include "SymbolTable.h"
26#include "Symbols.h"
27#include "Writer.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/Object/ELF.h"
30#include <functional>
31#include <vector>
32
33using namespace llvm;
34using namespace llvm::ELF;
35using namespace llvm::object;
36
37using namespace lld;
38using namespace lld::elf2;
39
40template <class ELFT, bool isRela>
41static void
42doForEachSuccessor(InputSectionBase<ELFT> *Sec,
43 std::function<void(InputSectionBase<ELFT> *)> Fn,
44 iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
45 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
46 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
47
48 ObjectFile<ELFT> *File = Sec->getFile();
49 for (const RelType &RI : Rels) {
50 // Global symbol
51 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
52 if (SymbolBody *B = File->getSymbolBody(SymIndex)) {
53 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
54 Fn(&D->Section);
55 continue;
56 }
57 // Local symbol
58 if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
59 if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
60 Fn(Sec);
61 }
62}
63
64// Calls Fn for each section that Sec refers to.
65template <class ELFT>
66static void forEachSuccessor(InputSection<ELFT> *Sec,
67 std::function<void(InputSectionBase<ELFT> *)> Fn) {
68 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
69 for (const Elf_Shdr *RelSec : Sec->RelocSections) {
70 if (RelSec->sh_type == SHT_RELA)
71 doForEachSuccessor(Sec, Fn, Sec->getFile()->getObj().relas(RelSec));
72 else
73 doForEachSuccessor(Sec, Fn, Sec->getFile()->getObj().rels(RelSec));
74 }
75}
76
77// Sections listed below are special because they are used by the loader
78// just by being in an ELF file. They should not be garbage-collected.
79template <class ELFT> static bool isReserved(InputSectionBase<ELFT> *Sec) {
80 switch (Sec->getSectionHdr()->sh_type) {
81 case SHT_FINI_ARRAY:
82 case SHT_INIT_ARRAY:
83 case SHT_NOTE:
84 case SHT_PREINIT_ARRAY:
85 return true;
86 default:
87 StringRef S = Sec->getSectionName();
Rui Ueyama8a598f82015-10-22 21:42:05 +000088 return S.startswith(".init") || S.startswith(".fini") ||
89 S.startswith(".jcr") || S == ".eh_frame";
Rui Ueyamac4aaed92015-10-22 18:49:53 +000090 }
91}
92
93template <class ELFT> void lld::elf2::markLive(SymbolTable<ELFT> *Symtab) {
94 SmallVector<InputSectionBase<ELFT> *, 256> Q;
95
96 auto Enqueue = [&](InputSectionBase<ELFT> *Sec) {
97 if (!Sec || Sec->Live)
98 return;
99 Sec->Live = true;
100 Q.push_back(Sec);
101 };
102
103 auto MarkSymbol = [&](SymbolBody *Sym) {
104 if (Sym)
105 if (auto *D = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym->repl()))
106 Enqueue(&D->Section);
107 };
108
109 // Add GC root symbols.
110 MarkSymbol(Config->EntrySym);
111 MarkSymbol(Symtab->find(Config->Init));
112 MarkSymbol(Symtab->find(Config->Fini));
113 for (StringRef S : Config->Undefined)
114 MarkSymbol(Symtab->find(S));
115
116 // Preserve externally-visible symbols if the symbols defined by this
117 // file could override other ELF file's symbols at runtime.
118 if (Config->Shared || Config->ExportDynamic) {
119 for (const std::pair<StringRef, Symbol *> &P : Symtab->getSymbols()) {
120 SymbolBody *B = P.second->Body;
121 if (B->getVisibility() == STV_DEFAULT)
122 MarkSymbol(B);
123 }
124 }
125
126 // Preserve special sections.
127 for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab->getObjectFiles())
128 for (InputSectionBase<ELFT> *Sec : F->getSections())
129 if (Sec && Sec != &InputSection<ELFT>::Discarded)
130 if (isReserved(Sec))
131 Enqueue(Sec);
132
133 // Mark all reachable sections.
134 while (!Q.empty())
135 if (auto *Sec = dyn_cast<InputSection<ELFT>>(Q.pop_back_val()))
136 forEachSuccessor<ELFT>(Sec, Enqueue);
137}
138
139template void lld::elf2::markLive<ELF32LE>(SymbolTable<ELF32LE> *);
140template void lld::elf2::markLive<ELF32BE>(SymbolTable<ELF32BE> *);
141template void lld::elf2::markLive<ELF64LE>(SymbolTable<ELF64LE> *);
142template void lld::elf2::markLive<ELF64BE>(SymbolTable<ELF64BE> *);