[lld] [ELF] maybeWarnUnorderableSymbol: drop redundant `d &&`, assert isShared. NFC (PR #195543)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 3 13:03:26 PDT 2026


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

Test Defined first via dyn_cast. Lazy/Common/Placeholder cannot reach
here: readCallGraph runs after replaceCommonSymbols, buildSectionOrder
runs after demoteSymbolsAndComputeIsPreemptible, and redirectSymbols
detaches Placeholder foo at v1 stubs from objectFiles.

In symbol-ordering-file-warnings.s, add a common-symbol case and use
`count 0` for empty-output checks.

>From cd8304b94dde22df7707342d0443108a1dfc7daa Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 3 May 2026 13:00:44 -0700
Subject: [PATCH] [ELF] maybeWarnUnorderableSymbol: drop redundant `d &&`,
 assert isShared. NFC

Test Defined first via dyn_cast. Lazy/Common/Placeholder cannot reach
here: readCallGraph runs after replaceCommonSymbols, buildSectionOrder
runs after demoteSymbolsAndComputeIsPreemptible, and redirectSymbols
detaches Placeholder foo at v1 stubs from objectFiles.

In symbol-ordering-file-warnings.s, add a common-symbol case and use
`count 0` for empty-output checks.
---
 lld/ELF/Symbols.cpp                          | 22 +++++++++---------
 lld/test/ELF/symbol-ordering-file-warnings.s | 24 +++++++++++---------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index b1859b72afbe3..7bad4ceccec33 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -312,23 +312,23 @@ void elf::maybeWarnUnorderableSymbol(Ctx &ctx, const Symbol *sym) {
     return;
 
   const InputFile *file = sym->file;
-  auto *d = dyn_cast<Defined>(sym);
-
   auto report = [&](StringRef s) { Warn(ctx) << file << s << sym->getName(); };
-
-  if (sym->isUndefined()) {
+  if (auto *d = dyn_cast<Defined>(sym)) {
+    if (!d->section)
+      report(": unable to order absolute symbol: ");
+    else if (isa<OutputSection>(d->section))
+      report(": unable to order synthetic symbol: ");
+    else if (!d->section->isLive())
+      report(": unable to order discarded symbol: ");
+  } else if (sym->isUndefined()) {
     if (cast<Undefined>(sym)->discardedSecIdx)
       report(": unable to order discarded symbol: ");
     else
       report(": unable to order undefined symbol: ");
-  } else if (sym->isShared())
+  } else {
+    assert(sym->isShared());
     report(": unable to order shared symbol: ");
-  else if (d && !d->section)
-    report(": unable to order absolute symbol: ");
-  else if (d && isa<OutputSection>(d->section))
-    report(": unable to order synthetic symbol: ");
-  else if (d && !d->section->isLive())
-    report(": unable to order discarded symbol: ");
+  }
 }
 
 // Returns true if a symbol can be replaced at load-time by a symbol
diff --git a/lld/test/ELF/symbol-ordering-file-warnings.s b/lld/test/ELF/symbol-ordering-file-warnings.s
index e4611e4031aab..22258e85b63e1 100644
--- a/lld/test/ELF/symbol-ordering-file-warnings.s
+++ b/lld/test/ELF/symbol-ordering-file-warnings.s
@@ -11,8 +11,7 @@
 
 # Check that the warning can be disabled.
 # RUN: ld.lld %t1.o -o %t --symbol-ordering-file %t-order-missing.txt \
-# RUN:   --unresolved-symbols=ignore-all --no-warn-symbol-ordering 2>&1 | \
-# RUN:   FileCheck %s --check-prefixes=WARN --allow-empty
+# RUN:   --unresolved-symbols=ignore-all --no-warn-symbol-ordering 2>&1 | count 0
 
 # Check that the warning can be re-enabled
 # RUN: ld.lld %t1.o -o %t --symbol-ordering-file %t-order-missing.txt \
@@ -50,20 +49,21 @@
 # RUN: ld.lld %t1.o -o %t --symbol-ordering-file %t-order-discard.txt -T %t.script \
 # RUN:   --unresolved-symbols=ignore-all 2>&1 | FileCheck %s --check-prefixes=WARN,DISCARD
 
-# Check that LLD does not warn for discarded COMDAT symbols, if they are present in the kept instance.
-# RUN: echo "comdat" > %t-order-comdat.txt
-# RUN: ld.lld %t1.o %t2.o -o %t --symbol-ordering-file %t-order-comdat.txt \
-# RUN:   --unresolved-symbols=ignore-all 2>&1 | FileCheck %s --check-prefixes=WARN --allow-empty
+## No warning for symbols that resolve cleanly: a common symbol
+## (replaceCommonSymbols turns it into a Defined before
+## --symbol-ordering-file is processed), a discarded COMDAT whose kept
+## instance is in %t2.o, and a weak symbol replaced by an equivalent global.
+# RUN: echo "common" > %t-order-clean.txt
+# RUN: echo "comdat" >> %t-order-clean.txt
+# RUN: echo "glob_or_wk" >> %t-order-clean.txt
+# RUN: ld.lld %t1.o %t2.o -o %t --symbol-ordering-file %t-order-clean.txt \
+# RUN:   --unresolved-symbols=ignore-all 2>&1 | count 0
 
 # Check that if a COMDAT was unused and discarded via --gc-sections, warn for each instance.
+# RUN: echo "comdat" > %t-order-comdat.txt
 # RUN: ld.lld %t1.o %t2.o -o %t --symbol-ordering-file %t-order-comdat.txt --gc-sections \
 # RUN:   --unresolved-symbols=ignore-all 2>&1 | FileCheck %s --check-prefixes=WARN,COMDAT
 
-# Check that if a weak symbol is not kept, because of an equivalent global symbol, no warning is emitted.
-# RUN: echo "glob_or_wk" > %t-order-weak.txt
-# RUN: ld.lld %t1.o %t2.o -o %t --symbol-ordering-file %t-order-weak.txt \
-# RUN:   --unresolved-symbols=ignore-all 2>&1 | FileCheck %s --check-prefixes=WARN --allow-empty
-
 # Check that symbols only in unused archive members does not result in a warning.
 # RUN: rm -f %t.a
 # RUN: llvm-ar rc %t.a %t3.o
@@ -126,6 +126,8 @@ _start:
 
 absolute = 0x1234
 
+.comm common,4,4
+
 .section .text.comdat,"axG", at progbits,comdat,comdat
 .weak comdat
 comdat:



More information about the llvm-commits mailing list