Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -677,6 +677,8 @@ // Try and find an appropriate memory region to assign offsets in. for (BaseCommand *Base : Opt.Commands) { if (auto *Sec = dyn_cast(Base)) { + if (!Sec->Live) + continue; Sec->MemRegion = findMemoryRegion(Sec); // Handle align (e.g. ".foo : ALIGN(16) { ... }"). if (Sec->AlignExpr) @@ -713,8 +715,6 @@ DefPhdrs = Sec->Phdrs; } } - - removeEmptyCommands(); } static OutputSection *findFirstSection(PhdrEntry *Load) { Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -1054,8 +1054,7 @@ } template void Writer::sortSections() { - if (Script->Opt.HasSections) - Script->adjustSectionsBeforeSorting(); + Script->adjustSectionsBeforeSorting(); // Don't sort if using -r. It is not necessary and we want to preserve the // relative order for SHF_LINK_ORDER sections. @@ -1197,8 +1196,7 @@ // If there are no other sections in the output section, remove it from the // output. if (OS->Commands.empty()) - llvm::erase_if(Script->Opt.Commands, - [&](BaseCommand *Cmd) { return Cmd == OS; }); + OS->Live = false; } } @@ -1304,6 +1302,7 @@ removeUnusedSyntheticSections(); sortSections(); + Script->removeEmptyCommands(); // Now that we have the final list, create a list of all the // OutputSections for convenience.