diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -953,7 +953,7 @@ // We do not want to keep any special flags for output section // in case it is empty. - bool isEmpty = getInputSections(sec).empty(); + bool isEmpty = (getFirstInputSection(sec) == nullptr); if (isEmpty) sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) | SHF_WRITE | SHF_EXECINSTR); diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -118,7 +118,8 @@ int getPriority(StringRef s); -std::vector getInputSections(OutputSection* os); +InputSection *getFirstInputSection(const OutputSection *os); +std::vector getInputSections(const OutputSection *os); // All output sections that are handled by the linker specially are // globally accessible. Writer initializes them, so don't use them diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -357,8 +357,7 @@ } void OutputSection::finalize() { - std::vector v = getInputSections(this); - InputSection *first = v.empty() ? nullptr : v[0]; + InputSection *first = getFirstInputSection(this); if (flags & SHF_LINK_ORDER) { // We must preserve the link order dependency of sections with the @@ -466,7 +465,15 @@ return v; } -std::vector getInputSections(OutputSection *os) { +InputSection *getFirstInputSection(const OutputSection *os) { + for (BaseCommand *base : os->sectionCommands) + if (auto *isd = dyn_cast(base)) + if (!isd->sections.empty()) + return isd->sections[0]; + return nullptr; +} + +std::vector getInputSections(const OutputSection *os) { std::vector ret; for (BaseCommand *base : os->sectionCommands) if (auto *isd = dyn_cast(base))