[PATCH] D34800: [ELF] Remove unused synthetic sections from script commands

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 16:42:08 PDT 2017


LGTM, thanks!

Cheers,
Rafael

Petr Hosek via Phabricator via llvm-commits
<llvm-commits at lists.llvm.org> writes:

> phosek created this revision.
> phosek added a project: lld.
> Herald added a subscriber: emaste.
>
> Script commands are processed before unused synthetic sections are
> removed. Therefore, if a linker script matches one of these sections
> it'll get emitted as an empty output section because the logic for
> removing unused synthetic sections ignores script commands which
> could have already matched and captured one of these sections. This
> patch fixes that by also removing the unused synthetic sections from
> the script commands.
>
>
> Repository:
>   rL LLVM
>
> https://reviews.llvm.org/D34800
>
> Files:
>   ELF/Writer.cpp
>   test/ELF/linkerscript/unused-synthetic.s
>
>
> Index: test/ELF/linkerscript/unused-synthetic.s
> ===================================================================
> --- /dev/null
> +++ test/ELF/linkerscript/unused-synthetic.s
> @@ -0,0 +1,18 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> +# RUN: echo "SECTIONS { \
> +# RUN:    .got  : { *(.got) } \
> +# RUN:    .plt  : { *(.plt) } \
> +# RUN:    .text : { *(.text) } \
> +# RUN:  }" > %t.script
> +# RUN: ld.lld -shared -o %t.so --script %t.script %t.o
> +
> +# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
> +# CHECK-NOT:  .got
> +# CHECK-NOT:  .plt
> +# CHECK:      .text
> +# CHECK-NEXT: .dynsym
> +
> +.global _start
> +_start:
> +  nop
> Index: ELF/Writer.cpp
> ===================================================================
> --- ELF/Writer.cpp
> +++ ELF/Writer.cpp
> @@ -1149,8 +1149,17 @@
>      SS->Live = false;
>      // If there are no other sections in the output section, remove it from the
>      // output.
> -    if (OS->Sections.empty())
> +    if (OS->Sections.empty()) {
>        V.erase(std::find(V.begin(), V.end(), OS));
> +      // Also remove script commands matching the output section.
> +      auto &Cmds = Script->Opt.Commands;
> +      auto I = std::remove_if(Cmds.begin(), Cmds.end(), [&](BaseCommand *Cmd) {
> +        if (auto *OSCmd = dyn_cast<OutputSectionCommand>(Cmd))
> +          return OSCmd->Sec == OS;
> +        return false;
> +      });
> +      Cmds.erase(I, Cmds.end());
> +    }
>    }
>  }
>  
>
>
> Index: test/ELF/linkerscript/unused-synthetic.s
> ===================================================================
> --- /dev/null
> +++ test/ELF/linkerscript/unused-synthetic.s
> @@ -0,0 +1,18 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> +# RUN: echo "SECTIONS { \
> +# RUN:    .got  : { *(.got) } \
> +# RUN:    .plt  : { *(.plt) } \
> +# RUN:    .text : { *(.text) } \
> +# RUN:  }" > %t.script
> +# RUN: ld.lld -shared -o %t.so --script %t.script %t.o
> +
> +# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
> +# CHECK-NOT:  .got
> +# CHECK-NOT:  .plt
> +# CHECK:      .text
> +# CHECK-NEXT: .dynsym
> +
> +.global _start
> +_start:
> +  nop
> Index: ELF/Writer.cpp
> ===================================================================
> --- ELF/Writer.cpp
> +++ ELF/Writer.cpp
> @@ -1149,8 +1149,17 @@
>      SS->Live = false;
>      // If there are no other sections in the output section, remove it from the
>      // output.
> -    if (OS->Sections.empty())
> +    if (OS->Sections.empty()) {
>        V.erase(std::find(V.begin(), V.end(), OS));
> +      // Also remove script commands matching the output section.
> +      auto &Cmds = Script->Opt.Commands;
> +      auto I = std::remove_if(Cmds.begin(), Cmds.end(), [&](BaseCommand *Cmd) {
> +        if (auto *OSCmd = dyn_cast<OutputSectionCommand>(Cmd))
> +          return OSCmd->Sec == OS;
> +        return false;
> +      });
> +      Cmds.erase(I, Cmds.end());
> +    }
>    }
>  }
>  
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list