blob: 848fe8d42a6fd6fc4efc5f781331e5a82548ff6d [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:
20# CROSS_COMPILE: prefix added to readelf, objcopy tools
21# Arguments:
Colin Cross26c34ed2016-09-30 17:10:16 -070022# -i ${file}: input file (required)
Colin Cross665dce92016-04-28 14:50:03 -070023# -o ${file}: output file (required)
24# -d ${file}: deps file (required)
25# --keep-symbols
26# --keep-mini-debug-info
27# --add-gnu-debuglink
28
29OPTSTRING=d:i:o:-:
30
31usage() {
32 cat <<EOF
33Usage: strip.sh [options] -i in-file -o out-file -d deps-file
34Options:
35 --keep-symbols Keep symbols in out-file
36 --keep-mini-debug-info Keep compressed debug info in out-file
37 --add-gnu-debuglink Add a gnu-debuglink section to out-file
38EOF
39 exit 1
40}
41
42do_strip() {
43 "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
44}
45
46do_strip_keep_symbols() {
47 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \
48 `"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs`
49}
50
51do_strip_keep_mini_debug_info() {
Colin Cross1b594092017-04-13 16:19:00 -070052 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
53 if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then
54 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
55 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
56 "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
57 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
58 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
59 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
60 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
61 xz "${outfile}.mini_debuginfo"
62 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
63 else
64 cp -f "${infile}" "${outfile}.tmp"
65 fi
Colin Cross665dce92016-04-28 14:50:03 -070066}
67
68do_add_gnu_debuglink() {
69 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
70}
71
72while getopts $OPTSTRING opt; do
73 case "$opt" in
74 d) depsfile="${OPTARG}" ;;
75 i) infile="${OPTARG}" ;;
76 o) outfile="${OPTARG}" ;;
77 -)
78 case "${OPTARG}" in
79 keep-symbols) keep_symbols=true ;;
80 keep-mini-debug-info) keep_mini_debug_info=true ;;
81 add-gnu-debuglink) add_gnu_debuglink=true ;;
82 *) echo "Unknown option --${OPTARG}"; usage ;;
83 esac;;
84 ?) usage ;;
85 *) echo "'${opt}' '${OPTARG}'"
86 esac
87done
88
89if [ -z "${infile}" ]; then
90 echo "-i argument is required"
91 usage
92fi
93
94if [ -z "${outfile}" ]; then
95 echo "-o argument is required"
96 usage
97fi
98
99if [ -z "${depsfile}" ]; then
100 echo "-d argument is required"
101 usage
102fi
103
104if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
105 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
106 usage
107fi
108
109if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
110 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
111 usage
112fi
113
114rm -f "${outfile}.tmp"
115
116if [ ! -z "${keep_symbols}" ]; then
117 do_strip_keep_symbols
118elif [ ! -z "${keep_mini_debug_info}" ]; then
119 do_strip_keep_mini_debug_info
120else
121 do_strip
122fi
123
124if [ ! -z "${add_gnu_debuglink}" ]; then
125 do_add_gnu_debuglink
126fi
127
128rm -f "${outfile}"
129mv "${outfile}.tmp" "${outfile}"
130
131cat <<EOF > "${depsfile}"
132${outfile}: \
133 ${infile} \
134 ${CROSS_COMPILE}nm \
135 ${CROSS_COMPILE}objcopy \
136 ${CROSS_COMPILE}readelf \
137 ${CROSS_COMPILE}strip
138
139EOF