| 1 | package FFI::ExtractSymbols::Windows; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use File::ShareDir::Dist (); | ||
| 6 | use File::Which qw( which ); | ||
| 7 | use Path::Tiny; | ||
| 8 | |||
| 9 | my $config = File::ShareDir::Dist::dist_config('FFI-ExtractSymbols'); | ||
| 10 | |||
| 11 | # ABSTRACT: Windows (and Cygwin) implementation for FFI::ExtractSymbols | ||
| 12 | our $VERSION = '0.07'; # VERSION | ||
| 13 | |||
| 14 | |||
| 15 | return 1 if FFI::ExtractSymbols->can('extract_symbols') || $^O !~ /^(MSWin32|cygwin)$/; | ||
| 16 | |||
| 17 | $FFI::ExtractSymbols::mode = 'mixed'; | ||
| 18 | |||
| 19 | # | ||
| 20 | *FFI::ExtractSymbols::extract_symbols = sub | ||
| 21 | { | ||
| 22 | my($libpath, %callbacks) = @_; | ||
| 23 | $callbacks{$_} ||= sub {} for qw( export code data ); | ||
| 24 | |||
| 25 | my $raw = Path::Tiny->new($libpath)->slurp_raw; | ||
| 26 | |||
| 27 | # | ||
| 28 | return 1 if 0x5A4D != unpack 'v', substr $raw, 0, 2; # MZ signature failed | ||
| 29 | my $peo = unpack 'V', substr $raw, 0x3C, 4; | ||
| 30 | return 1 if "PE\0\0" ne substr $raw, $peo, 4; # PE signature failed | ||
| 31 | |||
| 32 | # | ||
| 33 | my ( $sizeOfOptionalHeader, undef, $magic ) = unpack 'vvv', substr $raw, $peo + 20, 8; | ||
| 34 | return 1 if !$sizeOfOptionalHeader; # No optional COFF and thus no exports | ||
| 35 | my $pe32plus = $magic == 0x20b; # 32bit: Ox10b 64bit: 0x20b ROM?: 0x107 | ||
| 36 | my $opt_header = substr $raw, $peo + 24, $sizeOfOptionalHeader; | ||
| 37 | |||
| 38 | # COFF header | ||
| 39 | my $numberOfSections = unpack 'v', substr $raw, $peo + 6, 2; | ||
| 40 | |||
| 41 | # Windows "optional" header | ||
| 42 | my $imageBase = $pe32plus ? unpack 'Q', substr $opt_header, 24, 8 : unpack 'V', | ||
| 43 | substr $opt_header, 28, 4; | ||
| 44 | my $numberOfRVAandSizes = unpack 'V', substr $opt_header, ( $pe32plus ? 108 : 112 ), 4; | ||
| 45 | my $sections; # local cache | ||
| 46 | my $sec_begin = $peo + 24 + $sizeOfOptionalHeader; | ||
| 47 | my $sec_data = substr $raw, $sec_begin, $numberOfSections * 40; | ||
| 48 | for my $x ( 0 .. $numberOfSections - 1 ) { | ||
| 49 | my $sec_head = $sec_begin + ( $x * 40 ); | ||
| 50 | my $sec_name = unpack 'Z*', substr $raw, $sec_head, 8; | ||
| 51 | $sections->{$sec_name} = [ unpack 'VV VVVV vv V', substr $raw, $sec_head + 8 ]; | ||
| 52 | } | ||
| 53 | |||
| 54 | # dig into directory | ||
| 55 | my ( $edata_pos, $edata_len ) = unpack 'VV', substr $opt_header, $pe32plus ? 112 : 96, 8; | ||
| 56 | my @fields = unpack 'V10', substr $raw, _rva2offset($edata_pos, $sections), 40; | ||
| 57 | my ( $ptr_func, $ptr_name, $ptr_ord ) = map { _rva2offset( $fields[$_], $sections ) } 7 .. 9; | ||
| 58 | my %retval = ( name => unpack 'Z*', substr $raw, _rva2offset( $fields[3], $sections ), 256 ); | ||
| 59 | my @ord = unpack 'V' x $fields[5], substr $raw, $ptr_func, 4 * $fields[5]; | ||
| 60 | |||
| 61 | for my $idx ( 0 .. $fields[5] ) { | ||
| 62 | my $ord_cur = unpack 'v', substr $raw, $ptr_ord + ( 2 * $idx ), 2; | ||
| 63 | my $func_cur = $ord[$ord_cur]; # Match the ordinal to the function RVA | ||
| 64 | next if $idx > ( $fields[6] - 1 ); | ||
| 65 | my $name_cur = unpack 'V', substr $raw, $ptr_name + ( 4 * $idx ), 4; | ||
| 66 | my $symbol = unpack 'Z*', substr $raw, _rva2offset($name_cur, $sections), 512; | ||
| 67 | $ord_cur += $fields[4]; # Add the ordinal base value | ||
| 68 | $callbacks{export}->($symbol, $symbol); | ||
| 69 | $callbacks{code} ->($symbol, $symbol); | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | sub _rva2offset | ||
| 74 | { | ||
| 75 | my ($virtual, $sections) = @_; | ||
| 76 | for my $section ( values %$sections ) { | ||
| 77 | if ( ( $virtual >= $section->[1] ) and ( $virtual < $section->[1] + $section->[0] ) ) { | ||
| 78 | return $virtual - ( $section->[1] - $section->[3] ); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |