check-bin-arch 2.6 KB

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