diff options
| -rw-r--r-- | lld/lib/ReaderWriter/ELF/DefaultLayout.h | 46 | ||||
| -rw-r--r-- | lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h | 33 |
2 files changed, 31 insertions, 48 deletions
diff --git a/lld/lib/ReaderWriter/ELF/DefaultLayout.h b/lld/lib/ReaderWriter/ELF/DefaultLayout.h index 24c625298e9..63f537b807a 100644 --- a/lld/lib/ReaderWriter/ELF/DefaultLayout.h +++ b/lld/lib/ReaderWriter/ELF/DefaultLayout.h @@ -313,10 +313,6 @@ public: return _copiedDynSymNames.count(sla->name()); } - /// \brief Handle SORT_BY_PRIORITY. - void sortOutputSectionByPriority(StringRef outputSectionName, - StringRef prefix); - protected: /// \brief TargetLayouts may use these functions to reorder the input sections /// in a order defined by their ABI. @@ -335,10 +331,6 @@ protected: new (_allocator) RelocationTable<ELFT>(_ctx, name, order)); } -private: - /// Helper function that returns the priority value from an input section. - uint32_t getPriorityFromSectionName(StringRef sectionName) const; - protected: llvm::BumpPtrAllocator _allocator; SectionMapT _sectionMap; @@ -671,44 +663,6 @@ template <class ELFT> void DefaultLayout<ELFT>::createOutputSections() { } } -template <class ELFT> -uint32_t -DefaultLayout<ELFT>::getPriorityFromSectionName(StringRef sectionName) const { - StringRef priority = sectionName.drop_front().rsplit('.').second; - uint32_t prio; - if (priority.getAsInteger(10, prio)) - return std::numeric_limits<uint32_t>::max(); - return prio; -} - -template <class ELFT> -void DefaultLayout<ELFT>::sortOutputSectionByPriority( - StringRef outputSectionName, StringRef prefix) { - OutputSection<ELFT> *outputSection = findOutputSection(outputSectionName); - if (!outputSection) - return; - - auto sections = outputSection->sections(); - - std::sort(sections.begin(), sections.end(), - [&](Chunk<ELFT> *lhs, Chunk<ELFT> *rhs) { - Section<ELFT> *lhsSection = dyn_cast<Section<ELFT>>(lhs); - Section<ELFT> *rhsSection = dyn_cast<Section<ELFT>>(rhs); - if (!lhsSection || !rhsSection) - return false; - StringRef lhsSectionName = lhsSection->inputSectionName(); - StringRef rhsSectionName = rhsSection->inputSectionName(); - - if (!prefix.empty()) { - if (!lhsSectionName.startswith(prefix) || - !rhsSectionName.startswith(prefix)) - return false; - } - return getPriorityFromSectionName(lhsSectionName) < - getPriorityFromSectionName(rhsSectionName); - }); -} - template <class ELFT> void DefaultLayout<ELFT>::assignSectionsToSegments() { ScopedTask task(getDefaultDomain(), "assignSectionsToSegments"); ELFLinkingContext::OutputMagic outputMagic = _ctx.getOutputMagic(); diff --git a/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h b/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h index e1e3064fe57..45c4c250bac 100644 --- a/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h @@ -19,13 +19,42 @@ namespace lld { namespace elf { + class X86_64TargetLayout : public TargetLayout<X86_64ELFType> { public: X86_64TargetLayout(X86_64LinkingContext &ctx) : TargetLayout(ctx) {} void finalizeOutputSectionLayout() override { - sortOutputSectionByPriority(".init_array", ".init_array"); - sortOutputSectionByPriority(".fini_array", ".fini_array"); + sortOutputSectionByPriority<X86_64ELFType>(".init_array"); + sortOutputSectionByPriority<X86_64ELFType>(".fini_array"); + } + +private: + uint32_t getPriority(StringRef sectionName) const { + StringRef priority = sectionName.drop_front().rsplit('.').second; + uint32_t prio; + if (priority.getAsInteger(10, prio)) + return std::numeric_limits<uint32_t>::max(); + return prio; + } + + template <typename T> void sortOutputSectionByPriority(StringRef prefix) { + OutputSection<T> *section = findOutputSection(prefix); + if (!section) + return; + auto sections = section->sections(); + std::sort(sections.begin(), sections.end(), + [&](Chunk<T> *lhs, Chunk<T> *rhs) { + Section<T> *lhsSection = dyn_cast<Section<T>>(lhs); + Section<T> *rhsSection = dyn_cast<Section<T>>(rhs); + if (!lhsSection || !rhsSection) + return false; + StringRef lhsName = lhsSection->inputSectionName(); + StringRef rhsName = rhsSection->inputSectionName(); + if (!lhsName.startswith(prefix) || !rhsName.startswith(prefix)) + return false; + return getPriority(lhsName) < getPriority(rhsName); + }); } }; |

