Last active
July 25, 2025 10:33
-
-
Save Larusso/bce0e32d39734e5aa15d815c872ffe63 to your computer and use it in GitHub Desktop.
A slightly changed version of check_elf_alignment.sh which fails with exit code and prints proper on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| progname="${0##*/}" | |
| progname="${progname%.sh}" | |
| # usage: check_elf_alignment.sh [path to *.so files|path to *.apk] | |
| cleanup_trap() { | |
| if [ -n "${tmp}" ] && [ -d "${tmp}" ]; then | |
| rm -rf "${tmp}" | |
| fi | |
| exit $1 | |
| } | |
| usage() { | |
| echo "Host side script to check the ELF alignment of shared libraries." | |
| echo "Shared libraries are reported ALIGNED when their ELF regions are" | |
| echo "16 KB or 64 KB aligned. Otherwise they are reported as UNALIGNED." | |
| echo | |
| echo "Usage: ${progname} [input-path|input-APK|input-APEX]" | |
| } | |
| if [ $# -ne 1 ]; then | |
| usage | |
| exit 1 | |
| fi | |
| case $1 in | |
| --help | -h | -\?) | |
| usage | |
| exit 0 | |
| ;; | |
| *) | |
| dir="$1" | |
| ;; | |
| esac | |
| if ! [ -f "$dir" ] && ! [ -d "$dir" ]; then | |
| echo "Invalid file: $dir" >&2 | |
| exit 1 | |
| fi | |
| if [[ "$dir" == *.apk ]]; then | |
| trap 'cleanup_trap' EXIT | |
| echo | |
| echo "Recursively analyzing $dir" | |
| echo | |
| if zipalign --help 2>&1 | grep -q "\-P <pagesize_kb>"; then | |
| echo "=== APK zip-alignment ===" | |
| zipalign -v -c -P 16 4 "$dir" | egrep 'lib/arm64-v8a|lib/x86_64|Verification' | |
| echo "=========================" | |
| else | |
| echo "NOTICE: Zip alignment check requires build-tools version 35.0.0-rc3 or higher." | |
| echo " You can install the latest build-tools by running the below command" | |
| echo " and updating your \$PATH:" | |
| echo | |
| echo " sdkmanager \"build-tools;35.0.0-rc3\"" | |
| fi | |
| dir_filename=$(basename "$dir") | |
| tmp=$(mktemp -d -t "${dir_filename%.apk}_out_XXXXX") | |
| unzip "$dir" lib/* -d "$tmp" >/dev/null 2>&1 | |
| dir="$tmp" | |
| fi | |
| if [[ "$dir" == *.apex ]]; then | |
| trap 'cleanup_trap' EXIT | |
| echo | |
| echo "Recursively analyzing $dir" | |
| echo | |
| dir_filename=$(basename "$dir") | |
| tmp=$(mktemp -d -t "${dir_filename%.apex}_out_XXXXX") | |
| deapexer extract "$dir" "$tmp" || { echo "Failed to deapex." && exit 1; } | |
| dir="$tmp" | |
| fi | |
| RED="\033[31m" | |
| GREEN="\033[32m" | |
| ENDCOLOR="\033[0m" | |
| unaligned_critical_libs=() | |
| echo | |
| echo "=== ELF alignment ===" | |
| base_dir="$dir" | |
| matches=$(find "$dir" -type f) | |
| IFS=$'\n' | |
| for match in $matches; do | |
| [[ "$match" == *.apk ]] && echo "WARNING: doesn't recursively inspect .apk file: $match" | |
| [[ "$match" == *.apex ]] && echo "WARNING: doesn't recursively inspect .apex file: $match" | |
| [[ $(file "$match") == *"ELF"* ]] || continue | |
| res=$(objdump -p "$match" | grep LOAD | awk '{ print $NF }' | head -1) | |
| rel_path="${match#$base_dir/}" | |
| if [[ $res =~ 2\*\*([0-9]+) ]]; then | |
| pow="${BASH_REMATCH[1]}" | |
| alignment_kb=$(( (1 << pow) / 1024 )) | |
| if (( pow >= 14 )); then | |
| printf "%b\n" "${rel_path}: ${GREEN}ALIGNED${ENDCOLOR} (${alignment_kb} KB)" | |
| else | |
| printf "%b\n" "${rel_path}: ${RED}UNALIGNED${ENDCOLOR} (${alignment_kb} KB)" | |
| # Only count unaligned files from critical ABIs | |
| if [[ "$rel_path" == lib/arm64-v8a/* || "$rel_path" == lib/x86_64/* ]]; then | |
| unaligned_critical_libs+=("$rel_path") | |
| fi | |
| fi | |
| else | |
| printf "%b\n" "${rel_path}: ${RED}UNKNOWN ALIGNMENT${ENDCOLOR} (${res})" | |
| # Be conservative and count it if it’s under the relevant ABIs | |
| if [[ "$rel_path" == lib/arm64-v8a/* || "$rel_path" == lib/x86_64/* ]]; then | |
| unaligned_critical_libs+=("$rel_path") | |
| fi | |
| fi | |
| done | |
| if [ ${#unaligned_critical_libs[@]} -gt 0 ]; then | |
| printf "%b\n" "${RED}Found ${#unaligned_critical_libs[@]} unaligned critical libs (arm64-v8a/x86_64).${ENDCOLOR}" | |
| echo "=====================" | |
| exit 1 | |
| elif [ -n "${dir_filename}" ]; then | |
| echo "ELF Verification Successful" | |
| echo "=====================" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment