Skip to content

Commit 0c2e3e1

Browse files
committed
updated pe parsing, split operators, update docs
1 parent 81144e5 commit 0c2e3e1

5 files changed

Lines changed: 622 additions & 598 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<p align="center">
22

3-
<a href="https://www.vtil.org/">
4-
<img width="256" heigth="256" src="https://vtil.org/logo.png">
3+
<a href="https://www.vtil.cc/">
4+
<img width="256" heigth="256" src="https://vtil.cc/assets/media/logo.png">
55
</a>
66

77
<h1 align="center">VTIL</h1>
@@ -39,7 +39,7 @@ This repository contains the core components of the VTIL Project used across the
3939

4040
It is currently incomplete as the initial release is not done yet, and documentation and FAQ will be within this repository and the organization website once they're done.
4141

42-
Until the initial release, you can keep up to date with the VTIL project by checking my [personal twitter account](https://twitter.com/_can1357) or the VTIL website [vtil.org](https://vtil.org/).
42+
Until the initial release, you can keep up to date with the VTIL project by checking my [personal twitter account](https://twitter.com/_can1357) or the VTIL website [vtil.cc](https://vtil.cc/).
4343

4444
## Building (Windows)
4545

VTIL-Architecture/arch/register_desc.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636

3737
namespace vtil
3838
{
39+
// Forward declarations: register_desc and register_cast are mutually
40+
// referencing the converting constructor in register_desc calls
41+
// register_cast<T>, and register_cast returns register_desc
42+
//
43+
struct register_desc;
44+
45+
template<typename T>
46+
struct register_cast;
47+
3948
// Flags that describe the properties of the register.
4049
//
4150
enum register_flag : uint32_t
@@ -339,9 +348,9 @@ namespace vtil
339348
REDUCE_TO( bit_count, ( uint64_t(architecture) << 56 ) | local_id, flags, bit_offset );
340349
};
341350

342-
// Should be overriden by the user to describe conversion of the
343-
// register type they use (e.g. x86_reg for Capstone/Keystone) into
344-
// VTIL register descriptors for seamless casting into vtil::operand type.
351+
// Primary template of register_cast triggers a static_assert for unsupported types.
352+
// Specialize this for your register enum (e.g. x86_reg, arm64_reg) to enable
353+
// seamless casting into vtil::operand.
345354
//
346355
template<typename T>
347356
struct register_cast
@@ -352,7 +361,10 @@ namespace vtil
352361
return {};
353362
}
354363
};
355-
template<>
364+
365+
// Specializations for known register types.
366+
//
367+
template<>
356368
struct register_cast<register_desc>
357369
{
358370
constexpr register_desc operator()( register_desc v ) { return v; }

VTIL-Common/formats/winpe.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ namespace vtil
104104

105105
// Directory indices
106106
//
107-
enum directory_id
107+
enum class directory_id
108108
{
109109
directory_entry_export = 0, // Export Directory
110110
directory_entry_import = 1, // Import Directory
@@ -396,7 +396,7 @@ namespace vtil
396396
uint32_t num_data_directories;
397397
data_directories_x86_t data_directories;
398398

399-
bool has_directory( directory_id id ) const { return has_directory( &data_directories.entries[ id ] ); }
399+
bool has_directory( directory_id id ) const { return has_directory( &data_directories.entries[ static_cast<size_t>( id ) ] ); }
400400
bool has_directory( const data_directory_t* dir ) const { return &data_directories.entries[ num_data_directories ] < dir && dir->present(); }
401401
};
402402
template<bool x64>
@@ -435,8 +435,8 @@ namespace vtil
435435
file_header_t file_header;
436436
optional_header_t<x64> optional_header;
437437

438-
auto get_sections() { return ( section_header_t* ) ( ( char* ) &optional_header + file_header.size_optional_header ); }
439-
auto get_sections() const { return ( const section_header_t* ) ( ( char* ) &optional_header + file_header.size_optional_header ); }
438+
auto get_sections() { return reinterpret_cast<section_header_t*>( reinterpret_cast<char*>( &optional_header ) + file_header.size_optional_header ); }
439+
auto get_sections() const { return reinterpret_cast<const section_header_t*>( reinterpret_cast<const char*>( &optional_header ) + file_header.size_optional_header ); }
440440
auto get_section( size_t n ) { return get_sections() + n; }
441441
auto get_section( size_t n ) const { return get_sections() + n; }
442442
};
@@ -467,11 +467,11 @@ namespace vtil
467467
uint16_t e_res2[ 10 ];
468468
uint32_t e_lfanew;
469469

