fix-rpath 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. PATCHELF="${1}"
  48. rootdir="${2}"
  49. sanitize_extra_args="${3}"
  50. file="${4}"
  51. # check if it's an ELF file
  52. rpath=$(${PATCHELF} --print-rpath "${file}" 2>&1)
  53. if test $? -ne 0 ; then
  54. return 0
  55. fi
  56. # make files writable if necessary
  57. changed=$(chmod -c u+w "${file}")
  58. # With per-package directory support, most RPATH of host
  59. # binaries will point to per-package directories. This won't
  60. # work with the --make-rpath-relative ${rootdir} invocation as
  61. # the per-package host directory is not within ${rootdir}. So,
  62. # we rewrite all RPATHs pointing to per-package directories so
  63. # that they point to the global host directry.
  64. changed_rpath=$(echo ${rpath} | sed "s@${PER_PACKAGE_DIR}/[^/]\+/host@${HOST_DIR}@")
  65. if test "${rpath}" != "${changed_rpath}" ; then
  66. ${PATCHELF} --set-rpath ${changed_rpath} "${file}"
  67. fi
  68. # call patchelf to sanitize the rpath
  69. ${PATCHELF} --make-rpath-relative "${rootdir}" ${sanitize_extra_args[@]} "${file}"
  70. # restore the original permission
  71. test "${changed}" != "" && chmod u-w "${file}"
  72. }
  73. main() {
  74. local rootdir
  75. local tree="${1}"
  76. local find_args=( )
  77. local sanitize_extra_args=( )
  78. if ! "${PATCHELF}" --version > /dev/null 2>&1; then
  79. echo "Error: can't execute patchelf utility '${PATCHELF}'"
  80. exit 1
  81. fi
  82. case "${tree}" in
  83. host)
  84. rootdir="${HOST_DIR}"
  85. # do not process the sysroot (only contains target binaries)
  86. find_args+=( "-path" "${STAGING_DIR}" "-prune" "-o" )
  87. # do not process the external toolchain installation directory to
  88. # avoid breaking it.
  89. test "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" != "" && \
  90. find_args+=( "-path" "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" "-prune" "-o" )
  91. for excludepath in ${HOST_EXCLUDEPATHS}; do
  92. find_args+=( "-path" "${HOST_DIR}""${excludepath}" "-prune" "-o" )
  93. done
  94. # do not process the patchelf binary but a copy to work-around "file in use"
  95. find_args+=( "-path" "${PATCHELF}" "-prune" "-o" )
  96. cp "${PATCHELF}" "${PATCHELF}.__to_be_patched"
  97. # we always want $ORIGIN-based rpaths to make it relocatable.
  98. sanitize_extra_args+=( "--relative-to-file" )
  99. ;;
  100. staging)
  101. rootdir="${STAGING_DIR}"
  102. # ELF files should not be in these sub-directories
  103. for excludepath in ${STAGING_EXCLUDEPATHS}; do
  104. find_args+=( "-path" "${STAGING_DIR}""${excludepath}" "-prune" "-o" )
  105. done
  106. # should be like for the target tree below
  107. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  108. ;;
  109. target)
  110. rootdir="${TARGET_DIR}"
  111. for excludepath in ${TARGET_EXCLUDEPATHS}; do
  112. find_args+=( "-path" "${TARGET_DIR}""${excludepath}" "-prune" "-o" )
  113. done
  114. # we don't want $ORIGIN-based rpaths but absolute paths without rootdir.
  115. # we also want to remove rpaths pointing to /lib or /usr/lib.
  116. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  117. ;;
  118. *)
  119. usage
  120. exit 1
  121. ;;
  122. esac
  123. find_args+=( "-type" "f" "-print0" )
  124. export -f patch_file
  125. # Limit the number of cores used
  126. find "${rootdir}" ${find_args[@]} | xargs -0 -r -P ${PARALLEL_JOBS} -I {} bash -c "patch_file '${PATCHELF}' '${rootdir}' '${sanitize_extra_args}' $@" _ {}
  127. # Restore patched patchelf utility
  128. test "${tree}" = "host" && mv "${PATCHELF}.__to_be_patched" "${PATCHELF}"
  129. # ignore errors
  130. return 0
  131. }
  132. main ${@}