bf96fb
#!/bin/bash
bf96fb
#find-debuginfo.sh - automagically generate debug info and file list
bf96fb
#for inclusion in an rpm spec file.
bf96fb
#
bf96fb
# Usage: find-debuginfo.sh [--strict-build-id] [-g] [-r]
bf96fb
#	 		   [-o debugfiles.list]
bf96fb
#			   [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
bf96fb
#			   [builddir]
bf96fb
#
bf96fb
# The -g flag says to use strip -g instead of full strip on DSOs or EXEs.
bf96fb
# The --strict-build-id flag says to exit with failure status if
bf96fb
# any ELF binary processed fails to contain a build-id note.
bf96fb
# The -r flag says to use eu-strip --reloc-debug-sections.
bf96fb
#
bf96fb
# A single -o switch before any -l or -p switches simply renames
bf96fb
# the primary output file from debugfiles.list to something else.
bf96fb
# A -o switch that follows a -p switch or some -l switches produces
bf96fb
# an additional output file with the debuginfo for the files in
bf96fb
# the -l filelist file, or whose names match the -p pattern.
bf96fb
# The -p argument is an grep -E -style regexp matching the a file name,
bf96fb
# and must not use anchors (^ or $).
bf96fb
#
bf96fb
# All file names in switches are relative to builddir (. if not given).
bf96fb
#
bf96fb
bf96fb
# With -g arg, pass it to strip on libraries or executables.
bf96fb
strip_g=false
bf96fb
bf96fb
# with -r arg, pass --reloc-debug-sections to eu-strip.
bf96fb
strip_r=false
bf96fb
bf96fb
# Barf on missing build IDs.
bf96fb
strict=false
bf96fb
bf96fb
BUILDDIR=.
bf96fb
out=debugfiles.list
bf96fb
nout=0
bf96fb
while [ $# -gt 0 ]; do
bf96fb
  case "$1" in
bf96fb
  --strict-build-id)
bf96fb
    strict=true
bf96fb
    ;;
bf96fb
  -g)
bf96fb
    strip_g=true
bf96fb
    ;;
bf96fb
  -o)
bf96fb
    if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
bf96fb
      out=$2
bf96fb
    else
bf96fb
      outs[$nout]=$2
bf96fb
      ((nout++))
bf96fb
    fi
bf96fb
    shift
bf96fb
    ;;
bf96fb
  -l)
bf96fb
    lists[$nout]="${lists[$nout]} $2"
bf96fb
    shift
bf96fb
    ;;
bf96fb
  -p)
bf96fb
    ptns[$nout]=$2
bf96fb
    shift
bf96fb
    ;;
bf96fb
  -r)
bf96fb
    strip_r=true
bf96fb
    ;;
bf96fb
  *)
bf96fb
    BUILDDIR=$1
bf96fb
    shift
bf96fb
    break
bf96fb
    ;;
bf96fb
  esac
bf96fb
  shift
bf96fb
done
bf96fb
bf96fb
i=0
bf96fb
while ((i < nout)); do
bf96fb
  outs[$i]="$BUILDDIR/${outs[$i]}"
bf96fb
  l=''
bf96fb
  for f in ${lists[$i]}; do
bf96fb
    l="$l $BUILDDIR/$f"
bf96fb
  done
bf96fb
  lists[$i]=$l
bf96fb
  ((++i))
bf96fb
done
bf96fb
bf96fb
LISTFILE="$BUILDDIR/$out"
bf96fb
SOURCEFILE="$BUILDDIR/debugsources.list"
bf96fb
LINKSFILE="$BUILDDIR/debuglinks.list"
bf96fb
bf96fb
> "$SOURCEFILE"
bf96fb
> "$LISTFILE"
bf96fb
> "$LINKSFILE"
bf96fb
bf96fb
debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
bf96fb
bf96fb
strip_to_debug()
bf96fb
{
bf96fb
  local g=
bf96fb
  local r=
bf96fb
  $strip_r && r=--reloc-debug-sections
bf96fb
  $strip_g && case "$(file -bi "$2")" in
bf96fb
  application/x-sharedlib*) g=-g ;;
bf96fb
  application/x-executable*) g=-g ;;
bf96fb
  esac
bf96fb
  eu-strip --remove-comment $r $g -f "$1" "$2" || exit
bf96fb
  chmod 444 "$1" || exit
