[lld] [ELF] Simplify checkDuplicate; cover SharedSymbol/CommonSymbol resolve. NFC (PR #195541)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 3 12:34:38 PDT 2026


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

Drop the redundant isDefined() check.

Extend common-shared.s to also exercise the case where the SharedSymbol's
st_size is not larger than the incoming CommonSymbol's.

>From d72dec89f8bcfed6155141127166591f2dca5062 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 3 May 2026 12:32:26 -0700
Subject: [PATCH] [ELF] Simplify checkDuplicate; cover
 SharedSymbol/CommonSymbol resolve. NFC

Drop the redundant isDefined() check.

Extend common-shared.s to also exercise the case where the SharedSymbol's
st_size is not larger than the incoming CommonSymbol's.
---
 lld/ELF/Symbols.cpp          |  2 +-
 lld/test/ELF/common-shared.s | 26 +++++++++++++++++++-------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 34727f24dddef..b1859b72afbe3 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -579,7 +579,7 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
 }
 
 void Symbol::checkDuplicate(Ctx &ctx, const Defined &other) const {
-  if (isDefined() && !isWeak() && !other.isWeak())
+  if (!isWeak() && !other.isWeak())
     reportDuplicate(ctx, *this, other.file,
                     dyn_cast_or_null<InputSectionBase>(other.section),
                     other.value);
diff --git a/lld/test/ELF/common-shared.s b/lld/test/ELF/common-shared.s
index 22f0d99a7517a..f62b2693aad8b 100644
--- a/lld/test/ELF/common-shared.s
+++ b/lld/test/ELF/common-shared.s
@@ -1,16 +1,28 @@
 # REQUIRES: x86
 ## When a common symbol is merged with a shared symbol, pick the larger st_size.
 
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
-# RUN: echo '.globl com; .comm com, 16' | llvm-mc -filetype=obj -triple=x86_64 - -o %t1.o
-# RUN: ld.lld -shared %t1.o -o %t1.so
+# RUN: split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 small.s -o small.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 big.s -o big.o
+# RUN: ld.lld -shared big.o -o big.so
+# RUN: ld.lld -shared small.o -o small.so
 
-# RUN: ld.lld %t.o %t1.so -o %t
-# RUN: llvm-readelf -s %t | FileCheck %s
-# RUN: ld.lld %t1.so %t.o -o %t
-# RUN: llvm-readelf -s %t | FileCheck %s
+## Common arrives first, then larger shared.
+# RUN: ld.lld small.o big.so -o out1
+# RUN: llvm-readelf -s out1 | FileCheck %s
+## Larger shared first, then common (overwrite path, size > this->size).
+# RUN: ld.lld big.so small.o -o out2
+# RUN: llvm-readelf -s out2 | FileCheck %s
+## Smaller shared first, then larger common (overwrite path, size <= this->size).
+# RUN: ld.lld small.so big.o -o out3
+# RUN: llvm-readelf -s out3 | FileCheck %s
 
 # CHECK: 16 OBJECT GLOBAL DEFAULT [[#]] com
 
+#--- small.s
 .globl com
 .comm com,1
+
+#--- big.s
+.globl com
+.comm com,16



More information about the llvm-commits mailing list