arrfab / rpms / shim

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