check-bin-arch 2.6 KB

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