Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -137,17 +137,11 @@ OS << OSec->Name << '\n'; // Dump symbols for each input section. - for (BaseCommand *Base : OSec->SectionCommands) { - auto *ISD = dyn_cast(Base); - if (!ISD) - continue; - for (InputSection *IS : ISD->Sections) { - writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), - IS->Alignment); - OS << indent(1) << toString(IS) << '\n'; - for (Symbol *Sym : SectionSyms[IS]) - OS << SymStr[Sym] << '\n'; - } + for (InputSection *IS : getInputSections(OSec)) { + writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment); + OS << indent(1) << toString(IS) << '\n'; + for (Symbol *Sym : SectionSyms[IS]) + OS << SymStr[Sym] << '\n'; } } } Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -121,6 +121,8 @@ int getPriority(StringRef S); +std::vector getInputSections(OutputSection* OS); + // All output sections that are handled by the linker specially are // globally accessible. Writer initializes them, so don't use them // until Writer is initialized. Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -231,12 +231,7 @@ } // Write leading padding. - std::vector Sections; - for (BaseCommand *Cmd : SectionCommands) - if (auto *ISD = dyn_cast(Cmd)) - for (InputSection *IS : ISD->Sections) - if (IS->Live) - Sections.push_back(IS); + std::vector Sections = getInputSections(this); uint32_t Filler = getFiller(); if (Filler) fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler); @@ -281,17 +276,13 @@ } template void OutputSection::finalize() { - InputSection *First = nullptr; - for (BaseCommand *Base : SectionCommands) { - if (auto *ISD = dyn_cast(Base)) { - if (ISD->Sections.empty()) - continue; - if (First == nullptr) - First = ISD->Sections.front(); - } - if (isa(Base) && Type == SHT_NOBITS) - Type = SHT_PROGBITS; - } + if (Type == SHT_NOBITS) + for (BaseCommand *Base : SectionCommands) + if (isa(Base)) + Type = SHT_PROGBITS; + + std::vector V = getInputSections(this); + InputSection *First = V.empty() ? nullptr : V[0]; if (Flags & SHF_LINK_ORDER) { // We must preserve the link order dependency of sections with the @@ -394,6 +385,14 @@ return V; } +std::vector elf::getInputSections(OutputSection *OS) { + std::vector Ret; + for (BaseCommand *Base : OS->SectionCommands) + if (auto *ISD = dyn_cast(Base)) + Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end()); + return Ret; +} + // Sorts input sections by section name suffixes, so that .foo.N comes // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. // We want to keep the original order if the priorities are the same Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -2579,13 +2579,9 @@ // The sentinel has to be removed if there are no other .ARM.exidx entries. bool ARMExidxSentinelSection::empty() const { - OutputSection *OS = getParent(); - for (auto *B : OS->SectionCommands) - if (auto *ISD = dyn_cast(B)) - for (auto *S : ISD->Sections) - if (!isa(S)) - return false; - return true; + return llvm::all_of(getInputSections(getParent()), [](InputSection *IS) { + return isa(IS); + }); } ThunkSection::ThunkSection(OutputSection *OS, uint64_t Off) Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1392,12 +1392,7 @@ // If there are no other alive sections or commands left in the output // section description, we remove it from the output. - bool IsEmpty = llvm::all_of(OS->SectionCommands, [](BaseCommand *B) { - if (auto *ISD = dyn_cast(B)) - return ISD->Sections.empty(); - return false; - }); - if (IsEmpty) + if (getInputSections(OS).empty()) OS->Live = false; } }