::get_image_size now returns virtual size, added ::has_relocations.

This commit is contained in:
Can Bölük 2020-08-29 09:54:56 +02:00
parent ab1995ceb0
commit 0dd7d78888
3 changed files with 31 additions and 6 deletions

View file

@ -169,13 +169,17 @@ namespace vtil
//
virtual uint64_t get_image_base() const = 0;
// Returns the image size.
//
virtual size_t get_image_size() const = 0;
// Returns the entry point's RVA if relevant, else nullopt.
//
virtual std::optional<uint64_t> get_entry_point() const = 0;
// Returns the image size and the raw byte array.
//
virtual size_t get_image_size() const = 0;
virtual size_t size() const = 0;
virtual void* data() = 0;
virtual const void* cdata() const = 0;
@ -206,13 +210,13 @@ namespace vtil
// Returns an enumeratable section list.
//
auto enum_sections() const { return make_range<section_iterator>( { this, 0 }, { this, get_section_count() } ); }
auto sections() const { return make_range<section_iterator>( { this, 0 }, { this, get_section_count() } ); }
// Returns the section associated with the given relative virtual address.
//
section_descriptor rva_to_section( uint64_t rva ) const
{
for ( auto scn : enum_sections() )
for ( auto scn : sections() )
if ( scn.virtual_address <= rva && rva < ( scn.virtual_address + scn.virtual_size ) )
return scn;
return {};
@ -243,12 +247,21 @@ namespace vtil
//
void enum_executable( const function_view<bool( const section_descriptor& )>& fn ) const
{
for ( auto scn : enum_sections() )
for ( auto scn : sections() )
if ( scn.execute && scn.physical_size && scn.virtual_size )
if ( fn( scn ) )
return;
}
// Returns whether the image has any relocations.
//
bool has_relocations() const
{
bool result = false;
enum_relocations( [ & ] ( auto&& ) { result = true; return true; } );
return result;
}
// Cast to bool redirects to ::is_valid.
//
explicit operator bool() const { return is_valid(); }

View file

@ -626,6 +626,17 @@ namespace vtil
return dos_header->get_nt_headers<false>()->optional_header.image_base;
}
size_t pe_image::get_image_size() const
{
// Get the image base from optional header.
//
auto dos_header = ( const dos_header_t* ) cdata();
if ( is_pe64() )
return dos_header->get_nt_headers<true>()->optional_header.size_image;
else
return dos_header->get_nt_headers<false>()->optional_header.size_image;
}
std::optional<uint64_t> pe_image::get_entry_point() const
{
// Get the entry point from optional header, return nullopt if zero.
@ -643,7 +654,7 @@ namespace vtil
// Get image boundaries and the dos header.
//
const void* data = cdata();
const void* data_limit = ( char* ) cdata() + get_image_size();
const void* data_limit = ( char* ) cdata() + size();
auto dos_header = ( const dos_header_t* ) cdata();
// Validate DOS header.

View file

@ -56,8 +56,9 @@ namespace vtil
virtual void add_section( section_descriptor& in_out, const void* data, size_t size ) override;
virtual void enum_relocations( const function_view<bool( const relocation_descriptor& )>& fn ) const override;
virtual uint64_t get_image_base() const override;
virtual size_t get_image_size() const override;
virtual std::optional<uint64_t> get_entry_point() const override;
virtual size_t get_image_size() const override { return raw_bytes.size(); }
virtual size_t size() const override { return raw_bytes.size(); }
virtual void* data() override { return raw_bytes.data(); }
virtual const void* cdata() const override { return raw_bytes.data(); }
virtual bool is_valid() const override;