Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
Colin Cross | d00350c | 2017-11-17 10:55:38 -0800 | [diff] [blame] | 3 | # 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 Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 17 | # Script to handle the various ways soong may need to strip binaries |
| 18 | # Inputs: |
| 19 | # Environment: |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 20 | # CLANG_BIN: path to the clang bin directory |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 21 | # CROSS_COMPILE: prefix added to readelf, objcopy tools |
Dan Willemsen | 8fec83a | 2018-03-09 10:47:52 -0800 | [diff] [blame] | 22 | # XZ: path to the xz binary |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 23 | # Arguments: |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 24 | # -i ${file}: input file (required) |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 25 | # -o ${file}: output file (required) |
| 26 | # -d ${file}: deps file (required) |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 27 | # -k symbols: Symbols to keep (optional) |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 28 | # --add-gnu-debuglink |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 29 | # --keep-mini-debug-info |
| 30 | # --keep-symbols |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 31 | # --keep-symbols-and-debug-frame |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 32 | # --use-gnu-strip |
Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 33 | # --remove-build-id |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 34 | |
Colin Cross | 8e877af | 2018-09-19 15:09:26 -0700 | [diff] [blame] | 35 | set -o pipefail |
| 36 | |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 37 | OPTSTRING=d:i:o:k:-: |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 38 | |
| 39 | usage() { |
| 40 | cat <<EOF |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 41 | Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 42 | Options: |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 43 | --add-gnu-debuglink Add a gnu-debuglink section to out-file |
| 44 | --keep-mini-debug-info Keep compressed debug info in out-file |
| 45 | --keep-symbols Keep symbols in out-file |
| 46 | --keep-symbols-and-debug-frame Keep symbols and .debug_frame in out-file |
| 47 | --use-gnu-strip Use strip/objcopy instead of llvm-{strip,objcopy} |
| 48 | --remove-build-id Remove the gnu build-id section in out-file |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 49 | EOF |
| 50 | exit 1 |
| 51 | } |
| 52 | |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 53 | # Without --use-gnu-strip, GNU strip is replaced with llvm-strip to work around |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 54 | # old GNU strip bug on lld output files, b/80093681. |
| 55 | # Similary, calls to objcopy are replaced with llvm-objcopy, |
| 56 | # with some exceptions. |
| 57 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 58 | do_strip() { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 59 | # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes, |
| 60 | # so we tell llvm-strip to keep it too. |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 61 | if [ -z "${use_gnu_strip}" ]; then |
Pirama Arumuga Nainar | 03b58e2 | 2019-01-17 11:53:03 -0800 | [diff] [blame] | 62 | "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp" |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 63 | else |
| 64 | "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp" |
| 65 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 68 | do_strip_keep_symbols_and_debug_frame() { |
| 69 | REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs` |
| 70 | if [ -z "${use_gnu_strip}" ]; then |
| 71 | "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
| 72 | else |
| 73 | "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
| 74 | fi |
| 75 | } |
| 76 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 77 | do_strip_keep_symbols() { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 78 | REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs` |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 79 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 80 | "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
Colin Cross | 0abcbe6 | 2018-09-07 17:35:56 -0700 | [diff] [blame] | 81 | else |
| 82 | "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 83 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 86 | do_strip_keep_symbol_list() { |
| 87 | if [ -z "${use_gnu_strip}" ]; then |
| 88 | echo "do_strip_keep_symbol_list does not work with llvm-objcopy" |
| 89 | echo "http://b/131631155" |
| 90 | usage |
| 91 | fi |
| 92 | |
| 93 | echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList" |
| 94 | KEEP_SYMBOLS="-w --strip-unneeded-symbol=* --keep-symbols=" |
| 95 | KEEP_SYMBOLS+="${outfile}.symbolList" |
| 96 | |
| 97 | "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS} |
| 98 | } |
| 99 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 100 | do_strip_keep_mini_debug_info() { |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 101 | rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" |
Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 102 | local fail= |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 103 | if [ -z "${use_gnu_strip}" ]; then |
Pirama Arumuga Nainar | 03b58e2 | 2019-01-17 11:53:03 -0800 | [diff] [blame] | 104 | "${CLANG_BIN}/llvm-strip" --strip-all -keep-section=.ARM.attributes -remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 105 | else |
Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 106 | "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 107 | fi |
Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 108 | if [ -z $fail ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 109 | # Current prebult llvm-objcopy does not support the following flags: |
| 110 | # --only-keep-debug --rename-section --keep-symbols |
| 111 | # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files, |
| 112 | # except the --add-section flag. |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 113 | "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug" |
Colin Cross | 8e877af | 2018-09-19 15:09:26 -0700 | [diff] [blame] | 114 | "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms" |
Colin Cross | f843290 | 2018-09-05 13:28:01 -0700 | [diff] [blame] | 115 | "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms" |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 116 | comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" |
Ryan Prichard | afefacf | 2018-03-27 22:15:45 -0700 | [diff] [blame] | 117 | echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty. |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 118 | "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo" |
| 119 | "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo" |
| 120 | "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo" |
Dan Willemsen | 8fec83a | 2018-03-09 10:47:52 -0800 | [diff] [blame] | 121 | "${XZ}" "${outfile}.mini_debuginfo" |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 122 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 123 | "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" |
| 124 | else |
| 125 | "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" |
| 126 | fi |
Colin Cross | 3adb2ed | 2018-10-18 11:05:56 -0700 | [diff] [blame] | 127 | rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 128 | else |
| 129 | cp -f "${infile}" "${outfile}.tmp" |
| 130 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | do_add_gnu_debuglink() { |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 134 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 135 | "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" |
| 136 | else |
| 137 | "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" |
| 138 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 141 | do_remove_build_id() { |
| 142 | if [ -z "${use_gnu_strip}" ]; then |
| 143 | "${CLANG_BIN}/llvm-strip" -remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id" |
| 144 | else |
| 145 | "${CROSS_COMPILE}strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id" |
| 146 | fi |
| 147 | rm -f "${outfile}.tmp" |
| 148 | mv "${outfile}.tmp.no-build-id" "${outfile}.tmp" |
| 149 | } |
| 150 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 151 | while getopts $OPTSTRING opt; do |
| 152 | case "$opt" in |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 153 | d) depsfile="${OPTARG}" ;; |
| 154 | i) infile="${OPTARG}" ;; |
| 155 | o) outfile="${OPTARG}" ;; |
| 156 | k) symbols_to_keep="${OPTARG}" ;; |
| 157 | -) |
| 158 | case "${OPTARG}" in |
| 159 | add-gnu-debuglink) add_gnu_debuglink=true ;; |
| 160 | keep-mini-debug-info) keep_mini_debug_info=true ;; |
| 161 | keep-symbols) keep_symbols=true ;; |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 162 | keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;; |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 163 | remove-build-id) remove_build_id=true ;; |
| 164 | use-gnu-strip) use_gnu_strip=true ;; |
| 165 | *) echo "Unknown option --${OPTARG}"; usage ;; |
| 166 | esac;; |
| 167 | ?) usage ;; |
| 168 | *) echo "'${opt}' '${OPTARG}'" |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 169 | esac |
| 170 | done |
| 171 | |
| 172 | if [ -z "${infile}" ]; then |
| 173 | echo "-i argument is required" |
| 174 | usage |
| 175 | fi |
| 176 | |
| 177 | if [ -z "${outfile}" ]; then |
| 178 | echo "-o argument is required" |
| 179 | usage |
| 180 | fi |
| 181 | |
| 182 | if [ -z "${depsfile}" ]; then |
| 183 | echo "-d argument is required" |
| 184 | usage |
| 185 | fi |
| 186 | |
| 187 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 188 | echo "--keep-symbols and --keep-mini-debug-info cannot be used together" |
| 189 | usage |
| 190 | fi |
| 191 | |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 192 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then |
| 193 | echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together" |
| 194 | usage |
| 195 | fi |
| 196 | |
| 197 | if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then |
| 198 | echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together" |
| 199 | usage |
| 200 | fi |
| 201 | |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 202 | if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then |
| 203 | echo "--keep-symbols and -k cannot be used together" |
| 204 | usage |
| 205 | fi |
| 206 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 207 | if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 208 | echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" |
| 209 | usage |
| 210 | fi |
| 211 | |
| 212 | rm -f "${outfile}.tmp" |
| 213 | |
| 214 | if [ ! -z "${keep_symbols}" ]; then |
| 215 | do_strip_keep_symbols |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 216 | elif [ ! -z "${symbols_to_keep}" ]; then |
| 217 | do_strip_keep_symbol_list |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 218 | elif [ ! -z "${keep_mini_debug_info}" ]; then |
| 219 | do_strip_keep_mini_debug_info |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 220 | elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then |
| 221 | do_strip_keep_symbols_and_debug_frame |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 222 | else |
| 223 | do_strip |
| 224 | fi |
| 225 | |
| 226 | if [ ! -z "${add_gnu_debuglink}" ]; then |
| 227 | do_add_gnu_debuglink |
| 228 | fi |
| 229 | |
Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 230 | if [ ! -z "${remove_build_id}" ]; then |
| 231 | do_remove_build_id |
| 232 | fi |
| 233 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 234 | rm -f "${outfile}" |
| 235 | mv "${outfile}.tmp" "${outfile}" |
| 236 | |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 237 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 238 | USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy" |
| 239 | else |
| 240 | USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip" |
| 241 | fi |
| 242 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 243 | cat <<EOF > "${depsfile}" |
| 244 | ${outfile}: \ |
| 245 | ${infile} \ |
| 246 | ${CROSS_COMPILE}nm \ |
| 247 | ${CROSS_COMPILE}objcopy \ |
| 248 | ${CROSS_COMPILE}readelf \ |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 249 | ${USED_STRIP_OBJCOPY} |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 250 | |
| 251 | EOF |