check-bin-arch 2.0 KB

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