blob: ff8aaa03e6ccf804f4e59e72266f6697f691a0e1 [file] [log] [blame]
Colin Cross665dce92016-04-28 14:50:03 -07001#!/bin/bash -e
2
Colin Crossd00350c2017-11-17 10:55:38 -08003# Copyright 2017 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Colin Cross665dce92016-04-28 14:50:03 -070017# Script to handle the various ways soong may need to strip binaries
18# Inputs:
19# Environment:
20# CROSS_COMPILE: prefix added to readelf, objcopy tools
Dan Willemsen8fec83a2018-03-09 10:47:52 -080021# XZ: path to the xz binary
Colin Cross665dce92016-04-28 14:50:03 -070022# Arguments:
Colin Cross26c34ed2016-09-30 17:10:16 -070023# -i ${file}: input file (required)
Colin Cross665dce92016-04-28 14:50:03 -070024# -o ${file}: output file (required)
25# -d ${file}: deps file (required)
26# --keep-symbols
27# --keep-mini-debug-info
28# --add-gnu-debuglink
29
30OPTSTRING=d:i:o:-:
31
32usage() {
33 cat <<EOF
34Usage: strip.sh [options] -i in-file -o out-file -d deps-file
35Options:
36 --keep-symbols Keep symbols in out-file
37 --keep-mini-debug-info Keep compressed debug info in out-file
38 --add-gnu-debuglink Add a gnu-debuglink section to out-file
39EOF
40 exit 1
41}
42
43do_strip() {
44 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
45}
46
47do_strip_keep_symbols() {
48 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \
49 `"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs`
50}
51
52do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -070053 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
54 if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then
55 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
56 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
57 "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
58 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
59 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
60 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
61 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -080062 "${XZ}" "${outfile}.mini_debuginfo"
Colin Cross1b594092017-04-13 16:19:00 -070063 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
64 else
65 cp -f "${infile}" "${outfile}.tmp"
66 fi
Colin Cross665dce92016-04-28 14:50:03 -070067}
68
69do_add_gnu_debuglink() {
70 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
71}
72
73while getopts $OPTSTRING opt; do
74 case "$opt" in
75 d) depsfile="${OPTARG}" ;;
76 i) infile="${OPTARG}" ;;
77 o) outfile="${OPTARG}" ;;
78 -)
79 case "${OPTARG}" in
80 keep-symbols) keep_symbols=true ;;
81 keep-mini-debug-info) keep_mini_debug_info=true ;;
82 add-gnu-debuglink) add_gnu_debuglink=true ;;
83 *) echo "Unknown option --${OPTARG}"; usage ;;
84 esac;;
85 ?) usage ;;
86 *) echo "'${opt}' '${OPTARG}'"
87 esac
88done
89
90if [ -z "${infile}" ]; then
91 echo "-i argument is required"
92 usage
93fi
94
95if [ -z "${outfile}" ]; then
96 echo "-o argument is required"
97 usage
98fi
99
100if [ -z "${depsfile}" ]; then
101 echo "-d argument is required"
102 usage
103fi
104
105if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
106 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
107 usage
108fi
109
110if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
111 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
112 usage
113fi
114
115rm -f "${outfile}.tmp"
116
117if [ ! -z "${keep_symbols}" ]; then
118 do_strip_keep_symbols
119elif [ ! -z "${keep_mini_debug_info}" ]; then
120 do_strip_keep_mini_debug_info
121else
122 do_strip
123fi
124
125if [ ! -z "${add_gnu_debuglink}" ]; then
126 do_add_gnu_debuglink
127fi
128
129rm -f "${outfile}"
130mv "${outfile}.tmp" "${outfile}"
131
132cat <<EOF > "${depsfile}"
133${outfile}: \
134 ${infile} \
135 ${CROSS_COMPILE}nm \
136 ${CROSS_COMPILE}objcopy \
137 ${CROSS_COMPILE}readelf \
138 ${CROSS_COMPILE}strip
139
140EOF