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# --windows 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 47 --windows Input file is Windows DLL or executable 48EOF 49 exit 1 50} 51 52do_strip() { 53 # GNU strip --strip-all does not strip .ARM.attributes, 54 # so we tell llvm-strip to keep it too. 55 local keep_section=--keep-section=.ARM.attributes 56 if [ -n "${windows}" ]; then 57 keep_section= 58 fi 59 "${CLANG_BIN}/llvm-strip" --strip-all ${keep_section} "${infile}" -o "${outfile}.tmp" 60} 61 62do_strip_keep_symbols_and_debug_frame() { 63 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {if ($2 != ".debug_frame") {print "--remove-section " $2}}' | xargs` 64 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} 65} 66 67do_strip_keep_symbols() { 68 REMOVE_SECTIONS=`"${CLANG_BIN}/llvm-readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs` 69 "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} 70} 71 72do_strip_keep_symbol_list() { 73 echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList" 74 75 KEEP_SYMBOLS="--strip-unneeded-symbol=* --keep-symbols=" 76 KEEP_SYMBOLS+="${outfile}.symbolList" 77 "${CLANG_BIN}/llvm-objcopy" -w "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS} 78} 79 80do_strip_keep_mini_debug_info_darwin() { 81 rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" 82 local fail= 83 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true 84 85 if [ -z $fail ]; then 86 "${CLANG_BIN}/llvm-objcopy" --only-keep-debug "${infile}" "${outfile}.debug" 87 "${CLANG_BIN}/llvm-nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms" 88 "${CLANG_BIN}/llvm-nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms" 89 comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols" 90 echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty. 91 "${CLANG_BIN}/llvm-objcopy" -S --keep-section .debug_frame --keep-symbols="${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" 92 "${XZ}" --keep --block-size=64k --threads=0 "${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_strip_keep_mini_debug_info_linux() { 102 rm -f "${outfile}.mini_debuginfo.xz" 103 local fail= 104 if [ -z "${windows}" ]; then 105 "${CLANG_BIN}/llvm-strip" --strip-all --keep-section=.ARM.attributes --remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true 106 else 107 # --keep-section not supported for Windows COFF. 108 fail=true 109 fi 110 111 if [ -z $fail ]; then 112 # create_minidebuginfo has issues with compressed debug sections. Just 113 # decompress them for now using objcopy which understands compressed 114 # debug sections. 115 # b/306150780 tracks supporting this directly in create_minidebuginfo 116 decompressed="$(mktemp)" 117 "${CLANG_BIN}/llvm-objcopy" --decompress-debug-sections \ 118 "${infile}" "${decompressed}" 119 120 "${CREATE_MINIDEBUGINFO}" "${decompressed}" "${outfile}.mini_debuginfo.xz" 121 "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" 122 rm -f "${outfile}.mini_debuginfo.xz" "${decompressed}" 123 else 124 cp -f "${infile}" "${outfile}.tmp" 125 fi 126} 127 128do_strip_keep_mini_debug_info() { 129 case $(uname) in 130 Linux) 131 do_strip_keep_mini_debug_info_linux 132 ;; 133 Darwin) 134 do_strip_keep_mini_debug_info_darwin 135 ;; 136 *) echo "unknown OS:" $(uname) >&2 && exit 1;; 137 esac 138} 139 140do_add_gnu_debuglink() { 141 "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" 142} 143 144do_remove_build_id() { 145 "${CLANG_BIN}/llvm-strip" --remove-section=.note.gnu.build-id "${outfile}.tmp" -o "${outfile}.tmp.no-build-id" 146 rm -f "${outfile}.tmp" 147 mv "${outfile}.tmp.no-build-id" "${outfile}.tmp" 148} 149 150while getopts $OPTSTRING opt; do 151 case "$opt" in 152 d) depsfile="${OPTARG}" ;; 153 i) infile="${OPTARG}" ;; 154 o) outfile="${OPTARG}" ;; 155 k) symbols_to_keep="${OPTARG}" ;; 156 -) 157 case "${OPTARG}" in 158 add-gnu-debuglink) add_gnu_debuglink=true ;; 159 keep-mini-debug-info) keep_mini_debug_info=true ;; 160 keep-symbols) keep_symbols=true ;; 161 keep-symbols-and-debug-frame) keep_symbols_and_debug_frame=true ;; 162 remove-build-id) remove_build_id=true ;; 163 windows) windows=true ;; 164 *) echo "Unknown option --${OPTARG}"; usage ;; 165 esac;; 166 ?) usage ;; 167 *) echo "'${opt}' '${OPTARG}'" 168 esac 169done 170 171if [ -z "${infile}" ]; then 172 echo "-i argument is required" 173 usage 174fi 175 176if [ -z "${outfile}" ]; then 177 echo "-o argument is required" 178 usage 179fi 180 181if [ -z "${depsfile}" ]; then 182 echo "-d argument is required" 183 usage 184fi 185 186if [ ! -z "${keep_symbols}" -a ! -z "${keep_mini_debug_info}" ]; then 187 echo "--keep-symbols and --keep-mini-debug-info cannot be used together" 188 usage 189fi 190 191if [ ! -z "${keep_symbols}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then 192 echo "--keep-symbols and --keep-symbols-and-debug-frame cannot be used together" 193 usage 194fi 195 196if [ ! -z "${keep_mini_debug_info}" -a ! -z "${keep_symbols_and_debug_frame}" ]; then 197 echo "--keep-symbols-mini-debug-info and --keep-symbols-and-debug-frame cannot be used together" 198 usage 199fi 200 201if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then 202 echo "--keep-symbols and -k cannot be used together" 203 usage 204fi 205 206if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then 207 echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info" 208 usage 209fi 210 211rm -f "${outfile}.tmp" 212 213if [ ! -z "${keep_symbols}" ]; then 214 do_strip_keep_symbols 215elif [ ! -z "${symbols_to_keep}" ]; then 216 do_strip_keep_symbol_list 217elif [ ! -z "${keep_mini_debug_info}" ]; then 218 do_strip_keep_mini_debug_info 219elif [ ! -z "${keep_symbols_and_debug_frame}" ]; then 220 do_strip_keep_symbols_and_debug_frame 221else 222 do_strip 223fi 224 225if [ ! -z "${add_gnu_debuglink}" ]; then 226 do_add_gnu_debuglink 227fi 228 229if [ ! -z "${remove_build_id}" ]; then 230 do_remove_build_id 231fi 232 233rm -f "${outfile}" 234mv "${outfile}.tmp" "${outfile}" 235 236cat <<EOF > "${depsfile}" 237${outfile}: \ 238 ${infile} \ 239 ${CLANG_BIN}/llvm-nm \ 240 ${CLANG_BIN}/llvm-objcopy \ 241 ${CLANG_BIN}/llvm-readelf \ 242 ${CLANG_BIN}/llvm-strip 243 244EOF 245