1#!/bin/bash 2# Get the elf file, judge 32 or 64, and strip off some symbol information and debugging information to make the file smaller 3# Copyright (C) 2022 Huawei Technologies Co., Ltd. 4# Licensed under the Mulan PSL v2. 5# You can use this software according to the terms and conditions of the Mulan PSL v2. 6# You may obtain a copy of Mulan PSL v2 at: 7# http://license.coscl.org.cn/MulanPSL2 8# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 9# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 10# PURPOSE. 11# See the Mulan PSL v2 for more details. 12set -e 13 14strip_file() { 15 STRIP_OPTION="-d -x -p" 16 is_elf=$(file "$1" | grep ELF) 17 if [ "x${is_elf}" = x ]; then 18 # not a ELF file 19 return 20 fi 21 if (echo "$1" | grep '\.so$') ; then 22 STRIP_OPTION="-s -p" 23 fi 24 set +o errexit 25 is_arm=$(readelf -h "$1" 2>/dev/null | grep -E "Machine:.*(ARM|AArch64)") 26 is_a32=$(readelf -h "$1" 2>/dev/null | grep "Class:.*ELF32") 27 is_a64=$(readelf -h "$1" 2>/dev/null | grep "Class:.*ELF64") 28 is_obj=$(readelf -h "$1" 2>/dev/null | grep "Type:.*REL") 29 if [ "x${is_obj}" != x ]; then 30 STRIP_OPTION="-d -p" 31 fi 32 set -o errexit 33 if [ "x${is_arm}" != x ]; then 34 IS_A32=0 35 IS_A64=0 36 if [ "x${is_a32}" != x ]; then 37 IS_A32=1 38 fi 39 if [ "x${is_a64}" != x ]; then 40 IS_A64=1 41 fi 42 43 ls -l "$1" 44 if [ "$IS_A32" = 1 ]; then 45 "${STRIP}" ${STRIP_OPTION} "$1" 46 fi 47 48 if [ "$IS_A64" = 1 ]; then 49 "${STRIP}" ${STRIP_OPTION} "$1" 50 fi 51 ls -l "$1" 52 fi 53} 54 55for fn in $*; do 56 strip_file "$fn" 57done 58