strip.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash -e
  2. # Copyright 2017 Google Inc. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Script to handle the various ways soong may need to strip binaries
  16. # Inputs:
  17. # Environment:
  18. # CROSS_COMPILE: prefix added to readelf, objcopy tools
  19. # Arguments:
  20. # -i ${file}: input file (required)
  21. # -o ${file}: output file (required)
  22. # -d ${file}: deps file (required)
  23. # --keep-symbols
  24. # --keep-mini-debug-info
  25. # --add-gnu-debuglink
  26. OPTSTRING=d:i:o:-:
  27. usage() {
  28. cat <<EOF
  29. Usage: strip.sh [options] -i in-file -o out-file -d deps-file
  30. Options:
  31. --keep-symbols Keep symbols in out-file
  32. --keep-mini-debug-info Keep compressed debug info in out-file
  33. --add-gnu-debuglink Add a gnu-debuglink section to out-file
  34. EOF
  35. exit 1
  36. }
  37. do_strip() {
  38. "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
  39. }
  40. do_strip_keep_symbols() {
  41. "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \
  42. `"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs`
  43. }
  44. do_strip_keep_mini_debug_info() {
  45. rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
  46. if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then
  47. "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
  48. "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
  49. "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
  50. comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
  51. "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
  52. "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
  53. "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
  54. xz "${outfile}.mini_debuginfo"
  55. "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
  56. else
  57. cp -f "${infile}" "${outfile}.tmp"
  58. fi
  59. }
  60. do_add_gnu_debuglink() {
  61. "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
  62. }
  63. while getopts $OPTSTRING opt; do
  64. case "$opt" in
  65. d) depsfile="${OPTARG}" ;;
  66. i) infile="${OPTARG}" ;;
  67. o) outfile="${OPTARG}" ;;
  68. -)
  69. case "${OPTARG}" in
  70. keep-symbols) keep_symbols=true ;;
  71. keep-mini-debug-info) keep_mini_debug_info=true ;;
  72. add-gnu-debuglink) add_gnu_debuglink=true ;;
  73. *) echo "Unknown option --${OPTARG}"; usage ;;
  74. esac;;
  75. ?) usage ;;
  76. *) echo "'${opt}' '${OPTARG}'"
  77. esac
  78. done
  79. if [ -z "${infile}" ]; then
  80. echo "-i argument is required"
  81. usage
  82. fi
  83. if [ -z "${outfile}" ]; then
  84. echo "-o argument is required"
  85. usage
  86. fi
  87. if [ -z "${depsfile}" ]; then
  88. echo "-d argument is required"
  89. usage
  90. fi
  91. if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then
  92. echo "--keep-symbols and --keep-mini-debug-info cannot be used together"
  93. usage
  94. fi
  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. rm -f "${outfile}.tmp"
  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. if [ ! -z "${add_gnu_debuglink}" ]; then
  108. do_add_gnu_debuglink
  109. fi
  110. rm -f "${outfile}"
  111. mv "${outfile}.tmp" "${outfile}"
  112. cat <<EOF > "${depsfile}"
  113. ${outfile}: \
  114. ${infile} \
  115. ${CROSS_COMPILE}nm \
  116. ${CROSS_COMPILE}objcopy \
  117. ${CROSS_COMPILE}readelf \
  118. ${CROSS_COMPILE}strip
  119. EOF