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