fix-rpath 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. main() {
  45. local rootdir
  46. local tree="${1}"
  47. local find_args=( )
  48. local sanitize_extra_args=( )
  49. if ! "${PATCHELF}" --version > /dev/null 2>&1; then
  50. echo "Error: can't execute patchelf utility '${PATCHELF}'"
  51. exit 1
  52. fi
  53. case "${tree}" in
  54. host)
  55. rootdir="${HOST_DIR}"
  56. # do not process the sysroot (only contains target binaries)
  57. find_args+=( "-path" "${STAGING_DIR}" "-prune" "-o" )
  58. # do not process the external toolchain installation directory to
  59. # avoid breaking it.
  60. test "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" != "" && \
  61. find_args+=( "-path" "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" "-prune" "-o" )
  62. for excludepath in ${HOST_EXCLUDEPATHS}; do
  63. find_args+=( "-path" "${HOST_DIR}""${excludepath}" "-prune" "-o" )
  64. done
  65. # do not process the patchelf binary but a copy to work-around "file in use"
  66. find_args+=( "-path" "${PATCHELF}" "-prune" "-o" )
  67. cp "${PATCHELF}" "${PATCHELF}.__to_be_patched"
  68. # we always want $ORIGIN-based rpaths to make it relocatable.
  69. sanitize_extra_args+=( "--relative-to-file" )
  70. ;;
  71. staging)
  72. rootdir="${STAGING_DIR}"
  73. # ELF files should not be in these sub-directories
  74. for excludepath in ${STAGING_EXCLUDEPATHS}; do
  75. find_args+=( "-path" "${STAGING_DIR}""${excludepath}" "-prune" "-o" )
  76. done
  77. # should be like for the target tree below
  78. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  79. ;;
  80. target)
  81. rootdir="${TARGET_DIR}"
  82. # we don't want $ORIGIN-based rpaths but absolute paths without rootdir.
  83. # we also want to remove rpaths pointing to /lib or /usr/lib.
  84. sanitize_extra_args+=( "--no-standard-lib-dirs" )
  85. ;;
  86. *)
  87. usage
  88. exit 1
  89. ;;
  90. esac
  91. find_args+=( "-type" "f" "-print" )
  92. while read file ; do
  93. # check if it's an ELF file
  94. if ${PATCHELF} --print-rpath "${file}" > /dev/null 2>&1; then
  95. # make files writable if necessary
  96. changed=$(chmod -c u+w "${file}")
  97. # call patchelf to sanitize the rpath
  98. ${PATCHELF} --make-rpath-relative "${rootdir}" ${sanitize_extra_args[@]} "${file}"
  99. # restore the original permission
  100. test "${changed}" != "" && chmod u-w "${file}"
  101. fi
  102. done < <(find "${rootdir}" ${find_args[@]})
  103. # Restore patched patchelf utility
  104. test "${tree}" = "host" && mv "${PATCHELF}.__to_be_patched" "${PATCHELF}"
  105. # ignore errors
  106. return 0
  107. }
  108. main ${@}