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