Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | # This script uses llvm-mc to disassemble byte patterns into possible |
| 4 | # x86/x86_64/arm/aarch64 instructions, trying to find a way to represent |
| 5 | # that in any of the instruction sets. |
| 6 | |
| 7 | use strict; |
| 8 | use warnings; |
| 9 | use Data::Dumper; |
| 10 | |
| 11 | #vpush-vpop.s:@ CHECK-ARM: vpop {s8, s9, s10, s11, s12} @ encoding: [0x05,0x4a,0xbd,0xec] |
| 12 | #vpush-vpop.s:@ CHECK-THUMB: vpush {d8, d9, d10, d11, d12} @ encoding: [0x2d,0xed,0x0a,0x8b] |
| 13 | my $syntax = "Syntax: $0 0xFFFF(FFFF)\n"; |
| 14 | my @hexs = &fix_endian($ARGV[0]); |
| 15 | #die Dumper \@hexs; |
| 16 | |
| 17 | foreach (@hexs) { |
| 18 | my $code = $_->{'code'}; |
| 19 | my $arch = $_->{'arch'}; |
| 20 | print "$arch: $code: "; |
| 21 | print `echo "$code" | llvm-mc -disassemble -triple $arch`; |
| 22 | } |
| 23 | |
| 24 | sub fix_endian() { |
| 25 | my ($hex) = @_; |
| 26 | |
| 27 | my ($a, $b, $c, $d) = ($hex =~ /^0?x?(\w{2})(\w{2})(\w{0,2})(\w{0,2})$/); |
| 28 | print "$a $b $c $d\n"; |
| 29 | die $syntax unless $a and $b; # at least two bytes |
| 30 | die $syntax if $c and not $d; # two or four bytes |
| 31 | # ARM/T2/x86 |
| 32 | if ($c) { |
| 33 | return ( |
| 34 | { 'code' => "0x$d 0x$c 0x$b 0x$a", 'arch' => "armv7" }, |
| 35 | { 'code' => "0x$b 0x$a 0x$d 0x$c", 'arch' => "thumbv7" }, |
| 36 | { 'code' => "0x$d 0x$c 0x$b 0x$a", 'arch' => "i686" }, |
| 37 | { 'code' => "0x$d 0x$c 0x$b 0x$a", 'arch' => "x86_64" } |
| 38 | ); |
| 39 | } else { |
| 40 | return ( { 'code' => "0x$b 0x$a", 'arch' => "thumb" } ); |
| 41 | } |
| 42 | } |