ELF: Move x86-64-only function from DefaultLayout to X86_64TargetLayout.

Also removed some over-generalization added in r232866, such as
making a function take two parameters and pass two equivalent
arguments to the function.

llvm-svn: 233882
This commit is contained in:
Rui Ueyama 2015-04-02 01:36:02 +00:00
parent f96cde9f68
commit 0052736eb1
2 changed files with 31 additions and 48 deletions

View File

@ -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();

View File

@ -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);
});
}
};