Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -161,9 +161,8 @@ OutputSectionFactory(); ~OutputSectionFactory(); - void addInputSec(InputSectionBase *IS, StringRef OutsecName); void addInputSec(InputSectionBase *IS, StringRef OutsecName, - OutputSection *&Sec); + OutputSection *Sec = nullptr); private: llvm::SmallDenseMap Map; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -206,45 +206,38 @@ } void OutputSectionFactory::addInputSec(InputSectionBase *IS, - StringRef OutsecName) { - // Sections with the SHT_GROUP attribute reach here only when the - r option - // is given. Such sections define "section groups", and InputFiles.cpp has - // dedup'ed section groups by their signatures. For the -r, we want to pass - // through all SHT_GROUP sections without merging them because merging them - // creates broken section contents. - if (IS->Type == SHT_GROUP) { - OutputSection *Out = nullptr; - addInputSec(IS, OutsecName, Out); - return; - } - - // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have - // relocation sections .rela.foo and .rela.bar for example. Most tools do - // not allow multiple REL[A] sections for output section. Hence we - // should combine these relocation sections into single output. - // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any - // other REL[A] sections created by linker itself. - if (!isa(IS) && - (IS->Type == SHT_REL || IS->Type == SHT_RELA)) { - auto *Sec = cast(IS); - OutputSection *Out = Sec->getRelocatedSection()->getOutputSection(); - addInputSec(IS, OutsecName, Out->RelocationSection); - return; - } - - SectionKey Key = createKey(IS, OutsecName); - OutputSection *&Sec = Map[Key]; - addInputSec(IS, OutsecName, Sec); -} - -void OutputSectionFactory::addInputSec(InputSectionBase *IS, StringRef OutsecName, - OutputSection *&Sec) { + OutputSection *Sec) { if (!IS->Live) { reportDiscarded(IS); return; } + OutputSection **OS = &Sec; + if (!Sec) { + // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have + // relocation sections .rela.foo and .rela.bar for example. Most tools do + // not allow multiple REL[A] sections for output section. Hence we + // should combine these relocation sections into single output. + // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any + // other REL[A] sections created by linker itself. + if (!isa(IS) && + (IS->Type == SHT_REL || IS->Type == SHT_RELA)) { + OutputSection *Out = + cast(IS)->getRelocatedSection()->getOutputSection(); + OS = &Out->RelocationSection; + } else if (IS->Type != SHT_GROUP) { + // Sections with the SHT_GROUP attribute reach here only when the - r + // option is given. Such sections define "section groups", and + // InputFiles.cpp has dedup'ed section groups by their signatures. For the + // -r, we want to pass through all SHT_GROUP sections without merging them + // because merging them creates broken section contents. + SectionKey Key = createKey(IS, OutsecName); + OS = &Map[Key]; + } + Sec = *OS; + } + if (Sec && Sec->Live) { if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) error("incompatible section flags for " + Sec->Name + "\n>>> " + @@ -264,7 +257,8 @@ Sec->Flags |= IS->Flags; } else { if (!Sec) { - Sec = Script->createOutputSection(OutsecName, ""); + *OS = Script->createOutputSection(OutsecName, ""); + Sec = *OS; Script->Opt.Commands.push_back(Sec); } Sec->Type = IS->Type;