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