annotate lld/ELF/MarkLive.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- MarkLive.cpp -------------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements --gc-sections, which is a feature to remove unused
anatofuz
parents:
diff changeset
10 // sections from output. Unused sections are sections that are not reachable
anatofuz
parents:
diff changeset
11 // from known GC-root symbols or sections. Naturally the feature is
anatofuz
parents:
diff changeset
12 // implemented as a mark-sweep garbage collector.
anatofuz
parents:
diff changeset
13 //
anatofuz
parents:
diff changeset
14 // Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
anatofuz
parents:
diff changeset
15 // by default. Starting with GC-root symbols or sections, markLive function
anatofuz
parents:
diff changeset
16 // defined in this file visits all reachable sections to set their Live
anatofuz
parents:
diff changeset
17 // bits. Writer will then ignore sections whose Live bits are off, so that
anatofuz
parents:
diff changeset
18 // such sections are not included into output.
anatofuz
parents:
diff changeset
19 //
anatofuz
parents:
diff changeset
20 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 #include "MarkLive.h"
anatofuz
parents:
diff changeset
23 #include "InputSection.h"
anatofuz
parents:
diff changeset
24 #include "LinkerScript.h"
anatofuz
parents:
diff changeset
25 #include "OutputSections.h"
anatofuz
parents:
diff changeset
26 #include "SymbolTable.h"
anatofuz
parents:
diff changeset
27 #include "Symbols.h"
anatofuz
parents:
diff changeset
28 #include "SyntheticSections.h"
anatofuz
parents:
diff changeset
29 #include "Target.h"
anatofuz
parents:
diff changeset
30 #include "lld/Common/Memory.h"
anatofuz
parents:
diff changeset
31 #include "lld/Common/Strings.h"
anatofuz
parents:
diff changeset
32 #include "llvm/ADT/STLExtras.h"
anatofuz
parents:
diff changeset
33 #include "llvm/Object/ELF.h"
anatofuz
parents:
diff changeset
34 #include "llvm/Support/TimeProfiler.h"
anatofuz
parents:
diff changeset
35 #include <functional>
anatofuz
parents:
diff changeset
36 #include <vector>
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 using namespace llvm;
anatofuz
parents:
diff changeset
39 using namespace llvm::ELF;
anatofuz
parents:
diff changeset
40 using namespace llvm::object;
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 namespace endian = llvm::support::endian;
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 namespace lld {
anatofuz
parents:
diff changeset
45 namespace elf {
anatofuz
parents:
diff changeset
46 namespace {
anatofuz
parents:
diff changeset
47 template <class ELFT> class MarkLive {
anatofuz
parents:
diff changeset
48 public:
anatofuz
parents:
diff changeset
49 MarkLive(unsigned partition) : partition(partition) {}
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 void run();
anatofuz
parents:
diff changeset
52 void moveToMain();
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 private:
anatofuz
parents:
diff changeset
55 void enqueue(InputSectionBase *sec, uint64_t offset);
anatofuz
parents:
diff changeset
56 void markSymbol(Symbol *sym);
anatofuz
parents:
diff changeset
57 void mark();
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 template <class RelTy>
anatofuz
parents:
diff changeset
60 void resolveReloc(InputSectionBase &sec, RelTy &rel, bool isLSDA);
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 template <class RelTy>
anatofuz
parents:
diff changeset
63 void scanEhFrameSection(EhInputSection &eh, ArrayRef<RelTy> rels);
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 // The index of the partition that we are currently processing.
anatofuz
parents:
diff changeset
66 unsigned partition;
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 // A list of sections to visit.
anatofuz
parents:
diff changeset
69 SmallVector<InputSection *, 256> queue;
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 // There are normally few input sections whose names are valid C
anatofuz
parents:
diff changeset
72 // identifiers, so we just store a std::vector instead of a multimap.
anatofuz
parents:
diff changeset
73 DenseMap<StringRef, std::vector<InputSectionBase *>> cNamedSections;
anatofuz
parents:
diff changeset
74 };
anatofuz
parents:
diff changeset
75 } // namespace
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 template <class ELFT>
anatofuz
parents:
diff changeset
78 static uint64_t getAddend(InputSectionBase &sec,
anatofuz
parents:
diff changeset
79 const typename ELFT::Rel &rel) {
anatofuz
parents:
diff changeset
80 return target->getImplicitAddend(sec.data().begin() + rel.r_offset,
anatofuz
parents:
diff changeset
81 rel.getType(config->isMips64EL));
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 template <class ELFT>
anatofuz
parents:
diff changeset
85 static uint64_t getAddend(InputSectionBase &sec,
anatofuz
parents:
diff changeset
86 const typename ELFT::Rela &rel) {
anatofuz
parents:
diff changeset
87 return rel.r_addend;
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 template <class ELFT>
anatofuz
parents:
diff changeset
91 template <class RelTy>
anatofuz
parents:
diff changeset
92 void MarkLive<ELFT>::resolveReloc(InputSectionBase &sec, RelTy &rel,
anatofuz
parents:
diff changeset
93 bool isLSDA) {
anatofuz
parents:
diff changeset
94 Symbol &sym = sec.getFile<ELFT>()->getRelocTargetSym(rel);
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 // If a symbol is referenced in a live section, it is used.
anatofuz
parents:
diff changeset
97 sym.used = true;
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 if (auto *d = dyn_cast<Defined>(&sym)) {
anatofuz
parents:
diff changeset
100 auto *relSec = dyn_cast_or_null<InputSectionBase>(d->section);
anatofuz
parents:
diff changeset
101 if (!relSec)
anatofuz
parents:
diff changeset
102 return;
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 uint64_t offset = d->value;
anatofuz
parents:
diff changeset
105 if (d->isSection())
anatofuz
parents:
diff changeset
106 offset += getAddend<ELFT>(sec, rel);
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 if (!isLSDA || !(relSec->flags & SHF_EXECINSTR))
anatofuz
parents:
diff changeset
109 enqueue(relSec, offset);
anatofuz
parents:
diff changeset
110 return;
anatofuz
parents:
diff changeset
111 }
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 if (auto *ss = dyn_cast<SharedSymbol>(&sym))
anatofuz
parents:
diff changeset
114 if (!ss->isWeak())
anatofuz
parents:
diff changeset
115 ss->getFile().isNeeded = true;
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 for (InputSectionBase *sec : cNamedSections.lookup(sym.getName()))
anatofuz
parents:
diff changeset
118 enqueue(sec, 0);
anatofuz
parents:
diff changeset
119 }
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 // The .eh_frame section is an unfortunate special case.
anatofuz
parents:
diff changeset
122 // The section is divided in CIEs and FDEs and the relocations it can have are
anatofuz
parents:
diff changeset
123 // * CIEs can refer to a personality function.
anatofuz
parents:
diff changeset
124 // * FDEs can refer to a LSDA
anatofuz
parents:
diff changeset
125 // * FDEs refer to the function they contain information about
anatofuz
parents:
diff changeset
126 // The last kind of relocation cannot keep the referred section alive, or they
anatofuz
parents:
diff changeset
127 // would keep everything alive in a common object file. In fact, each FDE is
anatofuz
parents:
diff changeset
128 // alive if the section it refers to is alive.
anatofuz
parents:
diff changeset
129 // To keep things simple, in here we just ignore the last relocation kind. The
anatofuz
parents:
diff changeset
130 // other two keep the referred section alive.
anatofuz
parents:
diff changeset
131 //
anatofuz
parents:
diff changeset
132 // A possible improvement would be to fully process .eh_frame in the middle of
anatofuz
parents:
diff changeset
133 // the gc pass. With that we would be able to also gc some sections holding
anatofuz
parents:
diff changeset
134 // LSDAs and personality functions if we found that they were unused.
anatofuz
parents:
diff changeset
135 template <class ELFT>
anatofuz
parents:
diff changeset
136 template <class RelTy>
anatofuz
parents:
diff changeset
137 void MarkLive<ELFT>::scanEhFrameSection(EhInputSection &eh,
anatofuz
parents:
diff changeset
138 ArrayRef<RelTy> rels) {
anatofuz
parents:
diff changeset
139 for (size_t i = 0, end = eh.pieces.size(); i < end; ++i) {
anatofuz
parents:
diff changeset
140 EhSectionPiece &piece = eh.pieces[i];
anatofuz
parents:
diff changeset
141 size_t firstRelI = piece.firstRelocation;
anatofuz
parents:
diff changeset
142 if (firstRelI == (unsigned)-1)
anatofuz
parents:
diff changeset
143 continue;
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 if (endian::read32<ELFT::TargetEndianness>(piece.data().data() + 4) == 0) {
anatofuz
parents:
diff changeset
146 // This is a CIE, we only need to worry about the first relocation. It is
anatofuz
parents:
diff changeset
147 // known to point to the personality function.
anatofuz
parents:
diff changeset
148 resolveReloc(eh, rels[firstRelI], false);
anatofuz
parents:
diff changeset
149 continue;
anatofuz
parents:
diff changeset
150 }
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 // This is a FDE. The relocations point to the described function or to
anatofuz
parents:
diff changeset
153 // a LSDA. We only need to keep the LSDA alive, so ignore anything that
anatofuz
parents:
diff changeset
154 // points to executable sections.
anatofuz
parents:
diff changeset
155 uint64_t pieceEnd = piece.inputOff + piece.size;
anatofuz
parents:
diff changeset
156 for (size_t j = firstRelI, end2 = rels.size(); j < end2; ++j)
anatofuz
parents:
diff changeset
157 if (rels[j].r_offset < pieceEnd)
anatofuz
parents:
diff changeset
158 resolveReloc(eh, rels[j], true);
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160 }
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 // Some sections are used directly by the loader, so they should never be
anatofuz
parents:
diff changeset
163 // garbage-collected. This function returns true if a given section is such
anatofuz
parents:
diff changeset
164 // section.
anatofuz
parents:
diff changeset
165 static bool isReserved(InputSectionBase *sec) {
anatofuz
parents:
diff changeset
166 switch (sec->type) {
anatofuz
parents:
diff changeset
167 case SHT_FINI_ARRAY:
anatofuz
parents:
diff changeset
168 case SHT_INIT_ARRAY:
anatofuz
parents:
diff changeset
169 case SHT_PREINIT_ARRAY:
anatofuz
parents:
diff changeset
170 return true;
anatofuz
parents:
diff changeset
171 case SHT_NOTE:
anatofuz
parents:
diff changeset
172 // SHT_NOTE sections in a group are subject to garbage collection.
anatofuz
parents:
diff changeset
173 return !sec->nextInSectionGroup;
anatofuz
parents:
diff changeset
174 default:
anatofuz
parents:
diff changeset
175 StringRef s = sec->name;
anatofuz
parents:
diff changeset
176 return s.startswith(".ctors") || s.startswith(".dtors") ||
anatofuz
parents:
diff changeset
177 s.startswith(".init") || s.startswith(".fini") ||
anatofuz
parents:
diff changeset
178 s.startswith(".jcr");
anatofuz
parents:
diff changeset
179 }
anatofuz
parents:
diff changeset
180 }
anatofuz
parents:
diff changeset
181
anatofuz
parents:
diff changeset
182 template <class ELFT>
anatofuz
parents:
diff changeset
183 void MarkLive<ELFT>::enqueue(InputSectionBase *sec, uint64_t offset) {
anatofuz
parents:
diff changeset
184 // Skip over discarded sections. This in theory shouldn't happen, because
anatofuz
parents:
diff changeset
185 // the ELF spec doesn't allow a relocation to point to a deduplicated
anatofuz
parents:
diff changeset
186 // COMDAT section directly. Unfortunately this happens in practice (e.g.
anatofuz
parents:
diff changeset
187 // .eh_frame) so we need to add a check.
anatofuz
parents:
diff changeset
188 if (sec == &InputSection::discarded)
anatofuz
parents:
diff changeset
189 return;
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 // Usually, a whole section is marked as live or dead, but in mergeable
anatofuz
parents:
diff changeset
192 // (splittable) sections, each piece of data has independent liveness bit.
anatofuz
parents:
diff changeset
193 // So we explicitly tell it which offset is in use.
anatofuz
parents:
diff changeset
194 if (auto *ms = dyn_cast<MergeInputSection>(sec))
anatofuz
parents:
diff changeset
195 ms->getSectionPiece(offset)->live = true;
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 // Set Sec->Partition to the meet (i.e. the "minimum") of Partition and
anatofuz
parents:
diff changeset
198 // Sec->Partition in the following lattice: 1 < other < 0. If Sec->Partition
anatofuz
parents:
diff changeset
199 // doesn't change, we don't need to do anything.
anatofuz
parents:
diff changeset
200 if (sec->partition == 1 || sec->partition == partition)
anatofuz
parents:
diff changeset
201 return;
anatofuz
parents:
diff changeset
202 sec->partition = sec->partition ? 1 : partition;
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 // Add input section to the queue.
anatofuz
parents:
diff changeset
205 if (InputSection *s = dyn_cast<InputSection>(sec))
anatofuz
parents:
diff changeset
206 queue.push_back(s);
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *sym) {
anatofuz
parents:
diff changeset
210 if (auto *d = dyn_cast_or_null<Defined>(sym))
anatofuz
parents:
diff changeset
211 if (auto *isec = dyn_cast_or_null<InputSectionBase>(d->section))
anatofuz
parents:
diff changeset
212 enqueue(isec, d->value);
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 // This is the main function of the garbage collector.
anatofuz
parents:
diff changeset
216 // Starting from GC-root sections, this function visits all reachable
anatofuz
parents:
diff changeset
217 // sections to set their "Live" bits.
anatofuz
parents:
diff changeset
218 template <class ELFT> void MarkLive<ELFT>::run() {
anatofuz
parents:
diff changeset
219 // Add GC root symbols.
anatofuz
parents:
diff changeset
220
anatofuz
parents:
diff changeset
221 // Preserve externally-visible symbols if the symbols defined by this
anatofuz
parents:
diff changeset
222 // file can interrupt other ELF file's symbols at runtime.
anatofuz
parents:
diff changeset
223 for (Symbol *sym : symtab->symbols())
anatofuz
parents:
diff changeset
224 if (sym->includeInDynsym() && sym->partition == partition)
anatofuz
parents:
diff changeset
225 markSymbol(sym);
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 // If this isn't the main partition, that's all that we need to preserve.
anatofuz
parents:
diff changeset
228 if (partition != 1) {
anatofuz
parents:
diff changeset
229 mark();
anatofuz
parents:
diff changeset
230 return;
anatofuz
parents:
diff changeset
231 }
anatofuz
parents:
diff changeset
232
anatofuz
parents:
diff changeset
233 markSymbol(symtab->find(config->entry));
anatofuz
parents:
diff changeset
234 markSymbol(symtab->find(config->init));
anatofuz
parents:
diff changeset
235 markSymbol(symtab->find(config->fini));
anatofuz
parents:
diff changeset
236 for (StringRef s : config->undefined)
anatofuz
parents:
diff changeset
237 markSymbol(symtab->find(s));
anatofuz
parents:
diff changeset
238 for (StringRef s : script->referencedSymbols)
anatofuz
parents:
diff changeset
239 markSymbol(symtab->find(s));
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241 // Preserve special sections and those which are specified in linker
anatofuz
parents:
diff changeset
242 // script KEEP command.
anatofuz
parents:
diff changeset
243 for (InputSectionBase *sec : inputSections) {
anatofuz
parents:
diff changeset
244 // Mark .eh_frame sections as live because there are usually no relocations
anatofuz
parents:
diff changeset
245 // that point to .eh_frames. Otherwise, the garbage collector would drop
anatofuz
parents:
diff changeset
246 // all of them. We also want to preserve personality routines and LSDA
anatofuz
parents:
diff changeset
247 // referenced by .eh_frame sections, so we scan them for that here.
anatofuz
parents:
diff changeset
248 if (auto *eh = dyn_cast<EhInputSection>(sec)) {
anatofuz
parents:
diff changeset
249 eh->markLive();
anatofuz
parents:
diff changeset
250 if (!eh->numRelocations)
anatofuz
parents:
diff changeset
251 continue;
anatofuz
parents:
diff changeset
252
anatofuz
parents:
diff changeset
253 if (eh->areRelocsRela)
anatofuz
parents:
diff changeset
254 scanEhFrameSection(*eh, eh->template relas<ELFT>());
anatofuz
parents:
diff changeset
255 else
anatofuz
parents:
diff changeset
256 scanEhFrameSection(*eh, eh->template rels<ELFT>());
anatofuz
parents:
diff changeset
257 }
anatofuz
parents:
diff changeset
258
anatofuz
parents:
diff changeset
259 if (sec->flags & SHF_LINK_ORDER)
anatofuz
parents:
diff changeset
260 continue;
anatofuz
parents:
diff changeset
261
anatofuz
parents:
diff changeset
262 if (isReserved(sec) || script->shouldKeep(sec)) {
anatofuz
parents:
diff changeset
263 enqueue(sec, 0);
anatofuz
parents:
diff changeset
264 } else if (isValidCIdentifier(sec->name)) {
anatofuz
parents:
diff changeset
265 cNamedSections[saver.save("__start_" + sec->name)].push_back(sec);
anatofuz
parents:
diff changeset
266 cNamedSections[saver.save("__stop_" + sec->name)].push_back(sec);
anatofuz
parents:
diff changeset
267 }
anatofuz
parents:
diff changeset
268 }
anatofuz
parents:
diff changeset
269
anatofuz
parents:
diff changeset
270 mark();
anatofuz
parents:
diff changeset
271 }
anatofuz
parents:
diff changeset
272
anatofuz
parents:
diff changeset
273 template <class ELFT> void MarkLive<ELFT>::mark() {
anatofuz
parents:
diff changeset
274 // Mark all reachable sections.
anatofuz
parents:
diff changeset
275 while (!queue.empty()) {
anatofuz
parents:
diff changeset
276 InputSectionBase &sec = *queue.pop_back_val();
anatofuz
parents:
diff changeset
277
anatofuz
parents:
diff changeset
278 if (sec.areRelocsRela) {
anatofuz
parents:
diff changeset
279 for (const typename ELFT::Rela &rel : sec.template relas<ELFT>())
anatofuz
parents:
diff changeset
280 resolveReloc(sec, rel, false);
anatofuz
parents:
diff changeset
281 } else {
anatofuz
parents:
diff changeset
282 for (const typename ELFT::Rel &rel : sec.template rels<ELFT>())
anatofuz
parents:
diff changeset
283 resolveReloc(sec, rel, false);
anatofuz
parents:
diff changeset
284 }
anatofuz
parents:
diff changeset
285
anatofuz
parents:
diff changeset
286 for (InputSectionBase *isec : sec.dependentSections)
anatofuz
parents:
diff changeset
287 enqueue(isec, 0);
anatofuz
parents:
diff changeset
288
anatofuz
parents:
diff changeset
289 // Mark the next group member.
anatofuz
parents:
diff changeset
290 if (sec.nextInSectionGroup)
anatofuz
parents:
diff changeset
291 enqueue(sec.nextInSectionGroup, 0);
anatofuz
parents:
diff changeset
292 }
anatofuz
parents:
diff changeset
293 }
anatofuz
parents:
diff changeset
294
anatofuz
parents:
diff changeset
295 // Move the sections for some symbols to the main partition, specifically ifuncs
anatofuz
parents:
diff changeset
296 // (because they can result in an IRELATIVE being added to the main partition's
anatofuz
parents:
diff changeset
297 // GOT, which means that the ifunc must be available when the main partition is
anatofuz
parents:
diff changeset
298 // loaded) and TLS symbols (because we only know how to correctly process TLS
anatofuz
parents:
diff changeset
299 // relocations for the main partition).
anatofuz
parents:
diff changeset
300 //
anatofuz
parents:
diff changeset
301 // We also need to move sections whose names are C identifiers that are referred
anatofuz
parents:
diff changeset
302 // to from __start_/__stop_ symbols because there will only be one set of
anatofuz
parents:
diff changeset
303 // symbols for the whole program.
anatofuz
parents:
diff changeset
304 template <class ELFT> void MarkLive<ELFT>::moveToMain() {
anatofuz
parents:
diff changeset
305 for (InputFile *file : objectFiles)
anatofuz
parents:
diff changeset
306 for (Symbol *s : file->getSymbols())
anatofuz
parents:
diff changeset
307 if (auto *d = dyn_cast<Defined>(s))
anatofuz
parents:
diff changeset
308 if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section &&
anatofuz
parents:
diff changeset
309 d->section->isLive())
anatofuz
parents:
diff changeset
310 markSymbol(s);
anatofuz
parents:
diff changeset
311
anatofuz
parents:
diff changeset
312 for (InputSectionBase *sec : inputSections) {
anatofuz
parents:
diff changeset
313 if (!sec->isLive() || !isValidCIdentifier(sec->name))
anatofuz
parents:
diff changeset
314 continue;
anatofuz
parents:
diff changeset
315 if (symtab->find(("__start_" + sec->name).str()) ||
anatofuz
parents:
diff changeset
316 symtab->find(("__stop_" + sec->name).str()))
anatofuz
parents:
diff changeset
317 enqueue(sec, 0);
anatofuz
parents:
diff changeset
318 }
anatofuz
parents:
diff changeset
319
anatofuz
parents:
diff changeset
320 mark();
anatofuz
parents:
diff changeset
321 }
anatofuz
parents:
diff changeset
322
anatofuz
parents:
diff changeset
323 // Before calling this function, Live bits are off for all
anatofuz
parents:
diff changeset
324 // input sections. This function make some or all of them on
anatofuz
parents:
diff changeset
325 // so that they are emitted to the output file.
anatofuz
parents:
diff changeset
326 template <class ELFT> void markLive() {
anatofuz
parents:
diff changeset
327 llvm::TimeTraceScope timeScope("markLive");
anatofuz
parents:
diff changeset
328 // If -gc-sections is not given, no sections are removed.
anatofuz
parents:
diff changeset
329 if (!config->gcSections) {
anatofuz
parents:
diff changeset
330 for (InputSectionBase *sec : inputSections)
anatofuz
parents:
diff changeset
331 sec->markLive();
anatofuz
parents:
diff changeset
332
anatofuz
parents:
diff changeset
333 // If a DSO defines a symbol referenced in a regular object, it is needed.
anatofuz
parents:
diff changeset
334 for (Symbol *sym : symtab->symbols())
anatofuz
parents:
diff changeset
335 if (auto *s = dyn_cast<SharedSymbol>(sym))
anatofuz
parents:
diff changeset
336 if (s->isUsedInRegularObj && !s->isWeak())
anatofuz
parents:
diff changeset
337 s->getFile().isNeeded = true;
anatofuz
parents:
diff changeset
338 return;
anatofuz
parents:
diff changeset
339 }
anatofuz
parents:
diff changeset
340
anatofuz
parents:
diff changeset
341 // Otherwise, do mark-sweep GC.
anatofuz
parents:
diff changeset
342 //
anatofuz
parents:
diff changeset
343 // The -gc-sections option works only for SHF_ALLOC sections
anatofuz
parents:
diff changeset
344 // (sections that are memory-mapped at runtime). So we can
anatofuz
parents:
diff changeset
345 // unconditionally make non-SHF_ALLOC sections alive except
anatofuz
parents:
diff changeset
346 // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
anatofuz
parents:
diff changeset
347 //
anatofuz
parents:
diff changeset
348 // Usually, non-SHF_ALLOC sections are not removed even if they are
anatofuz
parents:
diff changeset
349 // unreachable through relocations because reachability is not
anatofuz
parents:
diff changeset
350 // a good signal whether they are garbage or not (e.g. there is
anatofuz
parents:
diff changeset
351 // usually no section referring to a .comment section, but we
anatofuz
parents:
diff changeset
352 // want to keep it.).
anatofuz
parents:
diff changeset
353 //
anatofuz
parents:
diff changeset
354 // Note on SHF_LINK_ORDER: Such sections contain metadata and they
anatofuz
parents:
diff changeset
355 // have a reverse dependency on the InputSection they are linked with.
anatofuz
parents:
diff changeset
356 // We are able to garbage collect them.
anatofuz
parents:
diff changeset
357 //
anatofuz
parents:
diff changeset
358 // Note on SHF_REL{,A}: Such sections reach here only when -r
anatofuz
parents:
diff changeset
359 // or -emit-reloc were given. And they are subject of garbage
anatofuz
parents:
diff changeset
360 // collection because, if we remove a text section, we also
anatofuz
parents:
diff changeset
361 // remove its relocation section.
anatofuz
parents:
diff changeset
362 //
anatofuz
parents:
diff changeset
363 // Note on nextInSectionGroup: The ELF spec says that group sections are
anatofuz
parents:
diff changeset
364 // included or omitted as a unit. We take the interpretation that:
anatofuz
parents:
diff changeset
365 //
anatofuz
parents:
diff changeset
366 // - Group members (nextInSectionGroup != nullptr) are subject to garbage
anatofuz
parents:
diff changeset
367 // collection.
anatofuz
parents:
diff changeset
368 // - Groups members are retained or discarded as a unit.
anatofuz
parents:
diff changeset
369 for (InputSectionBase *sec : inputSections) {
anatofuz
parents:
diff changeset
370 bool isAlloc = (sec->flags & SHF_ALLOC);
anatofuz
parents:
diff changeset
371 bool isLinkOrder = (sec->flags & SHF_LINK_ORDER);
anatofuz
parents:
diff changeset
372 bool isRel = (sec->type == SHT_REL || sec->type == SHT_RELA);
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 if (!isAlloc && !isLinkOrder && !isRel && !sec->nextInSectionGroup)
anatofuz
parents:
diff changeset
375 sec->markLive();
anatofuz
parents:
diff changeset
376 }
anatofuz
parents:
diff changeset
377
anatofuz
parents:
diff changeset
378 // Follow the graph to mark all live sections.
anatofuz
parents:
diff changeset
379 for (unsigned curPart = 1; curPart <= partitions.size(); ++curPart)
anatofuz
parents:
diff changeset
380 MarkLive<ELFT>(curPart).run();
anatofuz
parents:
diff changeset
381
anatofuz
parents:
diff changeset
382 // If we have multiple partitions, some sections need to live in the main
anatofuz
parents:
diff changeset
383 // partition even if they were allocated to a loadable partition. Move them
anatofuz
parents:
diff changeset
384 // there now.
anatofuz
parents:
diff changeset
385 if (partitions.size() != 1)
anatofuz
parents:
diff changeset
386 MarkLive<ELFT>(1).moveToMain();
anatofuz
parents:
diff changeset
387
anatofuz
parents:
diff changeset
388 // Report garbage-collected sections.
anatofuz
parents:
diff changeset
389 if (config->printGcSections)
anatofuz
parents:
diff changeset
390 for (InputSectionBase *sec : inputSections)
anatofuz
parents:
diff changeset
391 if (!sec->isLive())
anatofuz
parents:
diff changeset
392 message("removing unused section " + toString(sec));
anatofuz
parents:
diff changeset
393 }
anatofuz
parents:
diff changeset
394
anatofuz
parents:
diff changeset
395 template void markLive<ELF32LE>();
anatofuz
parents:
diff changeset
396 template void markLive<ELF32BE>();
anatofuz
parents:
diff changeset
397 template void markLive<ELF64LE>();
anatofuz
parents:
diff changeset
398 template void markLive<ELF64BE>();
anatofuz
parents:
diff changeset
399
anatofuz
parents:
diff changeset
400 } // namespace elf
anatofuz
parents:
diff changeset
401 } // namespace lld