Add C wrapper for note, modinfo and dynamic sections

This commit is contained in:
Serge Lamikhov-Center 2020-10-16 01:30:12 +03:00
parent 49c2de32ec
commit 93c46505e0
3 changed files with 252 additions and 2 deletions

View File

@ -1,3 +1,25 @@
/*
Copyright (C) 2001-2020 by Serge Lamikhov-Center
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
@ -73,7 +95,7 @@ int main( int argc, char* argv[] )
}
elfio_symbol_section_accessor_delete( psymbols );
//------------------------NoA-----------------------------------------------------
//-----------------------------------------------------------------------------
// relocation
//-----------------------------------------------------------------------------
psection = elfio_get_section_by_name( pelfio, ".rela.dyn" );
@ -103,7 +125,41 @@ int main( int argc, char* argv[] )
str = elfio_string_get_string( pstring, pos );
// printf( "%s\n", str );
}
elfio_string_section_accessor_new( pstring );
elfio_string_section_accessor_delete( pstring );
//-----------------------------------------------------------------------------
// note
//-----------------------------------------------------------------------------
psection = elfio_get_section_by_name( pelfio, ".note.gnu.build-id" );
pnote_t pnote = elfio_note_section_accessor_new( pelfio, psection );
int noteno = elfio_note_get_notes_num( pnote );
for ( int i = 0; i < noteno; i++ ) {
Elf_Word type;
char name[128];
int name_len = 128;
char* desc;
Elf_Word descSize = 128;
elfio_note_get_note( pnote, i, &type, name, name_len, (void**)&desc,
&descSize );
// printf( "[%4d] %s %08x\n", i, name, descSize );
}
elfio_note_section_accessor_delete( pnote );
//-----------------------------------------------------------------------------
// dynamic
//-----------------------------------------------------------------------------
psection = elfio_get_section_by_name( pelfio, ".dynamic" );
pdynamic_t pdynamic =
elfio_dynamic_section_accessor_new( pelfio, psection );
int dynno = elfio_dynamic_get_entries_num( pdynamic );
for ( int i = 0; i < dynno; i++ ) {
Elf_Xword tag;
Elf_Xword value;
char str[128];
elfio_dynamic_get_entry( pdynamic, i, &tag, &value, str, 128 );
printf( "[%4d] %16lx %16lx %s\n", i, tag, value, str );
}
elfio_dynamic_section_accessor_delete( pdynamic );
elfio_delete( pelfio );

View File

@ -320,3 +320,134 @@ Elf_Word elfio_string_add_string( pstring_t pstring, char* str )
{
return pstring->add_string( str );
}
//-----------------------------------------------------------------------------
// note
//-----------------------------------------------------------------------------
pnote_t elfio_note_section_accessor_new( pelfio_t pelfio, psection_t psection )
{
return new note_section_accessor( *pelfio, psection );
}
void elfio_note_section_accessor_delete( pnote_t pnote ) { delete pnote; }
Elf_Word elfio_note_get_notes_num( pnote_t pnote )
{
return pnote->get_notes_num();
}
bool elfio_note_get_note( pnote_t pnote,
Elf_Word index,
Elf_Word* type,
char* name,
int name_len,
void** desc,
Elf_Word* descSize )
{
std::string name_str;
bool ret = pnote->get_note( index, *type, name_str, *desc, *descSize );
strncpy( name, name_str.c_str(), name_len - 1 );
return ret;
}
void elfio_note_add_note( pnote_t pnote,
Elf_Word type,
const char* name,
const void* desc,
Elf_Word descSize )
{
pnote->add_note( type, name, desc, descSize );
}
//-----------------------------------------------------------------------------
// modinfo
//-----------------------------------------------------------------------------
pmodinfo_t elfio_modinfo_section_accessor_new( psection_t psection )
{
return new modinfo_section_accessor( psection );
}
void elfio_modinfo_section_accessor_delete( pmodinfo_t pmodinfo )
{
delete pmodinfo;
}
Elf_Word elfio_modinfo_get_attribute_num( pmodinfo_t pmodinfo )
{
return pmodinfo->get_attribute_num();
}
bool elfio_modinfo_get_attribute( pmodinfo_t pmodinfo,
Elf_Word no,
char* field,
int field_len,
char* value,
int value_len )
{
std::string field_str;
std::string value_str;
bool ret = pmodinfo->get_attribute( no, field_str, value_str );
strncpy( field, field_str.c_str(), field_len - 1 );
strncpy( value, value_str.c_str(), value_len - 1 );
return ret;
}
bool elfio_modinfo_get_attribute_by_name( pmodinfo_t pmodinfo,
char* field_name,
char* value,
int value_len )
{
std::string value_str;
bool ret = pmodinfo->get_attribute( value_str, value_str );
strncpy( value, value_str.c_str(), value_len - 1 );
return ret;
}
Elf_Word
elfio_modinfo_add_attribute( pmodinfo_t pmodinfo, char* field, char* value )
{
return pmodinfo->add_attribute( field, value );
}
//-----------------------------------------------------------------------------
// dynamic
//-----------------------------------------------------------------------------
pdynamic_t elfio_dynamic_section_accessor_new( pelfio_t pelfio,
psection_t psection )
{
return new dynamic_section_accessor( *pelfio, psection );
}
void elfio_dynamic_section_accessor_delete( pdynamic_t pdynamic )
{
delete pdynamic;
}
Elf_Xword elfio_dynamic_get_entries_num( pdynamic_t pdynamic )
{
return pdynamic->get_entries_num();
}
bool elfio_dynamic_get_entry( pdynamic_t pdynamic,
Elf_Xword index,
Elf_Xword* tag,
Elf_Xword* value,
char* str,
int str_len )
{
std::string str_str;
bool ret = pdynamic->get_entry( index, *tag, *value, str_str );
strncpy( str, str_str.c_str(), str_len - 1 );
return ret;
}
void elfio_dynamic_add_entry( pdynamic_t pdynamic,
Elf_Xword tag,
Elf_Xword value )
{
pdynamic->add_entry( tag, value );
}

View File

@ -82,6 +82,9 @@ 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;
typedef ELFIO::note_section_accessor* pnote_t;
typedef ELFIO::modinfo_section_accessor* pmodinfo_t;
typedef ELFIO::dynamic_section_accessor* pdynamic_t;
extern "C"
{
@ -92,6 +95,9 @@ typedef void* psegment_t;
typedef void* psymbol_t;
typedef void* prelocation_t;
typedef void* pstring_t;
typedef void* pnote_t;
typedef void* pmodinfo_t;
typedef void* pdynamic_t;
typedef int bool;
#endif
@ -238,6 +244,63 @@ typedef int bool;
const char* elfio_string_get_string( pstring_t pstring, Elf_Word index );
Elf_Word elfio_string_add_string( pstring_t pstring, char* str );
//-----------------------------------------------------------------------------
// note
//-----------------------------------------------------------------------------
pnote_t elfio_note_section_accessor_new( pelfio_t pelfio,
psection_t psection );
void elfio_note_section_accessor_delete( pnote_t pstring );
Elf_Word elfio_note_get_notes_num( pnote_t pnote );
bool elfio_note_get_note( pnote_t pnote,
Elf_Word index,
Elf_Word* type,
char* name,
int name_len,
void** desc,
Elf_Word* descSize );
void elfio_note_add_note( pnote_t pnote,
Elf_Word type,
const char* name,
const void* desc,
Elf_Word descSize );
//-----------------------------------------------------------------------------
// modinfo
//-----------------------------------------------------------------------------
pmodinfo_t elfio_modinfo_section_accessor_new( psection_t psection );
void elfio_modinfo_section_accessor_delete( pmodinfo_t pmodinfo );
Elf_Word elfio_modinfo_get_attribute_num( pmodinfo_t pmodinfo );
bool elfio_modinfo_get_attribute( pmodinfo_t pmodinfo,
Elf_Word no,
char* field,
int field_len,
char* value,
int value_len );
bool elfio_modinfo_get_attribute_by_name( pmodinfo_t pmodinfo,
char* field_name,
char* value,
int value_len );
Elf_Word elfio_modinfo_add_attribute( pmodinfo_t pmodinfo,
char* field,
char* value );
//-----------------------------------------------------------------------------
// dynamic
//-----------------------------------------------------------------------------
pdynamic_t elfio_dynamic_section_accessor_new( pelfio_t pelfio,
psection_t psection );
void elfio_dynamic_section_accessor_delete( pdynamic_t pdynamic );
Elf_Xword elfio_dynamic_get_entries_num( pdynamic_t pdynamic );
bool elfio_dynamic_get_entry( pdynamic_t pdynamic,
Elf_Xword index,
Elf_Xword* tag,
Elf_Xword* value,
char* str,
int str_len );
void elfio_dynamic_add_entry( pdynamic_t pdynamic,
Elf_Xword tag,
Elf_Xword value );
#ifdef __cplusplus
}
#endif