From 3e4a2fabfd0baed62af8ab18ddc1ac6c4be137df Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center <to_serge@users.sourceforge.net> Date: Sat, 22 Feb 2020 11:31:27 +0200 Subject: [PATCH] Add tests for the new flavor of get_symbol() --- ELFIOTest/ELFIOTest1.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ elfio/elfio.hpp | 1 - elfio/elfio_symbols.hpp | 18 ++++++--------- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/ELFIOTest/ELFIOTest1.cpp b/ELFIOTest/ELFIOTest1.cpp index 46669f4..efb49e0 100644 --- a/ELFIOTest/ELFIOTest1.cpp +++ b/ELFIOTest/ELFIOTest1.cpp @@ -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 ); +} diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index b8c9150..0ab8552 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -39,7 +39,6 @@ THE SOFTWARE. #include <deque> #include <iterator> #include <typeinfo> -#include <cassert> #include <elfio/elf_types.hpp> #include <elfio/elfio_utils.hpp> diff --git a/elfio/elfio_symbols.hpp b/elfio/elfio_symbols.hpp index 7d86d2e..3a0689a 100644 --- a/elfio/elfio_symbols.hpp +++ b/elfio/elfio_symbols.hpp @@ -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; -- GitLab