-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelf-arch
executable file
·147 lines (136 loc) · 3.88 KB
/
elf-arch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl -w
my $a;
if ($#ARGV==2 && $ARGV[0] eq '-a')
{
shift;
$a=shift
}
!$#ARGV or die "Usage: $0 [-a] <file>\n";
open F, '<', $ARGV[0] or die "$0: can't read $ARGV[0]: $!\n";
my $header;
my $hlen = sysread F, $header, 56;
defined $hlen or die "$0: can't read $ARGV[0]: $!\n";
$hlen==56 or die "$0: $ARGV[0] not an ELF: only $hlen bytes long\n";
sub elf()
{
my ($word, $end, $version, $os, $abi, $isa) = unpack('xxxxCCCCCxxxxxxxxxv', $header);
$version==1 or return "invalid";
my $id=sprintf("%d:%s:%x", 16*(1<<$word), substr("-lb",$end,1), $isa);
my $wo32=substr("xVN",$end,1);
my $flags_offset=0x18+(6<<$word);
my $flags=unpack("x$flags_offset$wo32",$header);
my %elf_arch=(
"64:l:9026" => "alpha",
"64:l:3e" => "amd64",
"32:l:c3" => "arc",
"32:l:28" => "arm",
"64:l:b7" => "arm64",
"32:l:b7" => "arm64ilp32",
"32:b:f00" => "hppa",
"64:b:f00" => "hppa64",
"32:l:3" => "i386",
"64:l:32" => "ia64",
"64:l:102" => "loong64",
"32:b:400" => "m68k",
"32:b:800" => "mips",
"64:b:800" => "mips64",
"64:l:8" => "mips64el",
"32:l:8" => "mipsel",
"32:b:800" => "mips",
"32:l:71" => "nios2",
"32:b:5c00" => "or1k",
"32:b:1400" => "powerpc",
"64:b:1500" => "ppc64",
"64:l:15" => "ppc64el",
"32:l:f3" => "riscv32",
"64:l:f3" => "riscv64",
"64:b:1600" => "s390x",
"32:l:2a" => "sh4",
"32:b:200" => "sparc",
"64:b:2b00" => "sparc64",
"32:l:3e" => "x32",
);
$_=$elf_arch{$id}//"unknown";
if (/^mips/)
{
my $rel=$flags>>28&0xf;
if ($rel==10)
{ s/mips64/mips64r6/}
elsif ($rel==9)
{ s/mips/mipsr6/}
elsif ($rel==2 && $word==1)
{ s/mips/mipsn32/}
}
if ($os==9)
{ $_="kfreebsd-$_" }
elsif ($os==4)
{ $_="hurd-$_" }
elsif ($os && $os!=3) # Linux is 3 in theory but almost always 0
{ return "unknown" };
$_
}
sub pe()
{
sysseek F, 0x3c, 0;
$hlen = sysread F, $_, 4;
defined $hlen or return die "$0: can't read $ARGV[0]: $!\n";
$_=unpack 'V';
$_>0
and sysseek(F, $_, 0)
and sysread(F, $_, 4)==4
and $_ eq "PE\0\0"
and sysread(F, $_, 2)==2
or return 'invalid';
$_=unpack 'v';
my %pe_arch=(
0x0000 => 'pe-universal',
0x0184 => 'win-alpha32',
0x0284 => 'win-alpha64',
0x01d3 => 'win-am33',
0x8664 => 'win64',
0x01c0 => 'win-arm',
0xaa64 => 'win-arm64',
0x01c4 => 'win-armnt', # ARM Thumb-2 little endian
0x0ebc => 'ebc', # EFI byte code
0x014c => 'win32',
0x0200 => 'win-ia64',
0x6232 => 'win-loong32',
0x6264 => 'win-loong64',
0x9041 => 'win-m32r',
0x0266 => 'win-mips16',
0x0366 => 'win-mipsfpu',
0x0466 => 'win-mipsfpu16',
0x01f0 => 'win-powerpc',
0x01f1 => 'win-powerpcfp',
0x0166 => 'win-mipsr4000',
0x5032 => 'win-riscv32',
0x5064 => 'win-riscv64',
0x5128 => 'win-riscv128',
0x01a2 => 'win-sh3',
0x01a3 => 'win-sh3dsp',
0x01a6 => 'win-sh4',
0x01a8 => 'win-sh5',
0x01c2 => 'win-thumb',
0x0169 => 'win-wcemipsv2',
);
return $pe_arch{$_}//'pe-unknown';
}
my $ea = (unpack('N', $header)==0x7f454c46) ? elf :
(substr($header,0,2) eq 'MZ') ? pe :
"invalid";
defined($a) or print("$ea\n"), exit 0;
exit 0 if $ea eq $a; # Try unmangled first.
$a=~s/^musl-//;
# These usually have unmarked binaries.
$a=~s/^hurd-//;
$a=~s/^illumos-//;
$a=~s/^solaris-//;
$a=~s/^linux-//;
my %aliases=(
'arm' => ['armel', 'armhf'],
'powerpc' => ['powerpcspe'],
'sh4' => ['sh3'],
);
exit 0 if $ea eq $a;
exit 1 unless defined $aliases{$ea};
exit !grep { $_ eq $a } @{$aliases{$ea}}