blob: 0f77da8a90b20575dd44a7c14dad16616f5f01c8 [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:
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070020# CLANG_BIN: path to the clang bin directory
Colin Cross665dce92016-04-28 14:50:03 -070021# CROSS_COMPILE: prefix added to readelf, objcopy tools
Dan Willemsen8fec83a2018-03-09 10:47:52 -080022# XZ: path to the xz binary
Colin Cross665dce92016-04-28 14:50:03 -070023# Arguments:
Colin Cross26c34ed2016-09-30 17:10:16 -070024# -i ${file}: input file (required)
Colin Cross665dce92016-04-28 14:50:03 -070025# -o ${file}: output file (required)
26# -d ${file}: deps file (required)
Yi Kongacee27c2019-03-29 20:05:14 -070027# -k symbols: Symbols to keep (optional)
Colin Cross665dce92016-04-28 14:50:03 -070028# --add-gnu-debuglink
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070029# --keep-mini-debug-info
30# --keep-symbols
Yi Kongb5c34d72018-11-07 16:28:49 -080031# --use-gnu-strip
Vic Yangefd249e2018-11-12 20:19:56 -080032# --remove-build-id
Colin Cross665dce92016-04-28 14:50:03 -070033
Colin Cross8e877af2018-09-19 15:09:26 -070034set -o pipefail
35
Yi Kongacee27c2019-03-29 20:05:14 -070036OPTSTRING=d:i:o:k:-:
Colin Cross665dce92016-04-28 14:50:03 -070037
38usage() {
39 cat <<EOF
Yi Kongacee27c2019-03-29 20:05:14 -070040Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file
Colin Cross665dce92016-04-28 14:50:03 -070041Options:
Colin Cross665dce92016-04-28 14:50:03 -070042 --add-gnu-debuglink Add a gnu-debuglink section to out-file
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070043 --keep-mini-debug-info Keep compressed debug info in out-file
44 --keep-symbols Keep symbols in out-file
Yi Kongb5c34d72018-11-07 16:28:49 -080045 --use-gnu-strip Use strip/objcopy instead of llvm-{strip,objcopy}
Vic Yangefd249e2018-11-12 20:19:56 -080046 --remove-build-id Remove the gnu build-id section in out-file
Colin Cross665dce92016-04-28 14:50:03 -070047EOF
48 exit 1
49}
50
Yi Kongb5c34d72018-11-07 16:28:49 -080051# Without --use-gnu-strip, GNU strip is replaced with llvm-strip to work around
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070052# old GNU strip bug on lld output files, b/80093681.
53# Similary, calls to objcopy are replaced with llvm-objcopy,
54# with some exceptions.
55
Colin Cross665dce92016-04-28 14:50:03 -070056do_strip() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070057 # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
58 # so we tell llvm-strip to keep it too.
Yi Kongb5c34d72018-11-07 16:28:49 -080059 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -080060 "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070061 else
62 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
63 fi
Colin Cross665dce92016-04-28 14:50:03 -070064}
65
66do_strip_keep_symbols() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070067 REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
Yi Kongb5c34d72018-11-07 16:28:49 -080068 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070069 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Colin Cross0abcbe62018-09-07 17:35:56 -070070 else
71 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070072 fi
Colin Cross665dce92016-04-28 14:50:03 -070073}
74
Yi Kongacee27c2019-03-29 20:05:14 -070075do_strip_keep_symbol_list() {
76 if [ -z "${use_gnu_strip}" ]; then
77 echo "do_strip_keep_symbol_list does not work with llvm-objcopy"
78 echo "http://b/131631155"
79 usage
80 fi
81
82 echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList"
83 KEEP_SYMBOLS="-w --strip-unneeded-symbol=* --keep-symbols="
84 KEEP_SYMBOLS+="${outfile}.symbolList"
85
86 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS}
87}
88
Colin Cross665dce92016-04-28 14:50:03 -070089do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -070090 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
Colin Cross02b04bb2018-09-05 13:14:09 -070091 local fail=
Yi Kongb5c34d72018-11-07 16:28:49 -080092 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -080093 "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes -remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070094 else
Colin Cross02b04bb2018-09-05 13:14:09 -070095 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070096 fi
Colin Cross02b04bb2018-09-05 13:14:09 -070097 if [ -z $fail ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070098 # Current prebult llvm-objcopy does not support the following flags:
99 # --only-keep-debug --rename-section --keep-symbols
100 # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
101 # except the --add-section flag.
Colin Cross1b594092017-04-13 16:19:00 -0700102 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
Colin Cross8e877af2018-09-19 15:09:26 -0700103 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms"
Colin Crossf8432902018-09-05 13:28:01 -0700104 "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms"
Colin Cross1b594092017-04-13 16:19:00 -0700105 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -0700106 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -0700107 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
108 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
109 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800110 "${XZ}" "${outfile}.mini_debuginfo"
Yi Kongb5c34d72018-11-07 16:28:49 -0800111 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700112 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
113 else
114 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
115 fi
Colin Cross3adb2ed2018-10-18 11:05:56 -0700116 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
Colin Cross1b594092017-04-13 16:19:00 -0700117 else
118 cp -f "${infile}" "${outfile}.tmp"
119 fi
Colin Cross665dce92016-04-28 14:50:03 -0700120}
121
122do_add_gnu_debuglink() {
Yi Kongb5c34d72018-11-07 16:28:49 -0800123 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700124 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
125 else
126 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
127 fi
Colin Cross665dce92016-04-28 14:50:03 -0700128}
129
Vic Yangefd249e2018-11-12 20:19:56 -0800130do_remove_build_id() {
131 if [ -z "${use_gnu_strip}" ]; then
132 "${CLANG_BIN}/llvm-strip" -remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
133 else
134 "${CROSS_COMPILE}strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
135 fi
136 rm -f "${outfile}.tmp"
137 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
138}
139
Colin Cross665dce92016-04-28 14:50:03 -0700140while getopts $OPTSTRING opt; do
141 case "$opt" in
Yi Kongacee27c2019-03-29 20:05:14 -0700142 d) depsfile="${OPTARG}" ;;
143 i) infile="${OPTARG}" ;;
144 o) outfile="${OPTARG}" ;;
145 k) symbols_to_keep="${OPTARG}" ;;
146 -)
147 case "${OPTARG}" in
148 add-gnu-debuglink) add_gnu_debuglink=true ;;
149 keep-mini-debug-info) keep_mini_debug_info=true ;;
150 keep-symbols) keep_symbols=true ;;
151 remove-build-id) remove_build_id=true ;;
152 use-gnu-strip) use_gnu_strip=true ;;
153 *) echo "Unknown option --${OPTARG}"; usage ;;
154 esac;;
155 ?) usage ;;
156 *) echo "'${opt}' '${OPTARG}'"
Colin Cross665dce92016-04-28 14:50:03 -0700157 esac
158done
159
160if [ -z "${infile}" ]; then
161 echo "-i argument is required"
162 usage
163fi
164
165if [ -z "${outfile}" ]; then
166 echo "-o argument is required"
167 usage
168fi
169
170if [ -z "${depsfile}" ]; then
171 echo "-d argument is required"
172 usage
173fi
174
175if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
176 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
177 usage
178fi
179
Yi Kongacee27c2019-03-29 20:05:14 -0700180if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then
181 echo "--keep-symbols and -k cannot be used together"
182 usage
183fi
184
Colin Cross665dce92016-04-28 14:50:03 -0700185if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
186 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
187 usage
188fi
189
190rm -f "${outfile}.tmp"
191
192if [ ! -z "${keep_symbols}" ]; then
193 do_strip_keep_symbols
Yi Kongacee27c2019-03-29 20:05:14 -0700194elif [ ! -z "${symbols_to_keep}" ]; then
195 do_strip_keep_symbol_list
Colin Cross665dce92016-04-28 14:50:03 -0700196elif [ ! -z "${keep_mini_debug_info}" ]; then
197 do_strip_keep_mini_debug_info
198else
199 do_strip
200fi
201
202if [ ! -z "${add_gnu_debuglink}" ]; then
203 do_add_gnu_debuglink
204fi
205
Vic Yangefd249e2018-11-12 20:19:56 -0800206if [ ! -z "${remove_build_id}" ]; then
207 do_remove_build_id
208fi
209
Colin Cross665dce92016-04-28 14:50:03 -0700210rm -f "${outfile}"
211mv "${outfile}.tmp" "${outfile}"
212
Yi Kongb5c34d72018-11-07 16:28:49 -0800213if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700214 USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
215else
216 USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
217fi
218
Colin Cross665dce92016-04-28 14:50:03 -0700219cat <<EOF > "${depsfile}"
220${outfile}: \
221 ${infile} \
222 ${CROSS_COMPILE}nm \
223 ${CROSS_COMPILE}objcopy \
224 ${CROSS_COMPILE}readelf \
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700225 ${USED_STRIP_OBJCOPY}
Colin Cross665dce92016-04-28 14:50:03 -0700226
227EOF