blob: e3e527381c623065332324695f2874a91ffc826a [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
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)
Yi Kongacee27c2019-03-29 20:05:14 -070026# -k symbols: Symbols to keep (optional)
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
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070030# --keep-symbols-and-debug-frame
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
Yi Kongacee27c2019-03-29 20:05:14 -070035OPTSTRING=d:i:o:k:-:
Colin Cross665dce92016-04-28 14:50:03 -070036
37usage() {
38 cat <<EOF
Yi Kongacee27c2019-03-29 20:05:14 -070039Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file
Colin Cross665dce92016-04-28 14:50:03 -070040Options:
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070041 --add-gnu-debuglink Add a gnu-debuglink section to out-file
42 --keep-mini-debug-info Keep compressed debug info in out-file
43 --keep-symbols Keep symbols in out-file
44 --keep-symbols-and-debug-frame Keep symbols and .debug_frame in out-file
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070045 --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
50do_strip() {
Yi Kongab5e5142019-09-12 21:24:12 -070051 # GNU strip --strip-all does not strip .ARM.attributes,
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070052 # so we tell llvm-strip to keep it too.
Yi Kongab5e5142019-09-12 21:24:12 -070053 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp"
Colin Cross665dce92016-04-28 14:50:03 -070054}
55
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070056do_strip_keep_symbols_and_debug_frame() {
Yi Kongab5e5142019-09-12 21:24:12 -070057 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs`
58 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070059}
60
Colin Cross665dce92016-04-28 14:50:03 -070061do_strip_keep_symbols() {
Yi Kongab5e5142019-09-12 21:24:12 -070062 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
63 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Colin Cross665dce92016-04-28 14:50:03 -070064}
65
Yi Kongacee27c2019-03-29 20:05:14 -070066do_strip_keep_symbol_list() {
Yi Kongacee27c2019-03-29 20:05:14 -070067 echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList"
Yi Kongacee27c2019-03-29 20:05:14 -070068
Yi Kong049ae6a2019-10-15 16:35:51 -070069 KEEP_SYMBOLS="--strip-unneeded-symbol=* --keep-symbols="
Yi Kongab5e5142019-09-12 21:24:12 -070070 KEEP_SYMBOLS+="${outfile}.symbolList"
David Srbecky789fe0f2021-04-08 16:52:05 +010071 "${CLANG_BIN}/llvm-objcopy" -w "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS}
Yi Kongacee27c2019-03-29 20:05:14 -070072}
73
Colin Cross665dce92016-04-28 14:50:03 -070074do_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 Kongab5e5142019-09-12 21:24:12 -070077 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true
78
Colin Cross02b04bb2018-09-05 13:14:09 -070079 if [ -z $fail ]; then
David Srbecky789fe0f2021-04-08 16:52:05 +010080 "${CLANG_BIN}/llvm-objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
Yi Kongeb63f762019-09-14 01:39:53 -070081 "${CLANG_BIN}/llvm-nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms"
82 "${CLANG_BIN}/llvm-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 -070083 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -070084 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
David Srbecky789fe0f2021-04-08 16:52:05 +010085 "${CLANG_BIN}/llvm-objcopy" -S --keep-section .debug_frame --keep-symbols="${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo"
86 "${XZ}" --keep --block-size=64k --threads=0 "${outfile}.mini_debuginfo"
Yi Kongab5e5142019-09-12 21:24:12 -070087
88 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
Colin Cross3adb2ed2018-10-18 11:05:56 -070089 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 -070090 else
91 cp -f "${infile}" "${outfile}.tmp"
92 fi
Colin Cross665dce92016-04-28 14:50:03 -070093}
94
95do_add_gnu_debuglink() {
Yi Kongab5e5142019-09-12 21:24:12 -070096 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
Colin Cross665dce92016-04-28 14:50:03 -070097}
98
Vic Yangefd249e2018-11-12 20:19:56 -080099do_remove_build_id() {
Yi Kongab5e5142019-09-12 21:24:12 -0700100 "${CLANG_BIN}/llvm-strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
Vic Yangefd249e2018-11-12 20:19:56 -0800101 rm -f "${outfile}.tmp"
102 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
103}
104
Colin Cross665dce92016-04-28 14:50:03 -0700105while getopts $OPTSTRING opt; do
106 case "$opt" in
Yi Kongacee27c2019-03-29 20:05:14 -0700107 d) depsfile="${OPTARG}" ;;
108 i) infile="${OPTARG}" ;;
109 o) outfile="${OPTARG}" ;;
110 k) symbols_to_keep="${OPTARG}" ;;
111 -)
112 case "${OPTARG}" in
113 add-gnu-debuglink) add_gnu_debuglink=true ;;
114 keep-mini-debug-info) keep_mini_debug_info=true ;;
115 keep-symbols) keep_symbols=true ;;
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700116 keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;;
Yi Kongacee27c2019-03-29 20:05:14 -0700117 remove-build-id) remove_build_id=true ;;
Yi Kongacee27c2019-03-29 20:05:14 -0700118 *) echo "Unknown option --${OPTARG}"; usage ;;
119 esac;;
120 ?) usage ;;
121 *) echo "'${opt}' '${OPTARG}'"
Colin Cross665dce92016-04-28 14:50:03 -0700122 esac
123done
124
125if [ -z "${infile}" ]; then
126 echo "-i argument is required"
127 usage
128fi
129
130if [ -z "${outfile}" ]; then
131 echo "-o argument is required"
132 usage
133fi
134
135if [ -z "${depsfile}" ]; then
136 echo "-d argument is required"
137 usage
138fi
139
140if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
141 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
142 usage
143fi
144
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700145if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
146 echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together"
147 usage
148fi
149
150if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
151 echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together"
152 usage
153fi
154
Yi Kongacee27c2019-03-29 20:05:14 -0700155if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then
156 echo "--keep-symbols and -k cannot be used together"
157 usage
158fi
159
Colin Cross665dce92016-04-28 14:50:03 -0700160if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
161 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
162 usage
163fi
164
165rm -f "${outfile}.tmp"
166
167if [ ! -z "${keep_symbols}" ]; then
168 do_strip_keep_symbols
Yi Kongacee27c2019-03-29 20:05:14 -0700169elif [ ! -z "${symbols_to_keep}" ]; then
170 do_strip_keep_symbol_list
Colin Cross665dce92016-04-28 14:50:03 -0700171elif [ ! -z "${keep_mini_debug_info}" ]; then
172 do_strip_keep_mini_debug_info
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700173elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then
174 do_strip_keep_symbols_and_debug_frame
Colin Cross665dce92016-04-28 14:50:03 -0700175else
176 do_strip
177fi
178
179if [ ! -z "${add_gnu_debuglink}" ]; then
180 do_add_gnu_debuglink
181fi
182
Vic Yangefd249e2018-11-12 20:19:56 -0800183if [ ! -z "${remove_build_id}" ]; then
184 do_remove_build_id
185fi
186
Colin Cross665dce92016-04-28 14:50:03 -0700187rm -f "${outfile}"
188mv "${outfile}.tmp" "${outfile}"
189
190cat <<EOF > "${depsfile}"
191${outfile}: \
192 ${infile} \
Yi Kongeb63f762019-09-14 01:39:53 -0700193 ${CLANG_BIN}/llvm-nm \
194 ${CLANG_BIN}/llvm-objcopy \
195 ${CLANG_BIN}/llvm-readelf \
196 ${CLANG_BIN}/llvm-strip
Colin Cross665dce92016-04-28 14:50:03 -0700197
198EOF