Implement swap_symbols() for relocation section.

The function can be used as a callback for arrange_local_symbols()
This commit is contained in:
Serge Lamikhov-Center 2020-08-19 22:44:58 -07:00
parent f3bde175a1
commit fe17eb40aa
3 changed files with 40 additions and 15 deletions

View File

@ -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

View File

@ -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:
//------------------------------------------------------------------------------

View File

@ -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);