@@ -48,11 +48,11 @@ namespace {
4848using SecOffset = std::pair<InputSectionBase *, unsigned >;
4949
5050// Something that can have an independent reason for being live.
51- using LiveObject = std::variant<InputSectionBase *, Symbol *, SecOffset>;
51+ using LiveItem = std::variant<InputSectionBase *, Symbol *, SecOffset>;
5252
53- // The most proximate reason that an object is live.
53+ // The most proximate reason that something is live.
5454struct LiveReason {
55- std::optional<LiveObject> obj ;
55+ std::optional<LiveItem> item ;
5656 StringRef desc;
5757};
5858
@@ -88,7 +88,7 @@ template <class ELFT, bool TrackWhyLive> class MarkLive {
8888 DenseMap<StringRef, SmallVector<InputSectionBase *, 0 >> cNamedSections;
8989
9090 // The most proximate reason that something is live.
91- DenseMap<LiveObject , LiveReason> whyLive;
91+ DenseMap<LiveItem , LiveReason> whyLive;
9292};
9393} // namespace
9494
@@ -164,7 +164,7 @@ void MarkLive<ELFT, TrackWhyLive>::resolveReloc(InputSectionBase &sec,
164164 }
165165
166166 for (InputSectionBase *sec : cNamedSections.lookup (sym.getName ()))
167- enqueue (sec, 0 , nullptr , reason);
167+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , reason);
168168}
169169
170170// The .eh_frame section is an unfortunate special case.
@@ -240,12 +240,12 @@ void MarkLive<ELFT, TrackWhyLive>::enqueue(InputSectionBase *sec,
240240
241241 if (TrackWhyLive) {
242242 if (sym) {
243- // If a specific symbol is referenced, that makes it alive. It may in turn
244- // make its section alive .
243+ // If a specific symbol is referenced, that keeps it live. The symbol then
244+ // keeps its section live .
245245 whyLive.try_emplace (sym, reason);
246246 whyLive.try_emplace (sec, LiveReason{sym, " contained live symbol" });
247247 } else {
248- // Otherwise, the reference generically makes the section live.
248+ // Otherwise, the reference generically keeps the section live.
249249 whyLive.try_emplace (sec, reason);
250250 }
251251 }
@@ -268,57 +268,49 @@ void MarkLive<ELFT, TrackWhyLive>::printWhyLive(Symbol *s) const {
268268 auto msg = Msg (ctx);
269269
270270 const auto printSymbol = [&](Symbol *s) {
271- if (s->isLocal ())
272- msg << s->file << " :(" << s << ' )' ;
273- else
274- msg << s;
271+ msg << s->file << " :(" << s << ' )' ;
275272 };
276273
277274 msg << " live symbol: " ;
278275 printSymbol (s);
279276
280- LiveObject cur = s;
277+ LiveItem cur = s;
281278 while (true ) {
282279 auto it = whyLive.find (cur);
283280 LiveReason reason;
284- // If there is a specific reason this object is live...
281+ // If there is a specific reason this item is live...
285282 if (it != whyLive.end ()) {
286283 reason = it->second ;
287284 } else {
288- // This object is live, but it has no tracked reason. It must be an
285+ // This item is live, but it has no tracked reason. It must be an
289286 // unreferenced symbol in a live section or a symbol with no section.
290- const auto getParentSec = [&]() -> InputSectionBase * {
291- auto *d = dyn_cast<Defined>(std::get<Symbol *>(cur));
292- if (!d)
293- return nullptr ;
294- return dyn_cast_or_null<InputSectionBase>(d->section );
295- };
296- InputSectionBase *sec = getParentSec ();
287+ InputSectionBase *sec = nullptr ;
288+ if (auto *d = dyn_cast<Defined>(std::get<Symbol *>(cur)))
289+ sec = dyn_cast_or_null<InputSectionBase>(d->section );
297290 reason = sec ? LiveReason{sec, " in live section" }
298291 : LiveReason{std::nullopt , " no section" };
299292 }
300293
301- if (reason.obj ) {
302- msg << " \n >>> " << reason.desc << " : " ;
303- // The reason may not yet have been resolved to a symbol; do so now.
304- if (std::holds_alternative<SecOffset>(*reason.obj )) {
305- const auto &so = std::get<SecOffset>(*reason.obj );
306- InputSectionBase *sec = so.first ;
307- Defined *sym = sec->getEnclosingSymbol (so.second );
308- cur = sym ? LiveObject (sym) : LiveObject (sec);
309- } else {
310- cur = *reason.obj ;
311- }
312-
313- if (std::holds_alternative<Symbol *>(cur))
314- printSymbol (std::get<Symbol *>(cur));
315- else
316- msg << std::get<InputSectionBase *>(cur);
317- }
318- if (!reason.obj ) {
294+ if (!reason.item ) {
319295 msg << " (" << reason.desc << ' )' ;
320296 break ;
321297 }
298+
299+ msg << " \n >>> " << reason.desc << " : " ;
300+ // The reason may not yet have been resolved to a symbol; do so now.
301+ if (std::holds_alternative<SecOffset>(*reason.item )) {
302+ const auto &so = std::get<SecOffset>(*reason.item );
303+ InputSectionBase *sec = so.first ;
304+ Defined *sym = sec->getEnclosingSymbol (so.second );
305+ cur = sym ? LiveItem (sym) : LiveItem (sec);
306+ } else {
307+ cur = *reason.item ;
308+ }
309+
310+ if (std::holds_alternative<Symbol *>(cur))
311+ printSymbol (std::get<Symbol *>(cur));
312+ else
313+ msg << std::get<InputSectionBase *>(cur);
322314 }
323315}
324316
@@ -374,7 +366,7 @@ void MarkLive<ELFT, TrackWhyLive>::run() {
374366 }
375367 for (InputSectionBase *sec : ctx.inputSections ) {
376368 if (sec->flags & SHF_GNU_RETAIN) {
377- enqueue (sec, 0 , nullptr , {std::nullopt , " retained" });
369+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , {std::nullopt , " retained" });
378370 continue ;
379371 }
380372 if (sec->flags & SHF_LINK_ORDER)
@@ -413,9 +405,10 @@ void MarkLive<ELFT, TrackWhyLive>::run() {
413405 // Preserve special sections and those which are specified in linker
414406 // script KEEP command.
415407 if (isReserved (sec)) {
416- enqueue (sec, 0 , nullptr , {std::nullopt , " reserved" });
408+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , {std::nullopt , " reserved" });
417409 } else if (ctx.script ->shouldKeep (sec)) {
418- enqueue (sec, 0 , nullptr , {std::nullopt , " KEEP in linker script" });
410+ enqueue (sec, /* offset=*/ 0 , /* sym=*/ nullptr ,
411+ {std::nullopt , " KEEP in linker script" });
419412 } else if ((!ctx.arg .zStartStopGC || sec->name .starts_with (" __libc_" )) &&
420413 isValidCIdentifier (sec->name )) {
421414 // As a workaround for glibc libc.a before 2.34
@@ -460,11 +453,11 @@ void MarkLive<ELFT, TrackWhyLive>::mark() {
460453 resolveReloc (sec, rel, false );
461454
462455 for (InputSectionBase *isec : sec.dependentSections )
463- enqueue (isec, 0 , nullptr , {&sec, " dependent section" });
456+ enqueue (isec, /* offset= */ 0 , /* sym= */ nullptr , {&sec, " dependent section" });
464457
465458 // Mark the next group member.
466459 if (sec.nextInSectionGroup )
467- enqueue (sec.nextInSectionGroup , 0 , nullptr ,
460+ enqueue (sec.nextInSectionGroup , /* offset= */ 0 , /* sym= */ nullptr ,
468461 {&sec, " next in section group" });
469462 }
470463}
@@ -492,7 +485,7 @@ void MarkLive<ELFT, TrackWhyLive>::moveToMain() {
492485 continue ;
493486 if (ctx.symtab ->find ((" __start_" + sec->name ).str ()) ||
494487 ctx.symtab ->find ((" __stop_" + sec->name ).str ()))
495- enqueue (sec, 0 , nullptr , {});
488+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , /* reason= */ {});
496489 }
497490
498491 mark ();
0 commit comments