ldd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #! /bin/bash
  2. # Copyright (C) 1996-2021 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. # <https://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-aarch64.so.1
  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) 2.33'
  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. " "2021"
  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. "<https://bugs.linaro.org/>"
  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_VERBOSE=$verbose"
  95. if test "$unused" = yes; then
  96. add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
  97. fi
  98. # The following command substitution is needed to make ldd work in SELinux
  99. # environments where the RTLD might not have permission to write to the
  100. # terminal. The extra "x" character prevents the shell from trimming trailing
  101. # newlines from command substitution results. This function is defined as a
  102. # subshell compound list (using "(...)") to prevent parameter assignments from
  103. # affecting the calling shell execution environment.
  104. try_trace() (
  105. output=$(eval $add_env '"$@"' 2>&1; rc=$?; printf 'x'; exit $rc)
  106. rc=$?
  107. printf '%s' "${output%x}"
  108. return $rc
  109. )
  110. case $# in
  111. 0)
  112. echo >&2 'ldd:' $"missing file arguments"
  113. echo >&2 $"Try \`ldd --help' for more information."
  114. exit 1
  115. ;;
  116. 1)
  117. single_file=t
  118. ;;
  119. *)
  120. single_file=f
  121. ;;
  122. esac
  123. result=0
  124. for file do
  125. # We don't list the file name when there is only one.
  126. test $single_file = t || echo "${file}:"
  127. case $file in
  128. */*) :
  129. ;;
  130. *) file=./$file
  131. ;;
  132. esac
  133. if test ! -e "$file"; then
  134. echo "ldd: ${file}:" $"No such file or directory" >&2
  135. result=1
  136. elif test ! -f "$file"; then
  137. echo "ldd: ${file}:" $"not regular file" >&2
  138. result=1
  139. elif test -r "$file"; then
  140. test -x "$file" || echo 'ldd:' $"\
  141. 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. 1)
  155. # This can be a non-ELF binary or no binary at all.
  156. nonelf "$file" || {
  157. echo $" not a dynamic executable" >&2
  158. result=1
  159. }
  160. ;;
  161. 0|2)
  162. try_trace "$RTLD" "$file" || result=1
  163. ;;
  164. *)
  165. echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
  166. exit 1
  167. ;;
  168. esac
  169. else
  170. echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
  171. result=1
  172. fi
  173. done
  174. exit $result
  175. # Local Variables:
  176. # mode:ksh
  177. # End: