check_elf_alignment.sh 3.3 KB

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