#!/usr/bin/perl -w
use strict;

my $tmp = $ENV{'TEVWH_TMP'} || die "TEVWH_TMP undefined.";
my $pagesize = $ENV{'TEVWH_PAGESIZE'} || die "TEVWH_PAGESIZE undefined.";
$pagesize = hex($pagesize);
my $align = $ENV{'TEVWH_ELF_ALIGN'} || die "TEVWH_ELF_ALIGN undefined.";
$align = hex($align);

my $min = 0xFFFFFFFF; my $max = 0; my $det_page = 0; my $det_align = 0;
my $nr_files = 0; my $filename; 
while(<>)
{
  if (m#^(/[^:\s]+):#) { $nr_files++; $filename = $1; next; }
  if (m#^$tmp/([^:\s]+):#) { $nr_files++; $filename = $1; next; }
  if (m#^Program Header:#)
  {
    my $nrLoad = 0; my $end = 0;
    while(<>)
    {
      last if (m#^\s*$#);
      next if (!m/^\s*LOAD\s+/);

      $nrLoad++;
      my @number = split /\s+/;
      my $addr = hex($number[5]);

      $_ = <>;
      @number = split /\s+/, $_;
      my $filesiz = hex($number[2]);

      if ($end != 0)
      {
	my $dist = $addr - $end;
	my $print = 0;
	if ($dist <= $pagesize) { $det_page++; $print = 1; }
	if ($dist < $align) { $det_align++; $print = 1; }
	printf "%-52s addr=%08x dist=%08x\n", $filename, $addr, $dist
	if ($print);
	$max = $dist if ($dist > $max);
	$min = $dist if ($dist < $min);
      }
      $end = $addr + $filesiz;
    }
    printf("%-52s has %d LOAD segments.\n", $filename, $nrLoad)
    if ($nrLoad != 2);
  }
}
printf "files=%04d; det_page=%04d; det_align=%04d; min=0x%08x; max=0x%08x\n",
  $nr_files, $det_page, $det_align, $min, $max;
