[PATCH] D30048: Don't print DISCARD sections as gced

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 10:21:23 PST 2017


rafael created this revision.

This is a small difference I noticed to gold and bfd. When given --print-gc-sections, we print sections a linkerscript marks DISCARD. The other linkers don't.


https://reviews.llvm.org/D30048

Files:
  ELF/LinkerScript.cpp
  ELF/OutputSections.cpp
  ELF/Writer.cpp
  ELF/Writer.h
  test/ELF/linkerscript/discard-print-gc.s


Index: test/ELF/linkerscript/discard-print-gc.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/discard-print-gc.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: echo "SECTIONS { /DISCARD/ : { *(.foo) } }" > %t.script
+# RUN: llvm-mc -triple x86_64-pc-linux %s -o %t.o -filetype=obj
+# RUN: ld.lld -o %t.so --gc-sections %t.o --print-gc-sections -shared 2>&1 | \
+# RUN:   FileCheck -check-prefix=CHECK %s
+# RUN: ld.lld -o %t.so -T %t.script %t.o --print-gc-sections -shared 2>&1 | \
+# RUN:   FileCheck -check-prefix=QUIET --allow-empty %s
+
+.section .foo,"a"
+.quad 0
+
+# CHECK: removing unused section from '.foo'
+# QUIET-NOT: removing unused section from '.foo'
Index: ELF/Writer.h
===================================================================
--- ELF/Writer.h
+++ ELF/Writer.h
@@ -52,7 +52,6 @@
 template <class ELFT>
 bool allocateHeaders(std::vector<PhdrEntry> &,
                      llvm::ArrayRef<OutputSectionBase *>, uint64_t Min);
-template <class ELFT> void reportDiscarded(InputSectionBase<ELFT> *IS);
 
 template <class ELFT> uint32_t getMipsEFlags();
 
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -119,15 +119,6 @@
   return Name;
 }
 
-template <class ELFT> void elf::reportDiscarded(InputSectionBase<ELFT> *IS) {
-  if (IS == In<ELFT>::ShStrTab)
-    error("discarding .shstrtab section is not allowed");
-  if (!Config->PrintGcSections)
-    return;
-  errs() << "removing unused section from '" << IS->Name << "' in file '"
-         << IS->getFile()->getName() << "'\n";
-}
-
 template <class ELFT> static bool needsInterpSection() {
   return !Symtab<ELFT>::X->getSharedFiles().empty() &&
          !Config->DynamicLinker.empty() &&
@@ -1855,8 +1846,3 @@
 template bool elf::isRelroSection<ELF32BE>(const OutputSectionBase *);
 template bool elf::isRelroSection<ELF64LE>(const OutputSectionBase *);
 template bool elf::isRelroSection<ELF64BE>(const OutputSectionBase *);
-
-template void elf::reportDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
-template void elf::reportDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
-template void elf::reportDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
-template void elf::reportDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -575,6 +575,13 @@
          Type == SHT_NOTE;
 }
 
+template <class ELFT> static void reportDiscarded(InputSectionBase<ELFT> *IS) {
+  if (!Config->PrintGcSections)
+    return;
+  errs() << "removing unused section from '" << IS->Name << "' in file '"
+         << IS->getFile()->getName() << "'\n";
+}
+
 template <class ELFT>
 void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase<ELFT> *IS,
                                              StringRef OutsecName) {
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -276,7 +276,8 @@
 void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
   for (InputSectionBase<ELFT> *S : V) {
     S->Live = false;
-    reportDiscarded(S);
+    if (S == In<ELFT>::ShStrTab)
+      error("discarding .shstrtab section is not allowed");
 
     InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S);
     if (!IS || IS->DependentSections.empty())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30048.88754.patch
Type: text/x-patch
Size: 3512 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170216/dbcdd4fd/attachment-0001.bin>


More information about the llvm-commits mailing list