blob: d53690716fca7faa62191ba358974cce96d09360 [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)
Colin Cross665dce92016-04-28 14:50:03 -070027# --add-gnu-debuglink
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070028# --keep-mini-debug-info
29# --keep-symbols
Yi Kongb5c34d72018-11-07 16:28:49 -080030# --use-gnu-strip
Vic Yangefd249e2018-11-12 20:19:56 -080031# --remove-build-id
Colin Cross665dce92016-04-28 14:50:03 -070032
Colin Cross8e877af2018-09-19 15:09:26 -070033set -o pipefail
34
Colin Cross665dce92016-04-28 14:50:03 -070035OPTSTRING=d:i:o:-:
36
37usage() {
38 cat <<EOF
39Usage: strip.sh [options] -i in-file -o out-file -d deps-file
40Options:
Colin Cross665dce92016-04-28 14:50:03 -070041 --add-gnu-debuglink Add a gnu-debuglink section to out-file
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070042 --keep-mini-debug-info Keep compressed debug info in out-file
43 --keep-symbols Keep symbols in out-file
Yi Kongb5c34d72018-11-07 16:28:49 -080044 --use-gnu-strip Use strip/objcopy instead of llvm-{strip,objcopy}
Vic Yangefd249e2018-11-12 20:19:56 -080045 --remove-build-id Remove the gnu build-id section in out-file
Colin Cross665dce92016-04-28 14:50:03 -070046EOF
47 exit 1
48}
49
Yi Kongb5c34d72018-11-07 16:28:49 -080050# Without --use-gnu-strip, GNU strip is replaced with llvm-strip to work around
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070051# old GNU strip bug on lld output files, b/80093681.
52# Similary, calls to objcopy are replaced with llvm-objcopy,
53# with some exceptions.
54
Colin Cross665dce92016-04-28 14:50:03 -070055do_strip() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070056 # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
57 # so we tell llvm-strip to keep it too.
Yi Kongb5c34d72018-11-07 16:28:49 -080058 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -080059 "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070060 else
61 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
62 fi
Colin Cross665dce92016-04-28 14:50:03 -070063}
64
65do_strip_keep_symbols() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070066 REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
Yi Kongb5c34d72018-11-07 16:28:49 -080067 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070068 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Colin Cross0abcbe62018-09-07 17:35:56 -070069 else
70 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070071 fi
Colin Cross665dce92016-04-28 14:50:03 -070072}
73
74do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -070075 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 -070076 local fail=
Yi Kongb5c34d72018-11-07 16:28:49 -080077 if [ -z "${use_gnu_strip}" ]; then
Pirama Arumuga Nainar03b58e22019-01-17 11:53:03 -080078 "${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 -070079 else
Colin Cross02b04bb2018-09-05 13:14:09 -070080 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070081 fi
Colin Cross02b04bb2018-09-05 13:14:09 -070082 if [ -z $fail ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070083 # Current prebult llvm-objcopy does not support the following flags:
84 # --only-keep-debug --rename-section --keep-symbols
85 # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
86 # except the --add-section flag.
Colin Cross1b594092017-04-13 16:19:00 -070087 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
Colin Cross8e877af2018-09-19 15:09:26 -070088 "${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 -070089 "${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 -070090 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -070091 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -070092 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
93 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
94 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -080095 "${XZ}" "${outfile}.mini_debuginfo"
Yi Kongb5c34d72018-11-07 16:28:49 -080096 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070097 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
98 else
99 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
100 fi
Colin Cross3adb2ed2018-10-18 11:05:56 -0700101 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 -0700102 else
103 cp -f "${infile}" "${outfile}.tmp"
104 fi
Colin Cross665dce92016-04-28 14:50:03 -0700105}
106
107do_add_gnu_debuglink() {
Yi Kongb5c34d72018-11-07 16:28:49 -0800108 if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700109 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
110 else
111 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
112 fi
Colin Cross665dce92016-04-28 14:50:03 -0700113}
114
Vic Yangefd249e2018-11-12 20:19:56 -0800115do_remove_build_id() {
116 if [ -z "${use_gnu_strip}" ]; then
117 "${CLANG_BIN}/llvm-strip" -remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
118 else
119 "${CROSS_COMPILE}strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
120 fi
121 rm -f "${outfile}.tmp"
122 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
123}
124
Colin Cross665dce92016-04-28 14:50:03 -0700125while getopts $OPTSTRING opt; do
126 case "$opt" in
127 d) depsfile="${OPTARG}" ;;
128 i) infile="${OPTARG}" ;;
129 o) outfile="${OPTARG}" ;;
130 -)
131 case "${OPTARG}" in
Colin Cross665dce92016-04-28 14:50:03 -0700132 add-gnu-debuglink) add_gnu_debuglink=true ;;
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700133 keep-mini-debug-info) keep_mini_debug_info=true ;;
134 keep-symbols) keep_symbols=true ;;
Vic Yangefd249e2018-11-12 20:19:56 -0800135 remove-build-id) remove_build_id=true ;;
Colin Cross665dce92016-04-28 14:50:03 -0700136 *) echo "Unknown option --${OPTARG}"; usage ;;
137 esac;;
138 ?) usage ;;
139 *) echo "'${opt}' '${OPTARG}'"
140 esac
141done
142
143if [ -z "${infile}" ]; then
144 echo "-i argument is required"
145 usage
146fi
147
148if [ -z "${outfile}" ]; then
149 echo "-o argument is required"
150 usage
151fi
152
153if [ -z "${depsfile}" ]; then
154 echo "-d argument is required"
155 usage
156fi
157
158if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
159 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
160 usage
161fi
162
163if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
164 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
165 usage
166fi
167
168rm -f "${outfile}.tmp"
169
170if [ ! -z "${keep_symbols}" ]; then
171 do_strip_keep_symbols
172elif [ ! -z "${keep_mini_debug_info}" ]; then
173 do_strip_keep_mini_debug_info
174else
175 do_strip
176fi
177
178if [ ! -z "${add_gnu_debuglink}" ]; then
179 do_add_gnu_debuglink
180fi
181
Vic Yangefd249e2018-11-12 20:19:56 -0800182if [ ! -z "${remove_build_id}" ]; then
183 do_remove_build_id
184fi
185
Colin Cross665dce92016-04-28 14:50:03 -0700186rm -f "${outfile}"
187mv "${outfile}.tmp" "${outfile}"
188
Yi Kongb5c34d72018-11-07 16:28:49 -0800189if [ -z "${use_gnu_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700190 USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
191else
192 USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
193fi
194
Colin Cross665dce92016-04-28 14:50:03 -0700195cat <<EOF > "${depsfile}"
196${outfile}: \
197 ${infile} \
198 ${CROSS_COMPILE}nm \
199 ${CROSS_COMPILE}objcopy \
200 ${CROSS_COMPILE}readelf \
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700201 ${USED_STRIP_OBJCOPY}
Colin Cross665dce92016-04-28 14:50:03 -0700202
203EOF