470-
template<bool x64> auto get_nt_headers() { return ( nt_headers_t<x64>* ) ( ( char* ) this + e_lfanew ); }
471-
template<bool x64> auto get_nt_headers() const { return ( const nt_headers_t<x64>* ) ( ( char* ) this + e_lfanew ); }
470+
template<bool x64> auto get_nt_headers() { return reinterpret_cast<nt_headers_t<x64>*>( reinterpret_cast<char*>( this ) + e_lfanew ); }
471+
template<bool x64> auto get_nt_headers() const { return reinterpret_cast<const nt_headers_t<x64>*>( reinterpret_cast<const char*>( this ) + e_lfanew ); }
472472
};
473473

474-
enum reloc_type_id
474+
enum class reloc_type_id : uint16_t
475475
{
476476
rel_based_absolute = 0,
477477
rel_based_high = 1,
@@ -494,9 +494,9 @@ namespace vtil
494494
uint32_t size_block;
495495
reloc_entry_t entries[ 1 ]; // Variable length array
496496

497-
auto get_next() { return ( reloc_block_t* ) ( ( char* ) this + this->size_block ); }
498-
auto get_next() const { return ( const reloc_block_t* ) ( ( char* ) this + this->size_block ); }
499-
size_t num_entries() const { return ( reloc_entry_t* ) get_next() - &entries[ 0 ]; }
497+
auto get_next() { return reinterpret_cast<reloc_block_t*>( reinterpret_cast<char*>( this ) + this->size_block ); }
498+
auto get_next() const { return reinterpret_cast<const reloc_block_t*>( reinterpret_cast<const char*>( this ) + this->size_block ); }
499+
size_t num_entries() const { return reinterpret_cast<const reloc_entry_t*>( get_next() ) - &entries[ 0 ]; }
500500
};
501501

502502
struct reloc_directory_t
@@ -511,18 +511,18 @@ namespace vtil
511511
template<typename S, typename T>
512512
static decltype( auto ) visit_nt( S* self, T&& fn )
513513
{
514-
auto dos_header = ( dos_header_t* ) self->cdata();
514+
auto dos_header = reinterpret_cast<dos_header_t*>( const_cast<void*>( self->cdata() ) );
515515
auto* nt_hdrs = dos_header->get_nt_headers<true>();
516516

517517
if( nt_hdrs->optional_header.magic == OPT_HDR64_MAGIC )
518-
return fn( carry_const( self, ( nt_headers_x64_t* ) nt_hdrs ) );
518+
return fn( carry_const( self, reinterpret_cast<nt_headers_x64_t*>( nt_hdrs ) ) );
519519
else
520-
return fn( carry_const( self, ( nt_headers_x86_t* ) nt_hdrs ) );
520+
return fn( carry_const( self, reinterpret_cast<nt_headers_x86_t*>( nt_hdrs ) ) );
521521
}
522522

523523
bool pe_image::is_pe64() const
524524
{
525-
auto dos_header = ( const dos_header_t* ) cdata();
525+
auto dos_header = reinterpret_cast<const dos_header_t*>( cdata() );
526526
return dos_header->get_nt_headers<true>()->optional_header.magic == OPT_HDR64_MAGIC;
527527
}
528528
uintptr_t pe_image::get_alignment_mask() const
@@ -541,15 +541,15 @@ namespace vtil
541541
{
542542
// Get the section count from file header.
543543
//
544-
auto dos_header = ( const dos_header_t* ) cdata();
544+
auto dos_header = reinterpret_cast<const dos_header_t*>( cdata() );
545545
return dos_header->get_nt_headers<true>()->file_header.num_sections;
546546
}
547547

