f21993
#!/bin/bash
f21993
# Wrapper script for find-debuginfo.sh
f21993
#
f21993
# Usage:
f21993
#  wrap-find-debuginfo.sh SYSROOT-PATH SCRIPT-PATH SCRIPT-ARGS...
f21993
#
f21993
# The wrapper saves the original version of ld.so found in SYSROOT-PATH,
f21993
# invokes SCRIPT-PATH with SCRIPT-ARGS, and then restores the
f21993
# LDSO-PATH file, followed by note merging and DWZ compression.
f21993
# As a result, ld.so has (mostly) unchanged debuginfo even
f21993
# after debuginfo extraction.
f21993
#
f21993
# For libc.so.6 and other shared objects, a set of strategic symbols
f21993
# is preserved in .symtab that are frequently used in valgrind
f21993
# suppressions and elsewhere.
f21993
f21993
set -evx
f21993
f21993
tar_tmp="$(mktemp)"
313519
declare -A libc_dlink_tmp_list
313519
ldso_annobin_sym_tmp_list=""
f21993
f21993
# Prefer a separately installed debugedit over the RPM-integrated one.
f21993
if command -v debugedit >/dev/null ; then
f21993
    debugedit=debugedit
f21993
else
f21993
    debugedit=/usr/lib/rpm/debugedit
f21993
fi
f21993
f21993
cleanup () {
313519
    rm -f "$tar_tmp" ${libc_dlink_tmp_list[@]} $ldso_annobin_sym_tmp_list
f21993
}
f21993
trap cleanup 0
f21993
f21993
sysroot_path="$1"
f21993
shift
f21993
script_path="$1"
f21993
shift
f21993
f21993
# See run_ldso setting in glibc.spec.
f21993
ldso_list=`cd "$sysroot_path"; find . -name 'ld-*.so' -type f`
f21993
libc_list=`cd "$sysroot_path"; find . -name 'libc-*.so' -type f`
f21993
libdl_list=`cd "$sysroot_path"; find . -name 'libdl-*.so' -type f`
f21993
libpthread_list=`cd "$sysroot_path"; find . -name 'libpthread-*.so' -type f`
f21993
librt_list=`cd "$sysroot_path"; find . -name 'librt-*.so' -type f`
f21993
f21993
full_list="$ldso_list $libc_list $libdl_list $libpthread_list $librt_list"
f21993
f21993
# Preserve the original files.
f21993
(cd "$sysroot_path"; ls -l $full_list)
f21993
(cd "$sysroot_path"; tar cvf "$tar_tmp" $full_list)
f21993
f21993
# Run the debuginfo extraction.
f21993
"$script_path" "$@"
f21993
313519
# libc.so.6: Extract the .gnu_debuglink section
313519
for f in $libc_list
313519
do
313519
  dlink_tmp="$(mktemp)"
313519
  libc_dlink_tmp_list["$f"]="$dlink_tmp"
313519
  objcopy -j.gnu_debuglink --set-section-flags .gnu_debuglink=alloc \
313519
      -O binary "$sysroot_path/$f" "$dlink_tmp"
313519
done
313519
f21993
# Restore the original files.
f21993
(cd "$sysroot_path"; tar xf "$tar_tmp")
f21993
(cd "$sysroot_path"; ls -l $full_list)
f21993
f21993
# Reduce the size of notes.  Primarily for annobin.
f21993
for p in $full_list
f21993
do
f21993
    objcopy --merge-notes "$sysroot_path/$p"
f21993
done
f21993
313519
# libc.so.6: Restore the .gnu_debuglink section
313519
for f in ${!libc_dlink_tmp_list[@]}
313519
do
313519
  dlink_tmp="${libc_dlink_tmp_list[$f]}"
313519
  objcopy --add-section .gnu_debuglink="$dlink_tmp" "$sysroot_path/$f"
313519
done
313519
313519
# ld.so does not have separated debuginfo and so the debuginfo file
313519
# generated by find-debuginfo is redundant.  Therefore, remove it.
313519
for ldso_debug in `find "$sysroot_path" -name 'ld-*.so*.debug' -type f`
313519
do
313519
  rm -f "$ldso_debug"
313519
done
313519
f21993
# libc.so.6 and other shared objects: Reduce to valuable symbols.
f21993
# Eliminate file symbols, annobin symbols, and symbols used by the
f21993
# glibc build to implement hidden aliases (__EI_*).  We would also
f21993
# like to remove __GI_* symbols, but even listing them explicitly (as
f21993
# in -K __GI_strlen) still causes strip to remove them, so there is no
f21993
# filtering of __GI_* here.  (Debuginfo is gone after this, so no need
f21993
# to optimize it.)
f21993
for p in $libc_list $libdl_list $libpthread_list $librt_list ; do
f21993
    strip -w \
f21993
	  -K '*' \
f21993
	  -K '!*.c' \
f21993
	  -K '!*.os' \
f21993
	  -K '!.annobin_*' \
f21993
	  -K '!__EI_*' \
f21993
	  -K '!__PRETTY_FUNCTION__*' \
f21993
	  "$sysroot_path/$p"
f21993
done
f21993
f21993
# ld.so: Rewrite the source file paths to match the extracted
f21993
# locations.  First compute the arguments for invoking debugedit.
f21993
# See find-debuginfo.sh.
f21993
debug_dest_name="/usr/src/debug"
f21993
last_arg=
f21993
while true ; do
f21993
    arg="$1"
f21993
    shift || break
f21993
    case "$arg" in
f21993
	(--unique-debug-src-base)
f21993
	    debug_dest_name="/usr/src/debug/$1"
f21993
	    shift
f21993
	    ;;
f21993
	(-*)
f21993
	    ;;
f21993
	(*)
f21993
	    last_arg="$arg"
f21993
	    ;;
f21993
    esac
f21993
done
f21993
debug_base_name=${last_arg:-$RPM_BUILD_ROOT}
f21993
for p in $ldso_list
f21993
do
f21993
    $debugedit -b "$debug_base_name" -d "$debug_dest_name" -n "$sysroot_path/$p"
313519
313519
    # Remove the .annobin* symbols (and only them).
313519
    ldso_annobin_sym_tmp="$(mktemp)"
313519
    ldso_annobin_sym_tmp_list+=" $ldso_annobin_sym_tmp"
313519
    if nm --format=posix "$sysroot_path/$p" | cut -d' ' -f1 \
313519
        | grep '^\.annobin' > "$ldso_annobin_sym_tmp"; then
313519
        objcopy --strip-symbols="$ldso_annobin_sym_tmp" "$sysroot_path/$p"
313519
    fi
f21993
done
f21993
f21993
# Apply single-file DWARF optimization.
f21993
for ldso in $ldso_list
f21993
do
f21993
    dwz "$sysroot_path/$p"
f21993
done