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