blob: 432582fc03dc58c444701220218782b9ad254396 [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
66 "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
67 else
68 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
69 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"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070074 if [ ! -z "${use_llvm_strip}" ]; then
75 "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" "${outfile}.tmp"
76 else
77 "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"
78 fi
79 if [ "$?" == "0" ]; then
80 # Current prebult llvm-objcopy does not support the following flags:
81 # --only-keep-debug --rename-section --keep-symbols
82 # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
83 # except the --add-section flag.
Colin Cross1b594092017-04-13 16:19:00 -070084 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
85 "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
86 "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
87 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
Ryan Prichardafefacf2018-03-27 22:15:45 -070088 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
Colin Cross1b594092017-04-13 16:19:00 -070089 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
90 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
91 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
Dan Willemsen8fec83a2018-03-09 10:47:52 -080092 "${XZ}" "${outfile}.mini_debuginfo"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -070093 if [ ! -z "${use_llvm_strip}" ]; then
94 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
95 else
96 "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
97 fi
Colin Cross1b594092017-04-13 16:19:00 -070098 else
99 cp -f "${infile}" "${outfile}.tmp"
100 fi
Colin Cross665dce92016-04-28 14:50:03 -0700101}
102
103do_add_gnu_debuglink() {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700104 if [ ! -z "${use_llvm_strip}" ]; then
105 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
106 else
107 "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
108 fi
Colin Cross665dce92016-04-28 14:50:03 -0700109}
110
111while getopts $OPTSTRING opt; do
112 case "$opt" in
113 d) depsfile="${OPTARG}" ;;
114 i) infile="${OPTARG}" ;;
115 o) outfile="${OPTARG}" ;;
116 -)
117 case "${OPTARG}" in
Colin Cross665dce92016-04-28 14:50:03 -0700118 add-gnu-debuglink) add_gnu_debuglink=true ;;
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700119 keep-mini-debug-info) keep_mini_debug_info=true ;;
120 keep-symbols) keep_symbols=true ;;
121 use-llvm-strip) use_llvm_strip=true ;;
Colin Cross665dce92016-04-28 14:50:03 -0700122 *) echo "Unknown option --${OPTARG}"; usage ;;
123 esac;;
124 ?) usage ;;
125 *) echo "'${opt}' '${OPTARG}'"
126 esac
127done
128
129if [ -z "${infile}" ]; then
130 echo "-i argument is required"
131 usage
132fi
133
134if [ -z "${outfile}" ]; then
135 echo "-o argument is required"
136 usage
137fi
138
139if [ -z "${depsfile}" ]; then
140 echo "-d argument is required"
141 usage
142fi
143
144if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
145 echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
146 usage
147fi
148
149if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
150 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
151 usage
152fi
153
154rm -f "${outfile}.tmp"
155
156if [ ! -z "${keep_symbols}" ]; then
157 do_strip_keep_symbols
158elif [ ! -z "${keep_mini_debug_info}" ]; then
159 do_strip_keep_mini_debug_info
160else
161 do_strip
162fi
163
164if [ ! -z "${add_gnu_debuglink}" ]; then
165 do_add_gnu_debuglink
166fi
167
168rm -f "${outfile}"
169mv "${outfile}.tmp" "${outfile}"
170
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700171if [ ! -z "${use_llvm_strip}" ]; then
172 USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
173else
174 USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
175fi
176
Colin Cross665dce92016-04-28 14:50:03 -0700177cat <<EOF > "${depsfile}"
178${outfile}: \
179 ${infile} \
180 ${CROSS_COMPILE}nm \
181 ${CROSS_COMPILE}objcopy \
182 ${CROSS_COMPILE}readelf \
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700183 ${USED_STRIP_OBJCOPY}
Colin Cross665dce92016-04-28 14:50:03 -0700184
185EOF