String section C language wrapper added

This commit is contained in:
Serge Lamikhov-Center 2020-10-12 20:06:26 +03:00
parent e55e5894c5
commit ab41401ab2
3 changed files with 83 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <string.h>
#include <elfio/elf_types.hpp>
#include "elfio_c_wrapper.h"
@ -12,15 +13,21 @@ int main( int argc, char* argv[] )
printf( "Can't load ELF file\n" );
}
//-----------------------------------------------------------------------------
// elfio
//-----------------------------------------------------------------------------
printf( "Header size : %d\n", elfio_get_header_size( pelfio ) );
printf( "Version : %d\n", elfio_get_version( pelfio ) );
printf( "Section Entry : %d\n", elfio_get_section_entry_size( pelfio ) );
printf( "Segment Entry : %d\n", elfio_get_segment_entry_size( pelfio ) );
/* Uncomment a block of the interest */
/* Uncomment a printf block of the interest */
//-----------------------------------------------------------------------------
// section
//-----------------------------------------------------------------------------
int secno = elfio_get_sections_num( pelfio );
printf( "\nSections No : %d\n", secno );
printf( "Sections No : %d\n", secno );
for ( int i = 0; i < secno; i++ ) {
psection_t psection = elfio_get_section_by_index( pelfio, i );
@ -32,8 +39,11 @@ int main( int argc, char* argv[] )
// elfio_section_get_size( psection ) );
}
//-----------------------------------------------------------------------------
// segment
//-----------------------------------------------------------------------------
int segno = elfio_get_segments_num( pelfio );
printf( "\nSegments No : %d\n", segno );
printf( "Segments No : %d\n", segno );
for ( int i = 0; i < segno; i++ ) {
psegment_t psegment = elfio_get_segment_by_index( pelfio, i );
@ -43,6 +53,9 @@ int main( int argc, char* argv[] )
// elfio_segment_get_file_size( psegment ) );
}
//-----------------------------------------------------------------------------
// symbol
//-----------------------------------------------------------------------------
psection_t psection = elfio_get_section_by_name( pelfio, ".symtab" );
psymbol_t psymbols = elfio_symbol_section_accessor_new( pelfio, psection );
int symno = elfio_symbol_get_symbols_num( psymbols );
@ -54,12 +67,44 @@ int main( int argc, char* argv[] )
unsigned char type;
Elf_Half section_index;
unsigned char other;
elfio_symbol_get_symbol( psymbols, i, name, 128, &value, &size,
&bind, &type, &section_index, &other );
elfio_symbol_get_symbol( psymbols, i, name, 128, &value, &size, &bind,
&type, &section_index, &other );
// printf( "[%4d] %10lu, %4lu %s\n", i, value, size, name );
}
elfio_symbol_section_accessor_delete( psymbols );
//------------------------NoA-----------------------------------------------------
// relocation
//-----------------------------------------------------------------------------
psection = elfio_get_section_by_name( pelfio, ".rela.dyn" );
prelocation_t preloc =
elfio_relocation_section_accessor_new( pelfio, psection );
int relno = elfio_relocation_get_entries_num( preloc );
for ( int i = 0; i < relno; i++ ) {
Elf64_Addr offset;
Elf_Word symbol;
Elf_Word type;
Elf_Sxword addend;
elfio_relocation_get_entry( preloc, i, &offset, &symbol, &type,
&addend );
// printf( "[%4d] %16lx, %08x %08x %16lx\n", i, offset, symbol, type, addend );
}
elfio_relocation_section_accessor_delete( preloc );
//-----------------------------------------------------------------------------
// string
//-----------------------------------------------------------------------------
psection = elfio_get_section_by_name( pelfio, ".strtab" );
pstring_t pstring = elfio_string_section_accessor_new( psection );
int pos = 0;
const char* str = elfio_string_get_string( pstring, pos );
while ( str ) {
pos += strlen( str ) + 1;
str = elfio_string_get_string( pstring, pos );
// printf( "%s\n", str );
}
elfio_string_section_accessor_new( pstring );
elfio_delete( pelfio );
return 0;

View File

@ -297,3 +297,26 @@ void elfio_relocation_swap_symbols( prelocation_t prelocation,
{
prelocation->swap_symbols( first, second );
}
//-----------------------------------------------------------------------------
// string
//-----------------------------------------------------------------------------
pstring_t elfio_string_section_accessor_new( psection_t psection )
{
return new string_section_accessor( psection );
}
void elfio_string_section_accessor_delete( pstring_t pstring )
{
delete pstring;
}
const char* elfio_string_get_string( pstring_t pstring, Elf_Word index )
{
return pstring->get_string( index );
}
Elf_Word elfio_string_add_string( pstring_t pstring, char* str )
{
return pstring->add_string( str );
}

View File

@ -81,6 +81,7 @@ typedef ELFIO::section* psection_t;
typedef ELFIO::segment* psegment_t;
typedef ELFIO::symbol_section_accessor* psymbol_t;
typedef ELFIO::relocation_section_accessor* prelocation_t;
typedef ELFIO::string_section_accessor* pstring_t;
extern "C"
{
@ -90,6 +91,7 @@ typedef void* psection_t;
typedef void* psegment_t;
typedef void* psymbol_t;
typedef void* prelocation_t;
typedef void* pstring_t;
typedef int bool;
#endif
@ -228,6 +230,14 @@ typedef int bool;
Elf_Xword first,
Elf_Xword second );
//-----------------------------------------------------------------------------
// string
//-----------------------------------------------------------------------------
pstring_t elfio_string_section_accessor_new( psection_t psection );
void elfio_string_section_accessor_delete( pstring_t pstring );
const char* elfio_string_get_string( pstring_t pstring, Elf_Word index );
Elf_Word elfio_string_add_string( pstring_t pstring, char* str );
#ifdef __cplusplus
}
#endif