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() { |
| 38 | "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms" |
| 39 | "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms" |
| 40 | comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" |
| 41 | "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug" |
| 42 | "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo" |
| 43 | "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo" |
| 44 | "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo" |
| 45 | "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" |
| 46 | rm -f "${outfile}.mini_debuginfo.xz" |
| 47 | xz "${outfile}.mini_debuginfo" |
| 48 | "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" |
| 49 | rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo.xz" |
| 50 | } |
| 51 | |
| 52 | do_add_gnu_debuglink() { |
| 53 | "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" |
| 54 | } |
| 55 | |
| 56 | while getopts $OPTSTRING opt; do |
| 57 | case "$opt" in |
| 58 | d) depsfile="${OPTARG}" ;; |
| 59 | i) infile="${OPTARG}" ;; |
| 60 | o) outfile="${OPTARG}" ;; |
| 61 | -) |
| 62 | case "${OPTARG}" in |
| 63 | keep-symbols) keep_symbols=true ;; |
| 64 | keep-mini-debug-info) keep_mini_debug_info=true ;; |
| 65 | add-gnu-debuglink) add_gnu_debuglink=true ;; |
| 66 | *) echo "Unknown option --${OPTARG}"; usage ;; |
| 67 | esac;; |
| 68 | ?) usage ;; |
| 69 | *) echo "'${opt}' '${OPTARG}'" |
| 70 | esac |
| 71 | done |
| 72 | |
| 73 | if [ -z "${infile}" ]; then |
| 74 | echo "-i argument is required" |
| 75 | usage |
| 76 | fi |
| 77 | |
| 78 | if [ -z "${outfile}" ]; then |
| 79 | echo "-o argument is required" |
| 80 | usage |
| 81 | fi |
| 82 | |
| 83 | if [ -z "${depsfile}" ]; then |
| 84 | echo "-d argument is required" |
| 85 | usage |
| 86 | fi |
| 87 | |
| 88 | if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 89 | echo "--keep-symbols and --keep-mini-debug-info cannot be used together" |
| 90 | usage |
| 91 | fi |
| 92 | |
| 93 | if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then |
| 94 | echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" |
| 95 | usage |
| 96 | fi |
| 97 | |
| 98 | rm -f "${outfile}.tmp" |
| 99 | |
| 100 | if [ ! -z "${keep_symbols}" ]; then |
| 101 | do_strip_keep_symbols |
| 102 | elif [ ! -z "${keep_mini_debug_info}" ]; then |
| 103 | do_strip_keep_mini_debug_info |
| 104 | else |
| 105 | do_strip |
| 106 | fi |
| 107 | |
| 108 | if [ ! -z "${add_gnu_debuglink}" ]; then |
| 109 | do_add_gnu_debuglink |
| 110 | fi |
| 111 | |
| 112 | rm -f "${outfile}" |
| 113 | mv "${outfile}.tmp" "${outfile}" |
| 114 | |
| 115 | cat <<EOF > "${depsfile}" |
| 116 | ${outfile}: \ |
| 117 | ${infile} \ |
| 118 | ${CROSS_COMPILE}nm \ |
| 119 | ${CROSS_COMPILE}objcopy \ |
| 120 | ${CROSS_COMPILE}readelf \ |
| 121 | ${CROSS_COMPILE}strip |
| 122 | |
| 123 | EOF |