Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement ELF::Binary::get_symbols()
Fix: #70
  • Loading branch information
romainthomas committed Jul 31, 2017
1 parent 782295b commit af6ab65
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions api/python/ELF/objects/pyBinary.cpp
Expand Up @@ -66,6 +66,11 @@ void init_ELF_Binary_class(py::module& m) {
"Return an iterator to dynamic " RST_CLASS_REF(lief.ELF.Symbol) "",
py::return_value_policy::reference_internal)

.def_property_readonly("symbols",
static_cast<no_const_getter<it_symbols>>(&Binary::get_symbols),
"Return an iterator over both **static** and **dynamic** " RST_CLASS_REF(lief.ELF.Symbol) "",
py::return_value_policy::reference_internal)

.def_property_readonly("exported_symbols",
static_cast<no_const_getter<it_exported_symbols>>(&Binary::get_exported_symbols),
"Return dynamic " RST_CLASS_REF(lief.ELF.Symbol) " which are exported",
Expand Down
2 changes: 1 addition & 1 deletion include/LIEF/ELF/Binary.hpp
Expand Up @@ -185,7 +185,7 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
//! @brief Return ELF interprer if any. (e.g. `/lib64/ld-linux-x86-64.so.2`)
std::string get_interpreter(void) const;

//! @brief Return static symbols and dynamic symbols
//! @brief Return both static and dynamic symbols
it_symbols get_symbols(void);
it_const_symbols get_symbols(void) const;

Expand Down
4 changes: 2 additions & 2 deletions include/LIEF/ELF/type_traits.hpp
Expand Up @@ -46,8 +46,8 @@ using it_dynamic_entries = ref_iterator<dynamic_entries_t&
using it_const_dynamic_entries = const_ref_iterator<const dynamic_entries_t&>;

using symbols_t = std::vector<Symbol*>;
using it_symbols = ref_iterator<symbols_t&>;
using it_const_symbols = const_ref_iterator<const symbols_t&>;
using it_symbols = ref_iterator<symbols_t>;
using it_const_symbols = const_ref_iterator<symbols_t>;

using relocations_t = std::vector<Relocation*>;

Expand Down
39 changes: 35 additions & 4 deletions src/ELF/Binary.cpp
Expand Up @@ -151,22 +151,53 @@ it_const_dynamic_entries Binary::get_dynamic_entries(void) const {
// -------

it_symbols Binary::get_static_symbols(void) {
return it_symbols{std::ref(this->static_symbols_)};
return this->static_symbols_;
}

it_const_symbols Binary::get_static_symbols(void) const {
return it_const_symbols{std::cref(this->static_symbols_)};
return this->static_symbols_;
}

// Dynamics
// --------

it_symbols Binary::get_dynamic_symbols(void) {
return it_symbols{std::ref(this->dynamic_symbols_)};
return this->dynamic_symbols_;
}

it_const_symbols Binary::get_dynamic_symbols(void) const {
return it_const_symbols{std::cref(this->dynamic_symbols_)};
return this->dynamic_symbols_;
}


it_symbols Binary::get_symbols(void) {
symbols_t symbols;
symbols.reserve(this->get_dynamic_symbols().size() + this->get_static_symbols().size());
for (Symbol& s : this->get_dynamic_symbols()) {
symbols.push_back(&s);
}

for (Symbol& s : this->get_static_symbols()) {
symbols.push_back(&s);
}

return it_symbols{symbols};
}

it_const_symbols Binary::get_symbols(void) const {
symbols_t symbols;

symbols.reserve(this->get_dynamic_symbols().size() + this->get_static_symbols().size());

for (const Symbol& s : this->get_dynamic_symbols()) {
symbols.push_back(const_cast<Symbol*>(&s));
}

for (const Symbol& s : this->get_static_symbols()) {
symbols.push_back(const_cast<Symbol*>(&s));
}

return it_const_symbols{symbols};
}

// Exported
Expand Down
10 changes: 10 additions & 0 deletions tests/elf/elf_test.py
Expand Up @@ -137,6 +137,16 @@ def test_notes(self):

self.assertEqual("".join(map(chr, n3.description)), "gold 1.12")

def test_symbols_access(self):
hello = lief.parse(get_sample('ELF/ELF64_x86-64_binary_hello-gdb.bin'))

symbols = hello.symbols
dynamic_symbols = hello.dynamic_symbols
static_symbols = hello.static_symbols

self.assertTrue(all(s in symbols for s in dynamic_symbols))
self.assertTrue(all(s in symbols for s in static_symbols))

def test_relocation_size(self):
aarch64_toybox = lief.parse(get_sample('ELF/ELF64_AARCH64_piebinary_toybox.pie'))
arm_ls = lief.parse(get_sample('ELF/ELF32_ARM_binary_ls.bin'))
Expand Down

0 comments on commit af6ab65

Please sign in to comment.