check-bin-arch 2.4 KB

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