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