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