diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.in | 3 | ||||
-rw-r--r-- | src/oh-strip.sh | 126 |
2 files changed, 125 insertions, 4 deletions
diff --git a/src/Makefile.in b/src/Makefile.in index f04d69f..74063c2 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -40,7 +40,8 @@ sed_script = s&@@PACKAGE_NAME@@&$(package_name)&;\ .SUFFIXES: .sh SRCS = \ - oh-installfiles.sh + oh-installfiles.sh \ + oh-strip.sh OBJS = $(SRCS:.sh=) distdir = ../$(package_name)-$(package_version)/src 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 <http://www.gnu.org/licenses/>. -. @@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 "${@}" |