check-bin-arch 2.7 KB

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