check-bin-arch 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. # List of hardcoded paths that should be ignored, as they may
  3. # contain binaries for an architecture different from the
  4. # architecture of the target.
  5. declare -a IGNORES=(
  6. # Skip firmware files, they could be ELF files for other
  7. # architectures
  8. "/lib/firmware"
  9. "/usr/lib/firmware"
  10. # Skip kernel modules
  11. # When building a 32-bit userland on 64-bit architectures, the kernel
  12. # and its modules may still be 64-bit. To keep the basic
  13. # check-bin-arch logic simple, just skip this directory.
  14. "/lib/modules"
  15. # Skip files in /usr/share, several packages (qemu,
  16. # pru-software-support) legitimately install ELF binaries that
  17. # are not for the target architecture
  18. "/usr/share"
  19. )
  20. while getopts p:l:r:a:i: OPT ; do
  21. case "${OPT}" in
  22. p) package="${OPTARG}";;
  23. l) pkg_list="${OPTARG}";;
  24. r) readelf="${OPTARG}";;
  25. a) arch_name="${OPTARG}";;
  26. i)
  27. # Ensure we do have single '/' as separators,
  28. # and that we have a leading and a trailing one.
  29. pattern="$(sed -r -e 's:/+:/:g; s:^/*:/:; s:/*$:/:;' <<<"${OPTARG}")"
  30. IGNORES+=("${pattern}")
  31. ;;
  32. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  33. \?) error "unknown option '%s'\n" "${OPTARG}";;
  34. esac
  35. done
  36. if test -z "${package}" -o -z "${pkg_list}" -o -z "${readelf}" -o -z "${arch_name}" ; then
  37. echo "Usage: $0 -p <pkg> -l <pkg-file-list> -r <readelf> -a <arch name> [-i PATH ...]"
  38. exit 1
  39. fi
  40. exitcode=0
  41. # Only split on new lines, for filenames-with-spaces
  42. IFS="
  43. "
  44. while read f; do
  45. for ignore in "${IGNORES[@]}"; do
  46. if [[ "${f}" =~ ^"${ignore}" ]]; then
  47. continue 2
  48. fi
  49. done
  50. # Skip symlinks. Some symlinks may have absolute paths as
  51. # target, pointing to host binaries while we're building.
  52. if [[ -L "${TARGET_DIR}/${f}" ]]; then
  53. continue
  54. fi
  55. # Get architecture using readelf. We pipe through 'head -1' so
  56. # that when the file is a static library (.a), we only take
  57. # into account the architecture of the first object file.
  58. arch=$(LC_ALL=C ${readelf} -h "${TARGET_DIR}/${f}" 2>&1 | \
  59. sed -r -e '/^ Machine: +(.+)/!d; s//\1/;' | head -1)
  60. # If no architecture found, assume it was not an ELF file
  61. if test "${arch}" = "" ; then
  62. continue
  63. fi
  64. # Architecture is correct
  65. if test "${arch}" = "${arch_name}" ; then
  66. continue
  67. fi
  68. printf 'ERROR: architecture for "%s" is "%s", should be "%s"\n' \
  69. "${f}" "${arch}" "${arch_name}"
  70. exitcode=1
  71. done < <( sed -r -e "/^${package},\.(.+)$/!d; s//\1/;" ${pkg_list} )
  72. exit ${exitcode}