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