macho: Improve macho_calculate_sizes
[nasm.git] / nsis / getpearch.pl
blob25155536601511216ea2c791bf19b2c83598ff37
1 #!/usr/bin/perl
3 # Get the appropriate variables to make an NSIS installer file
4 # based on the PE architecture of a specific file
7 use strict;
9 my %archnames = (
10 0x01de => 'am33',
11 0x8664 => 'x64',
12 0x01c0 => 'arm32',
13 0x01c4 => 'thumb',
14 0xaa64 => 'arm64',
15 0x0ebc => 'efi',
16 0x014c => 'x86',
17 0x0200 => 'ia64',
18 0x9041 => 'm32r',
19 0x0266 => 'mips16',
20 0x0366 => 'mips',
21 0x0466 => 'mips16',
22 0x01f0 => 'powerpc',
23 0x01f1 => 'powerpc',
24 0x0166 => 'mips',
25 0x01a2 => 'sh3',
26 0x01a3 => 'sh3',
27 0x01a6 => 'sh4',
28 0x01a8 => 'sh5',
29 0x01c2 => 'arm32',
30 0x0169 => 'wcemipsv2'
33 my ($file) = @ARGV;
34 open(my $fh, '<', $file)
35 or die "$0: cannot open file: $file: $!\n";
37 read($fh, my $mz, 2);
38 exit 0 if ($mz ne 'MZ');
40 exit 0 unless (seek($fh, 0x3c, 0));
41 exit 0 unless (read($fh, my $pe_offset, 1) == 1);
42 $pe_offset = unpack("C", $pe_offset);
44 exit 0 unless (seek($fh, $pe_offset, 0));
45 read($fh, my $pe, 4);
46 exit 0 unless ($pe eq "PE\0\0");
48 exit 0 unless (read($fh, my $arch, 2) == 2);
49 $arch = $archnames{unpack("v", $arch)};
50 if (defined($arch)) {
51 print "!define ARCH ${arch}\n";
54 exit 0 unless (seek($fh, 14, 1));
55 exit 0 unless (read($fh, my $auxheaderlen, 2) == 2);
56 exit 0 unless (unpack("v", $auxheaderlen) >= 2);
58 exit 0 unless (seek($fh, 2, 1));
59 exit 0 unless (read($fh, my $petype, 2) == 2);
60 $petype = unpack("v", $petype);
61 if ($petype == 0x010b) {
62 # It is a 32-bit PE32 file
63 print "!define BITS 32\n";
64 print "!define GLOBALINSTDIR \$PROGRAMFILES\n";
65 } elsif ($petype == 0x020b) {
66 # It is a 64-bit PE32+ file
67 print "!define BITS 64\n";
68 print "!define GLOBALINSTDIR \$PROGRAMFILES64\n";
69 } else {
70 # No idea...
73 close($fh);
74 exit 0;