Wikia

CHDK Wiki

Srsa 4c/GPL:stubs2disv7.pl

Talk0
573pages on
this wiki

< User:Srsa 4c

Inserts named function references into disassembly made by User:Srsa_4c/GPL:disassemblev7.pl

#!/usr/bin/perl
 
# modified version of http://chdk.wikia.com/wiki/GPL:stubs2dis.pl
# expects objdump disassembly made by http://chdk.wikia.com/wiki/User:Srsa_4c/GPL:disassemblev7.pl
# funcs_by_name.csv consists of lines like
# 0xff001234,function_name
 
# original copyright notice and notes below
 
# scan chdk's stub_entry*.S for
# NSTUB(CreateController, 0xFF86A6E4)
# and add this information to a disassemble file
#
# (c) 2008 chr
# GPL V3+
#
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
 
if (@ARGV != 1) {
    print("Usage e.g.:\n cat funcs_by_name.csv | $0 dump.bin.dis > outfile\n\n");
    exit;
}
 
$dissfile = $ARGV[0];
#$stubfile = $ARGV[1];
 
#####
print STDERR "scan stub file\n";
my %nstub_names; # hash by name to eliminate doubles
 
#open(IN, "<$stubfile") or die "cannot open $stubfile: $!";
while (<STDIN>) {
    if (/^0x([[:xdigit:]]*),(.*)/i) {
        my $addr = sprintf("%x", hex($1));
        #$nstub{$addr} = $2;
        $nstub_names{$2} = $addr;
    }
}
close IN;
# hash by address
my %nstub = reverse %nstub_names;
 
$nstub{ff81000c} = "TEST start";
print Dumper(\%nstub);
#exit;
 
#####
print STDERR "scan diss file\n";
 
open(IN, "<$dissfile") or die "cannot read $dissfile: $!";
 
while (<IN>) {
    if ($_ eq " ...\n") { print $_; next;}
# print $_;
    my ($addr, $op, $op2, $line) = $_ =~ /^([[:xdigit:]]*): \t([[:xdigit:]]*)([ [:xdigit:]]*)\t(.*)/;
    unless ($addr) {
        my ($loc) = $_ =~ /^loc_([[:xdigit:]]*):(.*)/;
        unless ($loc) {
            print $_;
            next;
        }
        if (my $func = $nstub{$loc}) {
            print "loc_$loc:\t; <$nstub{$loc}>$2\n";
        }
        else {
            print "loc_$loc:$2\n";
        }
        next;
    }
 
    my ($goto) = $line =~ /.+\tloc_([[:xdigit:]]+)/;
    if ($goto) {
        if (my $func = $nstub{$goto}) {
            $line .= "\t; <$nstub{$goto}>";
        }
    }
    else {
        my $s1, $s3;
        ($s1, $goto, $s3) = $line =~ /(.+:) \(([[:xdigit:]]+)\) (\.\.\.sub\?)$/;
        if ($goto) {
            my $hgoto = hex($goto) & 0xfffffffe;
            my $tgoto = sprintf("%x", $hgoto);
            if ($func = $nstub{$tgoto}) {
                $line = "$s1 ($goto) <$nstub{$tgoto}>";
            }
        }
    }
##    my $rel = hex($goto)-hex($addr);
##    if ($rel < 0) {
##        $rel = sprintf("-0x%X", -$rel);
##    }
##    else {
##        $rel = sprintf("+0x%X", $rel);
##    }
##    $line =~ s/<(.*)>/<$1 $rel>/;
    # print STDERR "$1 $rel\n";
 
    $line = "$op$op2\t$line";
##
## TODO: handle DEF() from stub_min.S
##
##  if (
##      ($line =~ /^(.*\tldr.*\[pc, #([-\d]+).*; )/) ||
##      ($line =~ /^(.*\tadd.*pc, #([-\d]+).*; )/)
##  ) {
##      $line = $1;
##      my $off = hex($addr) - hex($offset) + $2 + 8;
##      my $point = sprintf("%08x", hex($addr) + $2 + 8);
##      my $value = &get_word($off);
##      $line .= "$point: ($value) ";
##      if (my $str = $nstub{$point}) {
##          $line .= qq| *"$str"|;
##      }
##      elsif (my $str = $nstub{$value}) {
##          $line .= qq| **"$str"|;
##      }
##  } 
##  # ff815e1c: e24f0090    sub r0, pc, #144    ; 0x90
##  elsif ($line =~ /^(.*\tsub.*pc, #([-\d]+).*; )/) {
##      $line = $1;
##      my $off = hex($addr) - hex($offset) - $2 + 8;
##      my $point = sprintf("%08x", hex($addr) - $2 + 8);
##      my $value = &get_word($off);
##      $line .= "$point: ($value) ";
##      if (my $str = $nstub{$point}) {
##          $line .= qq| *"$str"|;
##      }
##      elsif (my $str = $nstub{$value}) {
##          $line .= qq| **"$str"|;
##      }
##  }
 
    if (my $str = $nstub{$addr}) {
        print qq|\nNSTUB($str, 0x$addr):\n|;
    }
    print "$addr: \t$line\n"; 
    # progress
    print STDERR "\r0x$addr  ";
}
close IN;
print STDERR "\n";

Around Wikia's network

Random Wiki