dev
sh 117 lines 3.03 KB
Raw
1 #!/bin/bash
2 progname="${0##*/}"
3 progname="${progname%.sh}"
4
5 # usage: check_elf_alignment.sh [path to *.so files|path to *.apk]
6
7 cleanup_trap() {
8 if [ -n "${tmp}" -a -d "${tmp}" ]; then
9 rm -rf ${tmp}
10 fi
11 exit $1
12 }
13
14 usage() {
15 echo "Host side script to check the ELF alignment of shared libraries."
16 echo "Shared libraries are reported ALIGNED when their ELF regions are"
17 echo "16 KB or 64 KB aligned. Otherwise they are reported as UNALIGNED."
18 echo
19 echo "Usage: ${progname} [input-path|input-APK|input-APEX]"
20 }
21
22 if [ ${#} -ne 1 ]; then
23 cd $(dirname $0)/../..
24 usage
25 cd build/app/outputs/flutter-apk
26 bash ../../../../scripts/android/check_16kb_align.sh app-release.apk
27 exit
28 fi
29
30 case ${1} in
31 --help | -h | -\?)
32 usage
33 exit
34 ;;
35
36 *)
37 dir="${1}"
38 ;;
39 esac
40
41 if ! [ -f "${dir}" -o -d "${dir}" ]; then
42 echo "Invalid file: ${dir}" >&2
43 exit 1
44 fi
45
46 if [[ "${dir}" == *.apk ]]; then
47 trap 'cleanup_trap' EXIT
48
49 echo
50 echo "Recursively analyzing $dir"
51 echo
52
53 if { zipalign --help 2>&1 | grep -q "\-P <pagesize_kb>"; }; then
54 echo "=== APK zip-alignment ==="
55 zipalign -v -c -P 16 4 "${dir}" | egrep 'lib/arm64-v8a|lib/x86_64|Verification'
56 echo "========================="
57 else
58 echo "NOTICE: Zip alignment check requires build-tools version 35.0.0-rc3 or higher."
59 echo " You can install the latest build-tools by running the below command"
60 echo " and updating your \$PATH:"
61 echo
62 echo " sdkmanager \"build-tools;35.0.0-rc3\""
63 fi
64
65 dir_filename=$(basename "${dir}")
66 tmp=$(mktemp -d -t "${dir_filename%.apk}_out_XXXXX")
67 unzip "${dir}" lib/* -d "${tmp}" >/dev/null 2>&1
68 dir="${tmp}"
69 fi
70
71 if [[ "${dir}" == *.apex ]]; then
72 trap 'cleanup_trap' EXIT
73
74 echo
75 echo "Recursively analyzing $dir"
76 echo
77
78 dir_filename=$(basename "${dir}")
79 tmp=$(mktemp -d -t "${dir_filename%.apex}_out_XXXXX")
80 deapexer extract "${dir}" "${tmp}" || { echo "Failed to deapex." && exit 1; }
81 dir="${tmp}"
82 fi
83
84 RED="\e[31m"
85 GREEN="\e[32m"
86 ENDCOLOR="\e[0m"
87
88 unaligned_libs=()
89
90 echo
91 echo "=== ELF alignment ==="
92
93 matches="$(find "${dir}" -type f | grep -E '/(arm64-v8a|x86_64)/')"
94 IFS=$'\n'
95 for match in $matches; do
96 # We could recursively call this script or rewrite it to though.
97 [[ "${match}" == *".apk" ]] && echo "WARNING: doesn't recursively inspect .apk file: ${match}"
98 [[ "${match}" == *".apex" ]] && echo "WARNING: doesn't recursively inspect .apex file: ${match}"
99
100 [[ $(file "${match}") == *"ELF"* ]] || continue
101
102 res="$(objdump -p "${match}" | grep LOAD | awk '{ print $NF }' | head -1)"
103 if [[ $res =~ 2\*\*(1[4-9]|[2-9][0-9]|[1-9][0-9]{2,}) ]]; then
104 echo -e "${match}: ${GREEN}ALIGNED${ENDCOLOR} ($res)"
105 else
106 echo -e "${match}: ${RED}UNALIGNED${ENDCOLOR} ($res)"
107 unaligned_libs+=("${match}")
108 fi
109 done
110
111 if [ ${#unaligned_libs[@]} -gt 0 ]; then
112 echo -e "${RED}Found ${#unaligned_libs[@]} unaligned libs (only arm64-v8a/x86_64 libs need to be aligned).${ENDCOLOR}"
113 exit ${#unaligned_libs[@]}
114 elif [ -n "${dir_filename}" ]; then
115 echo -e "ELF Verification Successful"
116 fi
117 echo "====================="