Here's two little perl scripts for misc/. Both are worksforme quality, but
I don't see why they shouldn't workforyou.
mount_image.pl will, given arguments of mountpoint image partitionnumber,
mount the given partition. It's very sensitive to the output format of
fdisk --
it's correct for fdisk v2.10o; run fdisk -v (little v!) for your version
number.
It assumes that you are root and that /dev/loop0 is free.
Requires a linuxish fdisk, mount, and losetup.
freqcount_top.pl will parse the freqcounts file, and give the top 22
most-emulated
opcodes, including dissassembley. Note that the disasm isn't all that good.
Requires a gnuish as and objdump.
-=- James Mastros
#!/usr/bin/perl -w
use strict;
# These parameters are correct for fdisk v2.10o
my $BLOCKSIZELINE=2; # The line number of the "Units = sectors of $ bytes"
my $PARTOFFSETLINE=4; # The line number where partition 0 would be (if there were a
partition 0)
# Get args: mount_image.pl mountpoint imagefile partationnumb.
# Partition number as linux uses.
my $mountpoint = shift or die "First argument should be mountpoint";
my $image = shift or die "Second argument should be image";
my $part = shift || 1;
my @fdisk = `fdisk -ul $image 2>/dev/null`;
my $blocksize = (split " ", $fdisk[$BLOCKSIZELINE])[6];
my $startblock = (split " ", $fdisk[$PARTOFFSETLINE+$part])[2];
print "blocksize: $blocksize startblock: $startblock\n";
system('losetup', '-o', $startblock*$blocksize, '/dev/loop0', $image);
exec('mount', '/dev/loop0', $mountpoint);
#!/usr/bin/perl -w
use strict;
sub sortdex {
my @in = @{+shift};
my @double;
my $i;
for $i (0 .. scalar(@in)-1) {
$double[$i]=sprintf("%8.8x %4.4x", $in[$i], $i);
# print $double[$i], "\n";
# $double[$i][0]=$in[$i];
# $double[$i][1]=$i;
}
# Decresing order
@double = sort {$b cmp $a} @double;
return \@double;
}
sub disasm {
use IO::File;
my $opcode = shift;
my $asmfile = new IO::File "/tmp/freqcount_top.pl-opcode.s",
O_WRONLY|O_CREAT|O_TRUNC or die "$! while opening /tmp/freqcount_top.pl-opcode.s";
local $/="\n";
print $asmfile <<"__END__";
.text
.global main
main:
.byte 0x$opcode
__END__
undef $asmfile;
chdir '/tmp';
`as /tmp/freqcount_top.pl-opcode.s -o /tmp/freqcount_top.pl-opcode.o`;
my @objdump = `objdump -D -w --start-address=0 --stop-address=1
/tmp/freqcount_top.pl-opcode.o`;
# my $i;
# foreach (@objdump) {
# $i++;
# print "$i: $_";
# };
# NOTE: very touchy on format of objdump output!
@objdump = split " ", $objdump[6];
return join " ", @objdump[2..$#objdump];
}
my(@freq, $filename, $fh);
use IO::File;
$filename = shift || 'freqcounts';
$fh = new IO::File "<$filename" or die "Opening $filename: $!";
{
local $/ = undef;
@freq = unpack("l*", <$fh>);
}
if (scalar(@freq)!=0x1ff) {
print "Odd, freqcount isn't of 0x1ff instructions (0x", scalar @freq, ")\n";
}
@freq = @{sortdex \@freq};
my $i;
for $i (0..23) {
my ($count, $opcode) = (split ' ', $freq[$i]);
# print disasm $opcode;
printf "%2d: 0x%s = %s with %d incovations\n", $i+1, $opcode, disasm($opcode),
hex $count;
}