Add a callback to arrange_local_symbols()

This commit is contained in:
Serge Lamikhov-Center 2020-08-19 07:28:47 -07:00
parent 9273958309
commit 5a5ba2dc09
2 changed files with 30 additions and 9 deletions

View File

@ -216,15 +216,15 @@ class symbol_section_accessor_template
//------------------------------------------------------------------------------
Elf_Xword
arrange_local_symbols()
arrange_local_symbols(std::function<void(Elf_Xword first, Elf_Xword second)> func = nullptr)
{
int nRet = 0;
if (elf_file.get_class() == ELFCLASS32) {
nRet = generic_arrange_local_symbols<Elf32_Sym>();
nRet = generic_arrange_local_symbols<Elf32_Sym>(func);
}
else {
nRet = generic_arrange_local_symbols<Elf64_Sym>();
nRet = generic_arrange_local_symbols<Elf64_Sym>(func);
}
return nRet;
@ -364,12 +364,12 @@ class symbol_section_accessor_template
//------------------------------------------------------------------------------
template <class T>
Elf_Xword
generic_arrange_local_symbols()
generic_arrange_local_symbols(std::function<void (Elf_Xword first, Elf_Xword second)> func)
{
const endianess_convertor &convertor = elf_file.get_convertor();
const Elf_Xword size = symbol_section->get_entry_size();
Elf_Xword first_not_local = 1; // Skip the first entry
Elf_Xword first_not_local = 1; // Skip the first entry. It is always NOTYPE
Elf_Xword current = 0;
Elf_Xword count = get_symbols_num();
@ -397,6 +397,9 @@ class symbol_section_accessor_template
if (first_not_local < count && current < count)
{
if (func)
func(first_not_local, current);
// Swap the symbols
T tmp;
memcpy(&tmp, p1, size);

View File

@ -617,8 +617,8 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
sym_sec->set_entry_size(writer.get_default_entry_size(SHT_SYMTAB));
symbol_section_accessor symbols(writer, sym_sec);
name = "Str1";
bind = STB_GLOBAL;
name = "Str1";
bind = STB_GLOBAL;
value = 1;
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
name = "Str2";
@ -650,11 +650,27 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
value = 8;
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
symbols.arrange_local_symbols();
symbols.arrange_local_symbols([symbols](Elf_Xword first, Elf_Xword second) -> void {
static int counter = 0;
BOOST_CHECK_EQUAL(first, ++counter);
// std::string name = "";
// ELFIO::Elf64_Addr value = 0;
// ELFIO::Elf_Xword size = 0;
// unsigned char bind = STB_LOCAL;
// unsigned char type = STT_FUNC;
// ELFIO::Elf_Half section_index = 0;
// unsigned char other = 0;
// std::cout << first << " " << second << std::endl;
// symbols.get_symbol(first, name, value, size, bind, type, section_index, other);
// std::cout << " " << name;
// symbols.get_symbol(second, name, value, size, bind, type, section_index, other);
// std::cout << " " << name << std::endl;
});
BOOST_REQUIRE_EQUAL(writer.save(file_name), true);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
elfio reader;
BOOST_REQUIRE_EQUAL(reader.load(file_name), true);
@ -675,6 +691,7 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
rsymbols.get_symbol(i, name, value, size, bind, type, section_index, other);
BOOST_CHECK_EQUAL(bind, (unsigned char)STB_LOCAL);
}
BOOST_CHECK_EQUAL(name, "Str7");
// Check that all symbols are not LOCAL after the bound value
for (Elf_Word i = bound; i < num; i++)
@ -683,4 +700,5 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
BOOST_CHECK_NE(bind, (unsigned char)STB_LOCAL);
}
BOOST_CHECK_EQUAL(name, "Str8");
}