Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add API to get symbol by name (abstract layer)
API Changes (Python/C++):
  LIEF::Binary::has_symbol
  LIEF::Binary::get_symbol

It works for ELF, PE, MachO
  • Loading branch information
romainthomas committed Aug 22, 2017
1 parent ae92f99 commit f121af5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
14 changes: 14 additions & 0 deletions api/python/Abstract/objects/pyBinary.cpp
Expand Up @@ -30,6 +30,9 @@ using setter_t = void (Binary::*)(T);
template<class T>
using it_t = T (Binary::*)(void);

template<class T, class P>
using no_const_func = T (Binary::*)(P);

void init_LIEF_Binary_class(py::module& m) {
py::class_<Binary>(m, "Binary")

Expand Down Expand Up @@ -80,6 +83,17 @@ void init_LIEF_Binary_class(py::module& m) {
"Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Symbol) "",
py::return_value_policy::reference_internal)

.def("has_symbol",
&Binary::has_symbol,
"Check if a " RST_CLASS_REF(lief.Symbol) " with the given name exists",
"symbol_name"_a)

.def("get_symbol",
static_cast<no_const_func<Symbol&, const std::string&>>(&Binary::get_symbol),
"Return the " RST_CLASS_REF(lief.Symbol) " with the given ``name``",
"symbol_name"_a,
py::return_value_policy::reference)

.def("get_function_address",
&Binary::get_function_address,
"Return the address of the given function name",
Expand Down
12 changes: 11 additions & 1 deletion include/LIEF/Abstract/Binary.hpp
Expand Up @@ -48,11 +48,19 @@ class DLL_PUBLIC Binary : public Visitable {
Header get_header(void) const;

//! @brief Return list of symbols whose elements **can** be modified
it_symbols get_symbols(void);
it_symbols get_symbols(void);

//! @brief Return list of symbols whose elements **can't** be modified
it_const_symbols get_symbols(void) const;

//! @brief Check if a Symbol with the given name exists
bool has_symbol(const std::string& name) const;

//! @brief Return the Symbol with the given name
const Symbol& get_symbol(const std::string& name) const;

Symbol& get_symbol(const std::string& name);

//! @brief Returns binary's sections
it_sections get_sections(void);
it_const_sections get_sections(void) const;
Expand Down Expand Up @@ -82,6 +90,8 @@ class DLL_PUBLIC Binary : public Visitable {
virtual void accept(Visitor& visitor) const override;




//! @brief Patch the content at virtual address @p address with @p patch_value
//!
//! @param[in] address Address to patch
Expand Down
34 changes: 34 additions & 0 deletions src/Abstract/Binary.cpp
Expand Up @@ -58,6 +58,40 @@ it_const_symbols Binary::get_symbols(void) const {
return it_const_symbols{const_cast<Binary*>(this)->get_abstract_symbols()};
}


bool Binary::has_symbol(const std::string& name) const {
symbols_t symbols = const_cast<Binary*>(this)->get_abstract_symbols();
auto&& it_symbol = std::find_if(
std::begin(symbols),
std::end(symbols),
[&name] (const Symbol* s) {
return s->name() == name;
});

return it_symbol != std::end(symbols);
}

const Symbol& Binary::get_symbol(const std::string& name) const {
if (not this->has_symbol(name)) {
throw not_found("Symbol '" + name + "' not found!");
}

symbols_t symbols = const_cast<Binary*>(this)->get_abstract_symbols();

auto&& it_symbol = std::find_if(
std::begin(symbols),
std::end(symbols),
[&name] (const Symbol* s) {
return s->name() == name;
});

return **it_symbol;
}

Symbol& Binary::get_symbol(const std::string& name) {
return const_cast<Symbol&>(static_cast<const Binary*>(this)->get_symbol(name));
}

it_sections Binary::get_sections(void) {
return it_sections{this->get_abstract_sections()};
}
Expand Down

0 comments on commit f121af5

Please sign in to comment.