check-bin-arch 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 files in /usr/share, several packages (qemu,
  27. # pru-software-support) legitimately install ELF binaries that
  28. # are not for the target architecture
  29. if [[ "${f}" =~ ^/usr/share/.* ]]; then
  30. continue
  31. fi
  32. # Skip symlinks. Some symlinks may have absolute paths as
  33. # target, pointing to host binaries while we're building.
  34. if [[ -L "${TARGET_DIR}/${f}" ]]; then
  35. continue
  36. fi
  37. # Get architecture using readelf. We pipe through 'head -1' so
  38. # that when the file is a static library (.a), we only take
  39. # into account the architecture of the first object file.
  40. arch=$(LC_ALL=C ${readelf} -h "${TARGET_DIR}/${f}" 2>&1 | \
  41. sed -r -e '/^ Machine: +(.+)/!d; s//\1/;' | head -1)
  42. # If no architecture found, assume it was not an ELF file
  43. if test "${arch}" = "" ; then
  44. continue
  45. fi
  46. # Architecture is correct
  47. if test "${arch}" = "${arch_name}" ; then
  48. continue
  49. fi
  50. printf 'ERROR: architecture for "%s" is "%s", should be "%s"\n' \
  51. "${f}" "${arch}" "${arch_name}"
  52. exitcode=1
  53. done < <( sed -r -e "/^${package},\.(.+)$/!d; s//\1/;" ${pkg_list} )
  54. exit ${exitcode}