3
# Check namespace cleanness of a library.
4
# Allowed symbols are passed as arguments.
5
# They may have trailing * = wildcard.
6
# Wildcards may be also specified as *::* (e.g. K*::* for all KDE classes)
7
# Symbols are listed as full function unmangled names without arguments,
8
# e.g. 'foo bar* nspace::*' allows foo(), foo(int), bar(), barbar()
9
# and all symbols in namespace/class nspace.
10
# If an argument has comma in it, it's a filename of a file containing
11
# allowed symbols, one per line.
14
$thisProg = "$0"; # This programs name
17
$allowed_symbols = "";
22
while( defined( $ARGV[ 0 ] ))
25
if( /^--verbose$|^-v$/ )
29
elsif( /^--help$|^-h$/ )
31
print STDOUT "Usage $thisProg [OPTION] ... library [allowed symbols] ...\n",
33
"Check if the given library has only allowed public symbols.\n",
35
" --allowweak=[symbol] allow only these weak symbols\n",
36
" -v, --verbose verbosely list files processed\n",
37
" -h, --help print this help, then exit\n";
40
elsif( /^--allowweak=(.*)$/ )
42
$allowed_weak .= " " . $1;
45
elsif( /^--allowweak$/ ) # simply list all weak
52
die "Invalid argument!\n";
62
$allowed_symbols .= " " . $_;
67
if( ! $weak_specified )
70
# allow all weak symbols by default
71
# instances of templates and similar stuff - unfortunately includes also things from other libraries,
72
# so it cannot be on by default
75
print STDERR "library:" . $library . "\n" if $debug;
76
print STDERR "allowed_symbols:" . $allowed_symbols . "\n" if $debug;
77
print STDERR "allowed_weak:" . $allowed_weak . "\n" if $debug;
79
$default_symbols = "_fini _init"; # system symbols
80
# on my system, every .so has :
82
# A _GLOBAL_OFFSET_TABLE_
88
# no need to list A symbols in $default_symbols
90
print STDERR "default_symbols: " . $default_symbols . "\n" if $debug;
92
print STDOUT "Namespace cleanness check for " . $library . " :\n";
95
if( $library =~ /\.la$/ )
97
# get the real library file from .la
98
open( FILEIN, $library ) || die "Couldn't open $! !\n";
99
while( $line = <FILEIN> )
101
if( $line =~ /library_names=\'([^ ]*).*/o )
109
print STDERR "Library file not found in .la file!\n";
112
my $libpath = $library;
113
$libpath =~ s%[^/]*$%%;
114
if( -e $libpath . ".libs/" . $lib_file )
116
$lib_file = $libpath . ".libs/" . $lib_file;
120
$lib_file = $libpath . $lib_file;
125
$lib_file = $library;
128
print STDERR "libfile: ". $lib_file . "\n" if $debug;
130
$allowed_symbols .= " " . $default_symbols;
132
sub process_symbols($\@\%\@);
137
process_symbols( $allowed_symbols, @wildcards, %exacts, @regwildcards );
138
@weak_wildcards = ();
140
@weak_regwildcards = ();
141
process_symbols( $allowed_weak, @weak_wildcards, %weak_exacts, @weak_regwildcards );
143
# grep is for stripping not exported symbols, which don't have address (=first column)
144
$nm_command = "nm -BDCg " . $lib_file . " | grep -v '^ ' |";
146
# TODO how portable is this nmcheck stuff?
148
print STDERR "nm command:" . $nm_command . "\n" if $debug;
150
open( FILEIN, $nm_command ) || die "nm command failed\n";
154
while( $line = <FILEIN> )
158
if( $line =~ /^[^ ]* (.) (.*)$/o )
165
die "Invalid line: " . $line . "\n";
168
print STDERR "Type: " . $type . " , symbol: " . $symbol . "\n" if $debug;
170
{ # these should be system symbols, so ignore them
174
my $orig_symbol = $symbol;
176
if( $symbol =~ /\(anonymous namespace\)/o )
177
{ # TODO tell to prefer named namespaces? (shorter symbols)
182
# the :: appending is to make "CLASS::*" work also for "vtable for CLASS"
183
$symbol =~ s/^typeinfo for (.*)$/$1::/o;
184
$symbol =~ s/^typeinfo fn for (.*)$/$1::/o;
185
$symbol =~ s/^typeinfo name for (.*)$/$1::/o;
186
$symbol =~ s/^vtable for (.*)$/$1::/o;
187
$symbol =~ s/^guard variable for (.*)$/$1::/o;
188
$symbol =~ s/^reference temporary for (.*)$/$1::/o;
189
$symbol =~ s/^VTT for (.*)$/$1::/o;
190
$symbol =~ s/^virtual thunk \[[^\]]*\] to (.*)$/$1::/o;
191
$symbol =~ s/^non-virtual thunk \[[^\]]*\] to (.*)$/$1::/o;
192
$symbol =~ s/^covariant return thunk \[[^\]]*\] to (.*)$/$1::/o;
193
$symbol =~ s/^construction vtable thunk for (.*)$/$1::/o;
194
$symbol =~ s/^construction vtable for .*-in-(.*) [0-9]*$/$1::/o;
196
# templates seem to have also return types mangled in their name, and nm prints it too
197
# they have also template arguments in the symbol
198
# get rid of both of those
199
while( $symbol =~ /<.*>/o )
201
$symbol =~ s/<[^<>]*>//o; # strip innermost <>
203
if( $symbol !~ /operator\(\)/o )
205
$symbol =~ s/ ?\(.*\).*$//o; # strip () and all after it
209
$symbol =~ s/(^|:| )operator\(\) ?\(.*\).*$//o; # strip () and all after it
211
$symbol =~ s/\[.*\] *$//o; # strip [in-charge] etc.
212
if( $symbol =~ /(^|:| )operator /o )
214
$symbol =~ s/.* ([^\s]*)operator /$1/o; # strip everything before 'X::operator blah'
218
$symbol =~ s/.* ([^\s]+) *$/$1/o; # get last word (strip return type)
221
# print STDERR "Processed symbol: " . $symbol . "\n" if $debug;
224
if( $exacts{ $symbol } )
230
for my $wild ( @wildcards )
232
if( index( $symbol, $wild ) == 0 )
241
for my $wild ( @regwildcards )
243
if( $symbol =~ /^$wild$/ )
250
if( ( ! $found ) && ( $type eq "W" || $type eq "V" ))
252
if( $weak_exacts{ $symbol } )
258
for my $wild ( @weak_wildcards )
260
if( index( $symbol, $wild ) == 0 )
269
for my $wild ( @weak_regwildcards )
271
if( $symbol =~ /^$wild$/ )
282
print STDERR "Public symbol " . $orig_symbol . " is not allowed!\n";
289
print STDOUT $exit_code == 0 ? "OK\n" : "FAILED\n";
293
sub process_symbols($\@\%\@)
295
my $allowed_symbols = $_[ 0 ];
296
my $wildcards_ref = $_[ 1 ];
297
my $exacts_ref = $_[ 2 ];
298
my $regwildcards_ref = $_[ 3 ];
300
$allowed_symbols =~ s/^ *//o; # strip whitespace
301
$allowed_symbols =~ s/ *$//o;
303
if( $allowed_symbols eq "NONE" )
305
$allowed_symbols = "";
308
my @symbols1 = split( ' ', $allowed_symbols );
311
while( defined( $symbols1[ $i ] ))
313
my $symbol = $symbols1[ $i ];
314
if( $symbol =~ /\./ ) # dot in name -> file
316
open( SYMIN, $symbol ) || die ( "Cannot open file " . $symbol . "!" );
317
while( $line = <SYMIN> )
319
$line =~ s/^\s*//o; # strip whitespace
321
if( $line !~ /^$/o # empty line
322
&& $line !~ /^\s*#/ ) # comment line starting with #
324
$symbols2[ $#symbols2 + 1 ] = $line;
331
$symbols2[ $#symbols2 + 1 ] = $symbol;
336
while( defined( $symbols2[ $i ] ))
338
my $symbol = $symbols2[ $i ];
340
|| $symbol =~ /^_[A-Z]/ )
342
die "Symbols containing a double underscore or beginning with an underscore and an upper-case letter are reserved!\n";
344
elsif( $symbol eq "main"
345
|| $symbol eq "main*" )
347
die "Symbol main is not allowed!\n";
349
if( $symbol =~ /^([^\*]*)\*$/o # trailing * without any * before it
350
&& $symbol !~ /operator\*$/o )
352
print STDERR "wildcard:" . $symbol . "\n" if $debug;
353
$wildcards_ref->[ $#{$wildcards_ref} + 1 ] = $1;
355
elsif( $symbol =~ /\*$/o
356
&& ( $symbol =~ /\*::/o || $symbol =~ /::\*/o )
358
&& $symbol !~ /operator\*$/o )
360
print STDERR "regwildcard:" . $symbol . "\n" if $debug;
361
$symbol =~ s/\*/\.\*/go; # change * to .* (regexp)
362
$regwildcards_ref->[ $#{$regwildcards_ref} + 1 ] = $symbol;
366
print STDERR "exact:" . $symbol . "\n" if $debug;
367
$exacts_ref->{ $symbol } = 1;