rstrip.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2006 OpenWrt.org
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. SELF=${0##*/}
  9. [ -z "$STRIP" ] && {
  10. echo "$SELF: strip command not defined (STRIP variable not set)"
  11. exit 1
  12. }
  13. TARGETS=$*
  14. [ -z "$TARGETS" ] && {
  15. echo "$SELF: no directories / files specified"
  16. echo "usage: $SELF [PATH...]"
  17. exit 1
  18. }
  19. find $TARGETS -not -path \*/lib/firmware/\* -a -type f -a -exec file {} \; | \
  20. sed -n -e 's/^\(.*\):.*ELF.*\(executable\|relocatable\|shared object\).*,.*/\1:\2/p' | \
  21. (
  22. IFS=":"
  23. while read F S; do
  24. echo "$SELF: $F: $S"
  25. [ "${S}" = "relocatable" ] && {
  26. [ "${F##*.}" == "o" ] && continue
  27. eval "$STRIP_KMOD $F"
  28. } || {
  29. b=$(stat -c '%a' $F)
  30. [ -z "$PATCHELF" ] || [ -z "$TOPDIR" ] || {
  31. old_rpath="$($PATCHELF --print-rpath $F)"; new_rpath=""
  32. for path in $old_rpath; do
  33. case "$path" in
  34. /lib/[^/]*|/usr/lib/[^/]*|\$ORIGIN/*|\$ORIGIN) new_rpath="${new_rpath:+$new_rpath:}$path" ;;
  35. *) echo "$SELF: $F: removing rpath $path" ;;
  36. esac
  37. done
  38. [ "$new_rpath" = "$old_rpath" ] || $PATCHELF --set-rpath "$new_rpath" $F
  39. }
  40. eval "$STRIP $F"
  41. a=$(stat -c '%a' $F)
  42. [ "$a" = "$b" ] || chmod $b $F
  43. }
  44. done
  45. true
  46. )