From e26b7aafe0e28ca2b07020f0bdd24f68230c5053 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 26 Sep 2017 00:54:24 +0000 Subject: [PATCH] Split MergeSyntheticSection into Merge{Tail,NoTail}Section. This patch alone is neutral in terms of code readability, but this change makes a following patch easier to read. llvm-svn: 314181 --- lld/ELF/SyntheticSections.cpp | 29 ++++++++++++++-------------- lld/ELF/SyntheticSections.h | 36 ++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 66b5351de4a7..57d03ffdf160 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2185,13 +2185,11 @@ void MergeSyntheticSection::addSection(MergeInputSection *MS) { Sections.push_back(MS); } +size_t MergeSyntheticSection::getSize() const { return Builder.getSize(); } + void MergeSyntheticSection::writeTo(uint8_t *Buf) { Builder.write(Buf); } -bool MergeSyntheticSection::shouldTailMerge() const { - return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; -} - -void MergeSyntheticSection::finalizeTailMerge() { +void MergeTailSection::finalizeContents() { // Add all string pieces to the string table builder to create section // contents. for (MergeInputSection *Sec : Sections) @@ -2211,7 +2209,7 @@ void MergeSyntheticSection::finalizeTailMerge() { Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); } -void MergeSyntheticSection::finalizeNoTailMerge() { +void MergeNoTailSection::finalizeContents() { // Add all string pieces to the string table builder to create section // contents. Because we are not tail-optimizing, offsets of strings are // fixed when they are added to the builder (string table builder contains @@ -2224,15 +2222,16 @@ void MergeSyntheticSection::finalizeNoTailMerge() { Builder.finalizeInOrder(); } -void MergeSyntheticSection::finalizeContents() { - if (shouldTailMerge()) - finalizeTailMerge(); - else - finalizeNoTailMerge(); +static MergeSyntheticSection *createMergeSynthetic(StringRef Name, + uint32_t Type, + uint64_t Flags, + uint32_t Alignment) { + bool ShouldTailMerge = (Flags & SHF_STRINGS) && Config->Optimize >= 2; + if (ShouldTailMerge) + return make(Name, Type, Flags, Alignment); + return make(Name, Type, Flags, Alignment); } -size_t MergeSyntheticSection::getSize() const { return Builder.getSize(); } - // This function decompresses compressed sections and scans over the input // sections to create mergeable synthetic sections. It removes // MergeInputSections from the input section array and adds new synthetic @@ -2270,8 +2269,8 @@ void elf::decompressAndMergeSections() { Sec->Alignment == Alignment; }); if (I == MergeSections.end()) { - MergeSyntheticSection *Syn = make( - OutsecName, MS->Type, MS->Flags, Alignment); + MergeSyntheticSection *Syn = + createMergeSynthetic(OutsecName, MS->Type, MS->Flags, Alignment); MergeSections.push_back(Syn); I = std::prev(MergeSections.end()); S = Syn; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 081d086d669b..dbd0724eaea1 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -661,22 +661,36 @@ public: // with different attributes in a single output sections. To do that // we put them into MergeSyntheticSection synthetic input sections which are // attached to regular output sections. -class MergeSyntheticSection final : public SyntheticSection { +class MergeSyntheticSection : public SyntheticSection { public: + void addSection(MergeInputSection *MS); + size_t getSize() const override; + void writeTo(uint8_t *Buf) override; + +protected: MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags, uint32_t Alignment); - void addSection(MergeInputSection *MS); - void writeTo(uint8_t *Buf) override; - void finalizeContents() override; - bool shouldTailMerge() const; - size_t getSize() const override; -private: - void finalizeTailMerge(); - void finalizeNoTailMerge(); - - llvm::StringTableBuilder Builder; std::vector Sections; + llvm::StringTableBuilder Builder; +}; + +class MergeTailSection final : public MergeSyntheticSection { +public: + MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags, + uint32_t Alignment) + : MergeSyntheticSection(Name, Type, Flags, Alignment) {} + + void finalizeContents() override; +}; + +class MergeNoTailSection final : public MergeSyntheticSection { +public: + MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags, + uint32_t Alignment) + : MergeSyntheticSection(Name, Type, Flags, Alignment) {} + + void finalizeContents() override; }; // .MIPS.abiflags section.