Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions elfio/elfio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@ class elfio
return false;
}

std::vector<Elf64_Addr> offsets;
for ( const auto &psec : sections) {
offsets.emplace_back( psec->get_offset() );
}

for ( Elf_Half i = 0; i < num; ++i ) {
if ( file_class == ELFCLASS64 ) {
segments_.emplace_back( new ( std::nothrow )
Expand Down Expand Up @@ -688,6 +693,7 @@ class elfio
seg->add_section_index( psec->get_index(), 0 );
}
}
seg->sort_sections( offsets );
}

return true;
Expand Down
11 changes: 11 additions & 0 deletions elfio/elfio_segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class segment
//! \brief Check if the offset is initialized
//! \return True if the offset is initialized, false otherwise
virtual bool is_offset_initialized() const = 0;
//------------------------------------------------------------------------------
//! \brief Sort sections in a segment according to offset
virtual void sort_sections( std::vector<Elf64_Addr>& offsets ) = 0;

protected:
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -380,6 +383,14 @@ template <class T> class segment_impl : public segment
//! \brief Set the stream size
//! \param value Stream size
void set_stream_size( size_t value ) { stream_size = value; }
//------------------------------------------------------------------------------
//! \brief Sort sections in a segment according to offset
virtual void sort_sections( std::vector<Elf64_Addr> &offsets )
{
std::sort( sections.begin(), sections.end(), [&]( Elf_Half& a, Elf_Half& b) {
return offsets[a] < offsets[b];
} );
}

//------------------------------------------------------------------------------
private:
Expand Down
Loading