[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:26:03 PDT 2026


================
@@ -375,6 +375,12 @@ template <class ELFT> void Writer<ELFT>::run() {
   }
 }
 
+static bool retainKeepsInSymtab(Ctx &ctx, const Symbol &sym) {
+  if (sym.hasFlag(USED) && ctx.arg.copyRelocs)
+    return true;
+  return ctx.arg.retainSymbols->contains(sym.getName());
----------------
MaskRay wrote:

Improved `lld/test/ELF/retain-symbols-file.s`. Without `&& ctx.arg.copyRelocs` there will be more .symtab entries

Current desired behavior
```
% myld.lld -shared --gc-sections --retain-symbols-file=retain a.o -o sym.so && readelf -W -rs sym.so
Relocation section '.rela.plt' at offset 0x350 contains 2 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000003488  0000000300000007 R_X86_64_JUMP_SLOT     000000000000138d used + 0
0000000000003490  0000000100000007 R_X86_64_JUMP_SLOT     0000000000000000 und + 0

Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND und
     2: 0000000000001380     0 NOTYPE  GLOBAL DEFAULT    6 _start
     3: 000000000000138d     0 NOTYPE  GLOBAL DEFAULT    6 used
     4: 000000000000138a     0 FUNC    GLOBAL DEFAULT    6 retain1
     5: 000000000000138b     0 NOTYPE  GLOBAL DEFAULT    6 retain2
     6: 000000000000138c     0 NOTYPE  GLOBAL DEFAULT    6 nonalloc_referenced

Symbol table '.symtab' contains 3 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 000000000000138a     0 FUNC    GLOBAL DEFAULT    6 retain1
     2: 000000000000138b     0 NOTYPE  GLOBAL DEFAULT    6 retain2
```

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


More information about the llvm-commits mailing list