548548
section_descriptor pe_image::get_section( size_t index ) const
549549
{
550550
// Get the NT headers.
551551
//
552-
auto dos_header = ( const dos_header_t* ) cdata();
552+
auto dos_header = reinterpret_cast<const dos_header_t*>( cdata() );
553553
auto nt_headers = dos_header->get_nt_headers<true>();
554554

555555
// Return invalid descriptor if out-of-boundaries.
@@ -577,7 +577,7 @@ namespace vtil
577577
{
578578
// Get the NT headers.
579579
//
580-
auto dos_header = ( dos_header_t* ) cdata();
580+
auto dos_header = reinterpret_cast<dos_header_t*>( data() );
581581
auto nt_headers = dos_header->get_nt_headers<true>();
582582

583583
// Fill section descriptor and return.
@@ -659,8 +659,8 @@ namespace vtil
659659
// Get image boundaries and the dos header.
660660
//
661661
const void* data = cdata();
662-
const void* data_limit = ( char* ) cdata() + size();
663-
auto dos_header = ( const dos_header_t* ) cdata();
662+
const void* data_limit = reinterpret_cast<const char*>( cdata() ) + size();
663+
auto dos_header = reinterpret_cast<const dos_header_t*>( cdata() );
664664

665665
// Validate DOS header.
666666
//
@@ -669,7 +669,7 @@ namespace vtil
669669

670670
// Validate image size.
671671
//
672-
if ( ( ( const char* ) data + dos_header->e_lfanew + std::min( sizeof( nt_headers_x64_t ), sizeof( nt_headers_x86_t ) ) ) > data_limit )
672+
if ( ( reinterpret_cast<const char*>( data ) + dos_header->e_lfanew + std::min( sizeof( nt_headers_x64_t ), sizeof( nt_headers_x86_t ) ) ) > data_limit )
673673
return false;
674674

675675
// Validate NT Magic.
@@ -717,7 +717,7 @@ namespace vtil
717717

718718
// Append a section and write the characteristics.
719719
//
720-
auto nt_hdrs = ( ( dos_header_t* ) this->data() )->get_nt_headers<true>();
720+
auto nt_hdrs = reinterpret_cast<dos_header_t*>( this->data() )->get_nt_headers<true>();
721721
size_t index = nt_hdrs->file_header.num_sections++;
722722
auto scn = nt_hdrs->get_section( index );
723723
memset( scn, 0, sizeof( section_header_t ) );
@@ -740,7 +740,7 @@ namespace vtil
740740
// Get block boundaries
741741
//
742742
const auto* block_begin = &rva_to_ptr<reloc_directory_t>( reloc_dir.rva )->first_block;
743-
const auto* block_end = ( const reloc_block_t* ) ( ( char* ) block_begin + reloc_dir.size );
743+
const auto* block_end = reinterpret_cast<const reloc_block_t*>( reinterpret_cast<const char*>( block_begin ) + reloc_dir.size );
744744

745745
// For each block:
746746
//
@@ -756,25 +756,25 @@ namespace vtil
756756
.rva = uint64_t( block->base_rva ) + block->entries[ i ].offset
757757
};
758758

759-
switch ( block->entries[ i ].type )
759+
switch ( static_cast<reloc_type_id>( block->entries[ i ].type ) )
760760
{
761-
case rel_based_dir64:
762-
entry.length = 8;
763-
entry.relocator = [ ] ( void* data, int64_t delta ) { *( ( uint64_t* ) data ) += delta; };
761+
case reloc_type_id::rel_based_dir64:
762+
entry.length = 8;
763+
entry.relocator = [ ] ( void* data, int64_t delta ) { *static_cast<uint64_t*>( data ) += delta; };
764764
break;
765-
case rel_based_high_low:
765+
case reloc_type_id::rel_based_high_low:
766766
entry.length = 4;
767-
entry.relocator = [ ] ( void* data, int64_t delta ) { *( ( int32_t* ) data ) += math::narrow_cast<int32_t>( delta ); };
767+
entry.relocator = [ ] ( void* data, int64_t delta ) { *static_cast<int32_t*>( data ) += math::narrow_cast<int32_t>( delta ); };
768768
break;
769-
case rel_based_low:
769+
case reloc_type_id::rel_based_low:
770770
entry.length = 2;
771-
entry.relocator = [ ] ( void* data, int64_t delta ) { *( ( int16_t* ) data ) += ( int16_t ) ( ( uint16_t ) delta ); };
771+
entry.relocator = [ ] ( void* data, int64_t delta ) { *static_cast<int16_t*>( data ) += static_cast<int16_t>( static_cast<uint16_t>( delta ) ); };
772772
break;
773-
case rel_based_high:
773+
case reloc_type_id::rel_based_high:
774774
entry.length = 2;
775-
entry.relocator = [ ] ( void* data, int64_t delta ) { *( ( int16_t* ) data ) += ( int16_t ) ( ( ( uint32_t ) delta ) >> 16 ); };
775+
entry.relocator = [ ] ( void* data, int64_t delta ) { *static_cast<int16_t*>( data ) += static_cast<int16_t>( static_cast<uint32_t>( delta ) >> 16 ); };
776776
break;
777-
case rel_based_absolute:
777+
case reloc_type_id::rel_based_absolute:
778778
entry.length = 0;
779779
entry.relocator = [ ] ( void* data, int64_t delta ) { /*nop*/ };
780780
break;

0 commit comments

Comments
 (0)