Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -217,6 +217,8 @@ uint64_t advance(uint64_t Size, unsigned Align); void output(InputSection *Sec); + void removeEmptyCommands(); + std::unique_ptr Ctx; OutputSection *Aether; @@ -234,7 +236,6 @@ void fabricateDefaultCommands(); void addOrphanSections(OutputSectionFactory &Factory); - void removeEmptyCommands(); void adjustSectionsBeforeSorting(); void adjustSectionsAfterSorting(); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -696,19 +696,6 @@ } void LinkerScript::adjustSectionsAfterSorting() { - // Try and find an appropriate memory region to assign offsets in. - for (BaseCommand *Base : SectionCommands) { - if (auto *Sec = dyn_cast(Base)) { - if (!Sec->Live) - continue; - Sec->MemRegion = findMemoryRegion(Sec); - // Handle align (e.g. ".foo : ALIGN(16) { ... }"). - if (Sec->AlignExpr) - Sec->Alignment = - std::max(Sec->Alignment, Sec->AlignExpr().getValue()); - } - } - // If output section command doesn't specify any segments, // and we haven't previously assigned any section to segment, // then we simply assign section to the very first load segment. @@ -738,6 +725,19 @@ DefPhdrs = Sec->Phdrs; } } + + removeEmptyCommands(); + + // Try and find an appropriate memory region to assign offsets in. + for (BaseCommand *Base : SectionCommands) { + if (auto *Sec = dyn_cast(Base)) { + Sec->MemRegion = findMemoryRegion(Sec); + // Handle align (e.g. ".foo : ALIGN(16) { ... }"). + if (Sec->AlignExpr) + Sec->Alignment = + std::max(Sec->Alignment, Sec->AlignExpr().getValue()); + } + } } static OutputSection *findFirstSection(PhdrEntry *Load) { Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1294,7 +1294,6 @@ removeUnusedSyntheticSections(); sortSections(); - Script->removeEmptyCommands(); // Now that we have the final list, create a list of all the // OutputSections for convenience.