From fe17eb40aa01fe1abd1ee1e3d3fc3a37524f8f8c Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Wed, 19 Aug 2020 22:44:58 -0700 Subject: [PATCH] Implement swap_symbols() for relocation section. The function can be used as a callback for arrange_local_symbols() --- .vscode/c_cpp_properties.json | 3 +-- elfio/elfio_relocation.hpp | 22 ++++++++++++++++++++++ tests/ELFIOTest1.cpp | 30 +++++++++++++++++------------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index c1ec578..82e03e7 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -9,8 +9,7 @@ "compilerPath": "/usr/bin/g++", "cStandard": "c11", "cppStandard": "c++17", - "intelliSenseMode": "clang-x64", - "configurationProvider": "ms-vscode.cmake-tools" + "intelliSenseMode": "clang-x64" } ], "version": 4 diff --git a/elfio/elfio_relocation.hpp b/elfio/elfio_relocation.hpp index d4630b1..a31fa23 100644 --- a/elfio/elfio_relocation.hpp +++ b/elfio/elfio_relocation.hpp @@ -306,6 +306,28 @@ class relocation_section_accessor_template add_entry( offset, sym_index, type ); } + //------------------------------------------------------------------------------ + void + swap_symbols(Elf_Xword first, Elf_Xword second) + { + Elf64_Addr offset; + Elf_Word symbol; + Elf_Word rtype; + Elf_Sxword addend; + for (Elf_Word i = 0; i < get_entries_num(); i++) + { + get_entry(i, offset, symbol, rtype, addend); + if (symbol == first) + { + set_entry(i, offset, (Elf_Word)second, rtype, addend); + } + if (symbol == second) + { + set_entry(i, offset, (Elf_Word)first, rtype, addend); + } + } + } + //------------------------------------------------------------------------------ private: //------------------------------------------------------------------------------ diff --git a/tests/ELFIOTest1.cpp b/tests/ELFIOTest1.cpp index 0b0a5ab..c26db7d 100644 --- a/tests/ELFIOTest1.cpp +++ b/tests/ELFIOTest1.cpp @@ -812,20 +812,24 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols_with_reallocation) before.push_back(name); } + // symbols.arrange_local_symbols([&](Elf_Xword first, Elf_Xword second) -> void { + // Elf64_Addr offset; + // Elf_Word symbol; + // Elf_Word rtype; + // Elf_Sxword addend; + // for (Elf_Word i = 0; i < rela.get_entries_num(); i++) { + // rela.get_entry(i, offset, symbol, rtype, addend); + // if (symbol == first) { + // rela.set_entry(i, offset, (Elf_Word)second, rtype, addend); + // } + // if (symbol == second) { + // rela.set_entry(i, offset, (Elf_Word)first, rtype, addend); + // } + // } + // }); + symbols.arrange_local_symbols([&](Elf_Xword first, Elf_Xword second) -> void { - Elf64_Addr offset; - Elf_Word symbol; - Elf_Word rtype; - Elf_Sxword addend; - for (Elf_Word i = 0; i < rela.get_entries_num(); i++) { - rela.get_entry(i, offset, symbol, rtype, addend); - if (symbol == first) { - rela.set_entry(i, offset, (Elf_Word)second, rtype, addend); - } - if (symbol == second) { - rela.set_entry(i, offset, (Elf_Word)first, rtype, addend); - } - } + rela.swap_symbols(first, second); }); BOOST_REQUIRE_EQUAL(writer.save(file_name), true);