[PATCH] D83549: [ELF] Do not force bringing out symbols passed by -init and -fini.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 10 05:54:07 PDT 2020


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

After D69985 <https://reviews.llvm.org/D69985>, symbols for `-init` and `-fini` were unconditionally marked as used even if they were just lazy symbols seen when scanning archives. That resulted in exposing them in the symbol table of an output file, as Undefined, which added unwanted dependencies. The patch fixes the issue by checking the kind of the symbols before marking.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83549

Files:
  lld/ELF/Driver.cpp
  lld/test/ELF/archive-init-fini.s


Index: lld/test/ELF/archive-init-fini.s
===================================================================
--- /dev/null
+++ lld/test/ELF/archive-init-fini.s
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+
+## This checks that LLD does not add "_init" and "_fini" symbols into
+## the symbol table of the output binary if the symbols are encountered in
+## an archive but not in fact used in input files.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
+# RUN: rm -f %t.a
+# RUN: llvm-ar rcs %t.a %t
+# RUN: ld.lld -shared -m elf_x86_64 %t.a -o %t.out
+# RUN: llvm-nm %t.out | \
+# RUN:   FileCheck %s --implicit-check-not=_init --implicit-check-not=_fini
+
+.global _init, _fini
+_init:
+_fini:
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -1944,9 +1944,9 @@
     handleUndefinedGlob(pat);
 
   // Mark -init and -fini symbols so that the LTO doesn't eliminate them.
-  if (Symbol *sym = symtab->find(config->init))
+  if (Symbol *sym = dyn_cast_or_null<Defined>(symtab->find(config->init)))
     sym->isUsedInRegularObj = true;
-  if (Symbol *sym = symtab->find(config->fini))
+  if (Symbol *sym = dyn_cast_or_null<Defined>(symtab->find(config->fini)))
     sym->isUsedInRegularObj = true;
 
   // If any of our inputs are bitcode files, the LTO code generator may create


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83549.277006.patch
Type: text/x-patch
Size: 1369 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200710/451fe903/attachment.bin>


More information about the llvm-commits mailing list