From 5b448b7e1cb90a3dd055ca701c74ef8924585183 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Sat, 10 Oct 2020 08:52:26 +0300 Subject: [PATCH] Add C symbol functions --- examples/c_interop/c_example.c | 5 ++- examples/c_interop/elfio_c_interop.cpp | 58 +++++++++++++++++++++++++- examples/c_interop/elfio_c_interop.h | 42 ++++++++++++++++--- 3 files changed, 96 insertions(+), 9 deletions(-) diff --git a/examples/c_interop/c_example.c b/examples/c_interop/c_example.c index df410f4..fcc3053 100644 --- a/examples/c_interop/c_example.c +++ b/examples/c_interop/c_example.c @@ -29,7 +29,7 @@ int main( int argc, char* argv[] ) elfio_section_get_address( psection ), elfio_section_get_size( psection ) ); } - +/* int segno = elfio_get_segments_num( pelfio ); printf( "\nSegments No : %d\n", segno ); @@ -40,6 +40,9 @@ int main( int argc, char* argv[] ) elfio_segment_get_memory_size( psegment ), elfio_segment_get_file_size( psegment ) ); } +*/ + + elfio_delete( pelfio ); diff --git a/examples/c_interop/elfio_c_interop.cpp b/examples/c_interop/elfio_c_interop.cpp index f22d582..eb1a4a8 100644 --- a/examples/c_interop/elfio_c_interop.cpp +++ b/examples/c_interop/elfio_c_interop.cpp @@ -178,8 +178,7 @@ Elf_Half elfio_segment_get_sections_num( psegment_t psegment ) return psegment->get_sections_num(); } -Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, - Elf_Half num ) +Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, Elf_Half num ) { return psegment->get_section_index_at( num ); } @@ -188,3 +187,58 @@ bool elfio_segment_is_offset_initialized( psegment_t psegment ) { return psegment->is_offset_initialized(); } + +//----------------------------------------------------------------------------- +// symbol +//----------------------------------------------------------------------------- +psymbol_t elfio_symbol_section_accessor_new( pelfio_t pelfio, + psection_t psection ) +{ + return new symbol_section_accessor( *pelfio, psection ); +} + +void elfio_symbol_section_accessor_delete( psymbol_t psymbol ) +{ + delete psymbol; +} + +Elf_Xword elfio_symbol_get_symbols_num( psymbol_t psymbol ) +{ + return psymbol->get_symbols_num(); +} + +bool get_symbol( psymbol_t psymbol, + Elf_Xword index, + char* name, + int name_len, + Elf64_Addr* value, + Elf_Xword* size, + unsigned char* bind, + unsigned char* type, + Elf_Half* section_index, + unsigned char* other ) +{ + std::string name_param; + bool ret = psymbol->get_symbol( index, name_param, *value, *size, *bind, + *type, *section_index, *other ); + strncpy( name, name_param.c_str(), name_len - 1 ); + + return ret; +} + +Elf_Word elfio_symbol_add_symbol( psymbol_t psymbol, + Elf_Word name, + Elf64_Addr value, + Elf_Xword size, + unsigned char info, + unsigned char other, + Elf_Half shndx ) +{ + return psymbol->add_symbol( name, value, size, info, other, shndx ); +} + +Elf_Xword elfio_symbol_arrange_local_symbols( + psymbol_t psymbol, void ( *func )( Elf_Xword first, Elf_Xword second ) ) +{ + return psymbol->arrange_local_symbols( func ); +} diff --git a/examples/c_interop/elfio_c_interop.h b/examples/c_interop/elfio_c_interop.h index 647569a..41188c9 100644 --- a/examples/c_interop/elfio_c_interop.h +++ b/examples/c_interop/elfio_c_interop.h @@ -76,9 +76,10 @@ THE SOFTWARE. void elfio_##CLASS##_set_##NAME( p##CLASS##_t p##CLASS, TYPE value ) #ifdef __cplusplus -typedef ELFIO::elfio* pelfio_t; -typedef ELFIO::section* psection_t; -typedef ELFIO::segment* psegment_t; +typedef ELFIO::elfio* pelfio_t; +typedef ELFIO::section* psection_t; +typedef ELFIO::segment* psegment_t; +typedef ELFIO::symbol_section_accessor* psymbol_t; extern "C" { @@ -86,6 +87,7 @@ extern "C" typedef void* pelfio_t; typedef void* psection_t; typedef void* psegment_t; +typedef void* psymbol_t; typedef int bool; #endif @@ -161,12 +163,40 @@ typedef int bool; ELFIO_C_GET_ACCESS( segment, Elf64_Off, offset ); char* elfio_segment_get_data( psegment_t psegment ); Elf_Half elfio_segment_add_section_index( psegment_t psegment, - Elf_Half index, - Elf_Xword addr_align ); + Elf_Half index, + Elf_Xword addr_align ); Elf_Half elfio_segment_get_sections_num( psegment_t psegment ); - Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, Elf_Half num ); + Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, + Elf_Half num ); bool elfio_segment_is_offset_initialized( psegment_t psegment ); + //----------------------------------------------------------------------------- + // symbol + //----------------------------------------------------------------------------- + psymbol_t elfio_symbol_section_accessor_new( pelfio_t pelfio, + psection_t psection ); + void elfio_symbol_section_accessor_delete( psymbol_t psymbol ); + Elf_Xword elfio_symbol_get_symbols_num( psymbol_t psymbol ); + bool elfio_symbol_get_symbol( psymbol_t psymbol, + Elf_Xword index, + char* name, + int name_len, + Elf64_Addr* value, + Elf_Xword* size, + unsigned char* bind, + unsigned char* type, + Elf_Half* section_index, + unsigned char* other ); + Elf_Word elfio_symbol_add_symbol( psymbol_t psymbol, + Elf_Word name, + Elf64_Addr value, + Elf_Xword size, + unsigned char info, + unsigned char other, + Elf_Half shndx ); + Elf_Xword elfio_symbol_arrange_local_symbols( + psymbol_t psymbol, void (*func)( Elf_Xword first, Elf_Xword second ) ); + #ifdef __cplusplus } #endif