mirror of
https://github.com/serge1/ELFIO.git
synced 2024-09-15 15:13:06 +00:00
Add 'validate' function to C language wrapper
This commit is contained in:
parent
00c1192ea2
commit
8037e5a42a
@ -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,
|
//! returns an empty string if no problems are detected,
|
||||||
//! or a string containing an error message if problems are found,
|
//! or a string containing an error message if problems are found,
|
||||||
//! with one error per line.
|
//! with one error per line.
|
||||||
@ -328,8 +303,32 @@ class elfio
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
private:
|
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()
|
void clean()
|
||||||
{
|
{
|
||||||
|
@ -29,10 +29,25 @@ THE SOFTWARE.
|
|||||||
int main( int argc, char* argv[] )
|
int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
pelfio_t pelfio = elfio_new();
|
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 ) {
|
if ( !ret ) {
|
||||||
printf( "Can't load ELF file\n" );
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
@ -108,6 +108,17 @@ psegment_t elfio_add_segment( pelfio_t pelfio )
|
|||||||
return pelfio->segments.add();
|
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
|
// section
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -481,6 +492,7 @@ bool elfio_array_get_entry( parray_t parray,
|
|||||||
return ret;
|
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 );
|
parray->add_entry( address );
|
||||||
}
|
}
|
||||||
|
@ -136,6 +136,7 @@ typedef int bool;
|
|||||||
Elf_Half elfio_get_segments_num( pelfio_t pelfio );
|
Elf_Half elfio_get_segments_num( pelfio_t pelfio );
|
||||||
psegment_t elfio_get_segment_by_index( pelfio_t pelfio, int index );
|
psegment_t elfio_get_segment_by_index( pelfio_t pelfio, int index );
|
||||||
psegment_t elfio_add_segment( pelfio_t pelfio );
|
psegment_t elfio_add_segment( pelfio_t pelfio );
|
||||||
|
bool elfio_validate( pelfio_t pelfio, char* msg, int msg_len );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// section
|
// section
|
||||||
|
@ -240,5 +240,6 @@ BOOST_AUTO_TEST_CASE( test_hex )
|
|||||||
BOOST_CHECK_EQUAL( to_hex_string( 10 ), "0xA" );
|
BOOST_CHECK_EQUAL( to_hex_string( 10 ), "0xA" );
|
||||||
BOOST_CHECK_EQUAL( to_hex_string( 0x12345678 ), "0x12345678" );
|
BOOST_CHECK_EQUAL( to_hex_string( 0x12345678 ), "0x12345678" );
|
||||||
BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFF ), "0xFFFFFFFF" );
|
BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFF ), "0xFFFFFFFF" );
|
||||||
BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFFFFFFFFFF ), "0xFFFFFFFFFFFFFFFF");
|
BOOST_CHECK_EQUAL( to_hex_string( 0xFFFFFFFFFFFFFFFF ),
|
||||||
|
"0xFFFFFFFFFFFFFFFF" );
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user