#!/bin/sh -ef
#
# verify-elf - verify ELF objects.
#
# $Id: verify-elf.in,v 1.4 2004/01/25 17:19:43 ldv Exp $
# Copyright (C) 2002, 2003, 2004  Dmitry V. Levin <ldv@altlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

. /usr/lib/rpm/functions
ValidateBuildRoot

: ${RPM_VERIFY_ELF_SKIPLIST:=}

rc=0
for f in "$@"; do
	if [ ! -f "$f" ]; then
		Info "$f: file unavailable"
		rc=1
		continue
	fi

	fname="${f#$RPM_BUILD_ROOT}"
	fname="${fname#.}"

	if [ -n "$RPM_VERIFY_ELF_SKIPLIST" ]; then
		for skip in $RPM_VERIFY_ELF_SKIPLIST; do
			if [ -z "${fname##$skip}" ]; then
				continue 2
			fi
		done
	fi

	t="$(/usr/bin/file -b "$f")"

	[ -z "${t##ELF *}" -o -z "${t##* ELF *}" ] || continue

	info=$(objdump -p "$f") || continue

	if [ -n "$VERIFY_ELF_RPATH" ]; then
		rpath=`printf %s "$info" |awk '{if ($1=="RPATH") print $2}'`
		while [ -n "$rpath" ]; do
			found=
			for p in $RPM_BUILD_ROOT $RPM_BUILD_DIR $RPM_SOURCE_DIR; do
				if printf %s "$rpath" |fgrep -qs "$p"; then
					Info "$f: RPATH entry contains \"$p\": $rpath"
					found=1
				fi
			done
			if [ -n "$found" ]; then
				rc=1
				break
			fi

			if printf %s "$rpath" |fgrep -qs :; then
				Info "$f: RPATH entry contains \":\": $rpath"
				[ "$VERIFY_ELF_RPATH" = relaxed ] || rc=1
				break
			fi

			if [ "$VERIFY_ELF_RPATH" = relaxed -o "$VERIFY_ELF_RPATH" = normal ]; then
				break
			fi

			Info "$f: RPATH entry found: $rpath"
			rc=1
			break
		done
	fi

	if [ -n "$VERIFY_ELF_TEXTREL" ]; then
		textrel=`printf %s "$info" |awk '{if ($1=="TEXTREL") print $2}'`
		while [ -n "$textrel" ]; do
			Info "$f: TEXTREL entry found: $textrel"
			if [ "$VERIFY_ELF_TEXTREL" = relaxed ]; then
				break
			fi
			rc=1
			break
		done
	fi
done

exit $rc
