Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ELF dtor functions
  • Loading branch information
romainthomas committed Jun 28, 2018
1 parent 31a4b19 commit 957384c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 33 deletions.
5 changes: 5 additions & 0 deletions api/python/ELF/objects/pyBinary.cpp
Expand Up @@ -477,6 +477,11 @@ void create<Binary>(py::module& m) {
py::return_value_policy::reference)


.def_property_readonly("dtor_functions",
&Binary::dtor_functions,
"Destuctor functions that are called the main execution")



.def(py::self += Segment())
.def(py::self += Section())
Expand Down
4 changes: 2 additions & 2 deletions include/LIEF/Abstract/Binary.hpp
Expand Up @@ -44,7 +44,7 @@ class LIEF_API Binary : public Object {
VA = 2, ///< Absolute
};

using ctor_t = std::vector<uint64_t>;
using functions_t = std::vector<uint64_t>;

public:
Binary(void);
Expand Down Expand Up @@ -140,7 +140,7 @@ class LIEF_API Binary : public Object {
virtual bool has_nx(void) const = 0;

//! Constructor functions that are called prior any other functions
virtual LIEF::Binary::ctor_t ctor_functions(void) const = 0;
virtual LIEF::Binary::functions_t ctor_functions(void) const = 0;

virtual std::ostream& print(std::ostream& os) const;

Expand Down
5 changes: 3 additions & 2 deletions include/LIEF/ELF/Binary.hpp
Expand Up @@ -460,7 +460,8 @@ class LIEF_API Binary : public LIEF::Binary {
//! to ``true``
void permute_dynamic_symbols(const std::vector<size_t>& permutation);

virtual LIEF::Binary::ctor_t ctor_functions(void) const override;
virtual LIEF::Binary::functions_t ctor_functions(void) const override;
LIEF::Binary::functions_t dtor_functions(void) const;

//! @brief ``true`` if the binary embed notes
bool has_notes(void) const;
Expand Down Expand Up @@ -540,7 +541,7 @@ class LIEF_API Binary : public LIEF::Binary {
Section& add_section(const Section& section);
symbols_t static_dyn_symbols(void) const;

LIEF::Binary::ctor_t ctor_functions(DYNAMIC_TAGS tag) const;
LIEF::Binary::functions_t tor_functions(DYNAMIC_TAGS tag) const;

//! The binary type
//! (i.e. `ELF32` or `ELF64`)
Expand Down
2 changes: 1 addition & 1 deletion include/LIEF/MachO/Binary.hpp
Expand Up @@ -416,7 +416,7 @@ class LIEF_API Binary : public LIEF::Binary {
LoadCommand& operator[](LOAD_COMMAND_TYPES type);
const LoadCommand& operator[](LOAD_COMMAND_TYPES type) const;

virtual LIEF::Binary::ctor_t ctor_functions(void) const override;
virtual LIEF::Binary::functions_t ctor_functions(void) const override;

private:
//! Default constructor
Expand Down
2 changes: 1 addition & 1 deletion include/LIEF/PE/Binary.hpp
Expand Up @@ -371,7 +371,7 @@ class LIEF_API Binary : public LIEF::Binary {
//! @brief Check if the binary uses ``NX`` protection
virtual bool has_nx(void) const override;

virtual LIEF::Binary::ctor_t ctor_functions(void) const override;
virtual LIEF::Binary::functions_t ctor_functions(void) const override;

bool operator==(const Binary& rhs) const;
bool operator!=(const Binary& rhs) const;
Expand Down
56 changes: 33 additions & 23 deletions src/ELF/Binary.cpp
Expand Up @@ -2084,42 +2084,34 @@ bool Binary::has_library(const std::string& name) const {
}


LIEF::Binary::ctor_t Binary::ctor_functions(DYNAMIC_TAGS tag) const {
LIEF::Binary::ctor_t functions;
LIEF::Binary::functions_t Binary::tor_functions(DYNAMIC_TAGS tag) const {
LIEF::Binary::functions_t functions;
if (this->has(tag)) {
const DynamicEntryArray::array_t& array = this->get(tag).as<DynamicEntryArray>()->array();

//const uint64_t address = this->get(tag).value();
//for (size_t i = 0; i < array.size(); ++i) {
// const uint64_t entry_addr = address + i * sizeof(uint64_t);
// std::cout << std::hex << entry_addr << std::endl;
// const Relocation* relocation = this->get_relocation(entry_addr);
// if (relocation != nullptr and relocation->addend() > 0) {
// functions.push_back(entry_addr + relocation->addend());
// } else {
// functions.push_back(array[i]);
// }
//}
std::move(
std::begin(array),
std::end(array),
std::back_inserter(functions));

functions.reserve(array.size());
for (uint64_t x : array) {
if (x != 0 and
static_cast<uint32_t>(x) != static_cast<uint32_t>(-1) and
x != static_cast<uint64_t>(-1)
) {
functions.push_back(x);
}
}
}
return functions;
}

// Ctor
LIEF::Binary::ctor_t Binary::ctor_functions(void) const {
LIEF::Binary::ctor_t functions;
LIEF::Binary::functions_t Binary::ctor_functions(void) const {
LIEF::Binary::functions_t functions;

LIEF::Binary::ctor_t init = this->ctor_functions(DYNAMIC_TAGS::DT_INIT_ARRAY);
LIEF::Binary::functions_t init = this->tor_functions(DYNAMIC_TAGS::DT_INIT_ARRAY);
std::move(
std::begin(init),
std::end(init),
std::back_inserter(functions));

LIEF::Binary::ctor_t preinit = this->ctor_functions(DYNAMIC_TAGS::DT_PREINIT_ARRAY);
LIEF::Binary::functions_t preinit = this->tor_functions(DYNAMIC_TAGS::DT_PREINIT_ARRAY);
std::move(
std::begin(preinit),
std::end(preinit),
Expand All @@ -2132,6 +2124,24 @@ LIEF::Binary::ctor_t Binary::ctor_functions(void) const {
}


LIEF::Binary::functions_t Binary::dtor_functions(void) const {

LIEF::Binary::functions_t functions;

LIEF::Binary::functions_t fini = this->tor_functions(DYNAMIC_TAGS::DT_FINI_ARRAY);
std::move(
std::begin(fini),
std::end(fini),
std::back_inserter(functions));

if (this->has(DYNAMIC_TAGS::DT_FINI)) {
functions.push_back(this->get(DYNAMIC_TAGS::DT_FINI).value());
}
return functions;

}


const Relocation* Binary::get_relocation(uint64_t address) const {
auto&& it = std::find_if(
std::begin(this->relocations_),
Expand Down
4 changes: 2 additions & 2 deletions src/MachO/Binary.cpp
Expand Up @@ -1371,8 +1371,8 @@ LIEF::Header Binary::get_abstract_header(void) const {
}


LIEF::Binary::ctor_t Binary::ctor_functions(void) const {
LIEF::Binary::ctor_t functions;
LIEF::Binary::functions_t Binary::ctor_functions(void) const {
LIEF::Binary::functions_t functions;
for (const Section& section : this->sections()) {
if (section.type() != MACHO_SECTION_TYPES::S_MOD_INIT_FUNC_POINTERS) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/PE/Binary.cpp
Expand Up @@ -1192,8 +1192,8 @@ const ResourcesManager Binary::resources_manager(void) const {
}


LIEF::Binary::ctor_t Binary::ctor_functions(void) const {
LIEF::Binary::ctor_t functions;
LIEF::Binary::functions_t Binary::ctor_functions(void) const {
LIEF::Binary::functions_t functions;

if (this->has_tls()) {
const std::vector<uint64_t>& clbs = this->tls().callbacks();
Expand Down

0 comments on commit 957384c

Please sign in to comment.