| 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 | 
| Dan Willemsen | 8fec83a | 2018-03-09 10:47:52 -0800 | [diff] [blame] | 21 | #   XZ: path to the xz binary | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 22 | #  Arguments: | 
| Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 23 | #   -i ${file}: input file (required) | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 24 | #   -o ${file}: output file (required) | 
|  | 25 | #   -d ${file}: deps file (required) | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 26 | #   -k symbols: Symbols to keep (optional) | 
| 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 | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 30 | #   --keep-symbols-and-debug-frame | 
| Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 31 | #   --remove-build-id | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 32 |  | 
| Colin Cross | 8e877af | 2018-09-19 15:09:26 -0700 | [diff] [blame] | 33 | set -o pipefail | 
|  | 34 |  | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 35 | OPTSTRING=d:i:o:k:-: | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | usage() { | 
|  | 38 | cat <<EOF | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 39 | 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] | 40 | Options: | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 41 | --add-gnu-debuglink             Add a gnu-debuglink section to out-file | 
|  | 42 | --keep-mini-debug-info          Keep compressed debug info in out-file | 
|  | 43 | --keep-symbols                  Keep symbols in out-file | 
|  | 44 | --keep-symbols-and-debug-frame  Keep symbols and .debug_frame in out-file | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 45 | --remove-build-id               Remove the gnu build-id section in out-file | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 46 | EOF | 
|  | 47 | exit 1 | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | do_strip() { | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 51 | # GNU strip --strip-all does not strip .ARM.attributes, | 
| Chih-Hung Hsieh | 30485c9 | 2018-06-04 10:37:43 -0700 | [diff] [blame] | 52 | # so we tell llvm-strip to keep it too. | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 53 | "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp" | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 56 | do_strip_keep_symbols_and_debug_frame() { | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 57 | REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs` | 
|  | 58 | "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 61 | do_strip_keep_symbols() { | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 62 | REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs` | 
|  | 63 | "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 66 | do_strip_keep_symbol_list() { | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 67 | echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList" | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 68 |  | 
| Yi Kong | 049ae6a | 2019-10-15 16:35:51 -0700 | [diff] [blame] | 69 | KEEP_SYMBOLS="--strip-unneeded-symbol=* --keep-symbols=" | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 70 | KEEP_SYMBOLS+="${outfile}.symbolList" | 
| David Srbecky | 789fe0f | 2021-04-08 16:52:05 +0100 | [diff] [blame] | 71 | "${CLANG_BIN}/llvm-objcopy" -w "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS} | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| David Srbecky | 69315e4 | 2021-04-29 21:06:47 +0100 | [diff] [blame] | 74 | do_strip_keep_mini_debug_info_darwin() { | 
| Colin Cross | 1b59409 | 2017-04-13 16:19:00 -0700 | [diff] [blame] | 75 | 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] | 76 | local fail= | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 77 | "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true | 
|  | 78 |  | 
| Colin Cross | 02b04bb | 2018-09-05 13:14:09 -0700 | [diff] [blame] | 79 | if [ -z $fail ]; then | 
| David Srbecky | 789fe0f | 2021-04-08 16:52:05 +0100 | [diff] [blame] | 80 | "${CLANG_BIN}/llvm-objcopy" --only-keep-debug "${infile}" "${outfile}.debug" | 
| Yi Kong | eb63f76 | 2019-09-14 01:39:53 -0700 | [diff] [blame] | 81 | "${CLANG_BIN}/llvm-nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms" | 
|  | 82 | "${CLANG_BIN}/llvm-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] | 83 | comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" | 
| Ryan Prichard | afefacf | 2018-03-27 22:15:45 -0700 | [diff] [blame] | 84 | echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty. | 
| David Srbecky | 789fe0f | 2021-04-08 16:52:05 +0100 | [diff] [blame] | 85 | "${CLANG_BIN}/llvm-objcopy" -S --keep-section .debug_frame --keep-symbols="${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" | 
|  | 86 | "${XZ}" --keep --block-size=64k --threads=0 "${outfile}.mini_debuginfo" | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" | 
| Colin Cross | 3adb2ed | 2018-10-18 11:05:56 -0700 | [diff] [blame] | 89 | 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] | 90 | else | 
|  | 91 | cp -f "${infile}" "${outfile}.tmp" | 
|  | 92 | fi | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| David Srbecky | 69315e4 | 2021-04-29 21:06:47 +0100 | [diff] [blame] | 95 | do_strip_keep_mini_debug_info_linux() { | 
|  | 96 | rm -f "${outfile}.mini_debuginfo.xz" | 
|  | 97 | local fail= | 
|  | 98 | "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true | 
|  | 99 |  | 
|  | 100 | if [ -z $fail ]; then | 
| Eric Rahm | eba99f4 | 2023-10-17 18:47:24 +0000 | [diff] [blame] | 101 | # create_minidebuginfo has issues with compressed debug sections. Just | 
|  | 102 | # decompress them for now using objcopy which understands compressed | 
|  | 103 | # debug sections. | 
|  | 104 | # b/306150780 tracks supporting this directly in create_minidebuginfo | 
|  | 105 | decompressed="$(mktemp)" | 
|  | 106 | "${CLANG_BIN}/llvm-objcopy" --decompress-debug-sections \ | 
|  | 107 | "${infile}" "${decompressed}" | 
|  | 108 |  | 
|  | 109 | "${CREATE_MINIDEBUGINFO}" "${decompressed}" "${outfile}.mini_debuginfo.xz" | 
| David Srbecky | 69315e4 | 2021-04-29 21:06:47 +0100 | [diff] [blame] | 110 | "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" | 
| Eric Rahm | eba99f4 | 2023-10-17 18:47:24 +0000 | [diff] [blame] | 111 | rm -f "${outfile}.mini_debuginfo.xz" "${decompressed}" | 
| David Srbecky | 69315e4 | 2021-04-29 21:06:47 +0100 | [diff] [blame] | 112 | else | 
|  | 113 | cp -f "${infile}" "${outfile}.tmp" | 
|  | 114 | fi | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | do_strip_keep_mini_debug_info() { | 
|  | 118 | case $(uname) in | 
|  | 119 | Linux) | 
|  | 120 | do_strip_keep_mini_debug_info_linux | 
|  | 121 | ;; | 
|  | 122 | Darwin) | 
|  | 123 | do_strip_keep_mini_debug_info_darwin | 
|  | 124 | ;; | 
|  | 125 | *) echo "unknown OS:" $(uname) >&2 && exit 1;; | 
|  | 126 | esac | 
|  | 127 | } | 
|  | 128 |  | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 129 | do_add_gnu_debuglink() { | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 130 | "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 133 | do_remove_build_id() { | 
| Yi Kong | ab5e514 | 2019-09-12 21:24:12 -0700 | [diff] [blame] | 134 | "${CLANG_BIN}/llvm-strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id" | 
| Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 135 | rm -f "${outfile}.tmp" | 
|  | 136 | mv "${outfile}.tmp.no-build-id" "${outfile}.tmp" | 
|  | 137 | } | 
|  | 138 |  | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 139 | while getopts $OPTSTRING opt; do | 
|  | 140 | case "$opt" in | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 141 | d) depsfile="${OPTARG}" ;; | 
|  | 142 | i) infile="${OPTARG}" ;; | 
|  | 143 | o) outfile="${OPTARG}" ;; | 
|  | 144 | k) symbols_to_keep="${OPTARG}" ;; | 
|  | 145 | -) | 
|  | 146 | case "${OPTARG}" in | 
|  | 147 | add-gnu-debuglink) add_gnu_debuglink=true ;; | 
|  | 148 | keep-mini-debug-info) keep_mini_debug_info=true ;; | 
|  | 149 | keep-symbols) keep_symbols=true ;; | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 150 | keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;; | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 151 | remove-build-id) remove_build_id=true ;; | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 152 | *) echo "Unknown option --${OPTARG}"; usage ;; | 
|  | 153 | esac;; | 
|  | 154 | ?) usage ;; | 
|  | 155 | *) echo "'${opt}' '${OPTARG}'" | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 156 | esac | 
|  | 157 | done | 
|  | 158 |  | 
|  | 159 | if [ -z "${infile}" ]; then | 
|  | 160 | echo "-i argument is required" | 
|  | 161 | usage | 
|  | 162 | fi | 
|  | 163 |  | 
|  | 164 | if [ -z "${outfile}" ]; then | 
|  | 165 | echo "-o argument is required" | 
|  | 166 | usage | 
|  | 167 | fi | 
|  | 168 |  | 
|  | 169 | if [ -z "${depsfile}" ]; then | 
|  | 170 | echo "-d argument is required" | 
|  | 171 | usage | 
|  | 172 | fi | 
|  | 173 |  | 
|  | 174 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then | 
|  | 175 | echo "--keep-symbols and --keep-mini-debug-info cannot be used together" | 
|  | 176 | usage | 
|  | 177 | fi | 
|  | 178 |  | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 179 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then | 
|  | 180 | echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together" | 
|  | 181 | usage | 
|  | 182 | fi | 
|  | 183 |  | 
|  | 184 | if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then | 
|  | 185 | echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together" | 
|  | 186 | usage | 
|  | 187 | fi | 
|  | 188 |  | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 189 | if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then | 
|  | 190 | echo "--keep-symbols and -k cannot be used together" | 
|  | 191 | usage | 
|  | 192 | fi | 
|  | 193 |  | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 194 | if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then | 
|  | 195 | echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" | 
|  | 196 | usage | 
|  | 197 | fi | 
|  | 198 |  | 
|  | 199 | rm -f "${outfile}.tmp" | 
|  | 200 |  | 
|  | 201 | if [ ! -z "${keep_symbols}" ]; then | 
|  | 202 | do_strip_keep_symbols | 
| Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 203 | elif [ ! -z "${symbols_to_keep}" ]; then | 
|  | 204 | do_strip_keep_symbol_list | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 205 | elif [ ! -z "${keep_mini_debug_info}" ]; then | 
|  | 206 | do_strip_keep_mini_debug_info | 
| Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 207 | elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then | 
|  | 208 | do_strip_keep_symbols_and_debug_frame | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 209 | else | 
|  | 210 | do_strip | 
|  | 211 | fi | 
|  | 212 |  | 
|  | 213 | if [ ! -z "${add_gnu_debuglink}" ]; then | 
|  | 214 | do_add_gnu_debuglink | 
|  | 215 | fi | 
|  | 216 |  | 
| Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 217 | if [ ! -z "${remove_build_id}" ]; then | 
|  | 218 | do_remove_build_id | 
|  | 219 | fi | 
|  | 220 |  | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 221 | rm -f "${outfile}" | 
|  | 222 | mv "${outfile}.tmp" "${outfile}" | 
|  | 223 |  | 
|  | 224 | cat <<EOF > "${depsfile}" | 
|  | 225 | ${outfile}: \ | 
|  | 226 | ${infile} \ | 
| Yi Kong | eb63f76 | 2019-09-14 01:39:53 -0700 | [diff] [blame] | 227 | ${CLANG_BIN}/llvm-nm \ | 
|  | 228 | ${CLANG_BIN}/llvm-objcopy \ | 
|  | 229 | ${CLANG_BIN}/llvm-readelf \ | 
|  | 230 | ${CLANG_BIN}/llvm-strip | 
| Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 231 |  | 
|  | 232 | EOF |