diff --git a/api/python/ELF/objects/pyBinary.cpp b/api/python/ELF/objects/pyBinary.cpp index 09a0371..27eaef8 100644 --- a/api/python/ELF/objects/pyBinary.cpp +++ b/api/python/ELF/objects/pyBinary.cpp @@ -270,6 +270,16 @@ void create<Binary>(py::module& m) { "Check if a section with the given name exists in the binary", "section_name"_a) + .def("has_section_with_offset", + &Binary::has_section_with_offset, + "Check if a section that handle the given offset exists", + "offset"_a) + + .def("has_section_with_va", + &Binary::has_section_with_va, + "Check if a section that handle the given virtual address exists", + "va"_a) + .def("get_section", static_cast<no_const_func<Section&, const std::string&>>(&Binary::get_section), "Return the " RST_CLASS_REF(lief.ELF.Section) " with the given ``name``", diff --git a/include/LIEF/ELF/Binary.hpp b/include/LIEF/ELF/Binary.hpp index d381a33..378046d 100644 --- a/include/LIEF/ELF/Binary.hpp +++ b/include/LIEF/ELF/Binary.hpp @@ -205,6 +205,12 @@ class LIEF_API Binary : public LIEF::Binary { //! @brief Check if a section with the given name exists in the binary bool has_section(const std::string& name) const; + //! Check if a section that handle the given offset exists + bool has_section_with_offset(uint64_t offset) const; + + //! Check if a section that handle the given virtual address exists + bool has_section_with_va(uint64_t va) const; + //! @brief Return Section with the given `name` Section& get_section(const std::string& name); const Section& get_section(const std::string& name) const; diff --git a/src/ELF/Binary.cpp b/src/ELF/Binary.cpp index d367e1e..e8a6c34 100644 --- a/src/ELF/Binary.cpp +++ b/src/ELF/Binary.cpp @@ -1528,6 +1528,34 @@ bool Binary::has_section(const std::string& name) const { }) != std::end(this->sections_); } +bool Binary::has_section_with_offset(uint64_t offset) const { + auto&& it_section = std::find_if( + this->sections_.cbegin(), + this->sections_.cend(), + [&offset] (const Section* section) { + if (section == nullptr) { + return false; + } + return ((section->offset() <= offset) and + (section->offset() + section->size()) > offset); + }); + return it_section != this->sections_.cend(); +} + +bool Binary::has_section_with_va(uint64_t va) const { + auto&& it_section = std::find_if( + this->sections_.cbegin(), + this->sections_.cend(), + [&va] (const Section* section) { + if (section == nullptr) { + return false; + } + return ((section->virtual_address() <= va) and + (section->virtual_address() + section->size()) > va); + }); + return it_section != this->sections_.cend(); +} + void Binary::strip(void) { this->static_symbols_ = {}; diff --git a/tests/elf/elf_test.py b/tests/elf/elf_test.py index 965ae8b..6da073d 100644 --- a/tests/elf/elf_test.py +++ b/tests/elf/elf_test.py @@ -265,6 +265,19 @@ class TestELF(TestCase): self.assertEqual(functions[-1].name, "_fini") + def test_misc(self): + sample = "ELF/ELF64_x86-64_binary_ld.bin" + ld = lief.parse(get_sample(sample)) + + text = ld.get_section(".text") + + self.assertFalse(ld.has_section_with_offset(0)) + self.assertFalse(ld.has_section_with_va(0xFFFFFFFF)) + + self.assertTrue(ld.has_section_with_offset(text.offset + 10)) + self.assertTrue(ld.has_section_with_va(text.virtual_address + 10)) + + if __name__ == '__main__': root_logger = logging.getLogger()