xref: /qemu/scripts/disas-objdump.pl (revision 8dc6d240)
1#!/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;
32    return "$cmd -m $mach --disassemble-all -b binary $outname";
33}
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;
41
42sub objcommand {
43    my $ret = $objdump[$inobjd];
44    if (!$ret) {
45        die "Host machine type not specified" if $inobjd == 1;
46        die "Target machine type not specified" if $inobjd == 2;
47        die "Internal error";
48    }
49    return $ret;
50}
51
52while (<>) {
53    # Collect the data from the relevant OBJD-* lines.
54    if (/^OBJD-H: /) {
55        die "Internal error" if $inobjd == 2;
56        $mem = $mem . pack("H*", substr($_, 8, -1));
57        $inobjd = 1;
58    } elsif (/^OBJD-T: /) {
59        die "Internal error" if $inobjd == 1;
60        $mem = $mem . pack("H*", substr($_, 8, -1));
61        $inobjd = 2;
62    }
63    # ... which will always be followed by a blank line,
64    # at which point we should produce our dump.
65    elsif ($inobjd) {
66        # Rewrite the temp file in one go; it will usually be small.
67        sysseek $outh, 0, 0;
68        truncate $outh, 0;
69        syswrite $outh, $mem;
70
71        # Pipe from objdump...
72        open IN, "-|", objcommand();
73
74        # ... copying all but the first 7 lines of boilerplate to our stdout.
75	my $i = 0;
76	while (<IN>) {
77	    print if (++$i > 7);
78        }
79        close IN;
80        print "\n";
81
82        $mem = "";
83        $inobjd = 0;
84    } else {
85        print;
86    }
87}
88