[PATCH] D88877: [ELF] Drop -whole-archive before processing dependent libraries.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 02:12:34 PDT 2020


ikudrin created this revision.
ikudrin added reviewers: bd1976llvm, MaskRay, ruiu.
ikudrin added projects: lld, LLVM.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.
ikudrin requested review of this revision.

Dependent libraries should not be processed in the "whole archive" way because that leads to pulling unused archive members and may result in triggering "duplicate symbol" errors.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88877

Files:
  lld/ELF/Driver.cpp
  lld/test/ELF/deplibs-ignore-whole-archive.s


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", at llvm_dependent_libraries,1
+  .asciz  "lib.a"
+  .asciz  "lib.a"
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 <machine_type> was not given, infer it from object files.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88877.296384.patch
Type: text/x-patch
Size: 2179 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201006/6dcb05dd/attachment.bin>


More information about the llvm-commits mailing list