Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -246,7 +246,6 @@ void fabricateDefaultCommands(); void addOrphanSections(OutputSectionFactory &Factory); void removeEmptyCommands(); - void adjustSectionsBeforeSorting(); void adjustSectionsAfterSorting(); std::vector createPhdrs(); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -367,6 +367,7 @@ CurAddressState->OutSec = Aether; Dot = 0; + uint64_t PrevFlags = SHF_ALLOC; for (size_t I = 0; I < Opt.Commands.size(); ++I) { // Handle symbol assignments outside of any output section. if (auto *Cmd = dyn_cast(Opt.Commands[I])) { @@ -421,6 +422,20 @@ Sec->SectionIndex = I; if (Sec->Noload) Sec->Type = SHT_NOBITS; + + if (Sec->Live) + PrevFlags = Sec->Flags; + if (Sec->Live || llvm::all_of(Sec->Commands, [](BaseCommand *Cmd) { + return isa(Cmd); + })) + continue; + // If the output section contains any other commands except input section + // descriptions, we create a corresponding output section. The bfd linker + // seems to only create them if '.' is assigned to. But creating these + // section should not have any bad consequeces and gives us a section to + // put in the content of commands like BYTE() or attach symbols assigned. + Sec->Live = true; + Sec->Flags = PrevFlags; } } CurAddressState = nullptr; @@ -642,37 +657,6 @@ }); } -static bool isAllSectionDescription(const OutputSection &Cmd) { - for (BaseCommand *Base : Cmd.Commands) - if (!isa(*Base)) - return false; - return true; -} - -void LinkerScript::adjustSectionsBeforeSorting() { - // If the output section contains only symbol assignments, create a - // corresponding output section. The bfd linker seems to only create them if - // '.' is assigned to, but creating these section should not have any bad - // consequeces and gives us a section to put the symbol in. - uint64_t Flags = SHF_ALLOC; - - for (BaseCommand * Cmd : Opt.Commands) { - auto *Sec = dyn_cast(Cmd); - if (!Sec) - continue; - if (Sec->Live) { - Flags = Sec->Flags; - continue; - } - - if (isAllSectionDescription(*Sec)) - continue; - - Sec->Live = true; - Sec->Flags = Flags; - } -} - void LinkerScript::adjustSectionsAfterSorting() { // Try and find an appropriate memory region to assign offsets in. for (BaseCommand *Base : Opt.Commands) { Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1048,8 +1048,6 @@ } template void Writer::sortSections() { - 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. if (Config->Relocatable)