[lld] [ELF] Make shouldAddProvideSym return values consistent when demoted to Undefined (PR #111945)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 11 08:45:29 PDT 2024
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/111945
>From 6ca49904c11182318fffee95307ba45ce1febc7b Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 10 Oct 2024 20:58:02 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
lld/ELF/LinkerScript.cpp | 7 ++++-
lld/test/ELF/linkerscript/provide-defined.s | 33 +++++++++++++++++++++
2 files changed, 39 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/linkerscript/provide-defined.s
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index f3f95ec589bd82..2247d592c8bca3 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1811,6 +1811,11 @@ void LinkerScript::addScriptReferencedSymbolsToSymTable() {
}
bool LinkerScript::shouldAddProvideSym(StringRef symName) {
+ // A Defined may be demoted to Undefined but the return value must stay the
+ // same. Use isUsedInRegularObj to ensure this. The exportDynamic condition,
+ // while not so accurate, allows PROVIDE to define a symbol referenced by a
+ // DSO.
Symbol *sym = elf::ctx.symtab->find(symName);
- return sym && !sym->isDefined() && !sym->isCommon();
+ return sym && !sym->isDefined() && !sym->isCommon() &&
+ (sym->isUsedInRegularObj || sym->exportDynamic);
}
diff --git a/lld/test/ELF/linkerscript/provide-defined.s b/lld/test/ELF/linkerscript/provide-defined.s
new file mode 100644
index 00000000000000..6ebfd706c16e1b
--- /dev/null
+++ b/lld/test/ELF/linkerscript/provide-defined.s
@@ -0,0 +1,33 @@
+# REQUIRES: x86
+## Test the GC behavior when the PROVIDE symbol is defined by a relocatable file.
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 b.s -o b.o
+# RUN: ld.lld -T a.t --gc-sections a.o b.o -o a
+# RUN: llvm-readelf -s a | FileCheck %s
+
+# CHECK: 1: {{.*}} 0 NOTYPE GLOBAL DEFAULT 1 _start
+# CHECK-NEXT:2: {{.*}} 0 NOTYPE GLOBAL DEFAULT 2 f3
+# CHECK-NOT: {{.}}
+
+#--- a.s
+.global _start, f1, f2, f3, bar
+_start:
+ call f3
+
+.section .text.f1,"ax"; f1:
+.section .text.f2,"ax"; f2:
+.section .text.f3,"ax"; f3:
+.section .text.bar,"ax"; bar:
+
+#--- b.s
+ call f2
+
+#--- a.t
+SECTIONS {
+ . = . + SIZEOF_HEADERS;
+ PROVIDE(f1 = bar+1);
+ PROVIDE(f2 = bar+2);
+ PROVIDE(f3 = bar+3);
+}
>From 665ccc46734eeb8b33930211e0499f9a4b478d1f Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 11 Oct 2024 08:45:21 -0700
Subject: [PATCH 2/2] add common symbol test
Created using spr 1.3.5-bogner
---
lld/test/ELF/linkerscript/provide-defined.s | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/lld/test/ELF/linkerscript/provide-defined.s b/lld/test/ELF/linkerscript/provide-defined.s
index 6ebfd706c16e1b..1d44bef3d4068d 100644
--- a/lld/test/ELF/linkerscript/provide-defined.s
+++ b/lld/test/ELF/linkerscript/provide-defined.s
@@ -17,10 +17,12 @@ _start:
call f3
.section .text.f1,"ax"; f1:
-.section .text.f2,"ax"; f2:
-.section .text.f3,"ax"; f3:
+.section .text.f2,"ax"; f2: # referenced by another relocatable file
+.section .text.f3,"ax"; f3: # live
.section .text.bar,"ax"; bar:
+.comm comm,4,4
+
#--- b.s
call f2
@@ -30,4 +32,5 @@ SECTIONS {
PROVIDE(f1 = bar+1);
PROVIDE(f2 = bar+2);
PROVIDE(f3 = bar+3);
+ PROVIDE(f4 = comm+4);
}
More information about the llvm-commits
mailing list