94084c
#!/bin/bash
94084c
# Wrapper script for find-debuginfo.sh
94084c
#
94084c
# Usage:
94084c
#  wrap-find-debuginfo.sh SYSROOT-PATH SCRIPT-PATH SCRIPT-ARGS...
94084c
#
94084c
# The wrapper saves the original version of ld.so found in SYSROOT-PATH,
94084c
# invokes SCRIPT-PATH with SCRIPT-ARGS, and then restores the
94084c
# LDSO-PATH file, followed by note merging and DWZ compression.
94084c
# As a result, ld.so has (mostly) unchanged debuginfo even
94084c
# after debuginfo extraction.
94084c
#
94084c
# For libc.so.6, a set of strategic symbols is preserved in .symtab
94084c
# that are frequently used in valgrind suppressions and elsewhere.
94084c
94084c
set -ex
94084c
94084c
ldso_tmp="$(mktemp)"
94084c
libc_tmp="$(mktemp)"
94084c
94084c
# Prefer a separately installed debugedit over the RPM-integrated one.
94084c
if command -v debugedit >/dev/null ; then
94084c
    debugedit=debugedit
94084c
else
94084c
    debugedit=/usr/lib/rpm/debugedit
94084c
fi
94084c
94084c
cleanup () {
94084c
    rm -f "$ldso_tmp" "$libc_tmp"
94084c
}
94084c
trap cleanup 0
94084c
94084c
sysroot_path="$1"
94084c
shift
94084c
script_path="$1"
94084c
shift
94084c
94084c
# See ldso_path setting in glibc.spec.
94084c
ldso_path=
94084c
for ldso_candidate in `find "$sysroot_path" -regextype posix-extended \
94084c
  -regex '.*/ld(-.*|64|)\.so\.[0-9]+$' -type f` ; do
94084c
    if test -z "$ldso_path" ; then
94084c
	ldso_path="$ldso_candidate"
94084c
    else
94084c
	echo "error: multiple ld.so candidates: $ldso_path, $ldso_candidate"
94084c
	exit 1
94084c
    fi
94084c
done
94084c
94084c
# libc.so.6 always uses this name, so it is simpler to locate.
94084c
libc_path=
94084c
for libc_candidate in `find "$sysroot_path" -name libc.so.6`; do
94084c
    if test -z "$libc_path" ; then
94084c
	libc_path="$libc_candidate"
94084c
    else
94084c
	echo "error: multiple libc.so.6 candidates: $libc_path, $libc_candidate"
94084c
	exit 1
94084c
    fi
94084c
done
94084c
94084c
94084c
# Preserve the original files.
94084c
cp "$ldso_path" "$ldso_tmp"
94084c
cp "$libc_path" "$libc_tmp"
94084c
94084c
# Run the debuginfo extraction.
94084c
"$script_path" "$@"
94084c
94084c
# Restore the original files.
94084c
cp "$ldso_tmp" "$ldso_path"
94084c
cp "$libc_tmp" "$libc_path"
94084c
94084c
# Reduce the size of notes.  Primarily for annobin.
94084c
objcopy --merge-notes "$ldso_path"
94084c
objcopy --merge-notes "$libc_path"
94084c
94084c
# libc.so.6: Reduce to valuable symbols.  Eliminate file symbols,
94084c
# annobin symbols, and symbols used by the glibc build to implement
94084c
# hidden aliases (__EI_*).  We would also like to remove __GI_*
94084c
# symbols, but even listing them explicitly (as in -K __GI_strlen)
94084c
# still causes strip to remove them, so there is no filtering of
94084c
# __GI_* here.  (Debuginfo is gone after this, so no need to optimize
94084c
# it.)
94084c
strip -w \
94084c
    -K '*' \
94084c
    -K '!*.c' \
94084c
    -K '!*.os' \
94084c
    -K '!.annobin_*' \
94084c
    -K '!__EI_*' \
94084c
    -K '!__PRETTY_FUNCTION__*' \
94084c
    "$libc_path"
94084c
94084c
# ld.so: Rewrite the source file paths to match the extracted
94084c
# locations.  First compute the arguments for invoking debugedit.
94084c
# See find-debuginfo.sh.
94084c
debug_dest_name="/usr/src/debug"
94084c
last_arg=
94084c
while true ; do
94084c
    arg="$1"
94084c
    shift || break
94084c
    case "$arg" in
94084c
	(--unique-debug-src-base)
94084c
	    debug_dest_name="/usr/src/debug/$1"
94084c
	    shift
94084c
	    ;;
94084c
	(-*)
94084c
	    ;;
94084c
	(*)
94084c
	    last_arg="$arg"
94084c
	    ;;
94084c
    esac
94084c
done
94084c
debug_base_name=${last_arg:-$RPM_BUILD_ROOT}
94084c
$debugedit -b "$debug_base_name" -d "$debug_dest_name" -n $ldso_path
94084c
94084c
# Apply single-file DWARF optimization.
94084c
dwz $ldso_path