csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
#!/bin/bash
2ff057
#find-debuginfo.sh - automagically generate debug info and file list
2ff057
#for inclusion in an rpm spec file.
2ff057
#
2ff057
# Usage: find-debuginfo.sh [--strict-build-id] [-g] [-r] [-m] [-i] [-n]
2ff057
#			   [--keep-section SECTION] [--remove-section SECTION]
2ff057
#	 		   [-j N] [--jobs N]
2ff057
#	 		   [-o debugfiles.list]
2ff057
#	 		   [-S debugsourcefiles.list]
2ff057
#			   [--run-dwz] [--dwz-low-mem-die-limit N]
2ff057
#			   [--dwz-max-die-limit N]
2ff057
#			   [--build-id-seed SEED]
2ff057
#			   [--unique-debug-suffix SUFFIX]
2ff057
#			   [--unique-debug-src-base BASE]
2ff057
#			   [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
2ff057
#			   [builddir]
2ff057
#
2ff057
# The -g flag says to use strip -g instead of full strip on DSOs or EXEs.
2ff057
# The -r flag says to use eu-strip --reloc-debug-sections.
2ff057
# Use --keep-section SECTION or --remove-section SECTION to explicitly
2ff057
# keep a (non-allocated) section in the main executable or explicitly
2ff057
# remove it into the .debug file. SECTION is an extended wildcard pattern.
2ff057
# Both options can be given more than once.
2ff057
#
2ff057
# The --strict-build-id flag says to exit with failure status if
2ff057
# any ELF binary processed fails to contain a build-id note.
2ff057
# The -m flag says to include a .gnu_debugdata section in the main binary.
2ff057
# The -i flag says to include a .gdb_index section in the .debug file.
2ff057
# The -n flag says to not recompute the build-id.
2ff057
#
2ff057
# The -j, --jobs N option will spawn N processes to do the debuginfo
2ff057
# extraction in parallel.
2ff057
#
2ff057
# A single -o switch before any -l or -p switches simply renames
2ff057
# the primary output file from debugfiles.list to something else.
2ff057
# A -o switch that follows a -p switch or some -l switches produces
2ff057
# an additional output file with the debuginfo for the files in
2ff057
# the -l filelist file, or whose names match the -p pattern.
2ff057
# The -p argument is an grep -E -style regexp matching the a file name,
2ff057
# and must not use anchors (^ or $).
2ff057
#
2ff057
# The --run-dwz flag instructs find-debuginfo.sh to run the dwz utility
2ff057
# if available, and --dwz-low-mem-die-limit and --dwz-max-die-limit
2ff057
# provide detailed limits.  See dwz(1) -l and -L option for details.
2ff057
#
2ff057
# If --build-id-seed SEED is given then debugedit is called to
2ff057
# update the build-ids it finds adding the SEED as seed to recalculate
2ff057
# the build-id hash.  This makes sure the build-ids in the ELF files
2ff057
# are unique between versions and releases of the same package.
2ff057
# (Use --build-id-seed "%{VERSION}-%{RELEASE}".)
2ff057
#
2ff057
# If --unique-debug-suffix SUFFIX is given then the debug files created
2ff057
# for <FILE> will be named <FILE>-<SUFFIX>.debug.  This makes sure .debug
2ff057
# are unique between package version, release and architecture.
2ff057
# (Use --unique-debug-suffix "-%{VERSION}-%{RELEASE}.%{_arch}".)
2ff057
#
2ff057
# If --unique-debug-src-base BASE is given then the source directory
2ff057
# will be called /usr/debug/src/<BASE>.  This makes sure the debug source
2ff057
# directories are unique between package version, release and architecture.
2ff057
# (Use --unique-debug-src-base "%{name}-%{VERSION}-%{RELEASE}.%{_arch}".)
2ff057
#
2ff057
# All file names in switches are relative to builddir (. if not given).
2ff057
#
2ff057
2ff057
# Figure out where we are installed so we can call other helper scripts.
2ff057
lib_rpm_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2ff057
2ff057
# With -g arg, pass it to strip on libraries or executables.
2ff057
strip_g=false
2ff057
2ff057
# with -r arg, pass --reloc-debug-sections to eu-strip.
2ff057
strip_r=false
2ff057
2ff057
# keep or remove arguments to eu-strip.
2ff057
keep_remove_args=
2ff057
2ff057
# with -m arg, add minimal debuginfo to binary.
2ff057
include_minidebug=false
2ff057
2ff057
# with -i arg, add GDB index to .debug file.
2ff057
include_gdb_index=false
2ff057
2ff057
# Barf on missing build IDs.
2ff057
strict=false
2ff057
2ff057
# Do not recompute build IDs.
2ff057
no_recompute_build_id=false
2ff057
2ff057
# DWZ parameters.
2ff057
run_dwz=false
2ff057
dwz_low_mem_die_limit=
2ff057
dwz_max_die_limit=
2ff057
2ff057
# build id seed given by the --build-id-seed option
2ff057
build_id_seed=
2ff057
2ff057
# Arch given by --unique-debug-arch
2ff057
unique_debug_suffix=
2ff057
2ff057
# Base given by --unique-debug-src-base
2ff057
unique_debug_src_base=
2ff057
2ff057
# Number of parallel jobs to spawn
2ff057
n_jobs=1
2ff057
2ff057
BUILDDIR=.
2ff057
out=debugfiles.list
2ff057
srcout=
2ff057
nout=0
2ff057
while [ $# -gt 0 ]; do
2ff057
  case "$1" in
2ff057
  --strict-build-id)
