Add API to check if a section associated with an Off/VA exists

This commit is contained in:
Romain Thomas 2019-05-28 09:47:33 +02:00
parent 8fb74bec60
commit fa3195b1bd
4 changed files with 57 additions and 0 deletions

View File

@ -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``",

View File

@ -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;

View File

@ -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_ = {};

View File

@ -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()