add_entry() functions are implemented for dynamic_section_accessor class

Signed-off-by: Serge Lamikhov-Center <to_serge@users.sourceforge.net>
This commit is contained in:
Serge Lamikhov-Center 2013-01-13 19:59:15 +02:00
parent 4b7061dc92
commit 1e82ef11b4
3 changed files with 143 additions and 32 deletions

View File

@ -986,3 +986,58 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_2 )
BOOST_CHECK_EQUAL( tag, DT_NULL );
BOOST_CHECK_EQUAL( value, 0 );
}
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
{
elfio reader;
reader.load( "../elf_examples/main" );
section* dynsec = reader.sections[".dynamic"];
dynamic_section_accessor da( reader, dynsec );
BOOST_CHECK_EQUAL( da.get_entries_num(), 26 );
section* strsec1 = reader.sections.add( ".dynstr" );
strsec1->set_type( SHT_STRTAB );
strsec1->set_entry_size( reader.get_default_entry_size( SHT_STRTAB ) );
section* dynsec1 = reader.sections.add( ".dynamic1" );
dynsec1->set_type( SHT_DYNAMIC );
dynsec1->set_entry_size( reader.get_default_entry_size( SHT_DYNAMIC ) );
dynsec1->set_link( strsec1->get_index() );
dynamic_section_accessor da1( reader, dynsec1 );
Elf_Xword tag, tag1;
Elf_Xword value, value1;
std::string str, str1;
for ( int i = 0; i < da.get_entries_num(); ++i ) {
da.get_entry( i, tag, value, str );
if ( tag == DT_NEEDED ||
tag == DT_SONAME ||
tag == DT_RPATH ||
tag == DT_RUNPATH ) {
da1.add_entry( tag, str );
}
else {
da1.add_entry( tag, value );
}
}
for ( int i = 0; i < da.get_entries_num(); ++i ) {
da.get_entry( i, tag, value, str );
da1.get_entry( i, tag1, value1, str1 );
BOOST_CHECK_EQUAL( tag, tag1 );
if ( tag == DT_NEEDED ||
tag == DT_SONAME ||
tag == DT_RPATH ||
tag == DT_RUNPATH ) {
BOOST_CHECK_EQUAL( str, str1 );
}
else {
BOOST_CHECK_EQUAL( value, value1 );
}
}
}

View File

@ -228,7 +228,6 @@ class elfio
//------------------------------------------------------------------------------
void clean()
{
// TODO: Instead of deletion, substitute to NULL object
delete header;
header = 0;
@ -268,7 +267,7 @@ class elfio
//------------------------------------------------------------------------------
section* create_section()
{
section* new_section;
section* new_section;
unsigned char file_class = get_class();
if ( file_class == ELFCLASS64 ) {
@ -291,7 +290,7 @@ class elfio
//------------------------------------------------------------------------------
segment* create_segment()
{
segment* new_segment;
segment* new_segment;
unsigned char file_class = header->get_class();
if ( file_class == ELFCLASS64 ) {

View File

@ -73,21 +73,45 @@ class dynamic_section_accessor
tag == DT_RPATH ||
tag == DT_RUNPATH ) {
string_section_accessor strsec =
elf_file.sections[ dynamic_section->get_link() ];
elf_file.sections[ get_string_table_index() ];
str = strsec.get_string( value );
}
else {
str = "";
str.clear();
}
return true;
}
//------------------------------------------------------------------------------
void
add_entry( Elf_Xword& tag,
Elf_Xword& value )
{
if ( elf_file.get_class() == ELFCLASS32 ) {
generic_add_entry< Elf32_Dyn >( tag, value );
}
else {
generic_add_entry< Elf64_Dyn >( tag, value );
}
}
//------------------------------------------------------------------------------
void
add_entry( Elf_Xword& tag,
std::string& str )
{
string_section_accessor strsec =
elf_file.sections[ get_string_table_index() ];
Elf_Xword value = strsec.add_string( str );
add_entry( tag, value );
}
//------------------------------------------------------------------------------
private:
//------------------------------------------------------------------------------
Elf_Half
get_symbol_table_index() const
get_string_table_index() const
{
return (Elf_Half)dynamic_section->get_link();
}
@ -107,6 +131,9 @@ class dynamic_section_accessor
tag = convertor( pEntry->d_tag );
switch ( tag ) {
case DT_NULL:
case DT_SYMBOLIC:
case DT_TEXTREL:
case DT_BIND_NOW:
value = 0;
break;
case DT_NEEDED:
@ -125,32 +152,6 @@ class dynamic_section_accessor
case DT_RUNPATH:
case DT_FLAGS:
case DT_PREINIT_ARRAYSZ:
/*
case DT_SUNW_SYMSZ:
case DT_SUNW_SORTENT:
case DT_SUNW_SYMSORTSZ:
case DT_SUNW_TLSSORTSZ:
case DT_SUNW_STRPAD:
case DT_SUNW_LDMACH:
case DT_SUNW_CAPCHAINENT:
case DT_SUNW_CAPCHAINSZ:
case DT_CHECKSUM:
case DT_PLTPADSZ:
case DT_MOVEENT:
case DT_MOVESZ:
case DT_POSFLAG_1:
case DT_SYMINSZ:
case DT_SYMINENT:
case DT_RELACOUNT:
case DT_RELCOUNT:
case DT_FLAGS_1:
case DT_VERDEFNUM:
case DT_VERNEEDNUM:
case DT_SPARC_REGISTER:
case DT_AUXILIARY:
case DT_USED:
case DT_FILTER:
*/
value = convertor( pEntry->d_un.d_val );
break;
case DT_PLTGOT:
@ -172,6 +173,62 @@ class dynamic_section_accessor
}
}
//------------------------------------------------------------------------------
template< class T >
void
generic_add_entry( Elf_Xword tag, Elf_Xword value )
{
const endianess_convertor& convertor = elf_file.get_convertor();
T entry;
switch ( tag ) {
case DT_NULL:
case DT_SYMBOLIC:
case DT_TEXTREL:
case DT_BIND_NOW:
value = 0;
case DT_NEEDED:
case DT_PLTRELSZ:
case DT_RELASZ:
case DT_RELAENT:
case DT_STRSZ:
case DT_SYMENT:
case DT_SONAME:
case DT_RPATH:
case DT_RELSZ:
case DT_RELENT:
case DT_PLTREL:
case DT_INIT_ARRAYSZ:
case DT_FINI_ARRAYSZ:
case DT_RUNPATH:
case DT_FLAGS:
case DT_PREINIT_ARRAYSZ:
entry.d_un.d_val = convertor( value );
break;
case DT_PLTGOT:
case DT_HASH:
case DT_STRTAB:
case DT_SYMTAB:
case DT_RELA:
case DT_INIT:
case DT_FINI:
case DT_REL:
case DT_DEBUG:
case DT_JMPREL:
case DT_INIT_ARRAY:
case DT_FINI_ARRAY:
case DT_PREINIT_ARRAY:
default:
entry.d_un.d_ptr = convertor( value );
break;
}
entry.d_tag = convertor( tag );
dynamic_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) );
}
//------------------------------------------------------------------------------
private:
const elfio& elf_file;