#find-debuginfo.sh - automagically generate debug info and file list
#for inclusion in an rpm spec file.
# Usage: find-debuginfo.sh [--strict-build-id] [-g] [-r] [-m] [-i] [-n]
# [--keep-section SECTION] [--remove-section SECTION]
# [-S debugsourcefiles.list]
# [--run-dwz] [--dwz-low-mem-die-limit N]
# [--dwz-max-die-limit N]
# [--unique-debug-suffix SUFFIX]
# [--unique-debug-src-base BASE]
# [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
# The -g flag says to use strip -g instead of full strip on DSOs or EXEs.
# The --g-libs flag says to use strip -g instead of full strip ONLY on DSOs.
# Options -g and --g-libs are mutually exclusive.
# The -r flag says to use eu-strip --reloc-debug-sections.
# Use --keep-section SECTION or --remove-section SECTION to explicitly
# keep a (non-allocated) section in the main executable or explicitly
# remove it into the .debug file. SECTION is an extended wildcard pattern.
# Both options can be given more than once.
# The --strict-build-id flag says to exit with failure status if
# any ELF binary processed fails to contain a build-id note.
# The -m flag says to include a .gnu_debugdata section in the main binary.
# The -i flag says to include a .gdb_index section in the .debug file.
# The -n flag says to not recompute the build-id.
# The -j, --jobs N option will spawn N processes to do the debuginfo
# extraction in parallel.
# A single -o switch before any -l or -p switches simply renames
# the primary output file from debugfiles.list to something else.
# A -o switch that follows a -p switch or some -l switches produces
# an additional output file with the debuginfo for the files in
# the -l filelist file, or whose names match the -p pattern.
# The -p argument is an grep -E -style regexp matching the a file name,
# and must not use anchors (^ or $).
# The --run-dwz flag instructs find-debuginfo.sh to run the dwz utility
# if available, and --dwz-low-mem-die-limit and --dwz-max-die-limit
# provide detailed limits. See dwz(1) -l and -L option for details.
# If --build-id-seed SEED is given then debugedit is called to
# update the build-ids it finds adding the SEED as seed to recalculate
# the build-id hash. This makes sure the build-ids in the ELF files
# are unique between versions and releases of the same package.
# (Use --build-id-seed "%{VERSION}-%{RELEASE}".)
# If --unique-debug-suffix SUFFIX is given then the debug files created
# for <FILE> will be named <FILE>-<SUFFIX>.debug. This makes sure .debug
# are unique between package version, release and architecture.
# (Use --unique-debug-suffix "-%{VERSION}-%{RELEASE}.%{_arch}".)
# If --unique-debug-src-base BASE is given then the source directory
# will be called /usr/debug/src/<BASE>. This makes sure the debug source
# directories are unique between package version, release and architecture.
# (Use --unique-debug-src-base "%{name}-%{VERSION}-%{RELEASE}.%{_arch}".)
# All file names in switches are relative to builddir (. if not given).
# Figure out where we are installed so we can call other helper scripts.
lib_rpm_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# With -g arg, pass it to strip on libraries or executables.
# With --g-libs arg, pass it to strip on libraries.
# with -r arg, pass --reloc-debug-sections to eu-strip.
# keep or remove arguments to eu-strip.
# with -m arg, add minimal debuginfo to binary.
# with -i arg, add GDB index to .debug file.
# Barf on missing build IDs.
# Do not recompute build IDs.
no_recompute_build_id=false
# build id seed given by the --build-id-seed option
# Arch given by --unique-debug-arch
# Base given by --unique-debug-src-base
# Number of parallel jobs to spawn
no_recompute_build_id=true
if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
lists[$nout]="${lists[$nout]} $2"
keep_remove_args="${keep_remove_args} --keep-section $2"
keep_remove_args="${keep_remove_args} --remove-section $2"
if test -n "$build_id_seed" -a "$no_recompute_build_id" = "true"; then
echo >&2 "*** ERROR: --build-id-seed (unique build-ids) and -n (do not recompute build-id) cannot be used together"
if [ "$strip_g" = "true" ] && [ "$strip_glibs" = "true" ]; then
echo >&2 "*** ERROR: -g and --g-libs cannot be used together"
outs[$i]="$BUILDDIR/${outs[$i]}"
for f in ${lists[$i]}; do
LISTFILE="$BUILDDIR/$out"
SOURCEFILE="$BUILDDIR/debugsources.list"
LINKSFILE="$BUILDDIR/debuglinks.list"
ELFBINSFILE="$BUILDDIR/elfbins.list"
debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
$strip_r && r=--reloc-debug-sections
$strip_g && case "$(file -bi "$2")" in
application/x-sharedlib*) g=-g ;;
application/x-executable*) g=-g ;;
application/x-pie-executable*) g=-g ;;
$strip_glibs && case "$(file -bi "$2")" in
application/x-sharedlib*) g=-g ;;
eu-strip --remove-comment $r $g ${keep_remove_args} -f "$1" "$2" || exit
local keep_symbols=`mktemp`
local mini_debuginfo=`mktemp`
# In the minisymtab we don't need the .debug_ sections (already removed
# by -S) but also not other non-allocated PROGBITS, NOTE or NOBITS sections.
# List and remove them explicitly. We do want to keep the allocated,
# symbol and NOBITS sections so cannot use --keep-only because that is
# too aggressive. Field $2 is the section name, $3 is the section type
# and $8 are the section flags.
local remove_sections=`readelf -W -S "$debuginfo" \
| awk '{ if (index($2,".debug_") != 1 \
&& ($3 == "PROGBITS" || $3 == "NOTE" || $3 == "NOBITS") \
printf "--remove-section "$2" " }'`
# Extract the dynamic symbols from the main binary, there is no need to also have these
# in the normal symbol table
nm -D "$binary" --format=posix --defined-only | awk '{ print $1 }' | sort > "$dynsyms"
# Extract all the text (i.e. function) symbols from the debuginfo
# Use format sysv to make sure we can match against the actual ELF FUNC
# symbol type. The binutils nm posix format symbol type chars are
# ambigous for architectures that might use function descriptors.
nm "$debuginfo" --format=sysv --defined-only | awk -F \| '{ if ($4 ~ "FUNC") print $1 }' | sort > "$funcsyms"
# Keep all the function symbols not already in the dynamic symbol table
comm -13 "$dynsyms" "$funcsyms" > "$keep_symbols"
# Copy the full debuginfo, keeping only a minumal set of symbols and removing some unnecessary sections
objcopy -S $remove_sections --keep-symbols="$keep_symbols" "$debuginfo" "$mini_debuginfo" &> /dev/null
#Inject the compressed data into the .gnu_debugdata section of the original binary
mini_debuginfo="${mini_debuginfo}.xz"
objcopy --add-section .gnu_debugdata="$mini_debuginfo" "$binary"
rm -f "$dynsyms" "$funcsyms" "$keep_symbols" "$mini_debuginfo"
# Make a relative symlink to $1 called $3$2
local t="$1" f="$2" pfx="$3"
local fn="${f#/}" tn="${t#/}"
while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
if [ "$d" != "$fn" ]; then
mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
# Make a symlink in /usr/lib/debug/$2 to $1
local l="/usr/lib/debug$2"
echo >> "$LINKSFILE" "$l $t"
link_relative "$t" "$l" "$RPM_BUILD_ROOT"
dn=$(dirname "${1#$RPM_BUILD_ROOT}")
bn=$(basename "$1" .debug)${unique_debug_suffix}.debug
$strict || strict_error=WARNING
temp=$(mktemp -d ${TMPDIR:-/tmp}/find-debuginfo.XXXXXX)
trap 'rm -rf "$temp"' EXIT
# Build a list of unstripped ELF files and their hardlinks
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
\( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
file -N -f - | sed -n -e 's/^\(.*\):[ ]*.*ELF.*, not stripped.*/\1/p' |
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
while read nlinks inum f; do
if [ $nlinks -gt 1 ]; then
if test -n "${!var}"; then
echo "$inum $f" >>"$temp/linked"
echo "$nlinks $inum $f" >>"$temp/primary"
local nlinks=$1 inum=$2 f=$3 id link linked
[ -f "${debugfn}" ] && return
echo "extracting debug info from $f"
# See also cpio SOURCEFILE copy. Directories must match up.
debug_base_name="$RPM_BUILD_DIR"
debug_dest_name="/usr/src/debug"
if [ ! -z "$unique_debug_src_base" ]; then
debug_base_name="$BUILDDIR"
debug_dest_name="/usr/src/debug/${unique_debug_src_base}"
if [ "$no_recompute_build_id" = "true" ]; then
id=$(${lib_rpm_dir}/debugedit -b "$debug_base_name" -d "$debug_dest_name" \
${build_id_seed:+--build-id-seed="$build_id_seed"} \
-l "$SOURCEFILE" "$f") || exit
echo >&2 "*** ${strict_error}: No build ID note found in $f"
# Add .gdb_index if requested.
if $include_gdb_index; then
if type gdb-add-index >/dev/null 2>&1; then
echo >&2 "*** ERROR: GDB index requested, but no gdb-add-index installed"
# Compress any annobin notes in the original binary.
# Ignore any errors, since older objcopy don't support --merge-notes.
objcopy --merge-notes "$f" 2>/dev/null || true
# A binary already copied into /usr/lib/debug doesn't get stripped,
# just has its file names collected and adjusted.
strip_to_debug "${debugfn}" "$f"
strip_to_debug "${debugfn}" "$f"
# strip -g implies we have full symtab, don't add mini symtab in that case.
# It only makes sense to add a minisymtab for executables and shared
# libraries. Other executable ELF files (like kernel modules) don't need it.
if [ "$include_minidebug" = "true" -a "$strip_g" = "false" ]; then
if [ "$strip_glibs" = "false" ]; then
case "$(file -bi "$f")" in
application/x-sharedlib*) skip_mini=false ;;
case "$(file -bi "$f")" in
application/x-executable*) skip_mini=false ;;
application/x-pie-executable*) skip_mini=false ;;
$skip_mini || add_minidebug "${debugfn}" "$f"
echo "./${f#$RPM_BUILD_ROOT}" >> "$ELFBINSFILE"
# If this file has multiple links, make the corresponding .debug files
# all links to one file too.
if [ $nlinks -gt 1 ]; then
grep "^$inum " "$temp/linked" | while read inum linked; do
echo "hard linked $link to $debugfn"
mkdir -p "$(dirname "$debugfn")" && ln -nf "$link" "$debugfn"
# 16^6 - 1 or about 16 million files
local SOURCEFILE=$temp/debugsources.$jobid ELFBINSFILE=$temp/elfbins.$jobid
# can't use read -n <n>, because it reads bytes one by one, allowing for
filenum=$(dd bs=$(( FILENUM_DIGITS + 1 )) count=1 status=none)
if test -z "$filenum"; then
do_file $(sed -n "$(( 0x$filenum )) p" "$temp/primary")
echo 0 >"$temp/res.$jobid"
n_files=$(wc -l <"$temp/primary")
if [ $n_jobs -gt $n_files ]; then
if [ $n_jobs -le 1 ]; then
while read nlinks inum f; do
do_file "$nlinks" "$inum" "$f"
for ((i = 1; i <= n_files; i++)); do
printf "%0${FILENUM_DIGITS}x\\n" $i
for ((i = 0; i < n_jobs; i++)); do
# The shell redirects stdin to /dev/null for background jobs. Work
# around this by duplicating fd 0
for f in "$temp"/res.*; do