fix-rpath 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2016 Samuel Martin <s.martin49@gmail.com>
  3. # Copyright (C) 2017 Wolfgang Grandegger <wg@grandegger.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. usage() {
  19. cat <<EOF >&2
  20. Usage: ${0} TREE_KIND
  21. Description:
  22. This script scans a tree and sanitize ELF files' RPATH found in there.
  23. Sanitization behaves the same whatever the kind of the processed tree,
  24. but the resulting RPATH differs. The rpath sanitization is done using
  25. "patchelf --make-rpath-relative".
  26. Arguments:
  27. TREE_KIND Kind of tree to be processed.
  28. Allowed values: host, target, staging
  29. Environment:
  30. PATCHELF patchelf program to use
  31. (default: HOST_DIR/bin/patchelf)
  32. HOST_DIR host directory
  33. STAGING_DIR staging directory
  34. TARGET_DIR target directory
  35. TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
  36. (default HOST_DIR/opt/ext-toolchain)
  37. PARALLEL_JOBS number of parallel jobs to run
  38. Returns: 0 if success or 1 in case of error
  39. EOF
  40. }
  41. : "${PATCHELF:=${HOST_DIR}/bin/patchelf}"
  42. # ELF files should not be in these sub-directories
  43. HOST_EXCLUDEPATHS="/share/terminfo"
  44. STAGING_EXCLUDEPATHS="/usr/include /usr/share/terminfo"
  45. TARGET_EXCLUDEPATHS="/lib/firmware"
  46. patch_file() {
  47. local PATCHELF rootdir file
  48. local -a sanitize_extra_args
  49. PATCHELF="${1}"
  50. rootdir="${2}"
  51. file="${3}"
  52. shift 3
  53. sanitize_extra_args=("${@}")
  54. # check if it's an ELF file
  55. rpath="$("${PATCHELF}" --print-rpath "${file}" 2>&1)"
  56. if test $? -ne 0 ; then
  57. return 0
  58. fi
  59. # make files writable if necessary
  60. changed="$(chmod -c u+w "${file}")"
  61. # With per-package directory support, most RPATH of host
  62. # binaries will point to per-package directories. This won't
  63. # work with the --make-rpath-relative ${rootdir} invocation as
  64. # the per-package host directory is not within ${rootdir}. So,
  65. # we rewrite all RPATHs pointing to per-package directories so
  66. # that they point to the global host directory.
  67. # shellcheck disable=SC2001 # ${var//search/replace} hard when search or replace have / in them
  68. changed_rpath="$(echo "${rpath}" | sed "s@${PER_PACKAGE_DIR}/[^/]\+/host@${HOST_DIR}@g")"
  69. if test "${rpath}" != "${changed_rpath}" ; then
  70. "${PATCHELF}" --set-rpath "${changed_rpath}" "${file}"
  71. fi
  72. # call patchelf to sanitize the rpath
  73. "${PATCHELF}" --make-rpath-relative "${rootdir}" "${sanitize_extra_args[@]}" "${file}"
  74. # restore the original permission
  75. test "${changed}" != "" && chmod u-w "${file}"
  76. }
  77. main() {
  78. local rootdir tree
  79. local -a find_args sanitize_extra_args
  80. tree="${1}"
  81. if ! "${PATCHELF}" --version > /dev/null 2>&1; then
  82. echo "Error: can't execute patchelf utility '${PATCHELF}'"
  83. exit 1
  84. fi
  85. case "${tree}" in
  86. host)
  87. rootdir="${HOST_DIR}"
  88. # do not process the sysroot (only contains target binaries)
  89. find_args+=( "-path" "${STAGING_DIR}" "-prune" "-o" )
  90. # do not process the external toolchain installation directory to
  91. # avoid breaking it.
  92. test "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" != "" && \
  93. find_args+=( "-path" "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" "-prune" "-o" )
  94. for excludepath in ${HOST_EXCLUDEPATHS}; do
  95. find_args+=( "-path" "${HOST_DIR}""${excludepath}" "-prune" "-o" )
  96. done
  97. # do not process the patchelf binary but a copy to work-around "file in use"
  98. find_args+=( "-path" "${PATCHELF}" "-prune" "-o" )
  99. cp "${PATCHELF}" "${PATCHELF}.__to_be_patched"
  100. # we always want $ORIGIN-based rpaths to make it relocatable.
  101. sanitize_extra_args+=( "--relative-to-file" )
  102. ;;
  103. staging)
  104. rootdir="${STAGING_DIR}"
  105. # ELF files should not be in these sub-directories
  106. for excludepath in ${STAGING_EXCLUDEPATHS}; do
  107. find_args+=( "-path" "${STAGING_DIR}""${excludepath}" "-prune" "-o" )
  108. done
  109. # should be like for the target tree below
  110. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  111. ;;
  112. target)
  113. rootdir="${TARGET_DIR}"
  114. for excludepath in ${TARGET_EXCLUDEPATHS}; do
  115. find_args+=( "-path" "${TARGET_DIR}""${excludepath}" "-prune" "-o" )
  116. done
  117. # we don't want $ORIGIN-based rpaths but absolute paths without rootdir.
  118. # we also want to remove rpaths pointing to /lib or /usr/lib.
  119. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  120. ;;
  121. *)
  122. usage
  123. exit 1
  124. ;;
  125. esac
  126. find_args+=( "-type" "f" "-print0" )
  127. export -f patch_file
  128. # Limit the number of cores used
  129. # shellcheck disable=SC2016 # ${@} has to be expanded in the sub-shell.
  130. find "${rootdir}" "${find_args[@]}" \
  131. | xargs -0 -r -P "${PARALLEL_JOBS:-1}" -I {} \
  132. bash -c 'patch_file "${@}"' _ "${PATCHELF}" "${rootdir}" {} "${sanitize_extra_args[@]}"
  133. # Restore patched patchelf utility
  134. test "${tree}" = "host" && mv "${PATCHELF}.__to_be_patched" "${PATCHELF}"
  135. # ignore errors
  136. return 0
  137. }
  138. main "${@}"