bf96fb
}
bf96fb
bf96fb
# Make a relative symlink to $1 called $3$2
bf96fb
shopt -s extglob
bf96fb
link_relative()
bf96fb
{
bf96fb
  local t="$1" f="$2" pfx="$3"
bf96fb
  local fn="${f#/}" tn="${t#/}"
bf96fb
  local fd td d
bf96fb
bf96fb
  while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
bf96fb
    fn="${fn#*/}"
bf96fb
    tn="${tn#*/}"
bf96fb
  done
bf96fb
bf96fb
  d="${fn%/*}"
bf96fb
  if [ "$d" != "$fn" ]; then
bf96fb
    d="${d//+([!\/])/..}"
bf96fb
    tn="${d}/${tn}"
bf96fb
  fi
bf96fb
bf96fb
  mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
bf96fb
}
bf96fb
bf96fb
# Make a symlink in /usr/lib/debug/$2 to $1
bf96fb
debug_link()
bf96fb
{
bf96fb
  local l="/usr/lib/debug$2"
bf96fb
  local t="$1"
bf96fb
  echo >> "$LINKSFILE" "$l $t"
bf96fb
  link_relative "$t" "$l" "$RPM_BUILD_ROOT"
bf96fb
}
bf96fb
bf96fb
# Provide .2, .3, ... symlinks to all filename instances of this build-id.
bf96fb
make_id_dup_link()
bf96fb
{
bf96fb
  local id="$1" file="$2" idfile
bf96fb
bf96fb
  local n=1
bf96fb
  while true; do
bf96fb
    idfile=".build-id/${id:0:2}/${id:2}.$n"
bf96fb
    [ $# -eq 3 ] && idfile="${idfile}$3"
bf96fb
    if [ ! -L "$RPM_BUILD_ROOT/usr/lib/debug/$idfile" ]; then
bf96fb
      break
bf96fb
    fi
bf96fb
    n=$[$n+1]
bf96fb
  done
bf96fb
  debug_link "$file" "/$idfile"
bf96fb
}
bf96fb
bf96fb
# Make a build-id symlink for id $1 with suffix $3 to file $2.
bf96fb
make_id_link()
bf96fb
{
bf96fb
  local id="$1" file="$2"
bf96fb
  local idfile=".build-id/${id:0:2}/${id:2}"
bf96fb
  [ $# -eq 3 ] && idfile="${idfile}$3"
bf96fb
  local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
bf96fb
bf96fb
  if [ ! -L "$root_idfile" ]; then
bf96fb
    debug_link "$file" "/$idfile"
bf96fb
    return
bf96fb
  fi
bf96fb
bf96fb
  make_id_dup_link "$@"
bf96fb
bf96fb
  [ $# -eq 3 ] && return 0
bf96fb
bf96fb
  local other=$(readlink -m "$root_idfile")
bf96fb
  other=${other#$RPM_BUILD_ROOT}
bf96fb
  if cmp -s "$root_idfile" "$RPM_BUILD_ROOT$file" ||
bf96fb
     eu-elfcmp -q "$root_idfile" "$RPM_BUILD_ROOT$file" 2> /dev/null; then
bf96fb
    # Two copies.  Maybe one has to be setuid or something.
bf96fb
    echo >&2 "*** WARNING: identical binaries are copied, not linked:"
bf96fb
    echo >&2 "        $file"
bf96fb
    echo >&2 "   and  $other"
bf96fb
  else
bf96fb
    # This is pathological, break the build.
bf96fb
    echo >&2 "*** ERROR: same build ID in nonidentical files!"
bf96fb
    echo >&2 "        $file"
bf96fb
    echo >&2 "   and  $other"
bf96fb
    exit 2
bf96fb
  fi
bf96fb
}
bf96fb
bf96fb
get_debugfn()
bf96fb
{
bf96fb
  dn=$(dirname "${1#$RPM_BUILD_ROOT}")
bf96fb
  [ "$dn" == "." ] && dn=""
bf96fb
  bn=$(basename "$1" .so).debug
bf96fb
  bn=$(basename "$bn" .debug).debug
bf96fb
  
bf96fb
  debugdn=${debugdir}${dn}
bf96fb
  debugfn=${debugdn}/${bn}
bf96fb
bf96fb
  [ -n "$2" ] && shadowfn=$(basename "$2")
bf96fb
}
bf96fb
bf96fb
set -o pipefail
bf96fb
bf96fb
strict_error=ERROR
bf96fb
$strict || strict_error=WARNING
bf96fb
bf96fb
handle_single_file()
bf96fb
{
bf96fb
  nlinks=$1 && shift
bf96fb
  inum=$1 && shift
bf96fb
  f=$1 && shift
bf96fb
  shadow=$1 && shift
bf96fb
  zf="$f"
bf96fb
  [ -n "${shadowfn}" ] && zf="${shadowfn}"
bf96fb
bf96fb
  get_debugfn "$f" "$shadow"
bf96fb
  [ -f "${debugfn}" ] && return
bf96fb
bf96fb
  # If this file has multiple links, keep track and make
bf96fb
  # the corresponding .debug files all links to one file too.
bf96fb
  if [ $nlinks -gt 1 ]; then
bf96fb
    eval linked=\$linked_$inum
bf96fb
    if [ -n "$linked" ]; then
bf96fb
      eval id=\$linkedid_$inum
bf96fb
      make_id_dup_link "$id" "$dn/$(basename ${zf})"
bf96fb
      make_id_dup_link "$id" "/usr/lib/debug$dn/$bn" .debug
bf96fb
      link=$debugfn
bf96fb
      get_debugfn "$linked"
bf96fb
      echo "hard linked $link to $debugfn"
bf96fb
      mkdir -p "$(dirname "$link")" && ln -nf "$debugfn" "$link"
bf96fb
      return
bf96fb
    else
bf96fb
      eval linked_$inum=\$f
bf96fb
      echo "file $f has $[$nlinks - 1] other hard links"
bf96fb
    fi
bf96fb
  fi
bf96fb
bf96fb
  echo "extracting debug info from $f"
bf96fb
  echo /usr/lib/rpm/debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
bf96fb
			      -i -l "$SOURCEFILE" "$f"
bf96fb
  id=$(/usr/lib/rpm/debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
bf96fb
			      -i -l "$SOURCEFILE" "$f") || exit
bf96fb
  if [ $nlinks -gt 1 ]; then
bf96fb
    eval linkedid_$inum=\$id
bf96fb
  fi
bf96fb
  if [ -z "$id" ]; then
bf96fb
    echo >&2 "*** ${strict_error}: No build ID note found in $zf"
bf96fb
    $strict && exit 2
bf96fb
  fi
bf96fb
bf96fb
  [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$zf" > /dev/null 2>&1
bf96fb
bf96fb
  # A binary already copied into /usr/lib/debug doesn't get stripped,
bf96fb
  # just has its file names collected and adjusted.
bf96fb
  case "$dn" in
bf96fb
  /usr/lib/debug/*)
bf96fb
    [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $zf)"
bf96fb
    return ;;
bf96fb
  esac
bf96fb
bf96fb
  mkdir -p "${debugdn}"
bf96fb
  if test -w "$f"; then
bf96fb
    strip_to_debug "${debugfn}" "$f"
bf96fb
  else
bf96fb
    chmod u+w "$f"
bf96fb
    strip_to_debug "${debugfn}" "$f"
bf96fb
    chmod u-w "$f"
bf96fb
  fi
bf96fb
bf96fb
  if [ -n "$id" ]; then
bf96fb
    make_id_link "$id" "$dn/$(basename ${zf})"
bf96fb
    make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
bf96fb
  fi
bf96fb
}
bf96fb
bf96fb
# Strip ELF binaries
bf96fb
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
bf96fb
     		     \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
bf96fb
		     -print |
bf96fb
file -N -f - | sed -n -e 's/^\(.*\):[ 	]*.*ELF.*, not stripped.*/\1/p' |
bf96fb
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
bf96fb
while read nlinks inum f; do
bf96fb
    handle_single_file $nlinks $inum $f
bf96fb
done || exit
bf96fb
bf96fb
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
bf96fb
                     -iname '*.efi' -print |
bf96fb
file -N -f - | sed -n -e 's/^\(.*\):[ 	]*.*PE32.*EFI .*/\1/p' |
bf96fb
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
bf96fb
while read nlinks inum f; do
bf96fb
    [ -f "$f" ] || continue
bf96fb
    [ -f "${f%%.efi}.so" ] || continue
bf96fb
    handle_single_file $nlinks $inum ${f%%.efi}.so $f
bf96fb
done || exit
bf96fb
bf96fb
# On Aarch64 file tells us "MS-DOS" instead of PE32+.  Why not.
bf96fb
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
bf96fb
                     -iname '*.efi' -print |
bf96fb
file -N -f - | sed -n -e 's/^\(.*\):[ 	]MS-DOS.*/\1/p' |
bf96fb
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
bf96fb
while read nlinks inum f; do
bf96fb
    [ -f "$f" ] || continue
bf96fb
    [ -f "${f%%.efi}.so" ] || continue
bf96fb
    handle_single_file $nlinks $inum ${f%%.efi}.so $f
bf96fb
done || exit
bf96fb
# For each symlink whose target has a .debug file,
bf96fb
# make a .debug symlink to that file.
bf96fb
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*" -type l -print |
bf96fb
while read f
bf96fb
do
bf96fb
  t=$(readlink -m "$f").debug
bf96fb
  f=${f#$RPM_BUILD_ROOT}
bf96fb
  t=${t#$RPM_BUILD_ROOT}
bf96fb
  if [ -f "$debugdir$t" ]; then
bf96fb
    echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
bf96fb
    debug_link "/usr/lib/debug$t" "${f}.debug"
bf96fb
  fi
bf96fb
done
bf96fb
bf96fb
if [ -s "$SOURCEFILE" ]; then
bf96fb
  mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
bf96fb
  LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
bf96fb
  (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
bf96fb
  # stupid cpio creates new directories in mode 0700, fixup
bf96fb
  find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
bf96fb
  xargs --no-run-if-empty -0 chmod a+rx
bf96fb
fi
bf96fb
bf96fb
if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
bf96fb
  ((nout > 0)) ||
bf96fb
  test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
bf96fb
  (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
bf96fb
  sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
bf96fb
bf96fb
  (cd "${RPM_BUILD_ROOT}/usr"
bf96fb
   test ! -d lib/debug || find lib/debug ! -type d
bf96fb
   test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
bf96fb
  ) | sed 's,^,/usr/,' >> "$LISTFILE"
bf96fb
fi
bf96fb
bf96fb
# Append to $1 only the lines from stdin not already in the file.
bf96fb
append_uniq()
bf96fb
{
bf96fb
  grep -F -f "$1" -x -v >> "$1"
bf96fb
}
bf96fb
bf96fb
# Helper to generate list of corresponding .debug files from a file list.
bf96fb
filelist_debugfiles()
bf96fb
{
bf96fb
  local extra="$1"
bf96fb
  shift
bf96fb
  sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
bf96fb
s/^%[a-z0-9_][a-z0-9_]* *//
bf96fb
/^$/d
bf96fb
'"$extra" "$@"
bf96fb
}
bf96fb
bf96fb
# Write an output debuginfo file list based on given input file lists.
bf96fb
filtered_list()
bf96fb
{
bf96fb
  local out="$1"
bf96fb
  shift
bf96fb
  test $# -gt 0 || return
bf96fb
  grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
bf96fb
  	-x $LISTFILE >> $out
bf96fb
  sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
bf96fb
h
bf96fb
s,^.*$,s# &$##p,p
bf96fb
g
bf96fb
s,^.*$,s# /usr/lib/debug&.debug$##p,p
bf96fb
' "$@") "$LINKSFILE" | append_uniq "$out"
bf96fb
}
bf96fb
bf96fb
# Write an output debuginfo file list based on an grep -E -style regexp.
bf96fb
pattern_list()
bf96fb
{
bf96fb
  local out="$1" ptn="$2"
bf96fb
  test -n "$ptn" || return
bf96fb
  grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
bf96fb
  sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
bf96fb
}
bf96fb
bf96fb
#
bf96fb
# When given multiple -o switches, split up the output as directed.
bf96fb
#
bf96fb
i=0
bf96fb
while ((i < nout)); do
bf96fb
  > ${outs[$i]}
bf96fb
  filtered_list ${outs[$i]} ${lists[$i]}
bf96fb
  pattern_list ${outs[$i]} "${ptns[$i]}"
bf96fb
  grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
bf96fb
  mv "${LISTFILE}.new" "$LISTFILE"
bf96fb
  ((++i))
bf96fb
done
bf96fb
if ((nout > 0)); then
bf96fb
  # Now add the right %dir lines to each output list.
bf96fb
  (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
bf96fb
  sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@;;g;}#' |
bf96fb
  LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
bf96fb
  i=0
bf96fb
  while ((i < nout)); do
bf96fb
    sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
bf96fb
    cat "${outs[$i]}" >> "${outs[$i]}.new"
bf96fb
    mv -f "${outs[$i]}.new" "${outs[$i]}"
bf96fb
    ((++i))
bf96fb
  done
bf96fb
  sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
bf96fb
  cat "$LISTFILE" >> "${LISTFILE}.new"
bf96fb
  mv "${LISTFILE}.new" "$LISTFILE"
bf96fb
fi