Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -258,6 +258,9 @@ void addSymbol(SymbolAssignment *Cmd); void processCommands(OutputSectionFactory &Factory); + void addInputSection(OutputSectionFactory &Factory, InputSectionBase *IS, + StringRef OutsecName, OutputSection *Sec); + // Parsed linker script configurations are set to this struct. ScriptConfiguration Opt; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -351,6 +351,19 @@ return Ret; } +void LinkerScript::addInputSection(OutputSectionFactory &F, + InputSectionBase *IS, StringRef OutsecName, + OutputSection *Sec) { + if (!IS->Live) { + reportDiscarded(IS); + return; + } + if (!Sec) + F.addInputSec(IS, OutsecName); + else + F.addInputSecToOutput(IS, OutsecName, Sec); +} + void LinkerScript::processCommands(OutputSectionFactory &Factory) { // A symbol can be assigned before any section is mentioned in the linker // script. In an DSO, the symbol values are addresses, so the only important @@ -421,7 +434,7 @@ // Add input sections to an output section. for (InputSectionBase *S : V) - Factory.addInputSec(S, Sec->Name, Sec); + addInputSection(Factory, S, Sec->Name, Sec); assert(Sec->SectionIndex == INT_MAX); Sec->SectionIndex = I; if (Sec->Noload) @@ -466,11 +479,11 @@ return false; }); if (I == End) { - Factory.addInputSec(S, Name); + addInputSection(Factory, S, Name, nullptr); assert(S->getOutputSection()->SectionIndex == INT_MAX); } else { OutputSection *Sec = cast(*I); - Factory.addInputSec(S, Name, Sec); + addInputSection(Factory, S, Name, Sec); unsigned Index = std::distance(Opt.Commands.begin(), I); assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index); Sec->SectionIndex = Index; Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -162,8 +162,8 @@ ~OutputSectionFactory(); void addInputSec(InputSectionBase *IS, StringRef OutsecName); - void addInputSec(InputSectionBase *IS, StringRef OutsecName, - OutputSection *&Sec); + void addInputSecToOutput(InputSectionBase *IS, StringRef OutsecName, + OutputSection *&Sec); private: llvm::SmallDenseMap Map; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -214,7 +214,7 @@ // creates broken section contents. if (IS->Type == SHT_GROUP) { OutputSection *Out = nullptr; - addInputSec(IS, OutsecName, Out); + addInputSecToOutput(IS, OutsecName, Out); return; } @@ -228,23 +228,18 @@ (IS->Type == SHT_REL || IS->Type == SHT_RELA)) { auto *Sec = cast(IS); OutputSection *Out = Sec->getRelocatedSection()->getOutputSection(); - addInputSec(IS, OutsecName, Out->RelocationSection); + addInputSecToOutput(IS, OutsecName, Out->RelocationSection); return; } SectionKey Key = createKey(IS, OutsecName); OutputSection *&Sec = Map[Key]; - addInputSec(IS, OutsecName, Sec); + addInputSecToOutput(IS, OutsecName, Sec); } -void OutputSectionFactory::addInputSec(InputSectionBase *IS, - StringRef OutsecName, - OutputSection *&Sec) { - if (!IS->Live) { - reportDiscarded(IS); - return; - } - +void OutputSectionFactory::addInputSecToOutput(InputSectionBase *IS, + StringRef OutsecName, + OutputSection *&Sec) { if (Sec && Sec->Live) { if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) error("incompatible section flags for " + Sec->Name + "\n>>> " + Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -910,8 +910,8 @@ std::vector Old = Script->Opt.Commands; Script->Opt.Commands.clear(); for (InputSectionBase *IS : InputSections) - if (IS) - Factory.addInputSec(IS, getOutputSectionName(IS->Name)); + Script->addInputSection(Factory, IS, getOutputSectionName(IS->Name), + nullptr); Script->Opt.Commands.insert(Script->Opt.Commands.end(), Old.begin(), Old.end());