Add 'validate' function to C language wrapper

This commit is contained in:
Serge Lamikhov-Center 2021-01-08 00:11:20 +02:00
parent 00c1192ea2
commit 8037e5a42a
6 changed files with 64 additions and 36 deletions

View File

@ -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()
{

View File

@ -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;
}
//-----------------------------------------------------------------------------

View File

@ -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 );
}

View File

@ -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

View File

@ -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 );
}

View File

@ -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" );
}