Skip to content

Commit 34fcdea

Browse files
author
Wang Yinghao
committed
Fix: sort secitons in segment when loading
1 parent 7d30a22 commit 34fcdea

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

elfio/elfio.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@ class elfio
632632
return false;
633633
}
634634

635+
std::vector<Elf64_Addr> offsets;
636+
for ( const auto &psec : sections) {
637+
offsets.emplace_back( psec->get_offset() );
638+
}
639+
635640
for ( Elf_Half i = 0; i < num; ++i ) {
636641
if ( file_class == ELFCLASS64 ) {
637642
segments_.emplace_back( new ( std::nothrow )
@@ -688,6 +693,7 @@ class elfio
688693
seg->add_section_index( psec->get_index(), 0 );
689694
}
690695
}
696+
seg->sort_sections( offsets );
691697
}
692698

693699
return true;

elfio/elfio_segment.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class segment
111111
//! \brief Check if the offset is initialized
112112
//! \return True if the offset is initialized, false otherwise
113113
virtual bool is_offset_initialized() const = 0;
114+
//------------------------------------------------------------------------------
115+
//! \brief Sort sections in a segment according to offset
116+
virtual void sort_sections( std::vector<Elf64_Addr>& offsets ) = 0;
114117

115118
protected:
116119
//------------------------------------------------------------------------------
@@ -380,6 +383,14 @@ template <class T> class segment_impl : public segment
380383
//! \brief Set the stream size
381384
//! \param value Stream size
382385
void set_stream_size( size_t value ) { stream_size = value; }
386+
//------------------------------------------------------------------------------
387+
//! \brief Sort sections in a segment according to offset
388+
virtual void sort_sections( std::vector<Elf64_Addr> &offsets )
389+
{
390+
std::sort( sections.begin(), sections.end(), [&]( Elf_Half& a, Elf_Half& b) {
391+
return offsets[a] < offsets[b];
392+
} );
393+
}
383394

384395
//------------------------------------------------------------------------------
385396
private:

0 commit comments

Comments
 (0)