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