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