diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2016-09-20 13:12:07 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2016-09-20 13:12:07 +0000 |
commit | 6d38e4dbe1603c916343d25c47b4c9ced283455c (patch) | |
tree | c92e5e5cb10bf903246407067a941f3bd537d4ec | |
parent | 2e721aa8c8f14ad00fb8ee3e6c8bff4e30d497a3 (diff) | |
download | llvm-6d38e4dbe1603c916343d25c47b4c9ced283455c.zip llvm-6d38e4dbe1603c916343d25c47b4c9ced283455c.tar.gz llvm-6d38e4dbe1603c916343d25c47b4c9ced283455c.tar.bz2 |
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.
llvm-svn: 281968
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 23 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/align-empty.s | 14 |
2 files changed, 37 insertions, 0 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index e4f8378..ea2c14b 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -446,6 +446,29 @@ void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) { } 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. diff --git a/lld/test/ELF/linkerscript/align-empty.s b/lld/test/ELF/linkerscript/align-empty.s new file mode 100644 index 0000000..4af943f --- /dev/null +++ b/lld/test/ELF/linkerscript/align-empty.s @@ -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 |