github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/scripts/strip.sh (about)

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