#! /usr/bin/perl -W

use Math::BigInt;
use strict;

my %sym_tab = (new Math::BigInt(0) => "ERRRRRRROR");
my $sections = "BdDdTtVvWwuU";
my %sec_tab;

my %test;

sub as_hex($)
{
  my $i = shift;
  my $h = substr $i->as_hex(), 2;
  $h = ('0' x (16-length($h))) . $h;
  return $h;
}

if (!defined $ARGV[0])
{
  print "$0 image(s)\n";
  print " input is read from stdin\n";
  exit 1;
}

my $nm = 'nm';
$nm = "$ENV{'SYSTEM_TARGET'}$nm" if defined $ENV{"SYSTEM_TARGET"};
$nm = 'arm-softfloat-elf-nm' if !(system("file -L $ARGV[0] | grep -qw ARM") >> 8);

my @args = @ARGV;

while (@ARGV)
  {
    my $img = shift;

    foreach my $l (split('\n', qx{$nm $img | c++filt}))
      {
	
	if ($l =~ /^([0-9a-fA-F]*)\s+([$sections])\s+(.*)$/)
	  {
	    my ($addr, $sec, $sym) = (new Math::BigInt("0x$1"), $2, $3);
	    if (defined $addr && ref $addr && !$addr->is_nan())
	      {
		my $hexaddr = as_hex($addr);
		$sym_tab{$hexaddr} = $sym;
		$sec_tab{$hexaddr} = $sec;
	      }
	  }
      }
  }
my @sorted_sym_tab_keys = sort keys %sym_tab;
my $min_addr = $sorted_sym_tab_keys[0];
my $max_addr = $sorted_sym_tab_keys[@sorted_sym_tab_keys - 1];

print "Scanning image done, proceed.\n";

sub get_location($)
{
	my $addr = as_hex(shift);
	foreach my $img (@args)
	{
		my $loc = `addr2line -e $img $addr`;
		$loc =~ s/^$ENV{'PWD'}//;
		return $loc unless $loc eq '??:0';
	}
}

sub find_sym($)
{
  my $addr = as_hex(shift);
  my $hit = '0';

  return new Math::BigInt(0)
    if $addr lt $min_addr or $addr gt $max_addr;

  foreach my $s (@sorted_sym_tab_keys)
  {
    if ($s gt $addr)
    {
      return new Math::BigInt("0x$hit");
    }

    $hit = $s;
  }

  return new Math::BigInt(0);
}

sub print_func($)
{
  my $addr = new Math::BigInt("0x".shift);
  my $hit  = find_sym($addr);
  my $offset = $addr-$hit;
  my $o = $hit->as_hex();
  my $loc = get_location($addr);

  return unless $hit;

  printf " %s %30s(%s) + %6s = %s in %s\n",
	 $addr->as_hex(), $sym_tab{as_hex($hit)}, $sec_tab{as_hex($hit)},
	 $offset->as_hex(), $hit->as_hex(), $loc;
}


my $last_f = 0;
while (<>)
{
  if (/^(?:.*?\[\s*\d+\])?\s*(0x)?([0-9a-f]+)\s*$/i)
  {
    print_func("$2");
  }
  if (/^(?:.*?\[\s*\d+\])?\s*(0x)?([0-9a-f]+):([0-9a-f]+):([0-9a-f]+):([0-9a-f]+)\s*$/i)
  {
    print_func("$2$3$4$5");
  }
  if (/^(?:.*?\[\s*\d+\])?\s*(0x)?([0-9a-f]+):([0-9a-f]+)\s*$/i)
  {
    print_func("$2$3");
  }
}


