From 1880bbed392528c1a3e14e24a298b69d6936a4a8 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 26 Nov 2016 15:09:58 +0000 Subject: [PATCH] Split MergeOutputSection::finalize. llvm-svn: 287977 --- lld/ELF/OutputSections.cpp | 59 ++++++++++++++++++++++++++-------------------- lld/ELF/OutputSections.h | 3 +++ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 241f93d..e4536ef 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -489,39 +489,46 @@ template bool MergeOutputSection::shouldTailMerge() const { return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; } -template void MergeOutputSection::finalize() { +template void MergeOutputSection::finalizeTailMerge() { // Add all string pieces to the string table builder to create section - // contents. If we are not tail-optimizing, offsets of strings are fixed - // when they are added to the builder (string table builder contains a - // hash table from strings to offsets), so we record them if available. - for (MergeInputSection *Sec : Sections) { - for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) { - if (!Sec->Pieces[I].Live) - continue; - uint32_t OutputOffset = Builder.add(Sec->getData(I)); - - // Save the offset in the generated string table. - if (!shouldTailMerge()) - Sec->Pieces[I].OutputOff = OutputOffset; - } - } - - // Fix the string table content. After this, the contents - // will never change. - if (shouldTailMerge()) - Builder.finalize(); - else - Builder.finalizeInOrder(); + // contents. + for (MergeInputSection *Sec : Sections) + for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) + if (Sec->Pieces[I].Live) + Builder.add(Sec->getData(I)); + + // Fix the string table content. After this, the contents will never change. + Builder.finalize(); this->Size = Builder.getSize(); // finalize() fixed tail-optimized strings, so we can now get // offsets of strings. Get an offset for each string and save it // to a corresponding StringPiece for easy access. + for (MergeInputSection *Sec : Sections) + for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) + if (Sec->Pieces[I].Live) + Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); +} + +template void MergeOutputSection::finalizeNoTailMerge() { + // 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 + // a hash table from strings to offsets). + for (MergeInputSection *Sec : Sections) + for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) + if (Sec->Pieces[I].Live) + Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I)); + + Builder.finalizeInOrder(); + this->Size = Builder.getSize(); +} + +template void MergeOutputSection::finalize() { if (shouldTailMerge()) - for (MergeInputSection *Sec : Sections) - for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) - if (Sec->Pieces[I].Live) - Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); + finalizeTailMerge(); + else + finalizeNoTailMerge(); } template diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index edb028f..2dd213d 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -140,6 +140,9 @@ public: } private: + void finalizeTailMerge(); + void finalizeNoTailMerge(); + llvm::StringTableBuilder Builder; std::vector *> Sections; }; -- 2.7.4