diff options
author | Will McVicker <willmcvicker@google.com> | 2024-04-29 12:12:55 -0700 |
---|---|---|
committer | Will McVicker <willmcvicker@google.com> | 2024-04-29 13:36:57 -0700 |
commit | 561eea1986bc04ef14adeff5f5228fa45c41ab6a (patch) | |
tree | b77b75f63c524b649195ae84e02638b99ca38e7e /tools | |
parent | 5080f93ced2c132b96a775550e62dbc7d3f8770c (diff) | |
download | extras-561eea1986bc04ef14adeff5f5228fa45c41ab6a.tar.gz |
tools: Add script to check ELF alignment
This script can be used to check the ELF alignment of shared libraries
in the provided path or in the provided *.apk file.
Bug: 337895632
Change-Id: I3b40383eab076809a73e2ffcf920e6bd8b638cff
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/check_elf_alignment.sh | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tools/check_elf_alignment.sh b/tools/check_elf_alignment.sh new file mode 100755 index 00000000..da1f0b3f --- /dev/null +++ b/tools/check_elf_alignment.sh @@ -0,0 +1,64 @@ +#!/bin/bash +progname="${0##*/}" +progname="${progname%.sh}" + +# usage: check_elf_alignment.sh [path to *.so files|path to *.apk] + +cleanup_trap() { + if [ -n "${tmp}" -a -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]" +} + +if [ ${#} -ne 1 ]; then + usage + exit +fi + +case ${1} in + --help | -h | -\?) + usage + exit + ;; + + *) + dir="${1}" + ;; +esac + +if ! [ -f "${dir}" -o -d "${dir}" ]; then + echo "Invalid file: ${dir}" >&2 + exit 1 +fi + +if [[ ${dir} == *.apk ]]; then + trap 'cleanup_trap' EXIT + dir_filename=$(basename ${dir}) + tmp=$(mktemp -d -t ${dir_filename%.apk}_out_XXXXX) + unzip ${dir} lib/arm64-v8a/* lib/x86_64/* -d ${tmp} >/dev/null 2>&1 + dir=${tmp} +fi + +RED="\e[31m" +GREEN="\e[32m" +ENDCOLOR="\e[0m" + +matches="$(find ${dir} -name "*.so" -type f)" +IFS=$'\n' +for match in $matches; do + res="$(objdump -p ${match} | grep LOAD | awk '{ print $NF }' | head -1)" + if [[ $res =~ "2**14" ]] || [[ $res =~ "2**16" ]]; then + echo -e "${match}: ${GREEN}ALIGNED${ENDCOLOR} ($res)" + else + echo -e "${match}: ${RED}UNALIGNED${ENDCOLOR} ($res)" + fi +done |