blob: bd62619b2c474a72f32f92b63c773047168daf95 [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
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070031# --keep-symbols-and-debug-frame
Yi Kongb5c34d72018-11-07 16:28:49 -080032# --use-gnu-strip
Vic Yangefd249e2018-11-12 20:19:56 -080033# --remove-build-id
Colin Cross665dce92016-04-28 14:50:03 -070034
Colin Cross8e877af2018-09-19 15:09:26 -070035set -o pipefail
36
Yi Kongacee27c2019-03-29 20:05:14 -070037OPTSTRING=d:i:o:k:-:
Colin Cross665dce92016-04-28 14:50:03 -070038
39usage() {
40 cat <<EOF
Yi Kongacee27c2019-03-29 20:05:14 -070041Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file
Colin Cross665dce92016-04-28 14:50:03 -070042Options:
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070043 --add-gnu-debuglink Add a gnu-debuglink section to out-file
44 --keep-mini-debug-info Keep compressed debug info in out-file
45 --keep-symbols Keep symbols in out-file
46 --keep-symbols-and-debug-frame Keep symbols and .debug_frame in out-file
47 --use-gnu-strip Use strip/objcopy instead of llvm-{strip,objcopy}
48 --remove-build-id Remove the gnu build-id section in out-file
Colin Cross665dce92016-04-28 14:50:03 -070049EOF
50 exit 1
51}
52
Yi Kongb5c34d72018-11-07 16:28:49 -080053# Without --use-gnu-strip, GNU strip is replaced with llvm-strip to work around
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070054# old GNU strip bug on lld output files, b/80093681.
55# Similary, calls to objcopy are replaced with llvm-objcopy,
56# with some exceptions.
57
Colin Cross665dce92016-04-28 14:50:03 -070058do_strip() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070059 # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
60 # so we tell llvm-strip to keep it too.
Yi Kongb5c34d72018-11-07 16:28:49 -080061 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -080062 "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070063 else
64 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
65 fi
Colin Cross665dce92016-04-28 14:50:03 -070066}
67
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070068do_strip_keep_symbols_and_debug_frame() {
69 REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs`
70 if [ -z "${use_gnu_strip}" ]; then
71 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
72 else
73 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
74 fi
75}
76
Colin Cross665dce92016-04-28 14:50:03 -070077do_strip_keep_symbols() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070078 REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
Yi Kongb5c34d72018-11-07 16:28:49 -080079 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070080 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Colin Cross0abcbe62018-09-07 17:35:56 -070081 else
82 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070083 fi
Colin Cross665dce92016-04-28 14:50:03 -070084}
85
Yi Kongacee27c2019-03-29 20:05:14 -070086do_strip_keep_symbol_list() {
87 if [ -z "${use_gnu_strip}" ]; then
88 echo "do_strip_keep_symbol_list does not work with llvm-objcopy"
89 echo "http://b/131631155"
90 usage
91 fi
92
93 echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList"
94 KEEP_SYMBOLS="-w --strip-unneeded-symbol=* --keep-symbols="
95 KEEP_SYMBOLS+="${outfile}.symbolList"
96
97 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS}
98}
99
Colin Cross665dce92016-04-28 14:50:03 -0700100do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -0700101 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 -0700102 local fail=
Yi Kongb5c34d72018-11-07 16:28:49 -0800103 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -0800104 "${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 -0700105 else
Colin Cross02b04bb2018-09-05 13:14:09 -0700106 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700107 fi
Colin Cross02b04bb2018-09-05 13:14:09 -0700108 if [ -z $fail ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700109 # Current prebult llvm-objcopy does not support the following flags:
110 # --only-keep-debug --rename-section --keep-symbols
111 # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
112 # except the --add-section flag.
Colin Cross1b594092017-04-13 16:19:00 -0700113 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
Colin Cross8e877af2018-09-19 15:09:26 -0700114 "${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 -0700115 "${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 -0700116 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -0700117 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -0700118 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
119 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
120 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800121 "${XZ}" "${outfile}.mini_debuginfo"
Yi Kongb5c34d72018-11-07 16:28:49 -0800122 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700123 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
124 else
125 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
126 fi
Colin Cross3adb2ed2018-10-18 11:05:56 -0700127 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 -0700128 else
129 cp -f "${infile}" "${outfile}.tmp"
130 fi
Colin Cross665dce92016-04-28 14:50:03 -0700131}
132
133do_add_gnu_debuglink() {
Yi Kongb5c34d72018-11-07 16:28:49 -0800134 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700135 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
136 else
137 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
138 fi
Colin Cross665dce92016-04-28 14:50:03 -0700139}
140
Vic Yangefd249e2018-11-12 20:19:56 -0800141do_remove_build_id() {
142 if [ -z "${use_gnu_strip}" ]; then
143 "${CLANG_BIN}/llvm-strip" -remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
144 else
145 "${CROSS_COMPILE}strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
146 fi
147 rm -f "${outfile}.tmp"
148 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
149}
150
Colin Cross665dce92016-04-28 14:50:03 -0700151while getopts $OPTSTRING opt; do
152 case "$opt" in
Yi Kongacee27c2019-03-29 20:05:14 -0700153 d) depsfile="${OPTARG}" ;;
154 i) infile="${OPTARG}" ;;
155 o) outfile="${OPTARG}" ;;
156 k) symbols_to_keep="${OPTARG}" ;;
157 -)
158 case "${OPTARG}" in
159 add-gnu-debuglink) add_gnu_debuglink=true ;;
160 keep-mini-debug-info) keep_mini_debug_info=true ;;
161 keep-symbols) keep_symbols=true ;;
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700162 keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;;
Yi Kongacee27c2019-03-29 20:05:14 -0700163 remove-build-id) remove_build_id=true ;;
164 use-gnu-strip) use_gnu_strip=true ;;
165 *) echo "Unknown option --${OPTARG}"; usage ;;
166 esac;;
167 ?) usage ;;
168 *) echo "'${opt}' '${OPTARG}'"
Colin Cross665dce92016-04-28 14:50:03 -0700169 esac
170done
171
172if [ -z "${infile}" ]; then
173 echo "-i argument is required"
174 usage
175fi
176
177if [ -z "${outfile}" ]; then
178 echo "-o argument is required"
179 usage
180fi
181
182if [ -z "${depsfile}" ]; then
183 echo "-d argument is required"
184 usage
185fi
186
187if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
188 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
189 usage
190fi
191
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700192if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
193 echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together"
194 usage
195fi
196
197if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
198 echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together"
199 usage
200fi
201
Yi Kongacee27c2019-03-29 20:05:14 -0700202if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then
203 echo "--keep-symbols and -k cannot be used together"
204 usage
205fi
206
Colin Cross665dce92016-04-28 14:50:03 -0700207if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
208 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
209 usage
210fi
211
212rm -f "${outfile}.tmp"
213
214if [ ! -z "${keep_symbols}" ]; then
215 do_strip_keep_symbols
Yi Kongacee27c2019-03-29 20:05:14 -0700216elif [ ! -z "${symbols_to_keep}" ]; then
217 do_strip_keep_symbol_list
Colin Cross665dce92016-04-28 14:50:03 -0700218elif [ ! -z "${keep_mini_debug_info}" ]; then
219 do_strip_keep_mini_debug_info
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700220elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then
221 do_strip_keep_symbols_and_debug_frame
Colin Cross665dce92016-04-28 14:50:03 -0700222else
223 do_strip
224fi
225
226if [ ! -z "${add_gnu_debuglink}" ]; then
227 do_add_gnu_debuglink
228fi
229
Vic Yangefd249e2018-11-12 20:19:56 -0800230if [ ! -z "${remove_build_id}" ]; then
231 do_remove_build_id
232fi
233
Colin Cross665dce92016-04-28 14:50:03 -0700234rm -f "${outfile}"
235mv "${outfile}.tmp" "${outfile}"
236
Yi Kongb5c34d72018-11-07 16:28:49 -0800237if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700238 USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
239else
240 USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
241fi
242
Colin Cross665dce92016-04-28 14:50:03 -0700243cat <<EOF > "${depsfile}"
244${outfile}: \
245 ${infile} \
246 ${CROSS_COMPILE}nm \
247 ${CROSS_COMPILE}objcopy \
248 ${CROSS_COMPILE}readelf \
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700249 ${USED_STRIP_OBJCOPY}
Colin Cross665dce92016-04-28 14:50:03 -0700250
251EOF