2ff057
    strict=true
2ff057
    ;;
2ff057
  --run-dwz)
2ff057
    run_dwz=true
2ff057
    ;;
2ff057
  --dwz-low-mem-die-limit)
2ff057
    dwz_low_mem_die_limit=$2
2ff057
    shift
2ff057
    ;;
2ff057
  --dwz-max-die-limit)
2ff057
    dwz_max_die_limit=$2
2ff057
    shift
2ff057
    ;;
2ff057
  --build-id-seed)
2ff057
    build_id_seed=$2
2ff057
    shift
2ff057
    ;;
2ff057
  --unique-debug-suffix)
2ff057
    unique_debug_suffix=$2
2ff057
    shift
2ff057
    ;;
2ff057
  --unique-debug-src-base)
2ff057
    unique_debug_src_base=$2
2ff057
    shift
2ff057
    ;;
2ff057
  -g)
2ff057
    strip_g=true
2ff057
    ;;
2ff057
  -m)
2ff057
    include_minidebug=true
2ff057
    ;;
2ff057
  -n)
2ff057
    no_recompute_build_id=true
2ff057
    ;;
2ff057
  -i)
2ff057
    include_gdb_index=true
2ff057
    ;;
2ff057
  -o)
2ff057
    if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
2ff057
      out=$2
2ff057
    else
2ff057
      outs[$nout]=$2
2ff057
      ((nout++))
2ff057
    fi
2ff057
    shift
2ff057
    ;;
2ff057
  -l)
2ff057
    lists[$nout]="${lists[$nout]} $2"
2ff057
    shift
2ff057
    ;;
2ff057
  -p)
2ff057
    ptns[$nout]=$2
2ff057
    shift
2ff057
    ;;
2ff057
  -r)
2ff057
    strip_r=true
2ff057
    ;;
2ff057
  --keep-section)
2ff057
    keep_remove_args="${keep_remove_args} --keep-section $2"
2ff057
    shift
2ff057
    ;;
2ff057
  --remove-section)
2ff057
    keep_remove_args="${keep_remove_args} --remove-section $2"
2ff057
    shift
2ff057
    ;;
2ff057
  -j)
2ff057
    n_jobs=$2
2ff057
    shift
2ff057
    ;;
2ff057
  -j*)
2ff057
    n_jobs=${1#-j}
2ff057
    ;;
2ff057
  --jobs)
2ff057
    n_jobs=$2
2ff057
    shift
2ff057
    ;;
2ff057
  -S)
2ff057
    srcout=$2
2ff057
    shift
2ff057
    ;;
2ff057
  *)
2ff057
    BUILDDIR=$1
2ff057
    shift
2ff057
    break
2ff057
    ;;
2ff057
  esac
2ff057
  shift
2ff057
done
2ff057
2ff057
if test -n "$build_id_seed" -a "$no_recompute_build_id" = "true"; then
2ff057
  echo >&2 "*** ERROR: --build-id-seed (unique build-ids) and -n (do not recompute build-id) cannot be used together"
2ff057
  exit 2
2ff057
fi
2ff057
2ff057
i=0
2ff057
while ((i < nout)); do
2ff057
  outs[$i]="$BUILDDIR/${outs[$i]}"
