[lld] [ELF] --why-live: Skip symbol at index 0 and section symbols (PR #177099)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 22:24:41 PST 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/177099

Symbols of empty names can be matched by `--why-live='*'`, which are generally not useful.

* The first entry in a symbol table (STB_LOCAL and undefined)
* `STT_SECTION` symbols (emitted by LLVM integrated assembler when needed by relocations). These input section symbols will be demoted by `demoteAndCopyLocalSymbols`, so technically not really live. In addition, such symbols of non-allocable sections currently lead to crashes: `whyLive` does not record the section, causing the second iteration of the `while (true)` loop in printWhyLive to call `std::get<Symbol *>(cur)` when `cur` is an `InputSectionBase *`.

Fix #176890

>From 3bc7092989d212c3f0935e703e600777fb12b7af Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 20 Jan 2026 21:45:05 -0800
Subject: [PATCH] [ELF] --why-live: Skip symbol at index 0 and section symbols

Symbols of empty names can be matched by `--why-live='*'`, which are
generally not useful.

* The first entry in a symbol table (STB_LOCAL and undefined)
* `STT_SECTION` symbols (emitted by LLVM integrated assembler when
  needed by relocations). These input section symbols will be demoted by
  `demoteAndCopyLocalSymbols`, so technically not really live.
  In addition, such symbols of non-allocable sections currently lead to
  crashes: `whyLive` does not record the section, causing the second
  iteration of the `while (true)` loop in printWhyLive to call
  `std::get<Symbol *>(cur)` when `cur` is an `InputSectionBase *`.

Fix #176890
---
 lld/ELF/MarkLive.cpp       |  6 +++++-
 lld/test/ELF/why-live.test | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index a7b0f08c8d954..59d18839b3cb0 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -443,9 +443,13 @@ void MarkLive<ELFT, TrackWhyLive>::run() {
 
     for (Symbol *sym : ctx.symtab->getSymbols())
       handleSym(sym);
+    // Handle local symbols, skipping the symbol at index 0 and section
+    // symbols, which usually have empty names and technically not live. Note:
+    // a live section may lack an associated section symbol, making them
+    // unreliable liveness indicators.
     for (ELFFileBase *file : ctx.objectFiles)
       for (Symbol *sym : file->getSymbols())
-        if (sym->isLocal())
+        if (sym->isLocal() && sym->isDefined() && !sym->isSection())
           handleSym(sym);
   }
 }
diff --git a/lld/test/ELF/why-live.test b/lld/test/ELF/why-live.test
index d8f8cc7b6db64..4addb0dfc01b1 100644
--- a/lld/test/ELF/why-live.test
+++ b/lld/test/ELF/why-live.test
@@ -4,6 +4,7 @@
 # RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o shared.o shared.s
 # RUN: ld.lld -shared shared.o -o a.so
 # RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o a.o a.s
+# RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o b.o b.s
 
 #--- shared.s
 .globl test_shared
@@ -159,3 +160,23 @@ test_local:
 # MULTIPLE-DAG: live symbol: a.o:(test_section_offset)
 # MULTIPLE-DAG: live symbol: a.o:(test_section_offset_within_symbol)
 # MULTIPLE-NOT: live symbol
+
+#--- b.s
+## --why-live='*' skips symbol at index 0 and section symbols.
+# RUN: ld.lld b.o --gc-sections --why-live="*" | FileCheck %s --check-prefix=STAR --implicit-check-not='live symbol:'
+# STAR: live symbol: b.o:(_start) (entry point)
+# STAR: live symbol: b.o:(b.s) (no section)
+
+## STT_FILE symbol
+.file "b.s"
+
+.text
+.globl _start
+_start:
+  call .alloc
+
+.section .alloc,"ax"
+  ret
+
+.section .nonalloc
+  .long .nonalloc



More information about the llvm-commits mailing list