[lld] r281968 - Remove empty section commands.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 06:12:08 PDT 2016


Author: rafael
Date: Tue Sep 20 08:12:07 2016
New Revision: 281968

URL: http://llvm.org/viewvc/llvm-project?rev=281968&view=rev
Log:
Remove empty section commands.

We were already not creating them, and with this other parts of the
code don't have to worry about them.

Added:
    lld/trunk/test/ELF/linkerscript/align-empty.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281968&r1=281967&r2=281968&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Sep 20 08:12:07 2016
@@ -446,6 +446,29 @@ void LinkerScript<ELFT>::assignOffsets(O
 }
 
 template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
+  // It is common practice to use very generic linker scripts. So for any
+  // given run some of the output sections in the script will be empty.
+  // We could create corresponding empty output sections, but that would
+  // clutter the output.
+  // We instead remove trivially empty sections. The bfd linker seems even
+  // more aggressive at removing them.
+  auto Pos = std::remove_if(
+      Opt.Commands.begin(), Opt.Commands.end(),
+      [&](const std::unique_ptr<BaseCommand> &Base) {
+        auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
+        if (!Cmd)
+          return false;
+        std::vector<OutputSectionBase<ELFT> *> Secs =
+            findSections(*Cmd, *OutputSections);
+        if (!Secs.empty())
+          return false;
+        for (const std::unique_ptr<BaseCommand> &I : Cmd->Commands)
+          if (!isa<InputSectionDescription>(I.get()))
+            return false;
+        return true;
+      });
+  Opt.Commands.erase(Pos, Opt.Commands.end());
+
   // Orphan sections are sections present in the input files which
   // are not explicitly placed into the output file by the linker script.
   // We place orphan sections at end of file.

Added: lld/trunk/test/ELF/linkerscript/align-empty.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/align-empty.s?rev=281968&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/align-empty.s (added)
+++ lld/trunk/test/ELF/linkerscript/align-empty.s Tue Sep 20 08:12:07 2016
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { \
+# RUN:  abc : { } \
+# RUN:  . = ALIGN(0x1000); \
+# RUN:  .text : { *(.text) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t -shared
+# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
+# CHECK:      Sections:
+# CHECK-NEXT: Idx Name          Size      Address
+# CHECK-NEXT:   0               00000000 0000000000000000
+# CHECK-NEXT:   1 .text         00000000 0000000000001000




More information about the llvm-commits mailing list