#!/bin/sh

noparm=
mprdem=./mprdem

while test $# -gt 0; do
	case "$1" in
		-p)	 noparm=-p;;
                -mprdem) mprdem=$2; shift;;
		--)	 Break;;
		*)	 break;;
	esac
	shift
done

if test $# -ne 1; then
	echo "usage: mprnm [-p] a.out" >&2
	exit 1
fi

if file $1 | grep ELF >/dev/null 2>&1; then
	offset=1
else
	offset=2
fi

nm -n --no-cplus $1 |

awk '
	BEGIN {
		for (i=0; i<10; i++) hexv[i""]=i
		hexv["a"]=10; hexv["b"]=11; hexv["c"]=12
		hexv["d"]=13; hexv["e"]=14; hexv["f"]=15
	}
	function hex(h, d,l) {
		d=0; l=length(h)
		for (i=1; i<=l; i++)
			d = d*16 + hexv[substr(h, i, 1)]
		return d
	}

	NF==3 && $2 ~ /T|t/ && $3 !~ /(\.o$)|(g(nu|cc).*compiled)/ {
		if ($3 !~ /^_/)
			printf "%s\t%s\n", hex($1), $3
		else
			printf "%s\t%s\n", hex($1), substr($3, '$offset')
	}' |

$mprdem $noparm |

sed -e 's/global destructors keyed to /_GLOBAL_$D$/' \
    -e 's/global constructors keyed to /_GLOBAL_$C$/' \
    -e 's/::/<>/g' \
    -e 's/\([^0-9]\) /\1/g'

exit 0
