Blame SOURCES/find-debuginfo.sh

ab7326
#!/bin/bash
ab7326
ab7326
# NB: this is a patched version of find-debuginfo.sh from rpm-build package,
ab7326
# this one supports parallel extraction. (We have ~4000 modules to process!)
ab7326
# See https://bugzilla.redhat.com/show_bug.cgi?id=1586159
ab7326
# for how to recreate this script from rpm*.src.rpm
ab7326
ab7326
#find-debuginfo.sh - automagically generate debug info and file list
ab7326
#for inclusion in an rpm spec file.
ab7326
#
ab7326
# Usage: find-debuginfo.sh [--strict-build-id] [-g] [-r] [-m]
ab7326
#			   [-j N] [--jobs N]
ab7326
#			   [--g-libs]
ab7326
#	 		   [-o debugfiles.list]
ab7326
#			   [--run-dwz] [--dwz-low-mem-die-limit N]
ab7326
#			   [--dwz-max-die-limit N]
ab7326
#			   [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
ab7326
#			   [builddir]
ab7326
#
ab7326
# The -g flag says to use strip -g instead of full strip on DSOs or EXEs.
ab7326
# The --g-libs flag says to use strip -g instead of full strip ONLY on DSOs.
ab7326
# Options -g and --g-libs are mutually exclusive.
ab7326
# The --strict-build-id flag says to exit with failure status if
ab7326
# any ELF binary processed fails to contain a build-id note.
ab7326
# The -r flag says to use eu-strip --reloc-debug-sections.
ab7326
#
ab7326
# The -j, --jobs N option will spawn N processes to do the debuginfo
ab7326
# extraction in parallel.
ab7326
#
ab7326
# A single -o switch before any -l or -p switches simply renames
ab7326
# the primary output file from debugfiles.list to something else.
ab7326
# A -o switch that follows a -p switch or some -l switches produces
ab7326
# an additional output file with the debuginfo for the files in
ab7326
# the -l filelist file, or whose names match the -p pattern.
ab7326
# The -p argument is an grep -E -style regexp matching the a file name,
ab7326
# and must not use anchors (^ or $).
ab7326
#
ab7326
# The --run-dwz flag instructs find-debuginfo.sh to run the dwz utility
ab7326
# if available, and --dwz-low-mem-die-limit and --dwz-max-die-limit
ab7326
# provide detailed limits.  See dwz(1) -l and -L option for details.
ab7326
#
ab7326
# All file names in switches are relative to builddir (. if not given).
ab7326
#
ab7326
ab7326
# With -g arg, pass it to strip on libraries or executables.
ab7326
strip_g=false
ab7326
ab7326
# With --g-libs arg, pass it to strip on libraries.
ab7326
strip_glibs=false
ab7326
ab7326
# with -r arg, pass --reloc-debug-sections to eu-strip.
ab7326
strip_r=false
ab7326
ab7326
# with -m arg, add minimal debuginfo to binary.
ab7326
include_minidebug=false
ab7326
ab7326
# Barf on missing build IDs.
ab7326
strict=false
ab7326
ab7326
# DWZ parameters.
ab7326
run_dwz=false
ab7326
dwz_low_mem_die_limit=
ab7326
dwz_max_die_limit=
ab7326
ab7326
# Number of parallel jobs to spawn
ab7326
n_jobs=1
ab7326
ab7326
BUILDDIR=.
ab7326
out=debugfiles.list
ab7326
nout=0
ab7326
while [ $# -gt 0 ]; do
ab7326
  case "$1" in
ab7326
  --strict-build-id)
ab7326
    strict=true
ab7326
    ;;
ab7326
  --run-dwz)
ab7326
    run_dwz=true
ab7326
    ;;
ab7326
  --dwz-low-mem-die-limit)
ab7326
    dwz_low_mem_die_limit=$2
ab7326
    shift
ab7326
    ;;
ab7326
  --dwz-max-die-limit)
ab7326
    dwz_max_die_limit=$2
ab7326
    shift
ab7326
    ;;
ab7326
  --g-libs)
ab7326
    strip_glibs=true
ab7326
    ;;
ab7326
  -g)
ab7326
    strip_g=true
ab7326
    ;;
ab7326
  -m)
ab7326
    include_minidebug=true
ab7326
    ;;
ab7326
  -o)
ab7326
    if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
ab7326
      out=$2
ab7326
    else
ab7326
      outs[$nout]=$2
ab7326
      ((nout++))
ab7326
    fi
ab7326
    shift
