Skip to content

Commit

Permalink
Add test that verifies that get_symbols_num() returns increased value…
Browse files Browse the repository at this point in the history
… after add_symbol()
  • Loading branch information
serge1 authored and Serge Lamikhov-Center committed Jun 14, 2024
1 parent 92b9b67 commit a872f7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"program": "${workspaceFolder}/build/tests/ELFIOTest",
"args": [
"--gtest_filter=ELFIOTest.load32",
//"--gtest_filter=ELFIOTest.load32",
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/tests",
Expand All @@ -24,7 +24,7 @@
"ignoreFailures": true
}
],
"preLaunchTask": "ELFIO Test build",
//"preLaunchTask": "ELFIO Test build",
"miDebuggerPath": "/usr/bin/gdb"
},
{
Expand Down
16 changes: 8 additions & 8 deletions elfio/elfio_symbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ template <class S> class symbol_section_accessor_template
for ( Elf_Xword i = 0; !ret && i < get_symbols_num(); i++ ) {
std::string symbol_name;
if ( get_symbol( i, symbol_name, value, size, bind, type,
section_index, other ) ) {
if ( symbol_name == name ) {
ret = true;
}
section_index, other ) &&
( symbol_name == name ) ) {
ret = true;
}
}
}
Expand Down Expand Up @@ -346,7 +345,7 @@ template <class S> class symbol_section_accessor_template
bloom_size = convertor( bloom_size );
bloom_shift = convertor( bloom_shift );

T* bloom_filter =
auto* bloom_filter =
(T*)( hash_section->get_data() + 4 * sizeof( uint32_t ) );

uint32_t hash = elf_gnu_hash( (const unsigned char*)name.c_str() );
Expand Down Expand Up @@ -377,13 +376,14 @@ template <class S> class symbol_section_accessor_template
if ( ( chain_hash >> 1 ) == ( hash >> 1 ) &&
get_symbol( chain_index + symoffset, symname, value, size,
bind, type, section_index, other ) &&
name == symname ) {
( name == symname ) ) {
ret = true;
break;
}

if ( chain_hash & 1 )
break;

chain_hash = convertor( chains[++chain_index] );
}
}
Expand All @@ -398,7 +398,7 @@ template <class S> class symbol_section_accessor_template
if ( symbol_section->get_entry_size() < sizeof( T ) ) {
return nullptr;
}
const T* pSym = reinterpret_cast<const T*>(
const auto* pSym = reinterpret_cast<const T*>(
symbol_section->get_data() +
index * symbol_section->get_entry_size() );

Expand Down Expand Up @@ -443,7 +443,7 @@ template <class S> class symbol_section_accessor_template

if ( nullptr != symbol_section->get_data() &&
index < get_symbols_num() ) {
const T* pSym = reinterpret_cast<const T*>(
const auto* pSym = reinterpret_cast<const T*>(
symbol_section->get_data() +
index * symbol_section->get_entry_size() );

Expand Down
8 changes: 8 additions & 0 deletions tests/ELFIOTest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ TEST( ELFIOTest, rearrange_local_symbols )
sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) );
symbol_section_accessor symbols( writer, sym_sec );

auto sym_num = symbols.get_symbols_num();

name = "Str1";
bind = STB_GLOBAL;
value = 1;
Expand Down Expand Up @@ -677,6 +679,8 @@ TEST( ELFIOTest, rearrange_local_symbols )
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
other, section_index );

ASSERT_EQ( symbols.get_symbols_num(), sym_num + 9);

symbols.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword ) -> void {
static int counter = 0;
EXPECT_EQ( first, ++counter );
Expand Down Expand Up @@ -788,12 +792,16 @@ TEST( ELFIOTest, rearrange_local_symbols_with_reallocation )
value = 7;
Elf_Word sym7 = symbols.add_symbol( str_writer, name.c_str(), value, size,
bind, type, other, section_index );
auto sym_num = symbols.get_symbols_num();

name = "Str8";
bind = STB_WEAK;
value = 8;
Elf_Word sym8 = symbols.add_symbol( str_writer, name.c_str(), value, size,
bind, type, other, section_index );

ASSERT_EQ( ( symbols.get_symbols_num() ), ( sym_num + 1 ) );

section* rel_sec = writer.sections.add( ".rel.text" );
rel_sec->set_type( SHT_REL );
rel_sec->set_info( text_sec->get_index() );
Expand Down

0 comments on commit a872f7c

Please sign in to comment.