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# CLANG_BIN: path to the clang bin directory 21# CROSS_COMPILE: prefix added to readelf, objcopy tools 22# XZ: path to the xz binary 23# Arguments: 24# -i ${file}: input file (required) 25# -o ${file}: output file (required) 26# -d ${file}: deps file (required) 27# -k symbols: Symbols to keep (optional) 28# --add-gnu-debuglink 29# --keep-mini-debug-info 30# --keep-symbols 31# --keep-symbols-and-debug-frame 32# --remove-build-id 33 34set -o pipefail 35 36OPTSTRING=d:i:o:k:-: 37 38usage() { 39 cat <<EOF 40Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file 41Options: 42 --add-gnu-debuglink Add a gnu-debuglink section to out-file 43 --keep-mini-debug-info Keep compressed debug info in out-file 44 --keep-symbols Keep symbols in out-file 45 --keep-symbols-and-debug-frame Keep symbols and .debug_frame in out-file 46 --remove-build-id Remove the gnu build-id section in out-file 47EOF 48 exit 1 49} 50 51do_strip() { 52 # GNU strip --strip-all does not strip .ARM.attributes, 53 # so we tell llvm-strip to keep it too. 54 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes "${infile}" -o "${outfile}.tmp" 55} 56 57do_strip_keep_symbols_and_debug_frame() { 58 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs` 59 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} 60} 61 62do_strip_keep_symbols() { 63 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs` 64 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} 65} 66 67do_strip_keep_symbol_list() { 68 echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList" 69 70 KEEP_SYMBOLS="--strip-unneeded-symbol=* --keep-symbols=" 71 KEEP_SYMBOLS+="${outfile}.symbolList" 72 "${CROSS_COMPILE}objcopy" -w "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS} 73} 74 75do_strip_keep_mini_debug_info() { 76 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" 77 local fail= 78 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true 79 80 if [ -z $fail ]; then 81 # Current prebult llvm-objcopy does not support --only-keep-debug flag, 82 # and cannot process object files that are produced with the flag. Use 83 # GNU objcopy instead for now. (b/141010852) 84 "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug" 85 "${CLANG_BIN}/llvm-nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms" 86 "${CLANG_BIN}/llvm-nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms" 87 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" 88 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty. 89 "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo" 90 "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo" 91 "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo" 92 "${XZ}" "${outfile}.mini_debuginfo" 93 94 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" 95 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" 96 else 97 cp -f "${infile}" "${outfile}.tmp" 98 fi 99} 100 101do_add_gnu_debuglink() { 102 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" 103} 104 105do_remove_build_id() { 106 "${CLANG_BIN}/llvm-strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id" 107 rm -f "${outfile}.tmp" 108 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp" 109} 110 111while getopts $OPTSTRING opt; do 112 case "$opt" in 113 d) depsfile="${OPTARG}" ;; 114 i) infile="${OPTARG}" ;; 115 o) outfile="${OPTARG}" ;; 116 k) symbols_to_keep="${OPTARG}" ;; 117 -) 118 case "${OPTARG}" in 119 add-gnu-debuglink) add_gnu_debuglink=true ;; 120 keep-mini-debug-info) keep_mini_debug_info=true ;; 121 keep-symbols) keep_symbols=true ;; 122 keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;; 123 remove-build-id) remove_build_id=true ;; 124 *) echo "Unknown option --${OPTARG}"; usage ;; 125 esac;; 126 ?) usage ;; 127 *) echo "'${opt}' '${OPTARG}'" 128 esac 129done 130 131if [ -z "${infile}" ]; then 132 echo "-i argument is required" 133 usage 134fi 135 136if [ -z "${outfile}" ]; then 137 echo "-o argument is required" 138 usage 139fi 140 141if [ -z "${depsfile}" ]; then 142 echo "-d argument is required" 143 usage 144fi 145 146if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then 147 echo "--keep-symbols and --keep-mini-debug-info cannot be used together" 148 usage 149fi 150 151if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then 152 echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together" 153 usage 154fi 155 156if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then 157 echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together" 158 usage 159fi 160 161if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then 162 echo "--keep-symbols and -k cannot be used together" 163 usage 164fi 165 166if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then 167 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" 168 usage 169fi 170 171rm -f "${outfile}.tmp" 172 173if [ ! -z "${keep_symbols}" ]; then 174 do_strip_keep_symbols 175elif [ ! -z "${symbols_to_keep}" ]; then 176 do_strip_keep_symbol_list 177elif [ ! -z "${keep_mini_debug_info}" ]; then 178 do_strip_keep_mini_debug_info 179elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then 180 do_strip_keep_symbols_and_debug_frame 181else 182 do_strip 183fi 184 185if [ ! -z "${add_gnu_debuglink}" ]; then 186 do_add_gnu_debuglink 187fi 188 189if [ ! -z "${remove_build_id}" ]; then 190 do_remove_build_id 191fi 192 193rm -f "${outfile}" 194mv "${outfile}.tmp" "${outfile}" 195 196cat <<EOF > "${depsfile}" 197${outfile}: \ 198 ${infile} \ 199 ${CROSS_COMPILE}objcopy \ 200 ${CLANG_BIN}/llvm-nm \ 201 ${CLANG_BIN}/llvm-objcopy \ 202 ${CLANG_BIN}/llvm-readelf \ 203 ${CLANG_BIN}/llvm-strip 204 205EOF 206