Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1201,19 +1201,19 @@ if ((SS == InX::Got || SS == InX::MipsGot) && ElfSym::GlobalOffsetTable) continue; - std::vector::iterator Empty = OS->Commands.end(); - for (auto I = OS->Commands.begin(), E = OS->Commands.end(); I != E; ++I) { + for (auto I = OS->Commands.begin(); I != OS->Commands.end();) { BaseCommand *B = *I; if (auto *ISD = dyn_cast(B)) { auto P = std::find(ISD->Sections.begin(), ISD->Sections.end(), SS); if (P != ISD->Sections.end()) ISD->Sections.erase(P); - if (ISD->Sections.empty()) - Empty = I; + if (ISD->Sections.empty()) { + I = OS->Commands.erase(I); + continue; + } } + ++I; } - if (Empty != OS->Commands.end()) - OS->Commands.erase(Empty); // If there are no other sections in the output section, remove it from the // output. Index: test/ELF/linkerscript/unused-synthetic.s =================================================================== --- test/ELF/linkerscript/unused-synthetic.s +++ test/ELF/linkerscript/unused-synthetic.s @@ -1,9 +1,9 @@ # 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: echo "SECTIONS { \ +# RUN: .got : { *(.got) *(.got) } \ +# RUN: .plt : { *(.plt) } \ +# RUN: .text : { *(.text) } \ # RUN: }" > %t.script # RUN: ld.lld -shared -o %t.so --script %t.script %t.o