blob: cf8f63144ef0c7422ae31e1fc3dda5cb823cb98e [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
30# --use-llvm-strip
Colin Cross665dce92016-04-28 14:50:03 -070031
32OPTSTRING=d:i:o:-:
33
34usage() {
35 cat <<EOF
36Usage: strip.sh [options] -i in-file -o out-file -d deps-file
37Options:
Colin Cross665dce92016-04-28 14:50:03 -070038 --add-gnu-debuglink Add a gnu-debuglink section to out-file
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070039 --keep-mini-debug-info Keep compressed debug info in out-file
40 --keep-symbols Keep symbols in out-file
41 --use-llvm-strip Use llvm-{strip,objcopy} instead of strip/objcopy
Colin Cross665dce92016-04-28 14:50:03 -070042EOF
43 exit 1
44}
45
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070046# With --use-llvm-strip, GNU strip is replaced with llvm-strip to work around
47# old GNU strip bug on lld output files, b/80093681.
48# Similary, calls to objcopy are replaced with llvm-objcopy,
49# with some exceptions.
50
Colin Cross665dce92016-04-28 14:50:03 -070051do_strip() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070052 # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
53 # so we tell llvm-strip to keep it too.
54 if [ ! -z "${use_llvm_strip}" ]; then
55 "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes "${infile}" "${outfile}.tmp"
56 else
57 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
58 fi
Colin Cross665dce92016-04-28 14:50:03 -070059}
60
61do_strip_keep_symbols() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070062 # Maybe we should replace this objcopy with llvm-objcopy, but
63 # we have not found a use case that is broken by objcopy yet.
64 REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
65 if [ ! -z "${use_llvm_strip}" ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070066 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Colin Cross0abcbe62018-09-07 17:35:56 -070067 else
68 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070069 fi
Colin Cross665dce92016-04-28 14:50:03 -070070}
71
72do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -070073 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 -070074 local fail=
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070075 if [ ! -z "${use_llvm_strip}" ]; then
Colin Cross02b04bb2018-09-05 13:14:09 -070076 "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070077 else
Colin Cross02b04bb2018-09-05 13:14:09 -070078 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070079 fi
Colin Cross02b04bb2018-09-05 13:14:09 -070080 if [ -z $fail ]; then
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070081 # Current prebult llvm-objcopy does not support the following flags:
82 # --only-keep-debug --rename-section --keep-symbols
83 # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
84 # except the --add-section flag.
Colin Cross1b594092017-04-13 16:19:00 -070085 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
Colin Crossf8432902018-09-05 13:28:01 -070086 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $1 }' | sort >"${outfile}.dynsyms"
87 "${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 -070088 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -070089 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -070090 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
91 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
92 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -080093 "${XZ}" "${outfile}.mini_debuginfo"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070094 if [ ! -z "${use_llvm_strip}" ]; then
95 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
96 else
97 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
98 fi
Colin Cross1b594092017-04-13 16:19:00 -070099 else
100 cp -f "${infile}" "${outfile}.tmp"
101 fi
Colin Cross665dce92016-04-28 14:50:03 -0700102}
103
104do_add_gnu_debuglink() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700105 if [ ! -z "${use_llvm_strip}" ]; then
106 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
107 else
108 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
109 fi
Colin Cross665dce92016-04-28 14:50:03 -0700110}
111
112while getopts $OPTSTRING opt; do
113 case "$opt" in
114 d) depsfile="${OPTARG}" ;;
115 i) infile="${OPTARG}" ;;
116 o) outfile="${OPTARG}" ;;
117 -)
118 case "${OPTARG}" in
Colin Cross665dce92016-04-28 14:50:03 -0700119 add-gnu-debuglink) add_gnu_debuglink=true ;;
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700120 keep-mini-debug-info) keep_mini_debug_info=true ;;
121 keep-symbols) keep_symbols=true ;;
122 use-llvm-strip) use_llvm_strip=true ;;
Colin Cross665dce92016-04-28 14:50:03 -0700123 *) echo "Unknown option --${OPTARG}"; usage ;;
124 esac;;
125 ?) usage ;;
126 *) echo "'${opt}' '${OPTARG}'"
127 esac
128done
129
130if [ -z "${infile}" ]; then
131 echo "-i argument is required"
132 usage
133fi
134
135if [ -z "${outfile}" ]; then
136 echo "-o argument is required"
137 usage
138fi
139
140if [ -z "${depsfile}" ]; then
141 echo "-d argument is required"
142 usage
143fi
144
145if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
146 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
147 usage
148fi
149
150if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
151 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
152 usage
153fi
154
155rm -f "${outfile}.tmp"
156
157if [ ! -z "${keep_symbols}" ]; then
158 do_strip_keep_symbols
159elif [ ! -z "${keep_mini_debug_info}" ]; then
160 do_strip_keep_mini_debug_info
161else
162 do_strip
163fi
164
165if [ ! -z "${add_gnu_debuglink}" ]; then
166 do_add_gnu_debuglink
167fi
168
169rm -f "${outfile}"
170mv "${outfile}.tmp" "${outfile}"
171
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700172if [ ! -z "${use_llvm_strip}" ]; then
173 USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
174else
175 USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
176fi
177
Colin Cross665dce92016-04-28 14:50:03 -0700178cat <<EOF > "${depsfile}"
179${outfile}: \
180 ${infile} \
181 ${CROSS_COMPILE}nm \
182 ${CROSS_COMPILE}objcopy \
183 ${CROSS_COMPILE}readelf \
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700184 ${USED_STRIP_OBJCOPY}
Colin Cross665dce92016-04-28 14:50:03 -0700185
186EOF