diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 13e88bf..bc2aa51 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -244,31 +244,6 @@ class elfio } //------------------------------------------------------------------------------ - private: - bool is_offset_in_section( Elf64_Off offset, const section* sec ) const - { - return ( offset >= sec->get_offset() ) && - ( offset < ( sec->get_offset() + sec->get_size() ) ); - } - - Elf64_Addr get_virtual_addr( Elf64_Off offset, const section* sec ) const - { - return sec->get_address() + offset - sec->get_offset(); - } - - const section* find_prog_section_for_offset( Elf64_Off offset ) const - { - for ( int i = 0; i < sections.size(); ++i ) { - const section* sec = sections[i]; - if ( sec->get_type() == SHT_PROGBITS ) - if ( is_offset_in_section( offset, sec ) ) - return sec; - } - return NULL; - } - - //------------------------------------------------------------------------------ - public: //! returns an empty string if no problems are detected, //! or a string containing an error message if problems are found, //! with one error per line. @@ -328,8 +303,32 @@ class elfio // clang-format on } - //------------------------------------------------------------------------------ private: + //------------------------------------------------------------------------------ + bool is_offset_in_section( Elf64_Off offset, const section* sec ) const + { + return ( offset >= sec->get_offset() ) && + ( offset < ( sec->get_offset() + sec->get_size() ) ); + } + + //------------------------------------------------------------------------------ + Elf64_Addr get_virtual_addr( Elf64_Off offset, const section* sec ) const + { + return sec->get_address() + offset - sec->get_offset(); + } + + //------------------------------------------------------------------------------ + const section* find_prog_section_for_offset( Elf64_Off offset ) const + { + for ( int i = 0; i < sections.size(); ++i ) { + const section* sec = sections[i]; + if ( sec->get_type() == SHT_PROGBITS ) + if ( is_offset_in_section( offset, sec ) ) + return sec; + } + return NULL; + } + //------------------------------------------------------------------------------ void clean() { diff --git a/examples/c_wrapper/c_example.c b/examples/c_wrapper/c_example.c index 4c94729..df77ebe 100644 --- a/examples/c_wrapper/c_example.c +++ b/examples/c_wrapper/c_example.c @@ -29,10 +29,25 @@ THE SOFTWARE. int main( int argc, char* argv[] ) { pelfio_t pelfio = elfio_new(); - bool ret = elfio_load( pelfio, argv[0] ); + bool ret; + + if ( argc == 1 ) + ret = elfio_load( pelfio, argv[0] ); + else + ret = elfio_load( pelfio, argv[1] ); if ( !ret ) { printf( "Can't load ELF file\n" ); + return 1; + } + + char msg[128]; + ret = elfio_validate( pelfio, msg, 128 ); + + if ( !ret ) { + printf( "Validation errors:\n" ); + printf( "%s\n", msg ); + return 2; } //----------------------------------------------------------------------------- diff --git a/examples/c_wrapper/elfio_c_wrapper.cpp b/examples/c_wrapper/elfio_c_wrapper.cpp index bf27e1e..e8c05be 100644 --- a/examples/c_wrapper/elfio_c_wrapper.cpp +++ b/examples/c_wrapper/elfio_c_wrapper.cpp @@ -108,6 +108,17 @@ psegment_t elfio_add_segment( pelfio_t pelfio ) return pelfio->segments.add(); } +bool elfio_validate( pelfio_t pelfio, char* msg, int msg_len ) +{ + std::string error = pelfio->validate(); + + if ( msg != nullptr && msg_len > 0 ) { + strncpy( msg, error.c_str(), (size_t)msg_len - 1 ); + } + + return error.empty(); +} + //----------------------------------------------------------------------------- // section //----------------------------------------------------------------------------- @@ -476,11 +487,12 @@ bool elfio_array_get_entry( parray_t parray, Elf_Xword index, Elf64_Addr* paddress ) { - bool ret = parray->get_entry( index, *paddress); + bool ret = parray->get_entry( index, *paddress ); return ret; } -void elfio_array_add_entry( parray_t parray, Elf64_Addr address ) { +void elfio_array_add_entry( parray_t parray, Elf64_Addr address ) +{ parray->add_entry( address ); } diff --git a/examples/c_wrapper/elfio_c_wrapper.h b/examples/c_wrapper/elfio_c_wrapper.h index 65a96d6..127ab44 100644 --- a/examples/c_wrapper/elfio_c_wrapper.h +++ b/examples/c_wrapper/elfio_c_wrapper.h @@ -136,6 +136,7 @@ typedef int bool; Elf_Half elfio_get_segments_num( pelfio_t pelfio ); psegment_t elfio_get_segment_by_index( pelfio_t pelfio, int index ); psegment_t elfio_add_segment( pelfio_t pelfio ); + bool elfio_validate( pelfio_t pelfio, char* msg, int msg_len ); //----------------------------------------------------------------------------- // section diff --git a/tests/ELFIOTest1.cpp b/tests/ELFIOTest1.cpp index bf1505a..0661d17 100644 --- a/tests/ELFIOTest1.cpp +++ b/tests/ELFIOTest1.cpp @@ -899,7 +899,7 @@ BOOST_AUTO_TEST_CASE( detect_mismatched_section_segment_tables ) * with address of section .ctors (0x804948e) at offset 0x48e */ std::string in = "elf_examples/mismatched_segments.elf"; - elfio elf; + elfio elf; BOOST_REQUIRE_EQUAL( elf.load( in ), true ); - BOOST_REQUIRE( elf.validate().length() > 0); + BOOST_REQUIRE( elf.validate().length() > 0 ); } diff --git a/tests/ELFIOTest2.cpp b/tests/ELFIOTest2.cpp index ba0aa8f..46dfb11 100644 --- a/tests/ELFIOTest2.cpp +++ b/tests/ELFIOTest2.cpp @@ -236,9 +236,10 @@ BOOST_AUTO_TEST_CASE( init_array_write_64 ) //////////////////////////////////////////////////////////////////////////////// BOOST_AUTO_TEST_CASE( test_hex ) { - BOOST_CHECK_EQUAL( to_hex_string( 1 ), "0x1"); - BOOST_CHECK_EQUAL( to_hex_string( 10 ), "0xA"); - BOOST_CHECK_EQUAL( to_hex_string( 0x12345678 ), "0x12345678"); - BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFF ), "0xFFFFFFFF"); - BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFFFFFFFFFF ), "0xFFFFFFFFFFFFFFFF"); + BOOST_CHECK_EQUAL( to_hex_string( 1 ), "0x1" ); + BOOST_CHECK_EQUAL( to_hex_string( 10 ), "0xA" ); + BOOST_CHECK_EQUAL( to_hex_string( 0x12345678 ), "0x12345678" ); + BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFF ), "0xFFFFFFFF" ); + BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFFFFFFFFFF ), + "0xFFFFFFFFFFFFFFFF" ); }