#! /usr/bin/perl
#
# This is a simple program to dump the objects in an exectuable.  We can
# use this to check for compactness of the MPI code and separation of the
# modules.  It uses the program objdump, and so runs only on some systems
# (probably only Linux)

#use strict;
#use Tk;
#
$mpi_only = 1;
$filename = "hellow";

open( FD, "objdump -t $filename |" ) || die "Could not process $filename\n";

# Skip to SYMBOL TABLE
while (<FD>) {
    if (/^SYMBOL TABLE/) { last; }
}

# Read and save
$codesize = 0x0;
$datasize = 0x0;
while (<FD>) {
    if ($mpi_only) {
	# Skip names that do not contain MPI
	if (! /MPI/ && ! /PMI/) { next; }
    }
    # lines are 
    # address stuff kind\tsize\s*name
    if (/^.*\s([\w\.\*]*)\t\s*([0-9,a-f]*)\s\s*(.*)/) {
	$kind = $1;
	$size = hex $2;
	$name = $3;
	if ($kind eq ".text") {
	    if ($size > 0) {
		&PrintOutput( "code", $name, $size );
		$codesize += $size;
	    }
	}
	elsif ($kind eq ".data" || $kind eq ".bss") {
	    if ($size > 0) {
		&PrintOutput( "data", $name, $size );
		$datasize += $size;
	    }
	}
	elsif ($kind eq "*UND*") {
	    next;
	}
	elsif ($size != 0) {
	    &PrintOutput( $kind, $name, $size );
	}
    }
}

sub PrintOutput {
    $kind = $_[0];
    $name = $_[1];
    $size = $_[2];
    print "$size\t$name\n";
}
#print "Code size = $codesize\n";
#print "Data size = $datasize\n";

