From 35154193d9fadf32e45e1b49319c64ac2678c5e0 Mon Sep 17 00:00:00 2001 From: April Date: Tue, 28 Jul 2026 18:24:55 +0300 Subject: [PATCH] Reuse section layout state when saving Reuse the section-generation bitmap produced during segment layout to identify sections that do not belong to any segment. This avoids repeatedly scanning every segment and its section list for each section during serialization. --- elfio/elfio.hpp | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 61cb48529..5147f9d6a 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -268,8 +268,10 @@ class elfio calc_segment_alignment(); - bool is_still_good = layout_segments_and_their_sections(); - is_still_good = is_still_good && layout_sections_without_segments(); + std::vector section_generated( sections_.size(), false ); + + bool is_still_good = layout_segments_and_their_sections( section_generated ); + is_still_good = is_still_good && layout_sections_without_segments( section_generated ); is_still_good = is_still_good && layout_section_table(); is_still_good = is_still_good && save_header( stream ); @@ -744,24 +746,6 @@ class elfio return true; } - //------------------------------------------------------------------------------ - //! \brief Check if a section is without a segment - //! \param section_index The index of the section - //! \return True if the section is without a segment, false otherwise - bool is_section_without_segment( unsigned int section_index ) const - { - bool found = false; - - for ( unsigned int j = 0; !found && ( j < segments.size() ); ++j ) { - for ( Elf_Half k = 0; - !found && ( k < segments[j]->get_sections_num() ); ++k ) { - found = segments[j]->get_section_index_at( k ) == section_index; - } - } - - return !found; - } - //------------------------------------------------------------------------------ //! \brief Check if a segment is a subsequence of another segment //! \param seg1 Pointer to the first segment @@ -833,10 +817,10 @@ class elfio //------------------------------------------------------------------------------ //! \brief Layout sections without segments //! \return True if successful, false otherwise - bool layout_sections_without_segments() + bool layout_sections_without_segments( const std::vector& section_generated ) { for ( unsigned int i = 0; i < sections_.size(); ++i ) { - if ( is_section_without_segment( i ) ) { + if ( !section_generated[i] ) { const auto& sec = sections_[i]; if ( Elf_Xword section_align = sec->get_addr_align(); @@ -877,10 +861,9 @@ class elfio //------------------------------------------------------------------------------ //! \brief Layout segments and their sections //! \return True if successful, false otherwise - bool layout_segments_and_their_sections() + bool layout_segments_and_their_sections( std::vector& section_generated ) { std::vector worklist; - std::vector section_generated( sections.size(), false ); // Get segments in a order in where segments which contain a // sub sequence of other segments are located at the end