#!/usr/bin/env perl
#
# Symbol dumper
#
# Dumps binary strings of the symbols of a library.
#
# --pancake
#

$|=1;
my $file=$ARGV[0];
my $limit=$ARGV[1];
my $delta=$ARGV[2];
my $offset=0;
my $count=int($limit); # byte counter
$limit=-1 if ($limit<1);
#print $count;

if ( $file eq "") {
	print "Usage: symdump [elf] [limit-bytes] [delta-offset]\n";
	exit(1);
}

open FD, "objdump -wd $file|" or die "Cannot exec objdump -wd $file\n";
while(<FD>) {
_sym:
	if (/.*\ <(.*)>:/) {
		$name = $1;
		if ($name=~/-/) {
			next;
		}
		print "$name ";
		while(<FD>) {
			chomp($_);
			if (/>:/) {
				print "\n";
				goto _sym;
			}
			last if (/section/);
			last if (/\.\.\./);
			# objdump output sucks, and those regexps too :D
			$str=$_;
			$str=~s/.*://g; # drop offset
			$str=~s/^\s*//g; # drop prefix spaces
			$str=~s/\ \ \s*.*$//g;
			$str=~s/\t.*$//g;
			$str=~s/\t//g;
			$str=~s/\ \ /\ /g;
			$str=~s/\n//g;
			@bytes=split(/ /,$str);
			foreach $i (0 .. $#bytes) {
				$offset++;
				next if ($offset<=$delta);
				print $bytes[$i];
				$count--;
				last if ($count==0);
			}
			last if ($count==0);
		}
		print "\n";
	}
	$offset=0;
	$count=int($limit);
}
close FD;

exit(0);