ab7326
    ;;
ab7326
  -l)
ab7326
    lists[$nout]="${lists[$nout]} $2"
ab7326
    shift
ab7326
    ;;
ab7326
  -p)
ab7326
    ptns[$nout]=$2
ab7326
    shift
ab7326
    ;;
ab7326
  -r)
ab7326
    strip_r=true
ab7326
    ;;
ab7326
  -j)
ab7326
    n_jobs=$2
ab7326
    shift
ab7326
    ;;
ab7326
  -j*)
ab7326
    n_jobs=${1#-j}
ab7326
    ;;
ab7326
  --jobs)
ab7326
    n_jobs=$2
ab7326
    shift
ab7326
    ;;
ab7326
  -*)
ab7326
    echo >&2 "find-debuginfo.sh: warning: unknown option '$1'"
ab7326
    ;;
ab7326
  *)
ab7326
    BUILDDIR=$1
ab7326
    shift
ab7326
    break
ab7326
    ;;
ab7326
  esac
ab7326
  shift
ab7326
done
ab7326
ab7326
if ("$strip_g" = "true") && ("$strip_glibs" = "true"); then
ab7326
  echo >&2 "*** ERROR: -g  and --g-libs cannot be used together"
ab7326
  exit 2
ab7326
fi
ab7326
ab7326
i=0
ab7326
while ((i < nout)); do
ab7326
  outs[$i]="$BUILDDIR/${outs[$i]}"
ab7326
  l=''
ab7326
  for f in ${lists[$i]}; do
ab7326
    l="$l $BUILDDIR/$f"
ab7326
  done
ab7326
  lists[$i]=$l
ab7326
  ((++i))
ab7326
done
ab7326
ab7326
LISTFILE="$BUILDDIR/$out"
ab7326
SOURCEFILE="$BUILDDIR/debugsources.list"
ab7326
LINKSFILE="$BUILDDIR/debuglinks.list"
ab7326
ELFBINSFILE="$BUILDDIR/elfbins.list"
ab7326
ab7326
> "$SOURCEFILE"
ab7326
> "$LISTFILE"
ab7326
> "$LINKSFILE"
ab7326
> "$ELFBINSFILE"
ab7326
ab7326
debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
ab7326
ab7326
strip_to_debug()
ab7326
{
ab7326
  local g=
ab7326
  local r=
ab7326
  $strip_r && r=--reloc-debug-sections
ab7326
  $strip_g && case "$(file -bi "$2")" in
ab7326
  application/x-sharedlib*) g=-g ;;
ab7326
  application/x-executable*) g=-g ;;
ab7326
  esac
ab7326
  $strip_glibs && case "$(file -bi "$2")" in
ab7326
    application/x-sharedlib*) g=-g ;;
ab7326
  esac
ab7326
  eu-strip --remove-comment $r $g -f "$1" "$2" || exit
ab7326
  chmod 444 "$1" || exit
