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) |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 27 | # --add-gnu-debuglink |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 28 | # --keep-mini-debug-info |
| 29 | # --keep-symbols |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 30 | # --use-gnu-strip |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 31 | |
Colin Cross | 8e877af | 2018-09-19 15:09:26 -0700 | [diff] [blame] | 32 | set -o pipefail |
| 33 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 34 | OPTSTRING=d:i:o:-: |
| 35 | |
| 36 | usage() { |
| 37 | cat <<EOF |
| 38 | Usage: strip.sh [options] -i in-file -o out-file -d deps-file |
| 39 | Options: |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 40 | --add-gnu-debuglink Add a gnu-debuglink section to out-file |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 41 | --keep-mini-debug-info Keep compressed debug info in out-file |
| 42 | --keep-symbols Keep symbols in out-file |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 43 | --use-gnu-strip Use strip/objcopy instead of llvm-{strip,objcopy} |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 44 | EOF |
| 45 | exit 1 |
| 46 | } |
| 47 | |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 48 | # 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] | 49 | # old GNU strip bug on lld output files, b/80093681. |
| 50 | # Similary, calls to objcopy are replaced with llvm-objcopy, |
| 51 | # with some exceptions. |
| 52 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 53 | do_strip() { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 54 | # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes, |
| 55 | # so we tell llvm-strip to keep it too. |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 56 | if [ -z "${use_gnu_strip}" ]; then |
Pirama Arumuga Nainar | 03b58e2 | 2019-01-17 11:53:03 -0800 | [diff] [blame] | 57 | "${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] | 58 | else |
| 59 | "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp" |
| 60 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | do_strip_keep_symbols() { |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 64 | 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] | 65 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 66 | "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
Colin Cross | 0abcbe6 | 2018-09-07 17:35:56 -0700 | [diff] [blame] | 67 | else |
| 68 | "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 69 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | do_strip_keep_mini_debug_info() { |
Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 73 | 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] | 74 | local fail= |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 75 | if [ -z "${use_gnu_strip}" ]; then |
Pirama Arumuga Nainar | 03b58e2 | 2019-01-17 11:53:03 -0800 | [diff] [blame] | 76 | "${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] | 77 | else |
Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 78 | "${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] | 79 | fi |
Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 80 | if [ -z $fail ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 81 | # 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 Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 85 | "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug" |
Colin Cross | 8e877af | 2018-09-19 15:09:26 -0700 | [diff] [blame] | 86 | "${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] | 87 | "${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] | 88 | comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" |
Ryan Prichard | afefacf | 2018-03-27 22:15:45 -0700 | [diff] [blame] | 89 | 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] | 90 | "${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 Willemsen | 8fec83a | 2018-03-09 10:47:52 -0800 | [diff] [blame] | 93 | "${XZ}" "${outfile}.mini_debuginfo" |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 94 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 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 Cross | 3adb2ed | 2018-10-18 11:05:56 -0700 | [diff] [blame] | 99 | 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] | 100 | else |
| 101 | cp -f "${infile}" "${outfile}.tmp" |
| 102 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | do_add_gnu_debuglink() { |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 106 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 107 | "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" |
| 108 | else |
| 109 | "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" |
| 110 | fi |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | while getopts $OPTSTRING opt; do |
| 114 | case "$opt" in |
| 115 | d) depsfile="${OPTARG}" ;; |
| 116 | i) infile="${OPTARG}" ;; |
| 117 | o) outfile="${OPTARG}" ;; |
| 118 | -) |
| 119 | case "${OPTARG}" in |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 120 | add-gnu-debuglink) add_gnu_debuglink=true ;; |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 121 | keep-mini-debug-info) keep_mini_debug_info=true ;; |
| 122 | keep-symbols) keep_symbols=true ;; |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 123 | use-gnu-strip) use_gnu_strip=true ;; |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 124 | *) echo "Unknown option --${OPTARG}"; usage ;; |
| 125 | esac;; |
| 126 | ?) usage ;; |
| 127 | *) echo "'${opt}' '${OPTARG}'" |
| 128 | esac |
| 129 | done |
| 130 | |
| 131 | if [ -z "${infile}" ]; then |
| 132 | echo "-i argument is required" |
| 133 | usage |
| 134 | fi |
| 135 | |
| 136 | if [ -z "${outfile}" ]; then |
| 137 | echo "-o argument is required" |
| 138 | usage |
| 139 | fi |
| 140 | |
| 141 | if [ -z "${depsfile}" ]; then |
| 142 | echo "-d argument is required" |
| 143 | usage |
| 144 | fi |
| 145 | |
| 146 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 147 | echo "--keep-symbols and --keep-mini-debug-info cannot be used together" |
| 148 | usage |
| 149 | fi |
| 150 | |
| 151 | if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 152 | echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" |
| 153 | usage |
| 154 | fi |
| 155 | |
| 156 | rm -f "${outfile}.tmp" |
| 157 | |
| 158 | if [ ! -z "${keep_symbols}" ]; then |
| 159 | do_strip_keep_symbols |
| 160 | elif [ ! -z "${keep_mini_debug_info}" ]; then |
| 161 | do_strip_keep_mini_debug_info |
| 162 | else |
| 163 | do_strip |
| 164 | fi |
| 165 | |
| 166 | if [ ! -z "${add_gnu_debuglink}" ]; then |
| 167 | do_add_gnu_debuglink |
| 168 | fi |
| 169 | |
| 170 | rm -f "${outfile}" |
| 171 | mv "${outfile}.tmp" "${outfile}" |
| 172 | |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 173 | if [ -z "${use_gnu_strip}" ]; then |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 174 | USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy" |
| 175 | else |
| 176 | USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip" |
| 177 | fi |
| 178 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 179 | cat <<EOF > "${depsfile}" |
| 180 | ${outfile}: \ |
| 181 | ${infile} \ |
| 182 | ${CROSS_COMPILE}nm \ |
| 183 | ${CROSS_COMPILE}objcopy \ |
| 184 | ${CROSS_COMPILE}readelf \ |
Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 185 | ${USED_STRIP_OBJCOPY} |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 186 | |
| 187 | EOF |