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