Blame SOURCES/find-debuginfo.sh

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