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])) { @@ -399,12 +400,6 @@ continue; } - // A directive may contain symbol definitions like this: - // ".foo : { ...; bar = .; }". Handle them. - for (BaseCommand *Base : Sec->Commands) - if (auto *OutCmd = dyn_cast(Base)) - addSymbol(OutCmd); - // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign // is given, input sections are aligned to that value, whether the // given value is larger or smaller than the original section alignment. @@ -421,6 +416,28 @@ Sec->SectionIndex = I; if (Sec->Noload) Sec->Type = SHT_NOBITS; + + if (Sec->Live) + PrevFlags = Sec->Flags; + + for (BaseCommand *Base : Sec->Commands) { + // A directive may contain symbol definitions like this: + // ".foo : { ...; bar = .; }". Handle them. + if (auto *OutCmd = dyn_cast(Base)) + addSymbol(OutCmd); + + if (Sec->Live || isa(Base)) + 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 +659,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)