• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -e
2
3# Script to handle the various ways soong may need to strip binaries
4# Inputs:
5#  Environment:
6#   CROSS_COMPILE: prefix added to readelf, objcopy tools
7#  Arguments:
8#   -i ${file}: input file (required)
9#   -o ${file}: output file (required)
10#   -d ${file}: deps file (required)
11#   --keep-symbols
12#   --keep-mini-debug-info
13#   --add-gnu-debuglink
14
15OPTSTRING=d:i:o:-:
16
17usage() {
18    cat <<EOF
19Usage: strip.sh [options] -i in-file -o out-file -d deps-file
20Options:
21        --keep-symbols          Keep symbols in out-file
22        --keep-mini-debug-info  Keep compressed debug info in out-file
23        --add-gnu-debuglink     Add a gnu-debuglink section to out-file
24EOF
25    exit 1
26}
27
28do_strip() {
29    "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
30}
31
32do_strip_keep_symbols() {
33    "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \
34	`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs`
35}
36
37do_strip_keep_mini_debug_info() {
38    rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
39    if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then
40        "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
41        "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
42        "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
43        comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
44        "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
45        "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
46        "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
47        xz "${outfile}.mini_debuginfo"
48        "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
49    else
50        cp -f "${infile}" "${outfile}.tmp"
51    fi
52}
53
54do_add_gnu_debuglink() {
55    "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
56}
57
58while getopts $OPTSTRING opt; do
59    case "$opt" in
60	d) depsfile="${OPTARG}" ;;
61	i) infile="${OPTARG}" ;;
62	o) outfile="${OPTARG}" ;;
63	-)
64	    case "${OPTARG}" in
65		keep-symbols) keep_symbols=true ;;
66		keep-mini-debug-info) keep_mini_debug_info=true ;;
67		add-gnu-debuglink) add_gnu_debuglink=true ;;
68		*) echo "Unknown option --${OPTARG}"; usage ;;
69	    esac;;
70	?) usage ;;
71	*) echo "'${opt}' '${OPTARG}'"
72    esac
73done
74
75if [ -z "${infile}" ]; then
76    echo "-i argument is required"
77    usage
78fi
79
80if [ -z "${outfile}" ]; then
81    echo "-o argument is required"
82    usage
83fi
84
85if [ -z "${depsfile}" ]; then
86    echo "-d argument is required"
87    usage
88fi
89
90if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
91    echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
92    usage
93fi
94
95if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
96    echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
97    usage
98fi
99
100rm -f "${outfile}.tmp"
101
102if [ ! -z "${keep_symbols}" ]; then
103    do_strip_keep_symbols
104elif [ ! -z "${keep_mini_debug_info}" ]; then
105    do_strip_keep_mini_debug_info
106else
107    do_strip
108fi
109
110if [ ! -z "${add_gnu_debuglink}" ]; then
111    do_add_gnu_debuglink
112fi
113
114rm -f "${outfile}"
115mv "${outfile}.tmp" "${outfile}"
116
117cat <<EOF > "${depsfile}"
118${outfile}: \
119  ${infile} \
120  ${CROSS_COMPILE}nm \
121  ${CROSS_COMPILE}objcopy \
122  ${CROSS_COMPILE}readelf \
123  ${CROSS_COMPILE}strip
124
125EOF
126