prepstrip: add support for elfutils strip
authorMike Frysinger <vapier@gentoo.org>
Tue, 11 Oct 2011 04:49:48 +0000 (00:49 -0400)
committerMike Frysinger <vapier@gentoo.org>
Tue, 11 Oct 2011 14:18:48 +0000 (10:18 -0400)
If people use strip from the elfutils package, take advantage of some of
its neat features (like splitting + stripping in one step).

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
bin/ebuild-helpers/prepstrip

index 7a08ababb95624163849ad28c606bc2175ace0e2..7f158c29dece45ba5e6a0ab08e23aefd9c37903f 100755 (executable)
@@ -34,10 +34,26 @@ for t in STRIP:strip OBJCOPY:objcopy READELF:readelf ; do
        type -P -- ${!v} >/dev/null || eval ${v}=${t}
 done
 
-# We'll leave out -R .note for now until we can check out the relevance
-# of the section when it has the ALLOC flag set on it ...
-export SAFE_STRIP_FLAGS="--strip-unneeded"
-export PORTAGE_STRIP_FLAGS=${PORTAGE_STRIP_FLAGS-${SAFE_STRIP_FLAGS} -R .comment}
+# Figure out what tool set we're using to strip stuff
+unset SAFE_STRIP_FLAGS DEF_STRIP_FLAGS SPLIT_STRIP_FLAGS
+case $(${STRIP} --version 2>/dev/null) in
+*elfutils*) # dev-libs/elfutils
+       # elfutils default behavior is always safe, so don't need to specify
+       # any flags at all
+       SAFE_STRIP_FLAGS=""
+       DEF_STRIP_FLAGS="--remove-comment"
+       SPLIT_STRIP_FLAGS="-f"
+       ;;
+*GNU*) # sys-devel/binutils
+       # We'll leave out -R .note for now until we can check out the relevance
+       # of the section when it has the ALLOC flag set on it ...
+       SAFE_STRIP_FLAGS="--strip-unneeded"
+       DEF_STRIP_FLAGS="-R .comment"
+       SPLIT_STRIP_FLAGS=
+       ;;
+esac
+: ${PORTAGE_STRIP_FLAGS=${SAFE_STRIP_FLAGS} ${DEF_STRIP_FLAGS}}
+
 prepstrip_sources_dir=/usr/src/debug/${CATEGORY}/${PF}
 
 type -P debugedit >/dev/null && debugedit_found=true || debugedit_found=false
@@ -95,8 +111,12 @@ save_elf_debug() {
                ln "${D}usr/lib/debug/${!inode:${#D}}.debug" "$y"
        else
                eval $inode=\$x
-               ${OBJCOPY} --only-keep-debug "${x}" "${y}"
-               ${OBJCOPY} --add-gnu-debuglink="${y}" "${x}"
+               if [[ -e ${T}/prepstrip.split.debug ]] ; then
+                       mv "${T}"/prepstrip.split.debug "${y}"
+               else
+                       ${OBJCOPY} --only-keep-debug "${x}" "${y}"
+                       ${OBJCOPY} --add-gnu-debuglink="${y}" "${x}"
+               fi
                local args="a-x,o-w"
                [[ -g ${x} || -u ${x} ]] && args+=",go-r"
                chmod ${args} "${y}"
@@ -117,6 +137,24 @@ save_elf_debug() {
        fi
 }
 
+process_elf() {
+       local x=$1 strip_flags=${*:2}
+
+       vecho "   ${x:${#D}}"
+       save_elf_sources "${x}"
+
+       if ${strip_this} ; then
+               # see if we can split & strip at the same time
+               if [[ -n ${SPLIT_STRIP_FLAGS} ]] ; then
+                       ${STRIP} ${strip_flags} -f "${T}"/prepstrip.split.debug "${x}"
+                       save_elf_debug "${x}"
+               else
+                       save_elf_debug "${x}"
+                       ${STRIP} ${strip_flags} "${x}"
+               fi
+       fi
+}
+
 # The existance of the section .symtab tells us that a binary is stripped.
 # We want to log already stripped binaries, as this may be a QA violation.
 # They prevent us from getting the splitdebug data.
@@ -186,19 +224,9 @@ do
                        ${STRIP} -g "${x}"
                fi
        elif [[ ${f} == *"SB executable"* || ${f} == *"SB shared object"* ]] ; then
-               vecho "   ${x:${#D}}"
-               save_elf_sources "${x}"
-               if ${strip_this} ; then
-                       save_elf_debug "${x}"
-                       ${STRIP} ${PORTAGE_STRIP_FLAGS} "${x}"
-               fi
+               process_elf "${x}" ${PORTAGE_STRIP_FLAGS}
        elif [[ ${f} == *"SB relocatable"* ]] ; then
-               vecho "   ${x:${#D}}"
-               save_elf_sources "${x}"
-               if ${strip_this} ; then
-                       [[ ${x} == *.ko ]] && save_elf_debug "${x}"
-                       ${STRIP} ${SAFE_STRIP_FLAGS} "${x}"
-               fi
+               process_elf "${x}" ${SAFE_STRIP_FLAGS}
        fi
 done