diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index cb9180f..daa114a 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -378,6 +378,18 @@ class elfio seg->load( stream, (std::streamoff)offset + i * entry_size ); seg->set_index( i ); + // add sections to the segments based on the load address + Elf64_Off segBaseOffset = seg->get_offset(); + Elf64_Off segEndOffset = segBaseOffset + seg->get_memory_size(); + Elf_Half sec_num = sections.size(); + for(Elf_Half j = 0; j < sec_num; ++j) { + const section* psec = sections[j]; + Elf64_Off secOffset = psec->get_offset(); + if( segBaseOffset <= secOffset && secOffset < segEndOffset) + seg->add_section_index(psec->get_index(), + psec->get_addr_align()); + } + // Add section into the segments' container segments_.push_back( seg ); } diff --git a/elfio/elfio_section.hpp b/elfio/elfio_section.hpp index b046f99..f8a85a3 100644 --- a/elfio/elfio_section.hpp +++ b/elfio/elfio_section.hpp @@ -54,6 +54,7 @@ class section virtual void append_data( const std::string& data ) = 0; protected: + virtual Elf64_Off get_offset() const = 0; virtual void set_index( Elf_Half ) = 0; virtual void load( std::ifstream& f, std::streampos header_offset ) = 0; @@ -194,6 +195,13 @@ class section_impl : public section //------------------------------------------------------------------------------ protected: +//------------------------------------------------------------------------------ + Elf64_Off + get_offset() const + { + return header.sh_offset; + } + //------------------------------------------------------------------------------ void set_index( Elf_Half value ) diff --git a/elfio/elfio_segment.hpp b/elfio/elfio_segment.hpp index 4a3121b..55f6a10 100644 --- a/elfio/elfio_segment.hpp +++ b/elfio/elfio_segment.hpp @@ -134,6 +134,12 @@ class segment_impl : public segment //------------------------------------------------------------------------------ protected: +//------------------------------------------------------------------------------ + Elf64_Off + get_offset() const + { + return ph.p_offset; + } //------------------------------------------------------------------------------ void set_index( Elf_Half value )