Blame SOURCES/shim-find-debuginfo.sh

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