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

my $min = 0xFFFFFFFF;
my $max = 0;
LOOP: while(my $filename = <>)
{
  chomp $filename;
  $filename =~ s/^\s*//; 
  if ( ! -e $filename )
  {
    printf "Can't find [%s]\n", $filename;
    next LOOP;
  }
  open(ELF, '-|', "readelf -l $filename 2>&1") || die "$1 ($filename)";

  my $nrLoad = 0;
  my $end = 0;
  while(my $line = <ELF>)
  {
    chomp $line;
    if ($line =~ m/^\s*LOAD\s*/)
    {
      $nrLoad++;

      my @number = split / +/, $line;
      my $virtaddr = hex($number[3]);
      my $filesiz = hex($number[5]);

      if ($end != 0)
      {
        my $dist = $virtaddr - $end;
	if ($dist < 0x1000)
	{
	  printf "%-32s virtaddr=%#08x dist=%#08x\n",
	    $filename, $virtaddr, $dist;
        }
	$max = $dist if ($dist > $max);
	$min = $dist if ($dist < $min);
      }
      $end = $virtaddr + $filesiz;
    }
  }
  if ($nrLoad != 2)
  {
    printf "%-32s has %d LOAD segments.\n", $filename, $nrLoad;
  }
  close ELF;
}
printf "\n%d files; min_distance=%#08x max_distance=%#08x\n",
  $., $min, $max;
