blob: 82249422cef8ca59d0ac3519f496ccd0863f4dca [file] [log] [blame]
Colin Cross665dce92016-04-28 14:50:03 -07001#!/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:
Colin Cross26c34ed2016-09-30 17:10:16 -07008# -i ${file}: input file (required)
Colin Cross665dce92016-04-28 14:50:03 -07009# -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 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
39 "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
40 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
41 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
42 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
43 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
44 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
45 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"
46 rm -f "${outfile}.mini_debuginfo.xz"
47 xz "${outfile}.mini_debuginfo"
48 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
49 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo.xz"
50}
51
52do_add_gnu_debuglink() {
53 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
54}
55
56while getopts $OPTSTRING opt; do
57 case "$opt" in
58 d) depsfile="${OPTARG}" ;;
59 i) infile="${OPTARG}" ;;
60 o) outfile="${OPTARG}" ;;
61 -)
62 case "${OPTARG}" in
63 keep-symbols) keep_symbols=true ;;
64 keep-mini-debug-info) keep_mini_debug_info=true ;;
65 add-gnu-debuglink) add_gnu_debuglink=true ;;
66 *) echo "Unknown option --${OPTARG}"; usage ;;
67 esac;;
68 ?) usage ;;
69 *) echo "'${opt}' '${OPTARG}'"
70 esac
71done
72
73if [ -z "${infile}" ]; then
74 echo "-i argument is required"
75 usage
76fi
77
78if [ -z "${outfile}" ]; then
79 echo "-o argument is required"
80 usage
81fi
82
83if [ -z "${depsfile}" ]; then
84 echo "-d argument is required"
85 usage
86fi
87
88if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
89 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
90 usage
91fi
92
93if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
94 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
95 usage
96fi
97
98rm -f "${outfile}.tmp"
99
100if [ ! -z "${keep_symbols}" ]; then
101 do_strip_keep_symbols
102elif [ ! -z "${keep_mini_debug_info}" ]; then
103 do_strip_keep_mini_debug_info
104else
105 do_strip
106fi
107
108if [ ! -z "${add_gnu_debuglink}" ]; then
109 do_add_gnu_debuglink
110fi
111
112rm -f "${outfile}"
113mv "${outfile}.tmp" "${outfile}"
114
115cat <<EOF > "${depsfile}"
116${outfile}: \
117 ${infile} \
118 ${CROSS_COMPILE}nm \
119 ${CROSS_COMPILE}objcopy \
120 ${CROSS_COMPILE}readelf \
121 ${CROSS_COMPILE}strip
122
123EOF