fix-rpath 4.3 KB

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