From d81d4c5e2efb453d9384685ffed6574ff8155a77 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Thu, 13 Jun 2024 16:22:12 +0300 Subject: [PATCH] Add test that verifies that get_symbols_num() returns increased value after add_symbol() --- .vscode/launch.json | 4 ++-- elfio/elfio_symbols.hpp | 20 ++++++++++---------- tests/ELFIOTest1.cpp | 8 ++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 48355c0d..43cb6b3c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", @@ -24,7 +24,7 @@ "ignoreFailures": true } ], - "preLaunchTask": "ELFIO Test build", + //"preLaunchTask": "ELFIO Test build", "miDebuggerPath": "/usr/bin/gdb" }, { diff --git a/elfio/elfio_symbols.hpp b/elfio/elfio_symbols.hpp index 8618af59..f84e7c5e 100644 --- a/elfio/elfio_symbols.hpp +++ b/elfio/elfio_symbols.hpp @@ -120,10 +120,9 @@ template 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; } } } @@ -295,7 +294,7 @@ template class symbol_section_accessor_template Elf_Half& section_index, unsigned char& other ) const { - bool ret = false; + bool ret = false; const endianness_convertor& convertor = elf_file.get_convertor(); Elf_Word nbucket = *(const Elf_Word*)hash_section->get_data(); @@ -334,7 +333,7 @@ template class symbol_section_accessor_template Elf_Half& section_index, unsigned char& other ) const { - bool ret = false; + bool ret = false; const endianness_convertor& convertor = elf_file.get_convertor(); uint32_t nbuckets = *( (uint32_t*)hash_section->get_data() + 0 ); @@ -346,7 +345,7 @@ template 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() ); @@ -377,13 +376,14 @@ template 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] ); } } @@ -398,7 +398,7 @@ template class symbol_section_accessor_template if ( symbol_section->get_entry_size() < sizeof( T ) ) { return nullptr; } - const T* pSym = reinterpret_cast( + const auto* pSym = reinterpret_cast( symbol_section->get_data() + index * symbol_section->get_entry_size() ); @@ -443,7 +443,7 @@ template class symbol_section_accessor_template if ( nullptr != symbol_section->get_data() && index < get_symbols_num() ) { - const T* pSym = reinterpret_cast( + const auto* pSym = reinterpret_cast( symbol_section->get_data() + index * symbol_section->get_entry_size() ); diff --git a/tests/ELFIOTest1.cpp b/tests/ELFIOTest1.cpp index 169a4404..b24a5f4e 100644 --- a/tests/ELFIOTest1.cpp +++ b/tests/ELFIOTest1.cpp @@ -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; @@ -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 ); @@ -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() );