Skip to content

Commit 074af0f

Browse files
authored
[lld][ELF] Add --why-live flag (inspired by Mach-O) (#127112)
This prints a stack of reasons that symbols that match the given glob(s) survived GC. It has no effect unless section GC occurs. This implementation does not require -ffunction-sections or -fdata-sections to produce readable results, althought it does tend to work better (as does GC). Details about the semantics: - Some chain of liveness reasons is reported; it isn't specified which chain. - A symbol or section may be live: - Intrisically (e.g., entry point) - Because needed by a live symbol or section - (Symbols only) Because part of a section live for another reason - (Sections only) Because they contain a live symbol - Both global and local symbols (`STB_LOCAL`) are supported. - References to symbol + offset are considered to point to: - If the referenced symbol is a section (`STT_SECTION`): - If a sized symbol encloses the referenced offset, the enclosing symbol. - Otherwise, the section itself, generically. - Otherwise, the referenced symbol.
1 parent 03eb825 commit 074af0f

File tree

7 files changed

+363
-38
lines changed

7 files changed

+363
-38
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ struct Config {
226226
llvm::StringRef thinLTOCacheDir;
227227
llvm::StringRef thinLTOIndexOnlyArg;
228228
llvm::StringRef whyExtract;
229+
llvm::SmallVector<llvm::GlobPattern, 0> whyLive;
229230
llvm::StringRef cmseInputLib;
230231
llvm::StringRef cmseOutputLib;
231232
ReportPolicy zBtiReport = ReportPolicy::None;

lld/ELF/Driver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,15 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15451545
ctx.arg.warnSymbolOrdering =
15461546
args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
15471547
ctx.arg.whyExtract = args.getLastArgValue(OPT_why_extract);
1548+
for (opt::Arg *arg : args.filtered(OPT_why_live)) {
1549+
StringRef value(arg->getValue());
1550+
if (Expected<GlobPattern> pat = GlobPattern::create(arg->getValue())) {
1551+
ctx.arg.whyLive.emplace_back(std::move(*pat));
1552+
} else {
1553+
ErrAlways(ctx) << arg->getSpelling() << ": " << pat.takeError();
1554+
continue;
1555+
}
1556+
}
15481557
ctx.arg.zCombreloc = getZFlag(args, "combreloc", "nocombreloc", true);
15491558
ctx.arg.zCopyreloc = getZFlag(args, "copyreloc", "nocopyreloc", true);
15501559
ctx.arg.zForceBti = hasZOption(args, "force-bti");

0 commit comments

Comments
 (0)