2ff057
  l=''
2ff057
  for f in ${lists[$i]}; do
2ff057
    l="$l $BUILDDIR/$f"
2ff057
  done
2ff057
  lists[$i]=$l
2ff057
  ((++i))
2ff057
done
2ff057
2ff057
LISTFILE="$BUILDDIR/$out"
2ff057
SOURCEFILE="$BUILDDIR/debugsources.list"
2ff057
LINKSFILE="$BUILDDIR/debuglinks.list"
2ff057
ELFBINSFILE="$BUILDDIR/elfbins.list"
2ff057
2ff057
> "$SOURCEFILE"
2ff057
> "$LISTFILE"
2ff057
> "$LINKSFILE"
2ff057
> "$ELFBINSFILE"
2ff057
2ff057
debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
2ff057
2ff057
strip_to_debug()
2ff057
{
2ff057
  local g=
2ff057
  local r=
2ff057
  $strip_r && r=--reloc-debug-sections
2ff057
  $strip_g && case "$(file -bi "$2")" in
2ff057
  application/x-sharedlib*) g=-g ;;
2ff057
  application/x-executable*) g=-g ;;
2ff057
  application/x-pie-executable*) g=-g ;;
2ff057
  esac
2ff057
  eu-strip --remove-comment $r $g ${keep_remove_args} -f "$1" "$2" || exit
2ff057
  chmod 444 "$1" || exit
2ff057
}
2ff057
2ff057
add_minidebug()
2ff057
{
2ff057
  local debuginfo="$1"
2ff057
  local binary="$2"
2ff057
2ff057
  local dynsyms=`mktemp`
2ff057
  local funcsyms=`mktemp`
2ff057
  local keep_symbols=`mktemp`
2ff057
  local mini_debuginfo=`mktemp`
2ff057
2ff057
  # In the minisymtab we don't need the .debug_ sections (already removed
2ff057
  # by -S) but also not other non-allocated PROGBITS, NOTE or NOBITS sections.
2ff057
  # List and remove them explicitly. We do want to keep the allocated,
2ff057
  # symbol and NOBITS sections so cannot use --keep-only because that is
2ff057
  # too aggressive. Field $2 is the section name, $3 is the section type
2ff057
  # and $8 are the section flags.
2ff057
  local remove_sections=`readelf -W -S "$debuginfo" \
2ff057
	| awk '{ if (index($2,".debug_") != 1 \
2ff057
		     && ($3 == "PROGBITS" || $3 == "NOTE" || $3 == "NOBITS") \
2ff057
		     && index($8,"A") == 0) \
2ff057
		   printf "--remove-section "$2" " }'`
2ff057
2ff057
  # Extract the dynamic symbols from the main binary, there is no need to also have these
2ff057
  # in the normal symbol table
2ff057
  nm -D "$binary" --format=posix --defined-only | awk '{ print $1 }' | sort > "$dynsyms"
2ff057
  # Extract all the text (i.e. function) symbols from the debuginfo
2ff057
  # Use format sysv to make sure we can match against the actual ELF FUNC
2ff057
  # symbol type. The binutils nm posix format symbol type chars are
2ff057
  # ambigous for architectures that might use function descriptors.
2ff057
  nm "$debuginfo" --format=sysv --defined-only | awk -F \| '{ if ($4 ~ "FUNC") print $1 }' | sort > "$funcsyms"
2ff057
  # Keep all the function symbols not already in the dynamic symbol table
2ff057
  comm -13 "$dynsyms" "$funcsyms" > "$keep_symbols"
2ff057
  # Copy the full debuginfo, keeping only a minumal set of symbols and removing some unnecessary sections
2ff057
  objcopy -S $remove_sections --keep-symbols="$keep_symbols" "$debuginfo" "$mini_debuginfo" &> /dev/null
2ff057
  #Inject the compressed data into the .gnu_debugdata section of the original binary
2ff057
  xz "$mini_debuginfo"
2ff057
  mini_debuginfo="${mini_debuginfo}.xz"
2ff057
  objcopy --add-section .gnu_debugdata="$mini_debuginfo" "$binary"
2ff057
  rm -f "$dynsyms" "$funcsyms" "$keep_symbols" "$mini_debuginfo"
2ff057
}
2ff057
2ff057
# Make a relative symlink to $1 called $3$2
2ff057
shopt -s extglob
2ff057
link_relative()
2ff057
{
2ff057
  local t="$1" f="$2" pfx="$3"
2ff057
  local fn="${f#/}" tn="${t#/}"
2ff057
  local fd td d
2ff057
2ff057
  while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
2ff057
    fn="${fn#*/}"
2ff057
    tn="${tn#*/}"
2ff057
  done
2ff057
2ff057
  d="${fn%/*}"
2ff057
  if [ "$d" != "$fn" ]; then
2ff057
    d="${d//+([!\/])/..}"
2ff057
    tn="${d}/${tn}"
2ff057
  fi
2ff057
2ff057
  mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
2ff057
}
2ff057
2ff057
# Make a symlink in /usr/lib/debug/$2 to $1
2ff057
debug_link()
2ff057
{
2ff057
  local l="/usr/lib/debug$2"
2ff057
  local t="$1"
2ff057
  echo >> "$LINKSFILE" "$l $t"
2ff057
  link_relative "$t" "$l" "$RPM_BUILD_ROOT"
2ff057
}
2ff057
2ff057
get_debugfn()
2ff057
{
2ff057
  dn=$(dirname "${1#$RPM_BUILD_ROOT}")
2ff057
  bn=$(basename "$1" .debug)${unique_debug_suffix}.debug
2ff057
  debugdn=${debugdir}${dn}
2ff057
  debugfn=${debugdn}/${bn}
2ff057
}
2ff057
2ff057
set -o pipefail
2ff057
2ff057
strict_error=ERROR
2ff057
$strict || strict_error=WARNING
2ff057
2ff057
temp=$(mktemp -d ${TMPDIR:-/tmp}/find-debuginfo.XXXXXX)
2ff057
trap 'rm -rf "$temp"' EXIT
2ff057
2ff057
# Build a list of unstripped ELF files and their hardlinks
2ff057
touch "$temp/primary"
2ff057
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
2ff057
     		     \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
