2016-04-29 05:50:03 +08:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2017-11-18 02:55:38 +08:00
|
|
|
# Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
# Script to handle the various ways soong may need to strip binaries
|
|
|
|
# Inputs:
|
|
|
|
# Environment:
|
2018-06-05 01:37:43 +08:00
|
|
|
# CLANG_BIN: path to the clang bin directory
|
2016-04-29 05:50:03 +08:00
|
|
|
# CROSS_COMPILE: prefix added to readelf, objcopy tools
|
2018-03-10 02:47:52 +08:00
|
|
|
# XZ: path to the xz binary
|
2016-04-29 05:50:03 +08:00
|
|
|
# Arguments:
|
2016-10-01 08:10:16 +08:00
|
|
|
# -i ${file}: input file (required)
|
2016-04-29 05:50:03 +08:00
|
|
|
# -o ${file}: output file (required)
|
|
|
|
# -d ${file}: deps file (required)
|
2019-03-30 11:05:14 +08:00
|
|
|
# -k symbols: Symbols to keep (optional)
|
2016-04-29 05:50:03 +08:00
|
|
|
# --add-gnu-debuglink
|
2018-06-05 01:37:43 +08:00
|
|
|
# --keep-mini-debug-info
|
|
|
|
# --keep-symbols
|
2019-05-18 07:39:54 +08:00
|
|
|
# --keep-symbols-and-debug-frame
|
2018-11-13 12:19:56 +08:00
|
|
|
# --remove-build-id
|
2016-04-29 05:50:03 +08:00
|
|
|
|
2018-09-20 06:09:26 +08:00
|
|
|
set -o pipefail
|
|
|
|
|
2019-03-30 11:05:14 +08:00
|
|
|
OPTSTRING=d:i:o:k:-:
|
2016-04-29 05:50:03 +08:00
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2019-03-30 11:05:14 +08:00
|
|
|
Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file
|
2016-04-29 05:50:03 +08:00
|
|
|
Options:
|
2019-05-18 07:39:54 +08:00
|
|
|
--add-gnu-debuglink Add a gnu-debuglink section to out-file
|
|
|
|
--keep-mini-debug-info Keep compressed debug info in out-file
|
|
|
|
--keep-symbols Keep symbols in out-file
|
|
|
|
--keep-symbols-and-debug-frame Keep symbols and .debug_frame in out-file
|
|
|
|
--remove-build-id Remove the gnu build-id section in out-file
|
2016-04-29 05:50:03 +08:00
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
do_strip() {
|
2019-09-13 12:24:12 +08:00
|
|
|
# GNU strip --strip-all does not strip .ARM.attributes,
|
2018-06-05 01:37:43 +08:00
|
|
|
# so we tell llvm-strip to keep it too.
|
2019-09-13 12:24:12 +08:00
|
|
|
"${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp"
|
2016-04-29 05:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-05-18 07:39:54 +08:00
|
|
|
do_strip_keep_symbols_and_debug_frame() {
|
2019-09-13 12:24:12 +08:00
|
|
|
REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs`
|
|
|
|
"${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
|
2019-05-18 07:39:54 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
do_strip_keep_symbols() {
|
2019-09-13 12:24:12 +08:00
|
|
|
REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
|
|
|
|
"${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
|
2016-04-29 05:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 11:05:14 +08:00
|
|
|
do_strip_keep_symbol_list() {
|
|
|
|
echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList"
|
|
|
|
|
2019-10-16 07:35:51 +08:00
|
|
|
KEEP_SYMBOLS="--strip-unneeded-symbol=* --keep-symbols="
|
2019-09-13 12:24:12 +08:00
|
|
|
KEEP_SYMBOLS+="${outfile}.symbolList"
|
2019-10-16 07:35:51 +08:00
|
|
|
"${CROSS_COMPILE}objcopy" -w "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS}
|
2019-03-30 11:05:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
do_strip_keep_mini_debug_info() {
|
2017-04-14 07:19:00 +08:00
|
|
|
rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
|
2018-09-06 04:14:09 +08:00
|
|
|
local fail=
|
2019-09-13 12:24:12 +08:00
|
|
|
"${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true
|
|
|
|
|
2018-09-06 04:14:09 +08:00
|
|
|
if [ -z $fail ]; then
|
2019-09-13 12:24:12 +08:00
|
|
|
# Current prebult llvm-objcopy does not support --only-keep-debug flag,
|
|
|
|
# and cannot process object files that are produced with the flag. Use
|
|
|
|
# GNU objcopy instead for now. (b/141010852)
|
2017-04-14 07:19:00 +08:00
|
|
|
"${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
|
2019-09-14 16:39:53 +08:00
|
|
|
"${CLANG_BIN}/llvm-nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms"
|
|
|
|
"${CLANG_BIN}/llvm-nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms"
|
2017-04-14 07:19:00 +08:00
|
|
|
comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
|
2018-03-28 13:15:45 +08:00
|
|
|
echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
|
2017-04-14 07:19:00 +08:00
|
|
|
"${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
|
|
|
|
"${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
|
|
|
|
"${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
|
2021-02-04 00:51:22 +08:00
|
|
|
"${XZ}" --block-size=64k --threads=0 "${outfile}.mini_debuginfo"
|
2019-09-13 12:24:12 +08:00
|
|
|
|
|
|
|
"${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
|
2018-10-19 02:05:56 +08:00
|
|
|
rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
|
2017-04-14 07:19:00 +08:00
|
|
|
else
|
|
|
|
cp -f "${infile}" "${outfile}.tmp"
|
|
|
|
fi
|
2016-04-29 05:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do_add_gnu_debuglink() {
|
2019-09-13 12:24:12 +08:00
|
|
|
"${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
|
2016-04-29 05:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-11-13 12:19:56 +08:00
|
|
|
do_remove_build_id() {
|
2019-09-13 12:24:12 +08:00
|
|
|
"${CLANG_BIN}/llvm-strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id"
|
2018-11-13 12:19:56 +08:00
|
|
|
rm -f "${outfile}.tmp"
|
|
|
|
mv "${outfile}.tmp.no-build-id" "${outfile}.tmp"
|
|
|
|
}
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
while getopts $OPTSTRING opt; do
|
|
|
|
case "$opt" in
|
2019-03-30 11:05:14 +08:00
|
|
|
d) depsfile="${OPTARG}" ;;
|
|
|
|
i) infile="${OPTARG}" ;;
|
|
|
|
o) outfile="${OPTARG}" ;;
|
|
|
|
k) symbols_to_keep="${OPTARG}" ;;
|
|
|
|
-)
|
|
|
|
case "${OPTARG}" in
|
|
|
|
add-gnu-debuglink) add_gnu_debuglink=true ;;
|
|
|
|
keep-mini-debug-info) keep_mini_debug_info=true ;;
|
|
|
|
keep-symbols) keep_symbols=true ;;
|
2019-05-18 07:39:54 +08:00
|
|
|
keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;;
|
2019-03-30 11:05:14 +08:00
|
|
|
remove-build-id) remove_build_id=true ;;
|
|
|
|
*) echo "Unknown option --${OPTARG}"; usage ;;
|
|
|
|
esac;;
|
|
|
|
?) usage ;;
|
|
|
|
*) echo "'${opt}' '${OPTARG}'"
|
2016-04-29 05:50:03 +08:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "${infile}" ]; then
|
|
|
|
echo "-i argument is required"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${outfile}" ]; then
|
|
|
|
echo "-o argument is required"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${depsfile}" ]; then
|
|
|
|
echo "-d argument is required"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
|
|
|
|
echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2019-05-18 07:39:54 +08:00
|
|
|
if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
|
|
|
|
echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then
|
|
|
|
echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2019-03-30 11:05:14 +08:00
|
|
|
if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then
|
|
|
|
echo "--keep-symbols and -k cannot be used together"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
|
|
|
|
echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f "${outfile}.tmp"
|
|
|
|
|
|
|
|
if [ ! -z "${keep_symbols}" ]; then
|
|
|
|
do_strip_keep_symbols
|
2019-03-30 11:05:14 +08:00
|
|
|
elif [ ! -z "${symbols_to_keep}" ]; then
|
|
|
|
do_strip_keep_symbol_list
|
2016-04-29 05:50:03 +08:00
|
|
|
elif [ ! -z "${keep_mini_debug_info}" ]; then
|
|
|
|
do_strip_keep_mini_debug_info
|
2019-05-18 07:39:54 +08:00
|
|
|
elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then
|
|
|
|
do_strip_keep_symbols_and_debug_frame
|
2016-04-29 05:50:03 +08:00
|
|
|
else
|
|
|
|
do_strip
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "${add_gnu_debuglink}" ]; then
|
|
|
|
do_add_gnu_debuglink
|
|
|
|
fi
|
|
|
|
|
2018-11-13 12:19:56 +08:00
|
|
|
if [ ! -z "${remove_build_id}" ]; then
|
|
|
|
do_remove_build_id
|
|
|
|
fi
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
rm -f "${outfile}"
|
|
|
|
mv "${outfile}.tmp" "${outfile}"
|
|
|
|
|
|
|
|
cat <<EOF > "${depsfile}"
|
|
|
|
${outfile}: \
|
|
|
|
${infile} \
|
|
|
|
${CROSS_COMPILE}objcopy \
|
2019-09-14 16:39:53 +08:00
|
|
|
${CLANG_BIN}/llvm-nm \
|
|
|
|
${CLANG_BIN}/llvm-objcopy \
|
|
|
|
${CLANG_BIN}/llvm-readelf \
|
|
|
|
${CLANG_BIN}/llvm-strip
|
2016-04-29 05:50:03 +08:00
|
|
|
|
|
|
|
EOF
|