Revert "Modify eu-strip option to perform strip in post script of rpm package & add...
[platform/upstream/rpm.git] / scripts / find-debuginfo.sh
index 57449f7..efcc72e 100644 (file)
@@ -95,13 +95,18 @@ debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
 
 strip_to_debug()
 {
-  local g=
   local r=
   $strip_r && r=--reloc-debug-sections
-  $strip_g && case "$(file -bi "$2")" in
-  application/x-sharedlib*) g=-g ;;
+  case $2 in
+      *.ko)
+         # don't attempt to create a minimal backtrace binary for
+         # kernel modules as this just causes the stripping process
+         # to be skipped entirely
+         eu-strip --remove-comment $r -f "$1" "$2" || exit
+         ;;
+      *)
+         eu-strip --remove-comment -g -f "$1" "$2" || exit
   esac
-  eu-strip --remove-comment $r $g -f "$1" "$2" || exit
   chmod 444 "$1" || exit
 }
 
@@ -133,12 +138,27 @@ debug_link()
   local l="/usr/lib/debug$2"
   local t="$1"
   echo >> "$LINKSFILE" "$l $t"
-  link_relative "$t" "$l" "$RPM_BUILD_ROOT"
+
+  # this should correspond to what brp-symlink is doing
+  case $t in
+      /usr*)
+         link_relative "$t" "$l" "$RPM_BUILD_ROOT"
+         ;;
+      *)
+         mkdir -p "$(dirname "$RPM_BUILD_ROOT$l")" && \
+             ln -snf "$t" "$RPM_BUILD_ROOT$l"
+         ;;
+  esac
 }
 
 # Provide .2, .3, ... symlinks to all filename instances of this build-id.
 make_id_dup_link()
 {
+  # See https://bugzilla.redhat.com/show_bug.cgi?id=641377 for the reasoning, 
+  # but it has seveal drawbacks as we would need to split the .1 suffixes into 
+  # different subpackages and it's about impossible to predict the number
+  # -> perhaps later
+  return
   local id="$1" file="$2" idfile
 
   local n=1
@@ -153,6 +173,20 @@ make_id_dup_link()
   debug_link "$file" "/$idfile"
 }
 