2ff057
		     -print |
2ff057
file -N -f - | sed -n -e 's/^\(.*\):[ 	]*.*ELF.*, not stripped.*/\1/p' |
2ff057
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
2ff057
while read nlinks inum f; do
2ff057
  if [ $nlinks -gt 1 ]; then
2ff057
    var=seen_$inum
2ff057
    if test -n "${!var}"; then
2ff057
      echo "$inum $f" >>"$temp/linked"
2ff057
      continue
2ff057
    else
2ff057
      read "$var" < <(echo 1)
2ff057
    fi
2ff057
  fi
2ff057
  echo "$nlinks $inum $f" >>"$temp/primary"
2ff057
done
2ff057
2ff057
# Strip ELF binaries
2ff057
do_file()
2ff057
{
2ff057
  local nlinks=$1 inum=$2 f=$3 id link linked
2ff057
2ff057
  get_debugfn "$f"
2ff057
  [ -f "${debugfn}" ] && return
2ff057
2ff057
  echo "extracting debug info from $f"
2ff057
  # See also cpio SOURCEFILE copy. Directories must match up.
2ff057
  debug_base_name="$RPM_BUILD_DIR"
2ff057
  debug_dest_name="/usr/src/debug"
2ff057
  if [ ! -z "$unique_debug_src_base" ]; then
2ff057
    debug_base_name="$BUILDDIR"
2ff057
    debug_dest_name="/usr/src/debug/${unique_debug_src_base}"
2ff057
  fi
2ff057
  no_recompute=
2ff057
  if [ "$no_recompute_build_id" = "true" ]; then
2ff057
    no_recompute="-n"
2ff057
  fi
2ff057
  id=$(${lib_rpm_dir}/debugedit -b "$debug_base_name" -d "$debug_dest_name" \
2ff057
			      $no_recompute -i \
2ff057
			      ${build_id_seed:+--build-id-seed="$build_id_seed"} \
2ff057
			      -l "$SOURCEFILE" "$f") || exit
2ff057
  if [ -z "$id" ]; then
2ff057
    echo >&2 "*** ${strict_error}: No build ID note found in $f"
2ff057
    $strict && exit 2
2ff057
  fi
2ff057
2ff057
  # Add .gdb_index if requested.
2ff057
  if $include_gdb_index; then
2ff057
    if type gdb-add-index >/dev/null 2>&1; then
2ff057
      gdb-add-index "$f"
2ff057
    else
2ff057
      echo >&2 "*** ERROR: GDB index requested, but no gdb-add-index installed"
2ff057
      exit 2
2ff057
    fi
2ff057
  fi
2ff057
2ff057
  # A binary already copied into /usr/lib/debug doesn't get stripped,
2ff057
  # just has its file names collected and adjusted.
2ff057
  case "$dn" in
2ff057
  /usr/lib/debug/*)
2ff057
    return ;;
2ff057
  esac
2ff057
2ff057
  mkdir -p "${debugdn}"
2ff057
  if test -w "$f"; then
2ff057
    strip_to_debug "${debugfn}" "$f"
2ff057
  else
2ff057
    chmod u+w "$f"
2ff057
    strip_to_debug "${debugfn}" "$f"
2ff057
    chmod u-w "$f"
2ff057
  fi
2ff057
2ff057
  # strip -g implies we have full symtab, don't add mini symtab in that case.
2ff057
  # It only makes sense to add a minisymtab for executables and shared
2ff057
  # libraries. Other executable ELF files (like kernel modules) don't need it.
2ff057
  if [ "$include_minidebug" = "true" -a "$strip_g" = "false" ]; then
2ff057
    skip_mini=true
2ff057
    case "$(file -bi "$f")" in
2ff057
      application/x-sharedlib*) skip_mini=false ;;
2ff057
      application/x-executable*) skip_mini=false ;;
2ff057
    esac
2ff057
    $skip_mini || add_minidebug "${debugfn}" "$f"
2ff057
  fi
2ff057
2ff057
  echo "./${f#$RPM_BUILD_ROOT}" >> "$ELFBINSFILE"
2ff057
2ff057
  # If this file has multiple links, make the corresponding .debug files
2ff057
  # all links to one file too.
2ff057
  if [ $nlinks -gt 1 ]; then
2ff057
    grep "^$inum " "$temp/linked" | while read inum linked; do
2ff057
      link=$debugfn
2ff057
      get_debugfn "$linked"
2ff057
      echo "hard linked $link to $debugfn"
2ff057
      mkdir -p "$(dirname "$debugfn")" && ln -nf "$link" "$debugfn"
2ff057
    done
2ff057
  fi
2ff057
}
2ff057
2ff057
# 16^6 - 1 or about 16 million files
2ff057
FILENUM_DIGITS=6
2ff057
run_job()
2ff057
{
2ff057
  local jobid=$1 filenum
2ff057
  local SOURCEFILE=$temp/debugsources.$jobid ELFBINSFILE=$temp/elfbins.$jobid
2ff057
2ff057
  >"$SOURCEFILE"
2ff057
  >"$ELFBINSFILE"
2ff057
  # can't use read -n <n>, because it reads bytes one by one, allowing for
2ff057
  # races
2ff057
  while :; do
2ff057
    filenum=$(dd bs=$(( FILENUM_DIGITS + 1 )) count=1 status=none)
2ff057
    if test -z "$filenum"; then
2ff057
      break
2ff057
    fi
2ff057
    do_file $(sed -n "$(( 0x$filenum )) p" "$temp/primary")
2ff057
  done
2ff057
  echo 0 >"$temp/res.$jobid"
2ff057
}
2ff057
2ff057
n_files=$(wc -l <"$temp/primary")
2ff057
if [ $n_jobs -gt $n_files ]; then
2ff057
  n_jobs=$n_files
2ff057
fi
2ff057
if [ $n_jobs -le 1 ]; then
2ff057
  while read nlinks inum f; do
2ff057
    do_file "$nlinks" "$inum" "$f"
2ff057
  done <"$temp/primary"
2ff057
else
2ff057
  for ((i = 1; i <= n_files; i++)); do
2ff057
    printf "%0${FILENUM_DIGITS}x\\n" $i
2ff057
  done | (
2ff057
    exec 3<&0
2ff057
    for ((i = 0; i < n_jobs; i++)); do
2ff057
      # The shell redirects stdin to /dev/null for background jobs. Work
2ff057
      # around this by duplicating fd 0
2ff057
      run_job $i <&3 &
2ff057
    done
2ff057
    wait
2ff057
  )
2ff057
  for f in "$temp"/res.*; do
2ff057
    res=$(< "$f")
2ff057
    if [ "$res" !=  "0" ]; then
2ff057
      exit 1
2ff057
    fi
2ff057
  done
2ff057
  cat "$temp"/debugsources.* >"$SOURCEFILE"
2ff057
  cat "$temp"/elfbins.* >"$ELFBINSFILE"
2ff057
fi
2ff057
2ff057
# Invoke the DWARF Compressor utility.
2ff057
if $run_dwz \
2ff057
   && [ -d "${RPM_BUILD_ROOT}/usr/lib/debug" ]; then
2ff057
  readarray dwz_files < <(cd "${RPM_BUILD_ROOT}/usr/lib/debug"; find -type f -name \*.debug | LC_ALL=C sort)
2ff057
  if [ ${#dwz_files[@]} -gt 0 ]; then
2ff057
    dwz_multifile_name="${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE}.${RPM_ARCH}"
2ff057
    dwz_multifile_suffix=
2ff057
    dwz_multifile_idx=0
2ff057
    while [ -f "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}${dwz_multifile_suffix}" ]; do
2ff057
      let ++dwz_multifile_idx
2ff057
      dwz_multifile_suffix=".${dwz_multifile_idx}"
2ff057
    done
2ff057
    dwz_multifile_name="${dwz_multifile_name}${dwz_multifile_suffix}"
2ff057
    dwz_opts="-h -q -r"
2ff057
    [ ${#dwz_files[@]} -gt 1 ] \
2ff057
      && dwz_opts="${dwz_opts} -m .dwz/${dwz_multifile_name}"
2ff057
    mkdir -p "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz"
2ff057
    [ -n "${dwz_low_mem_die_limit}" ] \
2ff057
      && dwz_opts="${dwz_opts} -l ${dwz_low_mem_die_limit}"
2ff057
    [ -n "${dwz_max_die_limit}" ] \
2ff057
      && dwz_opts="${dwz_opts} -L ${dwz_max_die_limit}"
2ff057
    if type dwz >/dev/null 2>&1; then
2ff057
      ( cd "${RPM_BUILD_ROOT}/usr/lib/debug" && dwz $dwz_opts ${dwz_files[@]} )
2ff057
    else
2ff057
      echo >&2 "*** ERROR: DWARF compression requested, but no dwz installed"
2ff057
      exit 2
2ff057
    fi
2ff057
    # Remove .dwz directory if empty
2ff057
    rmdir "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz" 2>/dev/null
2ff057
    if [ -f "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}" ]; then
2ff057
      id="`readelf -Wn "${RPM_BUILD_ROOT}/usr/lib/debug/.dwz/${dwz_multifile_name}" \
2ff057
	     2>/dev/null | sed -n 's/^    Build ID: \([0-9a-f]\+\)/\1/p'`"
2ff057
    fi
2ff057
2ff057
    # dwz invalidates .gnu_debuglink CRC32 in the main files.
2ff057
    cat "$ELFBINSFILE" |
2ff057
    (cd "$RPM_BUILD_ROOT"; \
2ff057
     xargs -d '\n' ${lib_rpm_dir}/sepdebugcrcfix usr/lib/debug)
2ff057
  fi
2ff057
fi
2ff057
2ff057
# For each symlink whose target has a .debug file,
2ff057
# make a .debug symlink to that file.
2ff057
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*" -type l -print |
2ff057
while read f
2ff057
do
2ff057
  t=$(readlink -m "$f").debug
2ff057
  f=${f#$RPM_BUILD_ROOT}
2ff057
  t=${t#$RPM_BUILD_ROOT}
2ff057
  if [ -f "$debugdir$t" ]; then
2ff057
    echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
2ff057
    debug_link "/usr/lib/debug$t" "${f}.debug"
2ff057
  fi
2ff057
done
2ff057
2ff057
if [ -s "$SOURCEFILE" ]; then
2ff057
  # See also debugedit invocation. Directories must match up.
2ff057
  debug_base_name="$RPM_BUILD_DIR"
2ff057
  debug_dest_name="/usr/src/debug"
2ff057
  if [ ! -z "$unique_debug_src_base" ]; then
2ff057
    debug_base_name="$BUILDDIR"
2ff057
    debug_dest_name="/usr/src/debug/${unique_debug_src_base}"
2ff057
  fi
2ff057
2ff057
  mkdir -p "${RPM_BUILD_ROOT}${debug_dest_name}"
2ff057
  # Filter out anything compiler generated which isn't a source file.
2ff057
  # e.g. <internal>, <built-in>, <__thread_local_inner macros>.
2ff057
  # Some compilers generate them as if they are part of the working
2ff057
  # directory (which is why we match against ^ or /).
2ff057
  LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(^|/)<[a-z _-]+>$' |
2ff057
  (cd "${debug_base_name}"; cpio -pd0mL "${RPM_BUILD_ROOT}${debug_dest_name}")
2ff057
  # stupid cpio creates new directories in mode 0700,
2ff057
  # and non-standard modes may be inherented from original directories, fixup
2ff057
  find "${RPM_BUILD_ROOT}${debug_dest_name}" -type d -print0 |
2ff057
  xargs --no-run-if-empty -0 chmod 0755
2ff057
fi
2ff057
2ff057
if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
2ff057
  ((nout > 0)) ||
2ff057
  test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
2ff057
  (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
2ff057
  sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
2ff057
2ff057
  (cd "${RPM_BUILD_ROOT}/usr"
2ff057
   test ! -d lib/debug || find lib/debug ! -type d
2ff057
   test ! -d src/debug -o -n "$srcout" || find src/debug -mindepth 1 -maxdepth 1
2ff057
  ) | sed 's,^,/usr/,' >> "$LISTFILE"
2ff057
fi
2ff057
2ff057
if [ -n "$srcout" ]; then
2ff057
  srcout="$BUILDDIR/$srcout"
2ff057
  > "$srcout"
2ff057
  if [ -d "${RPM_BUILD_ROOT}/usr/src/debug" ]; then
2ff057
    (cd "${RPM_BUILD_ROOT}/usr"
2ff057
     find src/debug -mindepth 1 -maxdepth 1
2ff057
    ) | sed 's,^,/usr/,' >> "$srcout"
2ff057
  fi
2ff057
fi
2ff057
2ff057
# Append to $1 only the lines from stdin not already in the file.
2ff057
append_uniq()
2ff057
{
2ff057
  grep -F -f "$1" -x -v >> "$1"
2ff057
}
2ff057
2ff057
# Helper to generate list of corresponding .debug files from a file list.
2ff057
filelist_debugfiles()
2ff057
{
2ff057
  local extra="$1"
2ff057
  shift
2ff057
  sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
2ff057
s/^%[a-z0-9_][a-z0-9_]* *//
2ff057
/^$/d
2ff057
'"$extra" "$@"
2ff057
}
2ff057
2ff057
# Write an output debuginfo file list based on given input file lists.
2ff057
filtered_list()
2ff057
{
2ff057
  local out="$1"
2ff057
  shift
2ff057
  test $# -gt 0 || return
2ff057
  grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
2ff057
  	-x $LISTFILE >> $out
2ff057
  sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
2ff057
h
2ff057
s,^.*$,s# &$##p,p
2ff057
g
2ff057
s,^.*$,s# /usr/lib/debug&.debug$##p,p
2ff057
' "$@") "$LINKSFILE" | append_uniq "$out"
2ff057
}
2ff057
2ff057
# Write an output debuginfo file list based on an grep -E -style regexp.
2ff057
pattern_list()
2ff057
{
2ff057
  local out="$1" ptn="$2"
2ff057
  test -n "$ptn" || return
2ff057
  grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
2ff057
  sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
2ff057
}
2ff057
2ff057
#
2ff057
# When given multiple -o switches, split up the output as directed.
2ff057
#
2ff057
i=0
2ff057
while ((i < nout)); do
2ff057
  > ${outs[$i]}
2ff057
  filtered_list ${outs[$i]} ${lists[$i]}
2ff057
  pattern_list ${outs[$i]} "${ptns[$i]}"
2ff057
  grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
2ff057
  mv "${LISTFILE}.new" "$LISTFILE"
2ff057
  ((++i))
2ff057
done
2ff057
if ((nout > 0)); then
2ff057
  # Now add the right %dir lines to each output list.
2ff057
  (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
2ff057
  sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@;;g;}#' |
2ff057
  LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
2ff057
  i=0
2ff057
  while ((i < nout)); do
2ff057
    sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
2ff057
    cat "${outs[$i]}" >> "${outs[$i]}.new"
2ff057
    mv -f "${outs[$i]}.new" "${outs[$i]}"
2ff057
    ((++i))
2ff057
  done
2ff057
  sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
2ff057
  cat "$LISTFILE" >> "${LISTFILE}.new"
2ff057
  mv "${LISTFILE}.new" "$LISTFILE"
2ff057
fi