orgads / rpms / kernel

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