annotate lld/ELF/MarkLive.cpp @ 192:d7606dcf6fce

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