[lld] db1988f - [ELF] Don't change binding to STB_WEAK for an undefined specified by -u

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 08:31:41 PDT 2020


Author: Fangrui Song
Date: 2020-10-08T08:31:34-07:00
New Revision: db1988f038843ad047fdab5b9f818306b06ea80a

URL: https://github.com/llvm/llvm-project/commit/db1988f038843ad047fdab5b9f818306b06ea80a
DIFF: https://github.com/llvm/llvm-project/commit/db1988f038843ad047fdab5b9f818306b06ea80a.diff

LOG: [ELF] Don't change binding to STB_WEAK for an undefined specified by -u

Similar to D66992.
In GNU ld, a -u specified symbol is a STB_DEFAULT undefined.
It cannot be changed to STB_WEAK by a later STB_WEAK undefined in a regular object file.

The behavior is consistent with our model because -u means "we need to fetch a lazy definition".
It should not be altered just because there is also a STB_WEAK undefined.

Note, our -u semantics are still different from GNU ld (https://github.com/ClangBuiltLinux/linux/issues/515):
we don't force the specified symbol to appear in .symtab This is a deliberate decision.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D88945

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/test/ELF/weak-undef-lib.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6aca2306d1e9..30575f66d017 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1992,7 +1992,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
   // Handle -u/--undefined before input files. If both a.a and b.so define foo,
   // -u foo a.a b.so will fetch a.a.
   for (StringRef name : config->undefined)
-    addUnusedUndefined(name);
+    addUnusedUndefined(name)->referenced = true;
 
   // Add all files to the symbol table. This will add almost all
   // symbols that we need to the symbol table. This process might

diff  --git a/lld/test/ELF/weak-undef-lib.s b/lld/test/ELF/weak-undef-lib.s
index 54e05dc7e987..19b59fddaa72 100644
--- a/lld/test/ELF/weak-undef-lib.s
+++ b/lld/test/ELF/weak-undef-lib.s
@@ -17,6 +17,17 @@
 # CHECK-NEXT: Other: 0
 # CHECK-NEXT: Section: Undefined
 
+## -u specifies a STB_DEFAULT undefined symbol, so the definition from %t2.o is
+## fetched.
+# RUN: ld.lld -u foo %t1.o --start-lib %t2.o -o %t1
+# RUN: llvm-readobj --syms %t1 | FileCheck %s --check-prefix=CHECK-U
+
+# CHECK-U:      Name: foo
+# CHECK-U:      Binding:
+# CHECK-U-SAME:          Global
+# CHECK-U:      Section:
+# CHECK-U-SAME:          .text
+
 .weak foo
 call foo at PLT
 


        


More information about the llvm-commits mailing list