Debian Codegraph @

build/Linux+BSD/portable/ldd-recursive

raw
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #
5 # Written by Igor Ljubuncic ([email protected])
6 # Yuval Nissan ([email protected])
7 #
8 # Version 1.0 on Mar 29, 2011
9 #
10 # This program performs recursive ldd checks for binaries and libraries
11 # It recurses through entire ldd tree for every listed binary and library
12 # It completes when no matches found in the current branch
13 # Same limitations to standard ldd apply
14 # ldd cannot check libraries with no permissions
15 #
16 ###############################################################################
17
18 # /*
19 #
20 # This file is provided under a dual BSD/GPLv2 license. When using or
21 # redistributing this file, you may do so under either license.
22 #
23 # GPL LICENSE SUMMARY
24 #
25 # Copyright(c) 2011 Intel Corporation. All rights reserved.
26 #
27 # This program is free software; you can redistribute it and/or modify
28 # it under the terms of version 2 of the GNU General Public License as
29 # published by the Free Software Foundation.
30 #
31 # This program is distributed in the hope that it will be useful, but
32 # WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 # General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License
37 # along with this program; if not, write to the Free Software
38 # Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
39 # The full GNU General Public License is included in this distribution
40 # in the file called LICENSE.GPL.
41 #
42 # Contact Information:
43 # Igor Ljubuncic, [email protected]
44 # P.O.B 1659, MATAM, 31015 Haifa, Israel
45 #
46 # BSD LICENSE
47 #
48 # Copyright(c) 2011 Intel Corporation. All rights reserved.
49 # All rights reserved.
50 #
51 # Redistribution and use in source and binary forms, with or without
52 # modification, are permitted provided that the following conditions
53 # are met:
54 #
55 # * Redistributions of source code must retain the above copyright
56 # notice, this list of conditions and the following disclaimer.
57 # * Redistributions in binary form must reproduce the above copyright
58 # notice, this list of conditions and the following disclaimer in
59 # the documentation and/or other materials provided with the
60 # distribution.
61 # * Neither the name of Intel Corporation nor the names of its
62 # contributors may be used to endorse or promote products derived
63 # from this software without specific prior written permission.
64 #
65 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
68 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
70 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
71 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
72 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
73 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
75 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 #
77 # */
78
79
80 use strict;
81 use Data::Dumper;
82 use Getopt::Long;
83
84 # global variables
85 # ----------------
86
87 my $result;
88 my @inputs = ();
89 my $helpoption=0;
90 my $debug=0;
91 my $verbose=0;
92 my $sep;
93 my $print_vars;
94 my $uniq;
95 my %uniq;
96
97 #############################################
98 #############################################
99 ### ###
100 ### FUNCTION MAIN ###
101 ### ###
102 #############################################
103 #############################################
104
105
106 GetOptions('h|help+' => \$helpoption,
107 'debug+' => \$verbose,
108 't=s' => \$sep,
109 'l' => \$print_vars,
110 'uniq' => \$uniq
111 );
112
113 if ($verbose==1) {
114 # Print messages for debug purposes
115 $debug = 1;
116 }
117
118 if (($helpoption==1) || ($ARGV[0] eq "")) {
119 print "\nRecursive ldd v1.00\n";
120 print "Written by Igor Ljubuncic (igor.ljubuncic\@intel.com)\n";
121 print "Written by Yuval Nissan (yuval.nissan\@intel.com)\n\n";
122 print "Usage mode:\n\n";
123 print "-d\t\tverbose output\n";
124 print "-h|help\t\tprint help\n";
125 print "-t\t\tdelimiter\n";
126 print "-l\t\tprint env. variables\n";
127 print "-uniq\t\tprint unique values only\n\n";
128 exit 0;
129 }
130
131 $sep ||= "\t";
132
133 push @inputs, @ARGV;
134
135 if($print_vars) {
136 print "ldd output can be affected by:\n";
137 print "\$LD_LIBRARY_PATH = '".$ENV{LD_LIBRARY_PATH}."'\n";
138 print "\$LD_PRELOAD = '".$ENV{LD_PRELOAD}."'\n\n";
139 }
140
141 &recurseLibs($inputs[0], 0);
142 delete $uniq{$inputs[0]};
143 print join("\n", keys(%uniq))."\n" if $uniq;
144
145 exit 0;
146
147 ##############################
148 ##############################
149 ## ##
150 ## FUNCTIONS ##
151 ## ##
152 ##############################
153 ##############################
154
155 sub recurseLibs
156 {
157 my $filename=shift;
158 my $depth = shift;
159 print "Working on file: $filename\n" if $debug;
160 print "$sep"x$depth if not $uniq;
161 ++$depth;
162 return if $uniq{$filename} and $uniq;
163 $uniq{$filename} = 1;
164 print "$filename\n" if not $uniq;
165 chomp(my @libraries = `/usr/bin/ldd $filename`);
166 print "Libraries:\n@libraries\n" if $debug;
167
168 foreach my $line (@libraries) {
169 next if not $line;
170 $line =~ s/^\s+//g;
171 $line =~ s/\s+$//g;
172 # If static or else
173 if (($line =~ /statically linked/) or ($line =~ /not a dynamic executable/)) {
174 return;
175 }
176 elsif($line =~ /not found/) {
177 print "$sep"x$depth if not $uniq;
178 print "$line\n" if not $uniq;
179 $uniq{$line} = 1;
180 next;
181 }
182
183
184 # Split and recurse on libraries (third value is the lib path):
185 my @newlibs = split(/\s+/,$line);
186 print Dumper(\@newlibs) if $debug;
187
188 # Skip if no mapped or directly linked
189 # Sane output comes with four elements
190 if (scalar(@newlibs) < 4) {
191 print "$sep"x$depth if not $uniq;
192 print $newlibs[0]."\n" if not $uniq;
193 $uniq{$newlibs[0]} = 1;
194 next;
195 }
196
197 print "\nI'm gonna enter recursion with $newlibs[2].\n\n" if $debug;
198 &recurseLibs($newlibs[2], $depth);
199
200 }
201 return;
202 }
203
204 __END__