Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -1419,6 +1419,17 @@ if (files.empty() && errorCount() == 0) error("no input files"); + + // A sequence started with -whole-archive flag might not be closed with + // -no-whole-archive at the end of the command line because in normal usage + // that is not required and makes no difference. + // Nonetheless, if there are dependent libraries, they should always be + // included in the normal way so that unnecessary members are not pulled. + // Moreover, it is very common for a dependent library to be mentioned several + // times in input files, and, as the flag forces the archive members to be + // processed immediately, that results in duplicating symbol definitions. + // Thus, force the flag off before processing dependent libraries. + inWholeArchive = false; } // If -m was not given, infer it from object files. Index: lld/test/ELF/deplibs-ignore-whole-archive.s =================================================================== --- /dev/null +++ lld/test/ELF/deplibs-ignore-whole-archive.s @@ -0,0 +1,28 @@ +# REQUIRES: x86 + +## This checks that if -whole-archive is left unterminated in the command line, +## that does not affect how dependent libraries are included in the link. +## They still should not pull unused members and the link should not terminate +## with the "duplicate symbol" error even if a dependent library is referenced +## more than once, which is very common with them. + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: echo ".global foo; foo:" | \ +# RUN: llvm-mc -filetype=obj -triple=x86_64 - -o %tfoo.o +# RUN: echo ".global bar; bar:" | \ +# RUN: llvm-mc -filetype=obj -triple=x86_64 - -o %tbar.o +# RUN: rm -rf %t.dir +# RUN: mkdir -p %t.dir +# RUN: llvm-ar rc %t.dir/lib.a %tfoo.o %tbar.o +# RUN: ld.lld %t.o -o %t -L %t.dir -whole-archive +# RUN: llvm-nm %t | FileCheck %s + +# CHECK-NOT: bar + + .global _start +_start: + call foo + + .section ".deplibs","MS",@llvm_dependent_libraries,1 + .asciz "lib.a" + .asciz "lib.a"