| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# check_db_symbols - print some statistics about the direct binding |
| 4 |
# status of symbols of a given binary |
| 5 |
# |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
################################################################### |
| 11 |
# Useful functions |
| 12 |
################################################################### |
| 13 |
|
| 14 |
my $ELFDUMP_BIN = '/usr/ccs/bin/elfdump'; |
| 15 |
|
| 16 |
# Returns the list of symbols bind flags for a given binary |
| 17 |
# as a hash where symbol name is the key and flag is the value |
| 18 |
sub get_symbols_flags { |
| 19 |
my ($binary) = @_; |
| 20 |
my %symbol_flags_of; |
| 21 |
|
| 22 |
local $ENV{'LANG'} = 'C'; |
| 23 |
open( my $elfdump, '-|', "$ELFDUMP_BIN -y $binary" ) |
| 24 |
or die "ERROR: Can't analyze $binary with elfdump !"; |
| 25 |
while ( my $line = <$elfdump> ) { |
| 26 |
chomp($line); |
| 27 |
next if not( |
| 28 |
$line =~ m{ |
| 29 |
\[(?<index>\d+)\]\s+ |
| 30 |
(?<flags>\S+)\s+ |
| 31 |
(?:(?<boundto> |
| 32 |
\[\d+\]\s(?<soname>\S+) |
| 33 |
|<self>)\s+)? |
| 34 |
(?<symbol>\S+) |
| 35 |
}x |
| 36 |
); |
| 37 |
|
| 38 |
my $soname = defined $+{'soname'} ? $+{'soname'} : '<self>'; |
| 39 |
$symbol_flags_of{$soname}{ $+{'symbol'} } = $+{'flags'}; |
| 40 |
} |
| 41 |
close($elfdump); |
| 42 |
|
| 43 |
return ( \%symbol_flags_of ); |
| 44 |
} |
| 45 |
|
| 46 |
my $LDD_BIN = '/usr/bin/ldd'; |
| 47 |
|
| 48 |
# Find the real location of libraries against which the given |
| 49 |
# binary is linked. |
| 50 |
# Returns a hash where soname is the key and real path is the value |
| 51 |
sub get_libraries_location { |
| 52 |
my ($binary) = @_; |
| 53 |
my %library_location; |
| 54 |
|
| 55 |
local $ENV{'LANG'} = 'C'; |
| 56 |
open( my $ldd, '-|', "$LDD_BIN $binary" ) |
| 57 |
or die "ERROR: Can't analyze $binary with elfdump !"; |
| 58 |
while ( my $line = <$ldd> ) { |
| 59 |
my ( $soname, $library_path ) = ( |
| 60 |
$line =~ m{\s*(\S+) # soname |
| 61 |
(?:\s+=>\s+ |
| 62 |
(\S+))? # library path |
| 63 |
}x |
| 64 |
); |
| 65 |
|
| 66 |
# (file not found) case |
| 67 |
if ( not defined($library_path) ) { |
| 68 |
$library_path = $soname; |
| 69 |
$soname = ( split( /\//, $soname ) )[-1] |
| 70 |
|
| 71 |
} |
| 72 |
elsif ( $library_path eq '(file' ) { |
| 73 |
$library_path = undef; |
| 74 |
} |
| 75 |
$library_location{$soname} = $library_path; |
| 76 |
} |
| 77 |
close($ldd); |
| 78 |
|
| 79 |
return ( \%library_location ); |
| 80 |
} |
| 81 |
|
| 82 |
sub usage { |
| 83 |
my ($exit_code) = @_; |
| 84 |
print <<'EOF'; |
| 85 |
Usage: check_db_symbols BINARY |
| 86 |
Print some statistics about the direct binding status of symbols of a given binary |
| 87 |
|
| 88 |
EOF |
| 89 |
exit($exit_code); |
| 90 |
} |
| 91 |
|
| 92 |
################################################################### |
| 93 |
# Main program |
| 94 |
################################################################### |
| 95 |
|
| 96 |
if ( @ARGV < 1 ) { |
| 97 |
usage(1); |
| 98 |
} |
| 99 |
|
| 100 |
my $binary = $ARGV[0]; |
| 101 |
|
| 102 |
if ( !-f $binary ) { |
| 103 |
print STDERR "ERROR: $binary file doesn't exist !!\n\n"; |
| 104 |
exit(2); |
| 105 |
} |
| 106 |
|
| 107 |
my $library_location_of = get_libraries_location($binary); |
| 108 |
my $symbol_flags_of = get_symbols_flags($binary); |
| 109 |
|
| 110 |
printf( |
| 111 |
"\n%-20s %10s %10s %10s\n", |
| 112 |
( |
| 113 |
'Library', |
| 114 |
'Directly bound', |
| 115 |
'Not directly bound', |
| 116 |
'Not directly bindable' |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
my @unfound_libraries; |
| 121 |
foreach my $soname ( keys( %{$symbol_flags_of} ) ) { |
| 122 |
next if ( $soname eq '<self>' ); |
| 123 |
|
| 124 |
my %symbol_counts = ( |
| 125 |
directly_bound => 0, |
| 126 |
not_directly_bound => 0, |
| 127 |
non_directly_bindable => 0, |
| 128 |
); |
| 129 |
|
| 130 |
my $library_location = $library_location_of->{$soname}; |
| 131 |
if ( not defined($library_location) ) { |
| 132 |
push( @unfound_libraries, $soname ); |
| 133 |
next; |
| 134 |
} |
| 135 |
my $soname_symbol_flags_for = get_symbols_flags($library_location); |
| 136 |
|
| 137 |
foreach my $symbol ( keys( %{ $symbol_flags_of->{$soname} } ) ) { |
| 138 |
my $flag = $symbol_flags_of->{$soname}{$symbol}; |
| 139 |
if ( $flag =~ /B/ ) { |
| 140 |
$symbol_counts{directly_bound}++; |
| 141 |
} |
| 142 |
else { |
| 143 |
if ( $soname_symbol_flags_for->{'<self>'}{$symbol} =~ /N/ ) { |
| 144 |
$symbol_counts{non_directly_bindable}++; |
| 145 |
} |
| 146 |
else { |
| 147 |
$symbol_counts{not_directly_bound}++; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
printf( |
| 152 |
"%-20s %10i %10i %10i\n", |
| 153 |
( |
| 154 |
$soname, |
| 155 |
@symbol_counts{ |
| 156 |
'directly_bound', 'not_directly_bound', |
| 157 |
'non_directly_bindable' |
| 158 |
} |
| 159 |
) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
if (@unfound_libraries) { |
| 164 |
print <<'EOF'; |
| 165 |
|
| 166 |
The following libraries were not found, please make sure |
| 167 |
I can find them next time by setting LD_LIBRARY_PATH adequately: |
| 168 |
EOF |
| 169 |
print( "\t" . join( ', ', @unfound_libraries ) . "\n" ); |
| 170 |
} |
| 171 |
|
| 172 |
print "\n"; |
| 173 |
|