#!/usr/bin/perl 
use warnings;
use strict;

my %emap = (
    'elf32-littlearm' => 'V*',
    'elf32-tradbigmips' => 'N*',
    'elf32-tradlittlemips' => 'V*',
    'elf64-littleaarch64' => 'V*',
);

my $outfile = shift or die(qq[
usage:  $0 outputfile 
stdin should be the output of objdump
]);
my $od;
my $format;
my $start;
my $load;
my $sz;
{
    local $/;
    $od = <>;
}
open(FO,">",$outfile);

=ignore
cfe63138:     file format elf32-littlearm
cfe63138
architecture: arm, flags 0x00000112:
=cut

if ($od =~ /^.*:\s+file\s+format\s+(\S+)\s*$/m)
{
    $format=$1;
}
print "format='$format'\n";
die("bad format $format") unless $emap{$format};

=ignore
EXEC_P, HAS_SYMS, D_PAGED
start address 0x00f00000
=cut

if ($od =~ /^start address (0x\w+)\s*$/m)
{
    $start = hex($1);
    printf("start %x\n",$start);
}

=ignore
    LOAD off    0x00008000 vaddr 0x00f00000 paddr 0x00f00000 align 2**15
=cut

if ($od =~ /^\s+LOAD off.* paddr\s+(0x\w+)\s/m)
{
    $load = hex($1);
    printf("load %x\n",$load);
}

=ignore
00f5ed84 g       *ABS* 00000000 _end
=cut

if ($od =~ /^(\w+) g .* _end\s*$/m)
{
    $sz = hex("0x$1") - $load;
    printf("sz %x\n",$sz);
}

print FO pack($emap{$format},$start,$load,$sz) or die("Error\n");

