#! /bin/sh
# Copyright (C) 1996-2004, 2005 Free Software Foundation, Inc.
# Copyright (C) 2006  Dmitry V. Levin <ldv@altlinux.org>
#
# This file 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

. /usr/lib/rpm/functions

warn=
bind_now=
debug=

while test $# -gt 0; do
  case "$1" in
  --undefined)
    warn=1
    bind_now=1
    shift
    ;;
  --bindings)
    warn=1
    bind_now=1
    debug=bindings
    shift
    ;;
  --)		# Stop option processing.
    shift; break
    ;;
  -*)
    Fatal "unrecognized option: $1"
    ;;
  *)
    break
    ;;
  esac
done

case $# in
	[01]) Fatal 'insufficient arguments' ;;
	2) ;;
	*) Fatal 'too many arguments' ;;
esac

file="$1" && shift
rpath="$1" && shift

info="$(readelf --wide --segments "$file")" ||
	Fatal "$file: unable to fetch ELF segment headers"
interp="$(printf '%s\n' "$info" |sed -ne 's,^[[:space:]]*\[Requesting program interpreter: \(/[^]]\+\)\]$,\1,p')" ||
	Fatal "$file: program interpreter not specified"
if [ -z "$interp" ]; then
	dump_ld_config='/usr/lib/rpm/dump_ld_config'
	info="$(readelf --wide --segments "$dump_ld_config")" ||
		Fatal "$dump_ld_config: unable to fetch ELF segment headers"
	interp="$(printf '%s\n' "$info" |sed -ne 's,^[[:space:]]*\[Requesting program interpreter: \(/[^]]\+\)\]$,\1,p')" ||
		Fatal "$dump_ld_config: program interpreter not specified"
	[ -n "$interp" ] ||
		Fatal 'Unable to recognize ELF program interpreter'
fi

if [ -n "${RPM_BUILD_ROOT-}" ] && [ -f "$RPM_BUILD_ROOT$interp" -a -x "$RPM_BUILD_ROOT$interp" ]; then
	rtld="$RPM_BUILD_ROOT$interp"
elif [ -f "$interp" -a -x "$interp" ]; then
	rtld="$interp"
else
	Fatal "$file: program interpreter $interp not found"
fi

verify_out="$("$rtld" --verify "$file")"
[ "$?" = 0 -o "$?" = 2 ] ||
	Fatal "$file: trace failed"

LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now LD_DEBUG=$debug LD_LIBRARY_VERSION=$verify_out \
	"$rtld" --library-path "$rpath" "$file" ||
	Fatal "$file: trace failed"

exit 0
