8d4b53c
#!/bin/bash
8d4b53c
# Wrapper script for find-debuginfo.sh
8d4b53c
#
8d4b53c
# Usage:
fd5c07b
#  wrap-find-debuginfo.sh SYSROOT-PATH SCRIPT-PATH SCRIPT-ARGS...
8d4b53c
#
fd5c07b
# The wrapper saves the original version of ld.so found in SYSROOT-PATH,
8d4b53c
# invokes SCRIPT-PATH with SCRIPT-ARGS, and then restores the
fd5c07b
# LDSO-PATH file, followed by note merging and DWZ compression.
fd5c07b
# As a result, ld.so has (mostly) unchanged debuginfo even
8d4b53c
# after debuginfo extraction.
14d5c92
#
14d5c92
# For libc.so.6, a set of strategic symbols is preserved in .symtab
4f2dd30
# that are frequently used in valgrind suppressions and elsewhere.
8d4b53c
8d4b53c
set -ex
8d4b53c
6c17684
workdir="$(mktemp -d -t find_debuginfo.XXXXXX)"
6c17684
6c17684
ldso_tmp="$workdir/ld.so"
6c17684
libc_tmp="$workdir/libc.so"
8d4b53c
7975168
# Prefer a separately installed debugedit over the RPM-integrated one.
7975168
if command -v debugedit >/dev/null ; then
7975168
    debugedit=debugedit
7975168
else
7975168
    debugedit=/usr/lib/rpm/debugedit
7975168
fi
7975168
8d4b53c
cleanup () {
6c17684
    rm -rf "$workdir"
8d4b53c
}
8d4b53c
trap cleanup 0
8d4b53c
fd5c07b
sysroot_path="$1"
8d4b53c
shift
8d4b53c
script_path="$1"
8d4b53c
shift
8d4b53c
fd5c07b
# See ldso_path setting in glibc.spec.
fd5c07b
ldso_path=
0bd93c5
for ldso_candidate in `find "$sysroot_path" -maxdepth 2 \
0bd93c5
  -regextype posix-extended -regex '.*/ld(-.*|64|)\.so\.[0-9]+$' -type f` ; do
fd5c07b
    if test -z "$ldso_path" ; then
fd5c07b
	ldso_path="$ldso_candidate"
e025eff
    elif [ -f "$sysroot_path/lib64/ld-linux-x86-64.so.2" ] && [ -f "$sysroot_path/lib/ld-linux.so.2" ]; then
e025eff
	# x86_64 with 32-bit libc built for glibc32 as well.  Finding
e025eff
	# multiple dynamic linkers is expected, not a bug; ensure the
e025eff
	# 64-bit one is used.
e025eff
	ldso_path="$sysroot_path/lib64/ld-linux-x86-64.so.2"
fd5c07b
    else
fd5c07b
	echo "error: multiple ld.so candidates: $ldso_path, $ldso_candidate"
fd5c07b
	exit 1
fd5c07b
    fi
fd5c07b
done
fd5c07b
14d5c92
# libc.so.6 always uses this name, so it is simpler to locate.
14d5c92
libc_path=
0bd93c5
for libc_candidate in `find "$sysroot_path" -maxdepth 2 -name libc.so.6`; do
14d5c92
    if test -z "$libc_path" ; then
14d5c92
	libc_path="$libc_candidate"
e025eff
    elif [ -f "$sysroot_path/lib64/ld-linux-x86-64.so.2" ] && [ -f "$sysroot_path/lib/ld-linux.so.2" ]; then
e025eff
	# x86_64 with 32-bit libc built for glibc32 as well.  The test
e025eff
	# here uses ld.so paths, not libc paths, to ensure it doesn't
e025eff
	# apply on any other architecture.
e025eff
	libc_path="$sysroot_path/lib64/libc.so.6"
14d5c92
    else
14d5c92
	echo "error: multiple libc.so.6 candidates: $libc_path, $libc_candidate"
14d5c92
	exit 1
14d5c92
    fi
14d5c92
done
14d5c92
14d5c92
14d5c92
# Preserve the original files.
8d4b53c
cp "$ldso_path" "$ldso_tmp"
14d5c92
cp "$libc_path" "$libc_tmp"
8d4b53c
8d4b53c
# Run the debuginfo extraction.
8d4b53c
"$script_path" "$@"
8d4b53c
Arjun Shankar 8e2d77f
# libc.so.6: Extract the .gnu_debuglink section
Arjun Shankar 8e2d77f
objcopy -j.gnu_debuglink --set-section-flags .gnu_debuglink=alloc \
6c17684
  -O binary "$libc_path" "$libc_tmp.debuglink"
