diff --git a/VTIL-Common/formats/image_descriptor.hpp b/VTIL-Common/formats/image_descriptor.hpp index dc2f45c..e7fa405 100644 --- a/VTIL-Common/formats/image_descriptor.hpp +++ b/VTIL-Common/formats/image_descriptor.hpp @@ -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 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( { this, 0 }, { this, get_section_count() } ); } + auto sections() const { return make_range( { 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& 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(); } diff --git a/VTIL-Common/formats/winpe.cpp b/VTIL-Common/formats/winpe.cpp index 991ccec..68ff8b0 100644 --- a/VTIL-Common/formats/winpe.cpp +++ b/VTIL-Common/formats/winpe.cpp @@ -626,6 +626,17 @@ namespace vtil return dos_header->get_nt_headers()->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()->optional_header.size_image; + else + return dos_header->get_nt_headers()->optional_header.size_image; + } + std::optional 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. diff --git a/VTIL-Common/formats/winpe.hpp b/VTIL-Common/formats/winpe.hpp index 9a1d3eb..8dde420 100644 --- a/VTIL-Common/formats/winpe.hpp +++ b/VTIL-Common/formats/winpe.hpp @@ -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& fn ) const override; virtual uint64_t get_image_base() const override; + virtual size_t get_image_size() const override; virtual std::optional 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;