diff --git a/elfio/elfio_dynamic.hpp b/elfio/elfio_dynamic.hpp index a0c483c..3464a03 100644 --- a/elfio/elfio_dynamic.hpp +++ b/elfio/elfio_dynamic.hpp @@ -23,6 +23,8 @@ THE SOFTWARE. #ifndef ELFIO_DYNAMIC_HPP #define ELFIO_DYNAMIC_HPP +#include + namespace ELFIO { //------------------------------------------------------------------------------ @@ -31,21 +33,30 @@ template class dynamic_section_accessor_template public: //------------------------------------------------------------------------------ dynamic_section_accessor_template( const elfio& elf_file_, S* section_ ) - : elf_file( elf_file_ ), dynamic_section( section_ ) + : elf_file( elf_file_ ), dynamic_section( section_ ), entries_num( 0 ) { } //------------------------------------------------------------------------------ Elf_Xword get_entries_num() const { - Elf_Xword nRet = 0; - - if ( 0 != dynamic_section->get_entry_size() ) { - nRet = + if ( ( 0 == entries_num ) && + ( 0 != dynamic_section->get_entry_size() ) ) { + entries_num = dynamic_section->get_size() / dynamic_section->get_entry_size(); + Elf_Xword i; + Elf_Xword tag; + Elf_Xword value; + std::string str; + for ( i = 0; i < entries_num; i++ ) { + get_entry( i, tag, value, str ); + if ( tag == DT_NULL ) + break; + } + entries_num = std::min( entries_num, i + 1 ); } - return nRet; + return entries_num; } //------------------------------------------------------------------------------ @@ -234,8 +245,9 @@ template class dynamic_section_accessor_template //------------------------------------------------------------------------------ private: - const elfio& elf_file; - S* dynamic_section; + const elfio& elf_file; + S* dynamic_section; + mutable Elf_Xword entries_num; }; using dynamic_section_accessor = dynamic_section_accessor_template
; diff --git a/elfio/elfio_modinfo.hpp b/elfio/elfio_modinfo.hpp index a169cf1..9bf5429 100644 --- a/elfio/elfio_modinfo.hpp +++ b/elfio/elfio_modinfo.hpp @@ -56,7 +56,7 @@ template class modinfo_section_accessor_template } //------------------------------------------------------------------------------ - bool get_attribute( std::string field_name, std::string& value ) const + bool get_attribute( const std::string field_name, std::string& value ) const { for ( auto i = content.begin(); i != content.end(); i++ ) { if ( field_name == i->first ) { @@ -69,7 +69,7 @@ template class modinfo_section_accessor_template } //------------------------------------------------------------------------------ - Elf_Word add_attribute( std::string field, std::string value ) + Elf_Word add_attribute( const std::string field, const std::string value ) { Elf_Word current_position = 0; diff --git a/tests/ELFIOTest.cpp b/tests/ELFIOTest.cpp index f37b9db..867f24b 100644 --- a/tests/ELFIOTest.cpp +++ b/tests/ELFIOTest.cpp @@ -892,7 +892,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_1 ) dynamic_section_accessor da( reader, dynsec ); - BOOST_CHECK_EQUAL( da.get_entries_num(), 26 ); + BOOST_CHECK_EQUAL( da.get_entries_num(), 21 ); Elf_Xword tag; Elf_Xword value; @@ -926,7 +926,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_2 ) dynamic_section_accessor da( reader, dynsec ); - BOOST_CHECK_EQUAL( da.get_entries_num(), 24 ); + BOOST_CHECK_EQUAL( da.get_entries_num(), 20 ); Elf_Xword tag; Elf_Xword value; @@ -956,7 +956,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 ) BOOST_REQUIRE( dynsec != NULL ); dynamic_section_accessor da( reader, dynsec ); - BOOST_CHECK_EQUAL( da.get_entries_num(), 26 ); + BOOST_CHECK_EQUAL( da.get_entries_num(), 21 ); section* strsec1 = reader.sections.add( ".dynstr" ); strsec1->set_type( SHT_STRTAB );