@@ -48,11 +48,11 @@ namespace {
48
48
using SecOffset = std::pair<InputSectionBase *, unsigned >;
49
49
50
50
// 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>;
52
52
53
- // The most proximate reason that an object is live.
53
+ // The most proximate reason that something is live.
54
54
struct LiveReason {
55
- std::optional<LiveObject> obj ;
55
+ std::optional<LiveItem> item ;
56
56
StringRef desc;
57
57
};
58
58
@@ -88,7 +88,7 @@ template <class ELFT, bool TrackWhyLive> class MarkLive {
88
88
DenseMap<StringRef, SmallVector<InputSectionBase *, 0 >> cNamedSections;
89
89
90
90
// The most proximate reason that something is live.
91
- DenseMap<LiveObject , LiveReason> whyLive;
91
+ DenseMap<LiveItem , LiveReason> whyLive;
92
92
};
93
93
} // namespace
94
94
@@ -164,7 +164,7 @@ void MarkLive<ELFT, TrackWhyLive>::resolveReloc(InputSectionBase &sec,
164
164
}
165
165
166
166
for (InputSectionBase *sec : cNamedSections.lookup (sym.getName ()))
167
- enqueue (sec, 0 , nullptr , reason);
167
+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , reason);
168
168
}
169
169
170
170
// The .eh_frame section is an unfortunate special case.
@@ -240,12 +240,12 @@ void MarkLive<ELFT, TrackWhyLive>::enqueue(InputSectionBase *sec,
240
240
241
241
if (TrackWhyLive) {
242
242
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 .
245
245
whyLive.try_emplace (sym, reason);
246
246
whyLive.try_emplace (sec, LiveReason{sym, " contained live symbol" });
247
247
} else {
248
- // Otherwise, the reference generically makes the section live.
248
+ // Otherwise, the reference generically keeps the section live.
249
249
whyLive.try_emplace (sec, reason);
250
250
}
251
251
}
@@ -268,57 +268,49 @@ void MarkLive<ELFT, TrackWhyLive>::printWhyLive(Symbol *s) const {
268
268
auto msg = Msg (ctx);
269
269
270
270
const auto printSymbol = [&](Symbol *s) {
271
- if (s->isLocal ())
272
- msg << s->file << " :(" << s << ' )' ;
273
- else
274
- msg << s;
271
+ msg << s->file << " :(" << s << ' )' ;
275
272
};
276
273
277
274
msg << " live symbol: " ;
278
275
printSymbol (s);
279
276
280
- LiveObject cur = s;
277
+ LiveItem cur = s;
281
278
while (true ) {
282
279
auto it = whyLive.find (cur);
283
280
LiveReason reason;
284
- // If there is a specific reason this object is live...
281
+ // If there is a specific reason this item is live...
285
282
if (it != whyLive.end ()) {
286
283
reason = it->second ;
287
284
} 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
289
286
// 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 );
297
290
reason = sec ? LiveReason{sec, " in live section" }
298
291
: LiveReason{std::nullopt, " no section" };
299
292
}
300
293
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 ) {
319
295
msg << " (" << reason.desc << ' )' ;
320
296
break ;
321
297
}
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);
322
314
}
323
315
}
324
316
@@ -374,7 +366,7 @@ void MarkLive<ELFT, TrackWhyLive>::run() {
374
366
}
375
367
for (InputSectionBase *sec : ctx.inputSections ) {
376
368
if (sec->flags & SHF_GNU_RETAIN) {
377
- enqueue (sec, 0 , nullptr , {std::nullopt, " retained" });
369
+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , {std::nullopt, " retained" });
378
370
continue ;
379
371
}
380
372
if (sec->flags & SHF_LINK_ORDER)
@@ -413,9 +405,10 @@ void MarkLive<ELFT, TrackWhyLive>::run() {
413
405
// Preserve special sections and those which are specified in linker
414
406
// script KEEP command.
415
407
if (isReserved (sec)) {
416
- enqueue (sec, 0 , nullptr , {std::nullopt, " reserved" });
408
+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , {std::nullopt, " reserved" });
417
409
} 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" });
419
412
} else if ((!ctx.arg .zStartStopGC || sec->name .starts_with (" __libc_" )) &&
420
413
isValidCIdentifier (sec->name )) {
421
414
// As a workaround for glibc libc.a before 2.34
@@ -460,11 +453,11 @@ void MarkLive<ELFT, TrackWhyLive>::mark() {
460
453
resolveReloc (sec, rel, false );
461
454
462
455
for (InputSectionBase *isec : sec.dependentSections )
463
- enqueue (isec, 0 , nullptr , {&sec, " dependent section" });
456
+ enqueue (isec, /* offset= */ 0 , /* sym= */ nullptr , {&sec, " dependent section" });
464
457
465
458
// Mark the next group member.
466
459
if (sec.nextInSectionGroup )
467
- enqueue (sec.nextInSectionGroup , 0 , nullptr ,
460
+ enqueue (sec.nextInSectionGroup , /* offset= */ 0 , /* sym= */ nullptr ,
468
461
{&sec, " next in section group" });
469
462
}
470
463
}
@@ -492,7 +485,7 @@ void MarkLive<ELFT, TrackWhyLive>::moveToMain() {
492
485
continue ;
493
486
if (ctx.symtab ->find ((" __start_" + sec->name ).str ()) ||
494
487
ctx.symtab ->find ((" __stop_" + sec->name ).str ()))
495
- enqueue (sec, 0 , nullptr , {});
488
+ enqueue (sec, /* offset= */ 0 , /* sym= */ nullptr , /* reason= */ {});
496
489
}
497
490
498
491
mark ();
0 commit comments