blob: 318c7ad5d4df354197afb0d427782e51829c7108 [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"
Ryan Prichardafefacf2018-03-27 22:15:45 -070059 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -070060 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
61 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
62 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -080063 "${XZ}" "${outfile}.mini_debuginfo"
Colin Cross1b594092017-04-13 16:19:00 -070064 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
65 else
66 cp -f "${infile}" "${outfile}.tmp"
67 fi
Colin Cross665dce92016-04-28 14:50:03 -070068}
69
70do_add_gnu_debuglink() {
71 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
72}
73
74while getopts $OPTSTRING opt; do
75 case "$opt" in
76 d) depsfile="${OPTARG}" ;;
77 i) infile="${OPTARG}" ;;
78 o) outfile="${OPTARG}" ;;
79 -)
80 case "${OPTARG}" in
81 keep-symbols) keep_symbols=true ;;
82 keep-mini-debug-info) keep_mini_debug_info=true ;;
83 add-gnu-debuglink) add_gnu_debuglink=true ;;
84 *) echo "Unknown option --${OPTARG}"; usage ;;
85 esac;;
86 ?) usage ;;
87 *) echo "'${opt}' '${OPTARG}'"
88 esac
89done
90
91if [ -z "${infile}" ]; then
92 echo "-i argument is required"
93 usage
94fi
95
96if [ -z "${outfile}" ]; then
97 echo "-o argument is required"
98 usage
99fi
100
101if [ -z "${depsfile}" ]; then
102 echo "-d argument is required"
103 usage
104fi
105
106if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
107 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
108 usage
109fi
110
111if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
112 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
113 usage
114fi
115
116rm -f "${outfile}.tmp"
117
118if [ ! -z "${keep_symbols}" ]; then
119 do_strip_keep_symbols
120elif [ ! -z "${keep_mini_debug_info}" ]; then
121 do_strip_keep_mini_debug_info
122else
123 do_strip
124fi
125
126if [ ! -z "${add_gnu_debuglink}" ]; then
127 do_add_gnu_debuglink
128fi
129
130rm -f "${outfile}"
131mv "${outfile}.tmp" "${outfile}"
132
133cat <<EOF > "${depsfile}"
134${outfile}: \
135 ${infile} \
136 ${CROSS_COMPILE}nm \
137 ${CROSS_COMPILE}objcopy \
138 ${CROSS_COMPILE}readelf \
139 ${CROSS_COMPILE}strip
140
141EOF