+# Compare two binaries but ignore the .note.gnu.build-id section
+elfcmp()
+{
+  local tmp1=$(mktemp -t ${1##*/}.XXXXXX)
+  local tmp2=$(mktemp -t ${2##*/}.XXXXXX)
+
+  objcopy -R .note.gnu.build-id -R .gnu_debuglink $1 $tmp1
+  objcopy -R .note.gnu.build-id -R .gnu_debuglink $2 $tmp2
+  cmp -s $tmp1 $tmp2
+  local res=$?
+  rm -f $tmp1 $tmp2
+  return $res
+}
+
 # Make a build-id symlink for id $1 with suffix $3 to file $2.
 make_id_link()
 {
@@ -172,8 +206,8 @@ make_id_link()
 
   local other=$(readlink -m "$root_idfile")
   other=${other#$RPM_BUILD_ROOT}
-  if cmp -s "$root_idfile" "$RPM_BUILD_ROOT$file" ||
-     eu-elfcmp -q "$root_idfile" "$RPM_BUILD_ROOT$file" 2> /dev/null; then
+  if cmp -s "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ||
+     elfcmp "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ; then
     # Two copies.  Maybe one has to be setuid or something.
     echo >&2 "*** WARNING: identical binaries are copied, not linked:"
     echo >&2 "        $file"
@@ -190,7 +224,8 @@ make_id_link()
 get_debugfn()
 {
   dn=$(dirname "${1#$RPM_BUILD_ROOT}")
-  bn=$(basename "$1" .debug).debug
+# Do not strip existing .debug suffixes
+  bn=$(basename "$1").debug
 
   debugdn=${debugdir}${dn}
   debugfn=${debugdn}/${bn}
@@ -201,13 +236,19 @@ set -o pipefail
 strict_error=ERROR
 $strict || strict_error=WARNING
 
-# Strip ELF binaries
-find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
-                    \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
-                    -print |
-file -N -f - | sed -n -e 's/^\(.*\):[  ]*.*ELF.*, not stripped/\1/p' |
-xargs --no-run-if-empty stat -c '%h %D_%i %n' |
+# Strip ELF binaries (and no static libraries)
+find $RPM_BUILD_ROOT ! -path "${debugdir}/*.debug" -type f \( -perm /111 -or -name "*.so*" -or -name "*.ko" \) ! -name "*.a" -print0 | sort -z |
+xargs --no-run-if-empty -0 stat -c '%h %D_%i %n' |
 while read nlinks inum f; do
+  case $(objdump -h $f 2>/dev/null | egrep -o '(debug[\.a-z_]*|gnu.version)') in
+    *debuglink*) continue ;;
+    *debug*) ;;
+    *gnu.version*)
+       echo "WARNING: "`echo $f | sed -e "s,^$RPM_BUILD_ROOT/*,/,"`" is already stripped!"
+       continue
+       ;;
+    *) continue ;;
+  esac
   get_debugfn "$f"
   [ -f "${debugfn}" ] && continue
 
@@ -231,8 +272,9 @@ while read nlinks inum f; do
   fi
 
   echo "extracting debug info from $f"
-  id=$(/usr/lib/rpm/debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
-                             -i -l "$SOURCEFILE" "$f") || exit
+  id=$($(DEBUGEDIT=$(which debugedit 2>/dev/null); \
+      echo ${DEBUGEDIT:-/usr/lib/rpm/debugedit}) -b "$RPM_BUILD_DIR" \
+      -d /usr/src/debug -i -l "$SOURCEFILE" "$f") || exit
   if [ $nlinks -gt 1 ]; then
     eval linkedid_$inum=\$id
   fi
@@ -252,12 +294,36 @@ while read nlinks inum f; do
   esac
 
   mkdir -p "${debugdn}"
-  if test -w "$f"; then
-    strip_to_debug "${debugfn}" "$f"
+  if [ -e "${BUILDDIR}/Kconfig" ] ; then
+      mode=$(stat -c %a "$f")
+      chmod +w "$f"
+      objcopy --only-keep-debug $f $debugfn || :
+      (
+         shopt -s extglob
+         strip_option="--strip-all"
+         case "$f" in
+             *.ko)
+                 strip_option="--strip-debug" ;;
+             *$STRIP_KEEP_SYMTAB*)
+                 if test -n "$STRIP_KEEP_SYMTAB"; then
+                     strip_option="--strip-debug"
+                 fi
+                 ;;
+         esac
+         if test "$NO_DEBUGINFO_STRIP_DEBUG" = true ; then
+             strip_option=
+         fi
+         objcopy --add-gnu-debuglink=$debugfn -R .comment -R .GCC.command.line $strip_option $f
+         chmod $mode $f
+      ) || :
   else
-    chmod u+w "$f"
-    strip_to_debug "${debugfn}" "$f"
-    chmod u-w "$f"
+      if test -w "$f"; then
+         strip_to_debug "${debugfn}" "$f"
+      else
+         chmod u+w "$f"
+         strip_to_debug "${debugfn}" "$f"
+         chmod u-w "$f"
+      fi
   fi
 
   if [ -n "$id" ]; then
@@ -266,19 +332,11 @@ while read nlinks inum f; do
   fi
 done || exit
 
-# For each symlink whose target has a .debug file,
-# make a .debug symlink to that file.
-find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*" -type l -print |
-while read f
-do
-  t=$(readlink -m "$f").debug
-  f=${f#$RPM_BUILD_ROOT}
-  t=${t#$RPM_BUILD_ROOT}
-  if [ -f "$debugdir$t" ]; then
-    echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
-    debug_link "/usr/lib/debug$t" "${f}.debug"
-  fi
-done
+# We used to make a .debug symlink for each symlink whose target
+# has a .debug file to that file.  This is not necessary because
+# the debuglink section contains only the destination of those links.
+# Creating those links anyway results in debuginfo packages for
+# devel packages just because of the .so symlinks in them.
 
 if [ -s "$SOURCEFILE" ]; then
   mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
@@ -287,20 +345,28 @@ if [ -s "$SOURCEFILE" ]; then
   # stupid cpio creates new directories in mode 0700, fixup
   find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
   xargs --no-run-if-empty -0 chmod a+rx
+  find "${RPM_BUILD_ROOT}/usr/src/debug" -type f -print0 |
+  xargs --no-run-if-empty -0 chmod a+r
 fi
 
 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
   ((nout > 0)) ||
   test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
-  (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
+  (cd "${RPM_BUILD_ROOT}/usr/lib"; test ! -d debug || find debug -type d) |
   sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
 
   (cd "${RPM_BUILD_ROOT}/usr"
    test ! -d lib/debug || find lib/debug ! -type d
-   test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
   ) | sed 's,^,/usr/,' >> "$LISTFILE"
 fi
 
+: > "$SOURCEFILE"
+if [ -d "${RPM_BUILD_ROOT}/usr/src" ]; then
+  (cd "${RPM_BUILD_ROOT}/usr"
+   test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
+  ) | sed 's,^,/usr/,' >> "$SOURCEFILE"
+fi
+
 # Append to $1 only the lines from stdin not already in the file.
 append_uniq()
 {