blob: 8f7e8182a1b3c83a9b1da85ae2221d6742b65c1a [file] [log] [blame]
Richard Henderson8dc6d242013-08-16 23:29:46 -07001#!/usr/bin/perl -w
2
3use File::Temp qw/ tempfile /;
4use Getopt::Long;
5
6# Default to the system objdump if a cross-compiler edition not given.
7my $aobjdump = "objdump";
8my $hobjdump = "";
9my $tobjdump = "";
10my $hmachine = "";
11my $tmachine = "";
12
13GetOptions ('O|objdump=s' => \$aobjdump,
14 'host-objdump=s' => \$hobjdump,
15 'target-objdump=s' => \$tobjdump,
16 'h|host-machine=s' => \$hmachine,
17 't|target-machine=s' => \$tmachine);
18
19# But we can't default the machines. Sanity check that we've at least one.
20die "No host or target machine type" if !$hmachine && !$tmachine;
21
22# Reuse one temp file for all of the hunks.
23my ($outh, $outname) = tempfile();
24binmode($outh);
25END { unlink $outname; }
26
27# Pre-construct the command-lines for executing the dump.
28sub mkobjcommand ($$) {
29 my ($cmd, $mach) = @_;
30 return 0 if !$mach;
31 $cmd = $aobjdump if !$cmd;
Richard Henderson42eed422013-08-16 23:29:47 -070032 return "$cmd -m $mach --disassemble-all -b binary";
Richard Henderson8dc6d242013-08-16 23:29:46 -070033}
34
35$objdump[1] = mkobjcommand($hobjdump, $hmachine);
36$objdump[2] = mkobjcommand($tobjdump, $tmachine);
37
38# Zero-initialize current dumping state.
39my $mem = "";
40my $inobjd = 0;
Richard Henderson42eed422013-08-16 23:29:47 -070041my $vma = 0;
Richard Henderson8dc6d242013-08-16 23:29:46 -070042
43sub objcommand {
44 my $ret = $objdump[$inobjd];
45 if (!$ret) {
46 die "Host machine type not specified" if $inobjd == 1;
47 die "Target machine type not specified" if $inobjd == 2;
48 die "Internal error";
49 }
50 return $ret;
51}
52
53while (<>) {
Richard Henderson42eed422013-08-16 23:29:47 -070054 # Collect the data from the relevant OBJD-* lines ...
Richard Henderson8dc6d242013-08-16 23:29:46 -070055 if (/^OBJD-H: /) {
56 die "Internal error" if $inobjd == 2;
57 $mem = $mem . pack("H*", substr($_, 8, -1));
58 $inobjd = 1;
59 } elsif (/^OBJD-T: /) {
60 die "Internal error" if $inobjd == 1;
61 $mem = $mem . pack("H*", substr($_, 8, -1));
62 $inobjd = 2;
63 }
64 # ... which will always be followed by a blank line,
65 # at which point we should produce our dump.
66 elsif ($inobjd) {
67 # Rewrite the temp file in one go; it will usually be small.
68 sysseek $outh, 0, 0;
69 truncate $outh, 0;
70 syswrite $outh, $mem;
71
Richard Henderson42eed422013-08-16 23:29:47 -070072 my $cmd = objcommand();
73 $cmd = $cmd . " --adjust-vma=" . $vma if $vma;
74 $cmd = $cmd . " " . $outname;
75
Richard Henderson8dc6d242013-08-16 23:29:46 -070076 # Pipe from objdump...
Richard Henderson42eed422013-08-16 23:29:47 -070077 open IN, "-|", $cmd;
Richard Henderson8dc6d242013-08-16 23:29:46 -070078
79 # ... copying all but the first 7 lines of boilerplate to our stdout.
80 my $i = 0;
81 while (<IN>) {
82 print if (++$i > 7);
83 }
84 close IN;
85 print "\n";
86
87 $mem = "";
88 $inobjd = 0;
Richard Henderson42eed422013-08-16 23:29:47 -070089 $vma = 0;
90 }
91 # The line before "OBJD-*" will be of the form "0x<hex>+: +\n".
92 # Extract the value for passing to --adjust-vma.
93 elsif (/^(0x[0-9a-fA-F]+):\s*$/) {
94 $vma = $1;
95 print;
Richard Henderson8dc6d242013-08-16 23:29:46 -070096 } else {
97 print;
98 }
99}