milis-ldd 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #! /bin/bash
  2. # Copyright (C) 1996-2016 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. # The GNU C Library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. # The GNU C Library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Lesser General Public License for more details.
  12. # You should have received a copy of the GNU Lesser General Public
  13. # License along with the GNU C Library; if not, see
  14. # <http://www.gnu.org/licenses/>.
  15. # This is the `ldd' command, which lists what shared libraries are
  16. # used by given dynamically-linked executables. It works by invoking the
  17. # run-time dynamic linker as a command and setting the environment
  18. # variable LD_TRACE_LOADED_OBJECTS to a non-empty value.
  19. # We should be able to find the translation right at the beginning.
  20. TEXTDOMAIN=libc
  21. TEXTDOMAINDIR=/usr/share/locale
  22. RTLDLIST="/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2 /libx32/ld-linux-x32.so.2"
  23. warn=
  24. bind_now=
  25. verbose=
  26. while test $# -gt 0; do
  27. case "$1" in
  28. --vers | --versi | --versio | --version)
  29. echo 'ldd (GNU libc) 2.23'
  30. printf $"Copyright (C) %s Free Software Foundation, Inc.
  31. This is free software; see the source for copying conditions. There is NO
  32. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. " "2016"
  34. printf $"Written by %s and %s.
  35. " "Roland McGrath" "Ulrich Drepper"
  36. exit 0
  37. ;;
  38. --h | --he | --hel | --help)
  39. echo $"Usage: ldd [OPTION]... FILE...
  40. --help print this help and exit
  41. --version print version information and exit
  42. -d, --data-relocs process data relocations
  43. -r, --function-relocs process data and function relocations
  44. -u, --unused print unused direct dependencies
  45. -v, --verbose print all information
  46. "
  47. printf $"For bug reporting instructions, please see:\\n%s.\\n" \
  48. "<http://www.gnu.org/software/libc/bugs.html>"
  49. exit 0
  50. ;;
  51. -d | --d | --da | --dat | --data | --data- | --data-r | --data-re | \
  52. --data-rel | --data-relo | --data-reloc | --data-relocs)
  53. warn=yes
  54. shift
  55. ;;
  56. -r | --f | --fu | --fun | --func | --funct | --functi | --functio | \
  57. --function | --function- | --function-r | --function-re | --function-rel | \
  58. --function-relo | --function-reloc | --function-relocs)
  59. warn=yes
  60. bind_now=yes
  61. shift
  62. ;;
  63. -v | --verb | --verbo | --verbos | --verbose)
  64. verbose=yes
  65. shift
  66. ;;
  67. -u | --u | --un | --unu | --unus | --unuse | --unused)
  68. unused=yes
  69. shift
  70. ;;
  71. --v | --ve | --ver)
  72. echo >&2 $"ldd: option \`$1' is ambiguous"
  73. exit 1
  74. ;;
  75. --) # Stop option processing.
  76. shift; break
  77. ;;
  78. -*)
  79. echo >&2 'ldd:' $"unrecognized option" "\`$1'"
  80. echo >&2 $"Try \`ldd --help' for more information."
  81. exit 1
  82. ;;
  83. *)
  84. break
  85. ;;
  86. esac
  87. done
  88. nonelf ()
  89. {
  90. # Maybe extra code for non-ELF binaries.
  91. return 1;
  92. }
  93. add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
  94. add_env="$add_env LD_LIBRARY_VERSION=\$verify_out"
  95. add_env="$add_env LD_VERBOSE=$verbose"
  96. if test "$unused" = yes; then
  97. add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
  98. fi
  99. # The following command substitution is needed to make ldd work in SELinux
  100. # environments where the RTLD might not have permission to write to the
  101. # terminal. The extra "x" character prevents the shell from trimming trailing
  102. # newlines from command substitution results. This function is defined as a
  103. # subshell compound list (using "(...)") to prevent parameter assignments from
  104. # affecting the calling shell execution environment.
  105. try_trace() (
  106. output=$(eval $add_env '"$@"' 2>&1; rc=$?; printf 'x'; exit $rc)
  107. rc=$?
  108. printf '%s' "${output%x}"
  109. return $rc
  110. )
  111. case $# in
  112. 0)
  113. echo >&2 'ldd:' $"missing file arguments"
  114. echo >&2 $"Try \`ldd --help' for more information."
  115. exit 1
  116. ;;
  117. 1)
  118. single_file=t
  119. ;;
  120. *)
  121. single_file=f
  122. ;;
  123. esac
  124. result=0
  125. for file do
  126. # We don't list the file name when there is only one.
  127. test $single_file = t || echo "${file}:"
  128. case $file in
  129. */*) :
  130. ;;
  131. *) file=./$file
  132. ;;
  133. esac
  134. if test ! -e "$file"; then
  135. echo "ldd: ${file}:" $"No such file or directory" >&2
  136. result=1
  137. elif test ! -f "$file"; then
  138. echo "ldd: ${file}:" $"not regular file" >&2
  139. result=1
  140. elif test -r "$file"; then
  141. #test -x "$file" || echo 'ldd:' $"warning: you do not have execution permission for" "\`$file'" >&2
  142. RTLD=
  143. ret=1
  144. for rtld in ${RTLDLIST}; do
  145. if test -x $rtld; then
  146. verify_out=`${rtld} --verify "$file"`
  147. ret=$?
  148. case $ret in
  149. [02]) RTLD=${rtld}; break;;
  150. esac
  151. fi
  152. done
  153. case $ret in
  154. 0)
  155. # If the program exits with exit code 5, it means the process has been
  156. # invoked with __libc_enable_secure. Fall back to running it through
  157. # the dynamic linker.
  158. try_trace "$file"
  159. rc=$?
  160. if [ $rc = 5 ]; then
  161. try_trace "$RTLD" "$file"
  162. rc=$?
  163. fi
  164. [ $rc = 0 ] || result=1
  165. ;;
  166. 1)
  167. # This can be a non-ELF binary or no binary at all.
  168. nonelf "$file" || {
  169. echo $" not a dynamic executable"
  170. result=1
  171. }
  172. ;;
  173. 2)
  174. try_trace "$RTLD" "$file" || result=1
  175. ;;
  176. *)
  177. echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
  178. exit 1
  179. ;;
  180. esac
  181. else
  182. echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
  183. result=1
  184. fi
  185. done
  186. exit $result
  187. # Local Variables:
  188. # mode:ksh
  189. # End: