Skip to content

Commit

Permalink
Fix some ambiguous API (fix #27)
Browse files Browse the repository at this point in the history
PE::Binary::get_content_from_virtual_address can now take either:
  Absolute Virtual Address / Relative Virtual Address

In the PE Python API:
  * Merge 'PE.parse_from_raw' and 'PE.parse'
  * Change 'PE.Binary.section_from_virtual_address' to 'PE.Binary.section_from_rva'

In the PE C++ API:
  * Change 'Binary::section_from_virtual_address' to 'Binary::section_from_rva'
  • Loading branch information
romainthomas committed Jun 25, 2017
1 parent 720164a commit f4372d0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
11 changes: 6 additions & 5 deletions api/python/ELF/objects/pyParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ void init_ELF_Parser_class(py::module& m) {
m.def("parse",
static_cast<Binary* (*) (const std::string&, DYNSYM_COUNT_METHODS)>(&Parser::parse),
"Parse the given binary and return a " RST_CLASS_REF(lief.ELF.Binary) " object\n\n"
"For *weird* binaries (e.g sectionless) you can choose which method use to count dynamic symbols "
"For *weird* binaries (e.g sectionless) you can choose the method to use to count dynamic symbols "
" (" RST_CLASS_REF(lief.ELF.DYNSYM_COUNT_METHODS) ")",
"filename"_a, py::arg("dynsym_count_method") = DYNSYM_COUNT_METHODS::COUNT_AUTO,
py::return_value_policy::take_ownership);


m.def("parse_from_raw",
m.def("parse",
static_cast<Binary* (*) (const std::vector<uint8_t>&, const std::string&, DYNSYM_COUNT_METHODS)>(&Parser::parse),
"Parse the given raw data and return a " RST_CLASS_REF(lief.ELF.Binary) " object",
py::arg("raw"), py::arg("name") = "", py::arg("dynsym_count_method") = DYNSYM_COUNT_METHODS::COUNT_AUTO,
"Parse the given binary and return a " RST_CLASS_REF(lief.ELF.Binary) " object\n\n"
"For *weird* binaries (e.g sectionless) you can choose the method to use to count dynamic symbols "
" (" RST_CLASS_REF(lief.ELF.DYNSYM_COUNT_METHODS) ")",
"raw"_a, py::arg("name") = "", py::arg("dynsym_count_method") = DYNSYM_COUNT_METHODS::COUNT_AUTO,
py::return_value_policy::take_ownership);
}
4 changes: 2 additions & 2 deletions api/python/PE/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ void init_PE_Binary_class(py::module& m) {
"offset"_a,
py::return_value_policy::reference)

.def("section_from_virtual_address",
static_cast<Section& (Binary::*)(uint64_t)>(&Binary::section_from_virtual_address),
.def("section_from_rva",
static_cast<Section& (Binary::*)(uint64_t)>(&Binary::section_from_rva),
"Return the " RST_CLASS_REF(lief.PE.Section) " which contains the **relative** virtual address",
py::return_value_policy::reference)

Expand Down
6 changes: 2 additions & 4 deletions api/python/PE/objects/pyParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@

void init_PE_Parser_class(py::module& m) {

// Parser (Parser)
m.def("parse",
static_cast<Binary* (*) (const std::string&)>(&Parser::parse),
"Parse the given binary and return a " RST_CLASS_REF(lief.PE.Binary) " object",
py::arg("filename"),
py::return_value_policy::take_ownership);


m.def("parse_from_raw",
m.def("parse",
static_cast<Binary* (*) (const std::vector<uint8_t>&, const std::string&)>(&Parser::parse),
"Parse the given raw data and return a " RST_CLASS_REF(lief.PE.Binary) " object",
"Parse the given binary and return a " RST_CLASS_REF(lief.PE.Binary) " object",
py::arg("raw"), py::arg("name") = "",
py::return_value_policy::take_ownership);
}
1 change: 0 additions & 1 deletion doc/sphinx/api/python/elf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Parser
*******

.. autofunction:: lief.ELF.parse
.. autofunction:: lief.ELF.parse_from_raw

----------

Expand Down
1 change: 0 additions & 1 deletion doc/sphinx/api/python/pe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Parser
*******

.. autofunction:: lief.PE.parse
.. autofunction:: lief.PE.parse_from_raw


Binary
Expand Down
4 changes: 2 additions & 2 deletions include/LIEF/PE/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
const Section& section_from_offset(uint64_t offset) const;

//! @brief Find the section associated with the `virtual address`
Section& section_from_virtual_address(uint64_t virtual_address);
const Section& section_from_virtual_address(uint64_t virtual_address) const;
Section& section_from_rva(uint64_t virtual_address);
const Section& section_from_rva(uint64_t virtual_address) const;

//! @brief Return binary's sections
it_sections get_sections(void);
Expand Down
19 changes: 12 additions & 7 deletions src/PE/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Section& Binary::section_from_offset(uint64_t offset) {
}


const Section& Binary::section_from_virtual_address(uint64_t virtual_address) const {
const Section& Binary::section_from_rva(uint64_t virtual_address) const {
auto&& it_section = std::find_if(
std::begin(this->sections_),
std::end(this->sections_),
Expand All @@ -248,8 +248,8 @@ const Section& Binary::section_from_virtual_address(uint64_t virtual_address) co
return **it_section;
}

Section& Binary::section_from_virtual_address(uint64_t virtual_address) {
return const_cast<Section&>(static_cast<const Binary*>(this)->section_from_virtual_address(virtual_address));
Section& Binary::section_from_rva(uint64_t virtual_address) {
return const_cast<Section&>(static_cast<const Binary*>(this)->section_from_rva(virtual_address));
}


Expand Down Expand Up @@ -961,7 +961,7 @@ uint64_t Binary::entrypoint(void) const {

void Binary::patch_address(uint64_t address, const std::vector<uint8_t>& patch_value) {
// Find the section associated with the virtual address
Section& section_topatch = this->section_from_virtual_address(address);
Section& section_topatch = this->section_from_rva(address);
const uint64_t offset = address - section_topatch.virtual_address();
std::vector<uint8_t> content = section_topatch.content();
std::copy(
Expand All @@ -977,7 +977,7 @@ void Binary::patch_address(uint64_t address, uint64_t patch_value, size_t size)
throw std::runtime_error("Invalid size (" + std::to_string(size) + ")");
}

Section& section_topatch = this->section_from_virtual_address(address);
Section& section_topatch = this->section_from_rva(address);
const uint64_t offset = address - section_topatch.virtual_address();
std::vector<uint8_t> content = section_topatch.content();

Expand All @@ -990,9 +990,14 @@ void Binary::patch_address(uint64_t address, uint64_t patch_value, size_t size)
}

std::vector<uint8_t> Binary::get_content_from_virtual_address(uint64_t virtual_address, uint64_t size) const {
const Section& section = this->section_from_virtual_address(virtual_address);
uint64_t rva = virtual_address;
const int64_t delta = virtual_address - this->optional_header().imagebase();
if (delta > 0) {
rva -= this->optional_header().imagebase();
}
const Section& section = this->section_from_rva(rva);
const std::vector<uint8_t>& content = section.content();
const uint64_t offset = virtual_address - section.virtual_address();
const uint64_t offset = rva - section.virtual_address();
uint64_t checked_size = size;
if ((offset + checked_size) > content.size()) {
checked_size = checked_size - (offset + checked_size - content.size());
Expand Down

0 comments on commit f4372d0

Please sign in to comment.