fix-rpath 5.3 KB

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