Arjun Shankar 8e2d77f
14d5c92
# Restore the original files.
8d4b53c
cp "$ldso_tmp" "$ldso_path"
14d5c92
cp "$libc_tmp" "$libc_path"
8d4b53c
8d4b53c
# Reduce the size of notes.  Primarily for annobin.
8d4b53c
objcopy --merge-notes "$ldso_path"
14d5c92
objcopy --merge-notes "$libc_path"
14d5c92
Arjun Shankar 8e2d77f
# libc.so.6: Restore the .gnu_debuglink section
6c17684
objcopy --add-section .gnu_debuglink="$libc_tmp.debuglink" "$libc_path"
Arjun Shankar 8e2d77f
Arjun Shankar 8e2d77f
# ld.so does not have separated debuginfo and so the debuginfo file
Arjun Shankar 8e2d77f
# generated by find-debuginfo is redundant.  Therefore, remove it.
Arjun Shankar 8e2d77f
ldso_debug=
0bd93c5
for ldso_debug_candidate in `find "$sysroot_path" -maxdepth 2 \
0bd93c5
    -regextype posix-extended \
Arjun Shankar 8e2d77f
  -regex '.*/ld(-.*|64|)\.so\.[0-9]+.*debug$' -type f` ; do
Arjun Shankar 8e2d77f
    if test -z "$ldso_debug" ; then
Arjun Shankar 8e2d77f
	ldso_debug="$ldso_debug_candidate"
Arjun Shankar 8e2d77f
    else
Arjun Shankar 8e2d77f
	echo "error: multiple ld.so debug candidates: $ldso_debug, $ldso_debug_candidate"
Arjun Shankar 8e2d77f
	exit 1
Arjun Shankar 8e2d77f
    fi
Arjun Shankar 8e2d77f
done
Arjun Shankar 8e2d77f
rm -f "$ldso_debug"
Arjun Shankar 8e2d77f
4f2dd30
# libc.so.6: Reduce to valuable symbols.  Eliminate file symbols,
4f2dd30
# annobin symbols, and symbols used by the glibc build to implement
5b63da3
# hidden aliases (__EI_*).  We would also like to remove __GI_*
5b63da3
# symbols, but even listing them explicitly (as in -K __GI_strlen)
5b63da3
# still causes strip to remove them, so there is no filtering of
5b63da3
# __GI_* here.  (Debuginfo is gone after this, so no need to optimize
5b63da3
# it.)
4f2dd30
strip -w \
4f2dd30
    -K '*' \
4f2dd30
    -K '!*.c' \
4f2dd30
    -K '!*.os' \
4f2dd30
    -K '!.annobin_*' \
4f2dd30
    -K '!__EI_*' \
4f2dd30
    -K '!__PRETTY_FUNCTION__*' \
14d5c92
    "$libc_path"
8d4b53c
14d5c92
# ld.so: Rewrite the source file paths to match the extracted
14d5c92
# locations.  First compute the arguments for invoking debugedit.
14d5c92
# See find-debuginfo.sh.
8d4b53c
debug_dest_name="/usr/src/debug"
8d4b53c
last_arg=
8d4b53c
while true ; do
8d4b53c
    arg="$1"
8d4b53c
    shift || break
8d4b53c
    case "$arg" in
8d4b53c
	(--unique-debug-src-base)
8d4b53c
	    debug_dest_name="/usr/src/debug/$1"
8d4b53c
	    shift
8d4b53c
	    ;;
8d4b53c
	(-*)
8d4b53c
	    ;;
8d4b53c
	(*)
8d4b53c
	    last_arg="$arg"
8d4b53c
	    ;;
8d4b53c
    esac
8d4b53c
done
8d4b53c
debug_base_name=${last_arg:-$RPM_BUILD_ROOT}
7975168
$debugedit -b "$debug_base_name" -d "$debug_dest_name" -n $ldso_path
b400cea
# Remove the .annobin* symbols (and only them).
4c6fa52
if nm --format=just-symbols "$ldso_path" \
4c6fa52
	| grep '^\.annobin' > "$ldso_tmp.annobin-symbols"; then
4c6fa52
    objcopy --strip-symbols="$ldso_tmp.annobin-symbols" "$ldso_path"
4c6fa52
fi
8d4b53c
8d4b53c
# Apply single-file DWARF optimization.
8d4b53c
dwz $ldso_path