From 957384cd361c4a485470f877658af2bf052dbe0a Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Thu, 28 Jun 2018 16:49:55 +0200 Subject: [PATCH] Add ELF dtor functions --- api/python/ELF/objects/pyBinary.cpp | 5 +++ include/LIEF/Abstract/Binary.hpp | 4 +-- include/LIEF/ELF/Binary.hpp | 5 +-- include/LIEF/MachO/Binary.hpp | 2 +- include/LIEF/PE/Binary.hpp | 2 +- src/ELF/Binary.cpp | 56 +++++++++++++++++------------ src/MachO/Binary.cpp | 4 +-- src/PE/Binary.cpp | 4 +-- 8 files changed, 49 insertions(+), 33 deletions(-) diff --git a/api/python/ELF/objects/pyBinary.cpp b/api/python/ELF/objects/pyBinary.cpp index 58d11d91..4ed55689 100644 --- a/api/python/ELF/objects/pyBinary.cpp +++ b/api/python/ELF/objects/pyBinary.cpp @@ -477,6 +477,11 @@ void create(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()) diff --git a/include/LIEF/Abstract/Binary.hpp b/include/LIEF/Abstract/Binary.hpp index 52926cdb..a9e5833e 100644 --- a/include/LIEF/Abstract/Binary.hpp +++ b/include/LIEF/Abstract/Binary.hpp @@ -44,7 +44,7 @@ class LIEF_API Binary : public Object { VA = 2, ///< Absolute }; - using ctor_t = std::vector; + using functions_t = std::vector; public: Binary(void); @@ -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; diff --git a/include/LIEF/ELF/Binary.hpp b/include/LIEF/ELF/Binary.hpp index a8560ed7..30b701c8 100644 --- a/include/LIEF/ELF/Binary.hpp +++ b/include/LIEF/ELF/Binary.hpp @@ -460,7 +460,8 @@ class LIEF_API Binary : public LIEF::Binary { //! to ``true`` void permute_dynamic_symbols(const std::vector& 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; @@ -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`) diff --git a/include/LIEF/MachO/Binary.hpp b/include/LIEF/MachO/Binary.hpp index ce7a1529..11810052 100644 --- a/include/LIEF/MachO/Binary.hpp +++ b/include/LIEF/MachO/Binary.hpp @@ -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 diff --git a/include/LIEF/PE/Binary.hpp b/include/LIEF/PE/Binary.hpp index 0d4b8db9..5ab5621b 100644 --- a/include/LIEF/PE/Binary.hpp +++ b/include/LIEF/PE/Binary.hpp @@ -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; diff --git a/src/ELF/Binary.cpp b/src/ELF/Binary.cpp index cf3816c0..82dc6056 100644 --- a/src/ELF/Binary.cpp +++ b/src/ELF/Binary.cpp @@ -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()->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(x) != static_cast(-1) and + x != static_cast(-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), @@ -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_), diff --git a/src/MachO/Binary.cpp b/src/MachO/Binary.cpp index e5ef5c55..2354edf1 100644 --- a/src/MachO/Binary.cpp +++ b/src/MachO/Binary.cpp @@ -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; diff --git a/src/PE/Binary.cpp b/src/PE/Binary.cpp index 416f69d2..36bb31e5 100644 --- a/src/PE/Binary.cpp +++ b/src/PE/Binary.cpp @@ -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& clbs = this->tls().callbacks();