From ab41401ab2bd8e4586772c345a2d636291590cb9 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Mon, 12 Oct 2020 20:06:26 +0300 Subject: [PATCH] String section C language wrapper added --- examples/c_wrapper/c_example.c | 55 +++++++++++++++++++++++--- examples/c_wrapper/elfio_c_wrapper.cpp | 23 +++++++++++ examples/c_wrapper/elfio_c_wrapper.h | 10 +++++ 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/examples/c_wrapper/c_example.c b/examples/c_wrapper/c_example.c index ecfec9a..3617347 100644 --- a/examples/c_wrapper/c_example.c +++ b/examples/c_wrapper/c_example.c @@ -1,4 +1,5 @@ #include +#include #include #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, §ion_index, &other ); + elfio_symbol_get_symbol( psymbols, i, name, 128, &value, &size, &bind, + &type, §ion_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; diff --git a/examples/c_wrapper/elfio_c_wrapper.cpp b/examples/c_wrapper/elfio_c_wrapper.cpp index 26704f9..0f79ab3 100644 --- a/examples/c_wrapper/elfio_c_wrapper.cpp +++ b/examples/c_wrapper/elfio_c_wrapper.cpp @@ -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 ); +} diff --git a/examples/c_wrapper/elfio_c_wrapper.h b/examples/c_wrapper/elfio_c_wrapper.h index 082bb65..2850627 100644 --- a/examples/c_wrapper/elfio_c_wrapper.h +++ b/examples/c_wrapper/elfio_c_wrapper.h @@ -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