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.
13//
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.
19//
20//===----------------------------------------------------------------------===//
21
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/Strings.h"
31#include "llvm/ADT/DenseMapInfoVariant.h"
32#include "llvm/ADT/STLExtras.h"
33#include "llvm/Support/Parallel.h"
34#include "llvm/Support/TimeProfiler.h"
35#include <variant>
36#include <vector>
37
38using namespace llvm;
39using namespace llvm::ELF;
40using namespace llvm::object;
41using namespace llvm::support::endian;
42using namespace lld;
43using namespace lld::elf;
44
45namespace {
46using SecOffset = std::pair<InputSectionBase *, unsigned>;
47
48// Something that can have an independent reason for being live.
49using LiveItem = std::variant<InputSectionBase *, Symbol *, SecOffset>;
50
51// The most proximate reason that something is live.
52struct LiveReason {
53 std::optional<LiveItem> item;
54 StringRef desc;
55};
56
57template <class ELFT, bool TrackWhyLive> class MarkLive {
58public:
59 MarkLive(Ctx &ctx) : ctx(ctx) {}
60
61 void run();
62 void printWhyLive(Symbol *s) const;
63
64private:
65 void enqueue(InputSectionBase *sec, uint64_t offset, Symbol *sym,
66 LiveReason reason);
67 void markSymbol(Symbol *sym, StringRef reason);
68 void mark();
69 void markParallel();
70
71 template <class RelTy>
72 void resolveReloc(InputSectionBase &sec, const RelTy &rel, bool fromFDE);
73
74 void scanEhFrameSection(EhInputSection &eh);
75
76 Ctx &ctx;
77
78 // A list of sections to visit.
79 SmallVector<InputSection *, 0> queue;
80
81 // There are normally few input sections whose names are valid C
82 // identifiers, so we just store a SmallVector instead of a multimap.
83 DenseMap<StringRef, SmallVector<InputSectionBase *, 0>> cNamedSections;
84
85 // The most proximate reason that something is live. This forms a DAG between
86 // LiveItems. Acyclicality is maintained by only admitting the first
87 // discovered reason for each LiveItem; this captures the acyclic region of
88 // the liveness graph around the GC roots.
89 DenseMap<LiveItem, LiveReason> whyLive;
90};
91} // namespace
92
93template <class ELFT>
94static uint64_t getAddend(Ctx &ctx, InputSectionBase &sec,
95 const typename ELFT::Rel &rel) {
96 return ctx.target->getImplicitAddend(buf: sec.content().begin() + rel.r_offset,
97 type: rel.getType(ctx.arg.isMips64EL));
98}
99
100template <class ELFT>
101static uint64_t getAddend(Ctx &, InputSectionBase &sec,
102 const typename ELFT::Rela &rel) {
103 return rel.r_addend;
104}
105
106// Currently, we assume all input CREL relocations have an explicit addend.
107template <class ELFT>
108static uint64_t getAddend(Ctx &, InputSectionBase &sec,
109 const typename ELFT::Crel &rel) {
110 return rel.r_addend;
111}
112
113template <class ELFT, bool TrackWhyLive>
114template <class RelTy>
115void MarkLive<ELFT, TrackWhyLive>::resolveReloc(InputSectionBase &sec,
116 const RelTy &rel,
117 bool fromFDE) {
118 // If a symbol is referenced in a live section, it is used.
119 Symbol *sym;
120 if constexpr (std::is_same_v<RelTy, Relocation>) {
121 assert(isa<EhInputSection>(sec));
122 sym = rel.sym;
123 } else {
124 sym = &sec.file->getRelocTargetSym(rel);
125 }
126 sym->setFlags(USED);
127
128 LiveReason reason;
129 if (TrackWhyLive) {
130 if constexpr (std::is_same_v<RelTy, Relocation>)
131 reason = {.item: SecOffset(&sec, rel.offset), .desc: "referenced by"};
132 else
133 reason = {.item: SecOffset(&sec, rel.r_offset), .desc: "referenced by"};
134 }
135
136 if (auto *d = dyn_cast<Defined>(Val: sym)) {
137 auto *relSec = dyn_cast_or_null<InputSectionBase>(Val: d->section);
138 if (!relSec)
139 return;
140
141 uint64_t offset = d->value;
142 if (d->isSection()) {
143 if constexpr (std::is_same_v<RelTy, Relocation>)
144 offset += rel.addend;
145 else
146 offset += getAddend<ELFT>(ctx, sec, rel);
147 // Skip out-of-bounds offsets to avoid an assertion failure in
148 // getSectionPiece.
149 if (auto *ms = dyn_cast<MergeInputSection>(Val: relSec);
150 ms && offset >= ms->content().size())
151 return;
152 }
153
154 // fromFDE being true means this is referenced by a FDE in a .eh_frame
155 // piece. The relocation points to the described function or to a LSDA. We
156 // only need to keep the LSDA live, so ignore anything that points to
157 // executable sections. If the LSDA is in a section group or has the
158 // SHF_LINK_ORDER flag, we ignore the relocation as well because (a) if the
159 // associated text section is live, the LSDA will be retained due to section
160 // group/SHF_LINK_ORDER rules (b) if the associated text section should be
161 // discarded, marking the LSDA will unnecessarily retain the text section.
162 if (!(fromFDE && std::is_same_v<RelTy, Relocation> &&
163 ((relSec->flags & (SHF_EXECINSTR | SHF_LINK_ORDER)) ||
164 relSec->nextInSectionGroup))) {
165 Symbol *canonicalSym = d;
166 if (TrackWhyLive && d->isSection()) {
167 // This is expensive, so ideally this would be deferred until it's known
168 // whether this reference contributes to a printed whyLive chain, but
169 // that determination cannot be made without knowing the enclosing
170 // symbol.
171 if (Symbol *s = relSec->getEnclosingSymbol(offset))
172 canonicalSym = s;
173 else
174 canonicalSym = nullptr;
175 }
176 enqueue(sec: relSec, offset, sym: canonicalSym, reason);
177 }
178 return;
179 }
180
181 if (auto *ss = dyn_cast<SharedSymbol>(Val: sym))
182 if (!ss->isWeak() && TrackWhyLive)
183 whyLive.try_emplace(Key: sym, Args&: reason);
184
185 for (InputSectionBase *sec : cNamedSections.lookup(Val: sym->getName()))
186 enqueue(sec, /*offset=*/0, /*sym=*/nullptr, reason);
187}
188
189// The .eh_frame section is an unfortunate special case.
190// The section is divided in CIEs and FDEs and the relocations it can have are
191// * CIEs can refer to a personality function.
192// * FDEs can refer to a LSDA
193// * FDEs refer to the function they contain information about
194// The last kind of relocation cannot keep the referred section alive, or they
195// would keep everything alive in a common object file. In fact, each FDE is
196// alive if the section it refers to is alive.
197// To keep things simple, in here we just ignore the last relocation kind. The
198// other two keep the referred section alive.
199//
200// A possible improvement would be to fully process .eh_frame in the middle of
201// the gc pass. With that we would be able to also gc some sections holding
202// LSDAs and personality functions if we found that they were unused.
203template <class ELFT, bool TrackWhyLive>
204void MarkLive<ELFT, TrackWhyLive>::scanEhFrameSection(EhInputSection &eh) {
205 if (TrackWhyLive)
206 whyLive.try_emplace(Key: &eh,
207 Args: LiveReason{.item: std::nullopt, .desc: "exception handling frame"});
208 ArrayRef<Relocation> rels = eh.rels;
209 for (const EhSectionPiece &cie : eh.cies)
210 if (cie.firstRelocation != unsigned(-1))
211 resolveReloc(eh, rels[cie.firstRelocation], false);
212 for (const EhSectionPiece &fde : eh.fdes) {
213 size_t firstRelI = fde.firstRelocation;
214 if (firstRelI == (unsigned)-1)
215 continue;
216 uint64_t pieceEnd = fde.inputOff + fde.size;
217 for (size_t j = firstRelI, end2 = rels.size();
218 j < end2 && rels[j].offset < pieceEnd; ++j)
219 resolveReloc(eh, rels[j], true);
220 }
221}
222
223// Some sections are used directly by the loader, so they should never be
224// garbage-collected. This function returns true if a given section is such
225// section.
226static bool isReserved(InputSectionBase *sec) {
227 switch (sec->type) {
228 case SHT_FINI_ARRAY:
229 case SHT_INIT_ARRAY:
230 case SHT_PREINIT_ARRAY:
231 return true;
232 case SHT_NOTE:
233 // SHT_NOTE sections in a group are subject to garbage collection.
234 return !sec->nextInSectionGroup;
235 default:
236 // Support SHT_PROGBITS .init_array (https://golang.org/issue/50295) and
237 // .init_array.N (https://github.com/rust-lang/rust/issues/92181) for a
238 // while.
239 StringRef s = sec->name;
240 return s == ".init" || s == ".fini" || s.starts_with(Prefix: ".init_array") ||
241 s == ".jcr" || s.starts_with(Prefix: ".ctors") || s.starts_with(Prefix: ".dtors");
242 }
243}
244
245template <class ELFT, bool TrackWhyLive>
246void MarkLive<ELFT, TrackWhyLive>::enqueue(InputSectionBase *sec,
247 uint64_t offset, Symbol *sym,
248 LiveReason reason) {
249 // Usually, a whole section is marked as live or dead, but in mergeable
250 // (splittable) sections, each piece of data has independent liveness bit.
251 // So we explicitly tell it which offset is in use.
252 if (auto *ms = dyn_cast<MergeInputSection>(Val: sec))
253 ms->getSectionPiece(offset).live = true;
254
255 if (sec->partition)
256 return;
257 sec->partition = 1;
258
259 if (TrackWhyLive) {
260 if (sym) {
261 // If a specific symbol is referenced, that keeps it live. The symbol then
262 // keeps its section live.
263 whyLive.try_emplace(Key: sym, Args&: reason);
264 whyLive.try_emplace(Key: sec, Args: LiveReason{.item: sym, .desc: "contained live symbol"});
265 } else {
266 // Otherwise, the reference generically keeps the section live.
267 whyLive.try_emplace(Key: sec, Args&: reason);
268 }
269 }
270
271 // Add input section to the queue.
272 if (InputSection *s = dyn_cast<InputSection>(Val: sec))
273 queue.push_back(Elt: s);
274}
275
276// Print the stack of reasons that the given symbol is live.
277template <class ELFT, bool TrackWhyLive>
278void MarkLive<ELFT, TrackWhyLive>::printWhyLive(Symbol *s) const {
279 // Skip dead symbols. A symbol is dead if it belongs to a dead section.
280 if (auto *d = dyn_cast<Defined>(Val: s)) {
281 auto *sec = dyn_cast_or_null<InputSectionBase>(Val: d->section);
282 if (sec && !sec->isLive())
283 return;
284 }
285
286 auto msg = Msg(ctx);
287
288 const auto printSymbol = [&](Symbol *s) {
289 msg << s->file << ":(" << s << ')';
290 };
291
292 msg << "live symbol: ";
293 printSymbol(s);
294
295 LiveItem cur = s;
296 while (true) {
297 auto it = whyLive.find(Val: cur);
298 LiveReason reason;
299 // If there is a specific reason this item is live...
300 if (it != whyLive.end()) {
301 reason = it->second;
302 } else {
303 // This item is live, but it has no tracked reason. It must be an
304 // unreferenced symbol in a live section or a symbol with no section.
305 InputSectionBase *sec = nullptr;
306 if (auto *d = dyn_cast<Defined>(Val: std::get<Symbol *>(v&: cur)))
307 sec = dyn_cast_or_null<InputSectionBase>(Val: d->section);
308 reason = sec ? LiveReason{.item: sec, .desc: "in live section"}
309 : LiveReason{.item: std::nullopt, .desc: "no section"};
310 }
311
312 if (!reason.item) {
313 msg << " (" << reason.desc << ')';
314 break;
315 }
316
317 msg << "\n>>> " << reason.desc << ": ";
318 // The reason may not yet have been resolved to a symbol; do so now.
319 if (std::holds_alternative<SecOffset>(v: *reason.item)) {
320 const auto &so = std::get<SecOffset>(v&: *reason.item);
321 InputSectionBase *sec = so.first;
322 Defined *sym = sec->getEnclosingSymbol(offset: so.second);
323 cur = sym ? LiveItem(sym) : LiveItem(sec);
324 } else {
325 cur = *reason.item;
326 }
327
328 if (std::holds_alternative<Symbol *>(v: cur))
329 printSymbol(std::get<Symbol *>(v&: cur));
330 else
331 msg << std::get<InputSectionBase *>(v&: cur);
332 }
333}
334
335template <class ELFT, bool TrackWhyLive>
336void MarkLive<ELFT, TrackWhyLive>::markSymbol(Symbol *sym, StringRef reason) {
337 if (auto *d = dyn_cast_or_null<Defined>(Val: sym))
338 if (auto *isec = dyn_cast_or_null<InputSectionBase>(Val: d->section))
339 enqueue(sec: isec, offset: d->value, sym, reason: {std::nullopt, reason});
340}
341
342// If -r or --emit-relocs, mark symbols referenced by relocations as used so
343// .symtab retains them and the relocations keep valid symbol indices. Callers
344// invoke this only when .symtab filtering is active (--discard-* or
345// --retain-symbols-file); otherwise .symtab keeps every symbol anyway.
346template <class ELFT> static void markUsedSymbols(InputSectionBase &sec) {
347 auto mark = [&](const auto &rel) {
348 sec.file->getRelocTargetSym(rel).setFlags(USED);
349 };
350 const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
351 for (const typename ELFT::Rel &rel : rels.rels)
352 mark(rel);
353 for (const typename ELFT::Rela &rel : rels.relas)
354 mark(rel);
355 for (const typename ELFT::Crel &rel : rels.crels)
356 mark(rel);
357}
358
359// This is the main function of the garbage collector.
360// Starting from GC-root sections, this function visits all reachable
361// sections to set their "Live" bits.
362template <class ELFT, bool TrackWhyLive>
363void MarkLive<ELFT, TrackWhyLive>::run() {
364 // Add GC root symbols.
365
366 // Preserve externally-visible symbols if the symbols defined by this
367 // file can interpose other ELF file's symbols at runtime.
368 for (Symbol *sym : ctx.symtab->getSymbols())
369 if (sym->isExported)
370 markSymbol(sym, reason: "externally visible symbol");
371
372 markSymbol(sym: ctx.symtab->find(name: ctx.arg.entry), reason: "entry point");
373 markSymbol(sym: ctx.symtab->find(name: ctx.arg.init), reason: "initializer function");
374 markSymbol(sym: ctx.symtab->find(name: ctx.arg.fini), reason: "finalizer function");
375 for (StringRef s : ctx.arg.undefined)
376 markSymbol(sym: ctx.symtab->find(name: s), reason: "undefined command line flag");
377 for (StringRef s : ctx.script->referencedSymbols)
378 markSymbol(sym: ctx.symtab->find(name: s), reason: "referenced by linker script");
379 for (auto [symName, _] : ctx.symtab->cmseSymMap) {
380 markSymbol(sym: ctx.symtab->cmseSymMap[symName].sym, reason: "ARM CMSE symbol");
381 markSymbol(sym: ctx.symtab->cmseSymMap[symName].acleSeSym, reason: "ARM CMSE symbol");
382 }
383
384 // Mark .eh_frame sections as live because there are usually no relocations
385 // that point to .eh_frames. Otherwise, the garbage collector would drop
386 // all of them. We also want to preserve personality routines and LSDA
387 // referenced by .eh_frame sections, so we scan them for that here.
388 for (EhInputSection *eh : ctx.ehInputSections)
389 scanEhFrameSection(eh&: *eh);
390 // See markUsedSymbols.
391 bool markUsed =
392 ctx.arg.copyRelocs &&
393 (ctx.arg.discard != DiscardPolicy::None || ctx.arg.retainSymbols);
394 for (InputSectionBase *sec : ctx.inputSections) {
395 if (sec->flags & SHF_GNU_RETAIN) {
396 enqueue(sec, /*offset=*/0, /*sym=*/nullptr, reason: {std::nullopt, "retained"});
397 continue;
398 }
399 if (sec->flags & SHF_LINK_ORDER)
400 continue;
401
402 // Usually, non-SHF_ALLOC sections are not removed even if they are
403 // unreachable through relocations because reachability is not a good signal
404 // whether they are garbage or not (e.g. there is usually no section
405 // referring to a .comment section, but we want to keep it.) When a
406 // non-SHF_ALLOC section is retained, we also retain sections dependent on
407 // it.
408 //
409 // Note on SHF_LINK_ORDER: Such sections contain metadata and they
410 // have a reverse dependency on the InputSection they are linked with.
411 // We are able to garbage collect them.
412 //
413 // Note on SHF_REL{,A}: Such sections reach here only when -r
414 // or --emit-reloc were given. And they are subject of garbage
415 // collection because, if we remove a text section, we also
416 // remove its relocation section.
417 //
418 // Note on nextInSectionGroup: The ELF spec says that group sections are
419 // included or omitted as a unit. We take the interpretation that:
420 //
421 // - Group members (nextInSectionGroup != nullptr) are subject to garbage
422 // collection.
423 // - Groups members are retained or discarded as a unit.
424 if (!(sec->flags & SHF_ALLOC)) {
425 if (!isStaticRelSecType(type: sec->type) && !sec->nextInSectionGroup) {
426 sec->markLive();
427 for (InputSection *isec : sec->dependentSections)
428 isec->markLive();
429 if (markUsed)
430 markUsedSymbols<ELFT>(*sec);
431 }
432 }
433
434 // Preserve special sections and those which are specified in linker
435 // script KEEP command.
436 if (isReserved(sec)) {
437 enqueue(sec, /*offset=*/0, /*sym=*/nullptr, reason: {std::nullopt, "reserved"});
438 } else if (ctx.script->shouldKeep(s: sec)) {
439 enqueue(sec, /*offset=*/0, /*sym=*/nullptr,
440 reason: {std::nullopt, "KEEP in linker script"});
441 } else if ((!ctx.arg.zStartStopGC || sec->name.starts_with(Prefix: "__libc_")) &&
442 isValidCIdentifier(s: sec->name)) {
443 // As a workaround for glibc libc.a before 2.34
444 // (https://sourceware.org/PR27492), retain __libc_atexit and similar
445 // sections regardless of zStartStopGC.
446 cNamedSections[ctx.saver.save(S: "__start_" + sec->name)].push_back(Elt: sec);
447 cNamedSections[ctx.saver.save(S: "__stop_" + sec->name)].push_back(Elt: sec);
448 }
449 }
450
451 mark();
452
453 if (TrackWhyLive) {
454 const auto handleSym = [&](Symbol *sym) {
455 if (llvm::any_of(ctx.arg.whyLive, [sym](const llvm::GlobPattern &pat) {
456 return pat.match(S: sym->getName());
457 }))
458 printWhyLive(s: sym);
459 };
460
461 for (Symbol *sym : ctx.symtab->getSymbols())
462 handleSym(sym);
463 // Handle local symbols, skipping the symbol at index 0 and section
464 // symbols, which usually have empty names and technically not live. Note:
465 // a live section may lack an associated section symbol, making them
466 // unreliable liveness indicators.
467 for (ELFFileBase *file : ctx.objectFiles)
468 for (Symbol *sym : file->getSymbols())
469 if (sym->isLocal() && sym->isDefined() && !sym->isSection())
470 handleSym(sym);
471 }
472}
473
474template <class ELFT, bool TrackWhyLive>
475void MarkLive<ELFT, TrackWhyLive>::mark() {
476 if constexpr (!TrackWhyLive) {
477 markParallel();
478 return;
479 }
480 while (!queue.empty()) {
481 InputSectionBase &sec = *queue.pop_back_val();
482
483 const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
484 for (const typename ELFT::Rel &rel : rels.rels)
485 resolveReloc(sec, rel, false);
486 for (const typename ELFT::Rela &rel : rels.relas)
487 resolveReloc(sec, rel, false);
488 for (const typename ELFT::Crel &rel : rels.crels)
489 resolveReloc(sec, rel, false);
490
491 for (InputSectionBase *isec : sec.dependentSections)
492 enqueue(sec: isec, /*offset=*/0, /*sym=*/nullptr,
493 reason: {&sec, "depended on by section"});
494
495 // Mark the next group member.
496 if (sec.nextInSectionGroup)
497 enqueue(sec: sec.nextInSectionGroup, /*offset=*/0, /*sym=*/nullptr,
498 reason: {&sec, "in section group with"});
499 }
500}
501
502// Helper function for markParallel. Walk all GC edges from sec, marking
503// everything that needs to be live. Call fn(target section, offset) for each
504// edge, which will mark the section live and handle further processing of edges
505// from that section.
506template <class ELFT, class Fn>
507static void processSectionEdges(
508 Ctx &ctx, InputSectionBase &sec,
509 const DenseMap<StringRef, SmallVector<InputSectionBase *, 0>>
510 &cNamedSections,
511 Fn fn) {
512 auto resolveEdge = [&](const auto &rel) {
513 Symbol &sym = sec.file->getRelocTargetSym(rel);
514 if (!sym.hasFlag(bit: USED))
515 sym.setFlags(USED);
516 if (auto *d = dyn_cast<Defined>(Val: &sym)) {
517 if (auto *relSec = dyn_cast_or_null<InputSectionBase>(Val: d->section)) {
518 uint64_t offset = d->value;
519 if (d->isSection()) {
520 offset += getAddend<ELFT>(ctx, sec, rel);
521 if (auto *ms = dyn_cast<MergeInputSection>(Val: relSec);
522 ms && offset >= ms->content().size())
523 return;
524 }
525 if (auto *ms = dyn_cast<MergeInputSection>(Val: relSec)) {
526 auto &piece = ms->getSectionPiece(offset);
527 auto *word =
528 reinterpret_cast<std::atomic<uint32_t> *>(&piece.inputOff + 1);
529 constexpr uint32_t liveBit = sys::IsBigEndianHost ? (1U << 31) : 1U;
530 word->fetch_or(i: liveBit, m: std::memory_order_relaxed);
531 }
532 fn(relSec, offset);
533 }
534 return;
535 }
536 for (InputSectionBase *csec : cNamedSections.lookup(Val: sym.getName()))
537 fn(csec, 0);
538 };
539 const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
540 for (const typename ELFT::Rel &rel : rels.rels)
541 resolveEdge(rel);
542 for (const typename ELFT::Rela &rel : rels.relas)
543 resolveEdge(rel);
544 for (const typename ELFT::Crel &rel : rels.crels)
545 resolveEdge(rel);
546 for (InputSectionBase *isec : sec.dependentSections)
547 fn(isec, 0);
548 if (sec.nextInSectionGroup)
549 fn(sec.nextInSectionGroup, 0);
550}
551
552// Parallel mark using level-synchronized BFS with depth-limited inline
553// recursion. Each parallelFor iteration processes a subtree up to depth 3
554// (DFS for cache locality), then queues deeper discoveries for the next level.
555template <class ELFT, bool TrackWhyLive>
556void MarkLive<ELFT, TrackWhyLive>::markParallel() {
557 const size_t numThreads = parallel::getThreadCount();
558 auto visit = [&](InputSection &sec, int depth,
559 SmallVector<InputSection *, 0> &localQueue,
560 auto &self) -> void {
561 processSectionEdges<ELFT>(
562 ctx, sec, cNamedSections,
563 [&](InputSectionBase *target, uint64_t offset) {
564 auto &part =
565 reinterpret_cast<std::atomic<uint8_t> &>(target->partition);
566 // Optimistic load-then-exchange avoids expensive atomic
567 // RMW on already-visited sections.
568 if (part.load(m: std::memory_order_relaxed) != 0 ||
569 part.exchange(i: 1, m: std::memory_order_relaxed) != 0)
570 return;
571 if (auto *s = dyn_cast<InputSection>(Val: target)) {
572 if (depth < 3)
573 self(*s, depth + 1, localQueue, self);
574 else
575 localQueue.push_back(Elt: s);
576 }
577 });
578 };
579
580 while (!queue.empty()) {
581 auto queues =
582 std::make_unique<SmallVector<InputSection *, 0>[]>(num: numThreads);
583 // Workers claim items off a shared counter and accumulate deeper
584 // discoveries into their own local queue, merged into `queue` below.
585 std::atomic<ptrdiff_t> next{ptrdiff_t(queue.size())};
586 parallelFor(0, numThreads, [&](size_t shard) {
587 for (ptrdiff_t i; (i = next.fetch_sub(i: 1, m: std::memory_order_relaxed)) > 0;)
588 visit(*queue[i - 1], 0, queues[shard], visit);
589 });
590 queue.clear();
591 for (size_t t = 0; t < numThreads; ++t)
592 queue.append(RHS: std::move(queues[t]));
593 }
594}
595
596// Before calling this function, Live bits are off for all
597// input sections. This function make some or all of them on
598// so that they are emitted to the output file.
599template <class ELFT> void elf::markLive(Ctx &ctx) {
600 llvm::TimeTraceScope timeScope("markLive");
601 // If --gc-sections is not given, retain all input sections.
602 if (!ctx.arg.gcSections) {
603 // If a DSO defines a symbol referenced in a regular object, it is needed.
604 for (Symbol *sym : ctx.symtab->getSymbols())
605 if (auto *s = dyn_cast<SharedSymbol>(Val: sym))
606 if (s->isUsedInRegularObj && !s->isWeak())
607 cast<SharedFile>(Val: s->file)->isNeeded = true;
608 // See markUsedSymbols.
609 if (ctx.arg.copyRelocs &&
610 (ctx.arg.discard != DiscardPolicy::None || ctx.arg.retainSymbols))
611 parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
612 for (InputSectionBase *sec : file->getSections())
613 if (sec)
614 markUsedSymbols<ELFT>(*sec);
615 });
616 return;
617 }
618
619 parallelForEach(ctx.inputSections,
620 [](InputSectionBase *sec) { sec->markDead(); });
621
622 // Follow the graph to mark all live sections.
623 if (ctx.arg.whyLive.empty())
624 MarkLive<ELFT, false>(ctx).run();
625 else
626 MarkLive<ELFT, true>(ctx).run();
627
628 // Determine which DSOs are needed. A DSO is needed if a non-weak SharedSymbol
629 // is used from a live section.
630 parallelForEach(ctx.symtab->getSymbols(), [](Symbol *sym) {
631 if (auto *ss = dyn_cast<SharedSymbol>(Val: sym))
632 if (ss->hasFlag(bit: USED) && !ss->isWeak())
633 cast<SharedFile>(Val: ss->file)->isNeeded = true;
634 });
635
636 // Report garbage-collected sections.
637 if (ctx.arg.printGcSections.empty())
638 return;
639 std::error_code ec;
640 raw_fd_ostream os = ctx.openAuxiliaryFile(ctx.arg.printGcSections, ec);
641 if (ec) {
642 Err(ctx) << "cannot open --print-gc-sections= file "
643 << ctx.arg.printGcSections << ": " << ec.message();
644 return;
645 }
646 for (InputSectionBase *sec : ctx.inputSections)
647 if (!sec->isLive())
648 os << "removing unused section " << toStr(ctx, sec) << '\n';
649}
650
651template void elf::markLive<ELF32LE>(Ctx &);
652template void elf::markLive<ELF32BE>(Ctx &);
653template void elf::markLive<ELF64LE>(Ctx &);
654template void elf::markLive<ELF64BE>(Ctx &);
655