Add tests for the new flavor of get_symbol()

This commit is contained in:
Serge Lamikhov-Center 2020-02-22 11:31:27 +02:00
parent 1a5ac38fae
commit 3e4a2fabfd
3 changed files with 54 additions and 12 deletions

View File

@ -436,3 +436,50 @@ BOOST_AUTO_TEST_CASE( elf_exe_loadsave_ppc32big3 )
checkExeAreEqual( in, out, SEG_ALIGN );
}
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( get_symbol_32 )
{
elfio elf;
std::string name;
ELFIO::Elf_Xword size;
unsigned char bind;
unsigned char type;
ELFIO::Elf_Half section_index;
unsigned char other;
std::string in = "../elf_examples/hello_32";
BOOST_REQUIRE_EQUAL( elf.load(in), true );
section* psymsec = elf.sections[ ".symtab" ];
const symbol_section_accessor symbols( elf, psymsec );
BOOST_CHECK_EQUAL( true,
symbols.get_symbol( 0x08048478, name, size, bind,
type, section_index, other) );
BOOST_CHECK_EQUAL( "_IO_stdin_used", name );
BOOST_CHECK_EQUAL( 14, section_index );
BOOST_CHECK_EQUAL( 4, size );
}
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( get_symbol_64 )
{
elfio elf;
std::string name;
ELFIO::Elf_Xword size;
unsigned char bind;
unsigned char type;
ELFIO::Elf_Half section_index;
unsigned char other;
std::string in = "../elf_examples/hello_64";
BOOST_REQUIRE_EQUAL( elf.load(in), true );
section* psymsec = elf.sections[ ".symtab" ];
const symbol_section_accessor symbols( elf, psymsec );
BOOST_CHECK_EQUAL( true,
symbols.get_symbol(0x00400498, name, size, bind,
type, section_index, other) );
BOOST_CHECK_EQUAL( "main", name );
BOOST_CHECK_EQUAL( 12, section_index );
BOOST_CHECK_EQUAL( 21, size );
}

View File

@ -39,7 +39,6 @@ THE SOFTWARE.
#include <deque>
#include <iterator>
#include <typeinfo>
#include <cassert>
#include <elfio/elf_types.hpp>
#include <elfio/elfio_utils.hpp>

View File

@ -92,9 +92,8 @@ class symbol_section_accessor_template
Elf_Word nchain = *(const Elf_Word*)( hash_section->get_data() +
sizeof( Elf_Word ) );
Elf_Word val = elf_hash( (const unsigned char*)name.c_str() );
Elf_Word y = *(const Elf_Word*)( hash_section->get_data() +
( 2 + val % nbucket ) * sizeof( Elf_Word ) );
Elf_Word y = *(const Elf_Word*)( hash_section->get_data() +
( 2 + val % nbucket ) * sizeof( Elf_Word ) );
std::string str;
get_symbol( y, str, value, size, bind, type, section_index, other );
while ( str != name && STN_UNDEF != y && y < nchain ) {
@ -125,9 +124,9 @@ class symbol_section_accessor_template
const endianess_convertor& convertor = elf_file.get_convertor();
section* string_section = elf_file.sections[get_string_table_index()];
Elf_Xword idx = 0;
bool match = false;
Elf64_Addr v = 0;
Elf_Xword idx = 0;
bool match = false;
Elf64_Addr v = 0;
if ( elf_file.get_class() == ELFCLASS32 ) {
match = generic_search_symbols<Elf32_Sym>([&convertor, &value](const Elf32_Sym* sym) {
@ -139,11 +138,8 @@ class symbol_section_accessor_template
}, idx);
}
if (match) {
bool found = get_symbol( idx, name, v, size, bind, type, section_index, other );
assert(found);
assert(v == value);
return true;
if ( match ) {
return get_symbol( idx, name, v, size, bind, type, section_index, other );
}
return false;