ab7326
}
ab7326
ab7326
add_minidebug()
ab7326
{
ab7326
  local debuginfo="$1"
ab7326
  local binary="$2"
ab7326
ab7326
  local dynsyms=`mktemp`
ab7326
  local funcsyms=`mktemp`
ab7326
  local keep_symbols=`mktemp`
ab7326
  local mini_debuginfo=`mktemp`
ab7326
ab7326
  # Extract the dynamic symbols from the main binary, there is no need to also have these
ab7326
  # in the normal symbol table
ab7326
  nm -D "$binary" --format=posix --defined-only | awk '{ print $1 }' | sort > "$dynsyms"
ab7326
  # Extract all the text (i.e. function) symbols from the debuginfo 
ab7326
  # Use format sysv to make sure we can match against the actual ELF FUNC
ab7326
  # symbol type. The binutils nm posix format symbol type chars are
ab7326
  # ambigous for architectures that might use function descriptors.
ab7326
  nm "$debuginfo" --format=sysv --defined-only | awk -F \| '{ if ($4 ~ "FUNC") print $1 }' | sort > "$funcsyms"
ab7326
  # Keep all the function symbols not already in the dynamic symbol table
ab7326
  comm -13 "$dynsyms" "$funcsyms" > "$keep_symbols"
ab7326
  # Copy the full debuginfo, keeping only a minumal set of symbols and removing some unnecessary sections
ab7326
  objcopy -S --remove-section .gdb_index --remove-section .comment --keep-symbols="$keep_symbols" "$debuginfo" "$mini_debuginfo" &> /dev/null
ab7326
  #Inject the compressed data into the .gnu_debugdata section of the original binary
ab7326
  xz "$mini_debuginfo"
ab7326
  mini_debuginfo="${mini_debuginfo}.xz"
ab7326
  objcopy --add-section .gnu_debugdata="$mini_debuginfo" "$binary"
ab7326
  rm -f "$dynsyms" "$funcsyms" "$keep_symbols" "$mini_debuginfo"
ab7326
}
ab7326
ab7326
# Make a relative symlink to $1 called $3$2
ab7326
shopt -s extglob
ab7326
link_relative()
ab7326
{
ab7326
  local t="$1" f="$2" pfx="$3"
ab7326
  local fn="${f#/}" tn="${t#/}"
ab7326
  local fd td d
ab7326
ab7326
  while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
ab7326
    fn="${fn#*/}"
ab7326
    tn="${tn#*/}"
ab7326
  done
ab7326
ab7326
  d="${fn%/*}"
ab7326
  if [ "$d" != "$fn" ]; then
ab7326
    d="${d//+([!\/])/..}"
ab7326
    tn="${d}/${tn}"
ab7326
  fi
ab7326
ab7326
  mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
ab7326
}
ab7326
ab7326
# Make a symlink in /usr/lib/debug/$2 to $1
ab7326
debug_link()
ab7326
{
ab7326
  local l="/usr/lib/debug$2"
ab7326
  local t="$1"
ab7326
  echo >> "$LINKSFILE" "$l $t"
ab7326
  link_relative "$t" "$l" "$RPM_BUILD_ROOT"
ab7326
}
ab7326
ab7326
# Provide .2, .3, ... symlinks to all filename instances of this build-id.
ab7326
make_id_dup_link()
ab7326
{
ab7326
  local id="$1" file="$2" idfile
ab7326
ab7326
  local n=1
ab7326
  while true; do
ab7326
    idfile=".build-id/${id:0:2}/${id:2}.$n"
ab7326
    [ $# -eq 3 ] && idfile="${idfile}$3"
ab7326
    if [ ! -L "$RPM_BUILD_ROOT/usr/lib/debug/$idfile" ]; then
ab7326
      break
ab7326
    fi
ab7326
    n=$[$n+1]
ab7326
  done
ab7326
  debug_link "$file" "/$idfile"
ab7326
}
ab7326
ab7326
# Make a build-id symlink for id $1 with suffix $3 to file $2.
ab7326
make_id_link()
ab7326
{
ab7326
  local id="$1" file="$2"
ab7326
  local idfile=".build-id/${id:0:2}/${id:2}"
ab7326
  [ $# -eq 3 ] && idfile="${idfile}$3"
ab7326
  local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
ab7326
ab7326
  if [ ! -L "$root_idfile" ]; then
ab7326
    debug_link "$file" "/$idfile"
ab7326
    return
ab7326
  fi
ab7326
ab7326
  make_id_dup_link "$@"
ab7326
ab7326
  [ $# -eq 3 ] && return 0
ab7326
ab7326
  local other=$(readlink -m "$root_idfile")
ab7326
  other=${other#$RPM_BUILD_ROOT}
ab7326
  if cmp -s "$root_idfile" "$RPM_BUILD_ROOT$file" ||
ab7326
     eu-elfcmp -q "$root_idfile" "$RPM_BUILD_ROOT$file" 2> /dev/null; then
ab7326
    # Two copies.  Maybe one has to be setuid or something.
ab7326
    echo >&2 "*** WARNING: identical binaries are copied, not linked:"
ab7326
    echo >&2 "        $file"
ab7326
    echo >&2 "   and  $other"
ab7326
  else
ab7326
    # This is pathological, break the build.
ab7326
    echo >&2 "*** ERROR: same build ID in nonidentical files!"
ab7326
    echo >&2 "        $file"
ab7326
    echo >&2 "   and  $other"
ab7326
    exit 2
ab7326
  fi
ab7326
}
ab7326
ab7326
get_debugfn()
ab7326
{
ab7326
  dn=$(dirname "${1#$RPM_BUILD_ROOT}")
ab7326
  bn=$(basename "$1" .debug).debug
ab7326
ab7326
  debugdn=${debugdir}${dn}
ab7326
  debugfn=${debugdn}/${bn}
ab7326
}
ab7326
ab7326
set -o pipefail
ab7326
ab7326
strict_error=ERROR
ab7326
$strict || strict_error=WARNING
ab7326
ab7326
temp=$(mktemp -d ${TMPDIR:-/tmp}/find-debuginfo.XXXXXX)
ab7326
trap 'rm -rf "$temp"' EXIT
ab7326
ab7326
# Build a list of unstripped ELF files and their hardlinks
ab7326
touch "$temp/primary"
ab7326
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
ab7326
     		     \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
ab7326
		     -print |
ab7326
file -N -f - | sed -n -e 's/^\(.*\):[ 	]*.*ELF.*, not stripped.*/\1/p' |
ab7326
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
ab7326
while read nlinks inum f; do
ab7326
  if [ $nlinks -gt 1 ]; then
ab7326
    var=seen_$inum
ab7326
    if test -n "${!var}"; then
ab7326
      echo "$inum $f" >>"$temp/linked"
ab7326
      continue
ab7326
    else
ab7326
      read "$var" < <(echo 1)
ab7326
    fi
ab7326
  fi
ab7326
  echo "$nlinks $inum $f" >>"$temp/primary"
ab7326
done
ab7326
ab7326
# Strip ELF binaries
ab7326
do_file()
ab7326
{
ab7326
  local nlinks=$1 inum=$2 f=$3 id link linked
ab7326
ab7326
  get_debugfn "$f"
ab7326
  [ -f "${debugfn}" ] && return
ab7326
ab7326
  echo "extracting debug info from $f"
ab7326
  id=$(/usr/lib/rpm/debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
ab7326
			      -i -l "$SOURCEFILE" "$f") || exit
ab7326
  if [ -z "$id" ]; then
ab7326
    echo >&2 "*** ${strict_error}: No build ID note found in $f"
ab7326
    $strict && exit 2
ab7326
  fi
ab7326
ab7326
  [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$f" > /dev/null 2>&1
ab7326
ab7326
  # A binary already copied into /usr/lib/debug doesn't get stripped,
ab7326
  # just has its file names collected and adjusted.
ab7326
  case "$dn" in
ab7326
  /usr/lib/debug/*)
ab7326
    [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
ab7326
    return ;;
ab7326
  esac
ab7326
ab7326
  mkdir -p "${debugdn}"
ab7326
  if test -w "$f"; then
ab7326
    strip_to_debug "${debugfn}" "$f"
ab7326
  else
ab7326
    chmod u+w "$f"
ab7326
    strip_to_debug "${debugfn}" "$f"
ab7326
    chmod u-w "$f"
ab7326
  fi
ab7326
ab7326
  # strip -g implies we have full symtab, don't add mini symtab in that case.
ab7326
  # It only makes sense to add a minisymtab for executables and shared
ab7326
  # libraries. Other executable ELF files (like kernel modules) don't need it.
ab7326
  if [ "$include_minidebug" = "true" -a "$strip_g" = "false" ]; then
ab7326
    skip_mini=true
ab7326
    if [ "$strip_glibs" = "false" ]; then
ab7326
      case "$(file -bi "$f")" in
ab7326
        application/x-sharedlib*) skip_mini=false ;;
ab7326
      esac
ab7326
    fi
ab7326
    case "$(file -bi "$f")" in
ab7326
      application/x-sharedlib*) skip_mini=false ;;
ab7326
      application/x-executable*) skip_mini=false ;;
ab7326
      application/x-pie-executable*) skip_mini=false ;;
ab7326
    esac
ab7326
    $skip_mini || add_minidebug "${debugfn}" "$f"
ab7326
  fi
ab7326
ab7326
  echo "./${f#$RPM_BUILD_ROOT}" >> "$ELFBINSFILE"
ab7326
  
ab7326
  if [ -n "$id" ]; then
ab7326
    make_id_link "$id" "$dn/$(basename $f)"
ab7326
    make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
ab7326
  fi
ab7326
ab7326
  # If this file has multiple links, make the corresponding .debug files
ab7326
  # all links to one file too.
ab7326
  if [ $nlinks -gt 1 ]; then
ab7326
    grep "^$inum " "$temp/linked" | while read inum linked; do
ab7326
      make_id_dup_link "$id" "$dn/$(basename $f)"
ab7326
      make_id_dup_link "$id" "/usr/lib/debug$dn/$bn" .debug
ab7326
      link=$debugfn
ab7326
      get_debugfn "$linked"
ab7326
      echo "hard linked $link to $debugfn"
ab7326
      mkdir -p "$(dirname "$debugfn")" && ln -nf "$link" "$debugfn"
ab7326
    done
ab7326
  fi
ab7326
}
ab7326
ab7326
# 16^6 - 1 or about 16 milion files
ab7326
FILENUM_DIGITS=6
ab7326
run_job()
ab7326
{
ab7326
  local jobid=$1 filenum
ab7326
  local SOURCEFILE=$temp/debugsources.$jobid ELFBINSFILE=$temp/elfbins.$jobid
ab7326
ab7326
  >"$SOURCEFILE"
ab7326
  >"$ELFBINSFILE"
ab7326
  # can't use read -n <n>, because it reads bytes one by one, allowing for
ab7326
  # races
ab7326
  while :; do
ab7326
    filenum=$(dd bs=$(( FILENUM_DIGITS + 1 )) count=1 status=none)
ab7326
    if test -z "$filenum"; then
ab7326
      break
ab7326
    fi
ab7326
    do_file $(sed -n "$(( 0x$filenum )) p" "$temp/primary")
ab7326
  done
ab7326
  echo 0 >"$temp/res.$jobid"
ab7326
}
ab7326
ab7326
n_files=$(wc -l <"$temp/primary")
ab7326
if [ $n_jobs -gt $n_files ]; then
ab7326
  n_jobs=$n_files
ab7326
fi
ab7326
if [ $n_jobs -le 1 ]; then
ab7326
  while read nlinks inum f; do
ab7326
    do_file "$nlinks" "$inum" "$f"
ab7326
  done <"$temp/primary"
ab7326
else
ab7326
  for ((i = 1; i <= n_files; i++)); do
ab7326
    printf "%0${FILENUM_DIGITS}x\\n" $i
ab7326
  done | (
ab7326
    exec 3<&0
ab7326
    for ((i = 0; i < n_jobs; i++)); do
ab7326
      # The shell redirects stdin to /dev/null for background jobs. Work
ab7326
      # around this by duplicating fd 0
ab7326
      run_job $i <&3 &
ab7326
    done
ab7326
    wait
ab7326
  )
ab7326
  for f in "$temp"/res.*; do
ab7326
    res=$(< "$f")
ab7326
    if [ "$res" !=  "0" ]; then
ab7326
      exit 1
ab7326
    fi
ab7326
  done
ab7326
  cat "$temp"/debugsources.* >"$SOURCEFILE"
ab7326
  cat "$temp"/elfbins.* >"$ELFBINSFILE"
ab7326
fi
ab7326
ab7326
# Invoke the DWARF Compressor utility.
ab7326
if $run_dwz && type dwz >/dev/null 2>&1 \
ab7326
   && [ -d "${RPM_BUILD_ROOT}/usr/lib/debug" ]; then
ab7326
  dwz_files="`cd "${RPM_BUILD_ROOT}/usr/lib/debug"; find -type f -name \*.debug`"
ab7326
  if [ -n "${dwz_files}" ]; then
ab7326
    dwz_multifile_name="${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE}.${RPM_ARCH}"
ab7326
    dwz_multifile_suffix=
ab7326
    dwz_multifile_idx=0
ab7326
    while [ -f "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}${dwz_multifile_suffix}" ]; do
ab7326
      let ++dwz_multifile_idx
ab7326
      dwz_multifile_suffix=".${dwz_multifile_idx}"
ab7326
    done
ab7326
    dwz_multfile_name="${dwz_multifile_name}${dwz_multifile_suffix}"
ab7326
    dwz_opts="-h -q -r -m .dwz/${dwz_multifile_name}"
ab7326
    mkdir -p "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz"
ab7326
    [ -n "${dwz_low_mem_die_limit}" ] \
ab7326
      && dwz_opts="${dwz_opts} -l ${dwz_low_mem_die_limit}"
ab7326
    [ -n "${dwz_max_die_limit}" ] \
ab7326
      && dwz_opts="${dwz_opts} -L ${dwz_max_die_limit}"
ab7326
    ( cd "${RPM_BUILD_ROOT}/usr/lib/debug" && dwz $dwz_opts $dwz_files )
ab7326
    # Remove .dwz directory if empty
ab7326
    rmdir "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz" 2>/dev/null
ab7326
    if [ -f "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}" ]; then
ab7326
      id="`readelf -Wn "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}" \
ab7326
	     2>/dev/null | sed -n 's/^    Build ID: \([0-9a-f]\+\)/\1/p'`"
ab7326
      [ -n "$id" ] \
ab7326
	&& make_id_link "$id" "/usr/lib/debug/.dwz/${dwz_multifile_name}" .debug
ab7326
    fi
ab7326
  fi
ab7326
fi
ab7326
ab7326
# dwz invalidates .gnu_debuglink CRC32 in the main files.
ab7326
cat "$ELFBINSFILE" |
ab7326
(cd "$RPM_BUILD_ROOT"; xargs -d '\n' /usr/lib/rpm/sepdebugcrcfix usr/lib/debug)
ab7326
ab7326
# For each symlink whose target has a .debug file,
ab7326
# make a .debug symlink to that file.
ab7326
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*" -type l -print |
ab7326
while read f
ab7326
do
ab7326
  t=$(readlink -m "$f").debug
ab7326
  f=${f#$RPM_BUILD_ROOT}
ab7326
  t=${t#$RPM_BUILD_ROOT}
ab7326
  if [ -f "$debugdir$t" ]; then
ab7326
    echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
ab7326
    debug_link "/usr/lib/debug$t" "${f}.debug"
ab7326
  fi
ab7326
done
ab7326
ab7326
if [ -s "$SOURCEFILE" ]; then
ab7326
  mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
ab7326
  LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
ab7326
  (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
ab7326
  # stupid cpio creates new directories in mode 0700,
ab7326
  # and non-standard modes may be inherented from original directories, fixup
ab7326
  find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
ab7326
  xargs --no-run-if-empty -0 chmod 0755
ab7326
fi
ab7326
ab7326
if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
ab7326
  ((nout > 0)) ||
ab7326
  test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
ab7326
  (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
ab7326
  sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
ab7326
ab7326
  (cd "${RPM_BUILD_ROOT}/usr"
ab7326
   test ! -d lib/debug || find lib/debug ! -type d
ab7326
   test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
ab7326
  ) | sed 's,^,/usr/,' >> "$LISTFILE"
ab7326
fi
ab7326
ab7326
# Append to $1 only the lines from stdin not already in the file.
ab7326
append_uniq()
ab7326
{
ab7326
  grep -F -f "$1" -x -v >> "$1"
ab7326
}
ab7326
ab7326
# Helper to generate list of corresponding .debug files from a file list.
ab7326
filelist_debugfiles()
ab7326
{
ab7326
  local extra="$1"
ab7326
  shift
ab7326
  sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
ab7326
s/^%[a-z0-9_][a-z0-9_]* *//
ab7326
/^$/d
ab7326
'"$extra" "$@"
ab7326
}
ab7326
ab7326
# Write an output debuginfo file list based on given input file lists.
ab7326
filtered_list()
ab7326
{
ab7326
  local out="$1"
ab7326
  shift
ab7326
  test $# -gt 0 || return
ab7326
  grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
ab7326
  	-x $LISTFILE >> $out
ab7326
  sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
ab7326
h
ab7326
s,^.*$,s# &$##p,p
ab7326
g
ab7326
s,^.*$,s# /usr/lib/debug&.debug$##p,p
ab7326
' "$@") "$LINKSFILE" | append_uniq "$out"
ab7326
}
ab7326
ab7326
# Write an output debuginfo file list based on an grep -E -style regexp.
ab7326
pattern_list()
ab7326
{
ab7326
  local out="$1" ptn="$2"
ab7326
  test -n "$ptn" || return
ab7326
  grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
ab7326
  sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
ab7326
}
ab7326
ab7326
#
ab7326
# When given multiple -o switches, split up the output as directed.
ab7326
#
ab7326
i=0
ab7326
while ((i < nout)); do
ab7326
  > ${outs[$i]}
ab7326
  filtered_list ${outs[$i]} ${lists[$i]}
ab7326
  pattern_list ${outs[$i]} "${ptns[$i]}"
ab7326
  grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
ab7326
  mv "${LISTFILE}.new" "$LISTFILE"
ab7326
  ((++i))
ab7326
done
ab7326
if ((nout > 0)); then
ab7326
  # Now add the right %dir lines to each output list.
ab7326
  (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
ab7326
  sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@;;g;}#' |
ab7326
  LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
ab7326
  i=0
ab7326
  while ((i < nout)); do
ab7326
    sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
ab7326
    cat "${outs[$i]}" >> "${outs[$i]}.new"
ab7326
    mv -f "${outs[$i]}.new" "${outs[$i]}"
ab7326
    ((++i))
ab7326
  done
ab7326
  sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
ab7326
  cat "$LISTFILE" >> "${LISTFILE}.new"
ab7326
  mv "${LISTFILE}.new" "$LISTFILE"
ab7326
fi