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