#!/bin/sh
#
# author:        Evan Teran <eteran@alum.rit.edu>
# homepage:      http://www.codef00.com/
# license:       GPLv2
# contributions: pancake <pancake@youterm.com> 
#


do_work() {
	# just calls nm with various options and normalizes the output with a little 
	# bit of awk
	nm --defined-only -S -P -n $1 2>/dev/null		| awk '{ if(NF == 3) { printf("%s %08x %s %s\n",$3, 0, $2, $1); } else if(NF == 4) { printf("%s %s %s %s\n",$3, $4, $2, $1); } }'
	nm --defined-only  -S -P -D -n $1  2>/dev/null	| awk '{ if(NF == 3) { printf("%s %08x %s %s\n",$3, 0, $2, $1); } else if(NF == 4) { printf("%s %s %s %s\n",$3, $4, $2, $1); } }'

	# uses objdump to get all the PLT entries for a given binary
	# they seem to always be 16 bytes big..this is a guess
	objdump -j".plt" -d $1 | grep "@plt>:" | awk '{ printf("%s %08x %s %s\n", $1, 0x10, "P", substr($2, 2, length($2) - 3)); }'
}

[ -z "$1" ] && \
echo >&2 "Usage: $0 [path/to/elf]" && exit 1

[ ! -e "$1" ] && \
echo >&2 "$1: Could not open this file." && exit 1

[ -z "`file -L $1| grep ELF`" ] && \
echo >&2 "$1: This is not an ELF." && exit 1

date

MD5=$(which md5sum 2>/dev/null)
if [ -z $MD5 ]; then
	MD5="$(which md5 2>/dev/null) -r"
fi

$MD5 $1

do_work $1 | sort | uniq


