[lld] [ELF] --retain-symbols-file: keep listed symbols in .symtab, not .dynsym (PR #209063)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 23:12:41 PDT 2026


================
@@ -339,10 +339,15 @@ void MarkLive<ELFT, TrackWhyLive>::markSymbol(Symbol *sym, StringRef reason) {
       enqueue(isec, d->value, sym, {std::nullopt, reason});
 }
 
-template <class ELFT> static void markUsedLocalSymbols(InputSectionBase &sec) {
+// If -r or --emit-relocs, ensure referenced symbols are preserved in .symtab.
+// --discard-* drops only locals; --retain-symbols-file also drops globals, so
+// mark those too.
+template <class ELFT>
+static void markUsedSymbols(Ctx &ctx, InputSectionBase &sec) {
+  bool retain = ctx.arg.retainSymbols.has_value();
   auto mark = [&](const auto &rel) {
     Symbol &sym = sec.file->getRelocTargetSym(rel);
-    if (sym.isLocal())
+    if (sym.isLocal() || retain)
----------------
MaskRay wrote:

In the `--emit-relocs --no-gc-sections` link of clang, `markLive` time will increase (`26.7 ms => 32.5 ms`) if I remove the condition.

https://github.com/llvm/llvm-project/pull/209063


More information about the llvm-commits mailing list