diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1992,7 +1992,7 @@ // 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 --- a/lld/test/ELF/weak-undef-lib.s +++ b/lld/test/ELF/weak-undef-lib.s @@ -17,6 +17,15 @@ # 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=CHECK1 + +# CHECK1: Name: foo +# CHECK1: Section: +# CHECK1-SAME: .text + .weak foo call foo@PLT