From 260c0b1781eeb56bcdb92d301a65b254449cd0ae Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 21 Oct 2012 16:17:18 -0400 Subject: Rewrite oh-strip. --- (limited to 'src/oh-strip.sh') diff --git a/src/oh-strip.sh b/src/oh-strip.sh index ca0487a..c177852 100644 --- a/src/oh-strip.sh +++ b/src/oh-strip.sh @@ -19,12 +19,132 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -. @@LIBDIR@@/messages -. @@LIBDIR@@/locale +. '@@LIBOPKBUILD_1@@/load.sm' + +ob_use locale +ob_use output + +CR=' +' main() { - oh_locale_set + dir='dest' + keep_debug='false' + + ob_set_locale_path '@@LOCALEDIR@@/%s/LC_MESSAGES/%s.ms' + ob_set_text_domain 'opkhelper' + + while getopts 'd:k' opt; do + case "${opt}" in + d) + dir="${OPTARG}" + ;; + k) + keep_debug='true' + ;; + ?) + ob_error "$(ob_get_msg 'bad_opt')" + exit 1 + ;; + esac + done + + find "${dir}" -type f -a ! -path '*/debug/*' -a -name '*.so*' | \ + while IFS="${CR}" read file; do + if file "${file}" | grep 'ELF.*shared' >/dev/null 2>&1; then + strip_so "${file}" + fi + done + + find "${dir}" -type f -a ! -path '*/debug/*' -a -perm '-a+x' | \ + while IFS="${CR}" read file; do + if file "${file}" | grep -E 'ELF.*(executable|shared)' \ + >/dev/null 2>&1; then + strip_exe "${file}" + fi + done + + find "${dir}" -type f -a ! -path '*/debug/*' \ + -a -name 'lib*.a' -a ! -name '*_g.a' | while IFS="{CR}" read file; do + strip_a "${file}" + done +} + +strip_so() +{ + file="${1}" + debug='' + + if ${keep_debug}; then + debug="$(keep_debug "${file}")" + fi + + ${STRIP} --remove-section=.comment --remove-section=.note \ + --strip-unneeded "${file}" + + if [ -n "${debug}" ]; then + link_debug "${file}" "${debug}" + fi +} + +strip_exe() +{ + file="${1}" + debug='' + + if ${keep_debug}; then + debug="$(keep_debug "${file}")" + fi + + ${STRIP} --remove-section=.comment --remove-section=.note \ + "${file}" + + if [ -n "${debug}" ]; then + link_debug "${file}" "${debug}" + fi +} + +strip_a() +{ + file="${1}" + + ${STRIP} --strip-debug "${file}" +} + +keep_debug() +{ + build_id_parts='' + debug='' + + if ! file "${file}" | grep 'not stripped' >/dev/null 2>&1; then + return + fi + + build_id_parts="$(${READELF} -n "${file}" | \ + sed -n 's/^.*Build ID: \([0-9a-f][0-9a-f]\)\([0-9a-f]*\)$/\1 \2/p')" + if [ -z "${build_id_parts}" ]; then + return + fi + + debug="/usr/lib/debug/.build-id/${build_id_parts% *}" + debug="${debug}/${build_id_parts#* }.debug" + + mkdir -p "${dir}/${debug%/*}" + + ${OBJCOPY} --only-keep-debug --compress-debug-sections \ + "${file}" "${dir}/${debug}" + chmod 644 "${dir}/${debug}" + + printf '%s\n' "${debug}" +} + +link_debug() +{ + file="${1}" + debug="${2}" + + ${OBJCOPY} --add-gnu-debuglink="${dir}/${debug}" "${file}" } main "${@}" -- cgit v0.9.1