Home Home > GIT Browse
summaryrefslogtreecommitdiff
blob: 1d2574c06ebef0bca0a8e55cd9f3b1ca3128bb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! /usr/bin/perl -w

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  Copyright 2006 Andreas Gruenbacher <agruen@suse.de>


use File::Temp qw(mktemp tempdir);
use File::Basename;
use FileHandle;
use Getopt::Long qw(:config gnu_getopt);
use strict;

my $check_for_duplicate_symbols;
my $ignore_undefined_symbols;
my $report_matching_symbols;
my $keep_silent;
my $kernel_version;

my $tmpdir;
my %modver;
my %where;
my $status = 0;

sub filetype($) {
    my ($file) = @_;
    stat $file
        or die "$file: $!\n";
    return `file -b --dereference "$file"`;
}

sub expand($) {
    my ($file) = @_;
    if (filetype($file) =~ /^gzip /) {
	my $tmp = mktemp("$tmpdir/" . basename($file) . ".XXXXXX");
	system("gzip -cd $file > $tmp");
	exit(1) if $?;
	return $tmp;
    } else {
	return $file;
    }
}

sub obj_26_exported_symbols($) {
    my ($obj) = @_;
    my @exported_symbols = map {
	    /^(?:00000000)?([0-9a-f]+) A __crc_(.*)/
	        ? ["0x$1", $2] : ();
	} `nm "$obj"`;
    exit(1) if $?;
    return @exported_symbols;
}

sub obj_26_imported_symbols($) {
    my ($obj) = @_;
    my @imported_symbols = map {
	    /^(0x[0-9a-f]+)\s+(.*)/
	        ? [$1, $2] : ();
	} `/sbin/modprobe --dump-modversions "$obj"`;
    exit(1) if $?;
    return @imported_symbols;
}

sub vmlinux_24_exported_symbols($) {
    my ($obj) = @_;

    my @exported_symbols = map {
	(/^(.*)_R([0-9a-f]+)$/)
	    ? ["0x$2", $1] : ();
	} split /\0+/, `objcopy -j .kstrtab -O binary $obj /dev/stdout`;
    return @exported_symbols;
}

sub obj_24_exported_symbols($) {
    my ($obj) = @_;
    my @exported_symbols = map {
	/^(?:[0-9a-f]+) R __kstrtab_(.*)_R([0-9a-f]+)/
	    ? ["0x$2", $1] : ();
	} `nm "$obj"`;
    exit(1) if $?;
    return @exported_symbols;
}

sub obj_24_imported_symbols($) {
    my ($obj) = @_;
    my @imported_symbols = map {
	(/^\s+U (.*)_R([0-9a-f]+)/ && $1 ne "__this_module")
	    ? ["0x$2", $1] : ();
        } `nm "$obj"`;
    return @imported_symbols;
}

sub version_check($) {
    my ($obj) = @_;
    unless (defined $kernel_version) {
	if (`/sbin/get_kernel_version $obj` =~ /\b2\.4\b/ ||
	    `/sbin/modinfo -F kernel_version $obj` =~ /\b2\.4\b/) {
	    $kernel_version = "2.4";
	} else {
	    $kernel_version = "2.6";
	}
    }
    return $kernel_version;
}

sub obj_exported_symbols($) {
    my ($obj) = @_;
    if (version_check($obj) eq "2.4") {
	return obj_24_exported_symbols($obj);
    } else {
	return obj_26_exported_symbols($obj);
    }
}

sub obj_imported_symbols($) {
    my ($obj) = @_;
    if (version_check($obj) eq "2.4") {
	return obj_24_imported_symbols($obj);
    } else {
	return obj_26_imported_symbols($obj);
    }
}

sub check_exports(@) {
    my (@exports) = @_;
    my %seen;

    for (my $n = 0; $n < @exports; $n++) {
	my ($modver, $symbol, $where) = @{$exports[$n]};
	$modver =~ s/^0x0*//;
	if (exists $modver{$symbol}) {
	    if ($modver ne $modver{$symbol}) {
		print "$where: $symbol($modver) conflicts with " .
		      "$symbol($modver{$symbol}) from $where{$symbol}\n"
		    unless $keep_silent;
		$status = 1;
	    } else {
		if ($check_for_duplicate_symbols) {
		    print "$where: $symbol($modver) redefined, " .
			  "previous definition in $where{$symbol}\n"
			unless $keep_silent;
		    $status = 1;
		}
	    }
	}
	$modver{$symbol} = $modver;
	$where{$symbol} = "$where";
	$seen{$symbol} = 1;
    }
}

sub check_imports($@) {
    my ($filename, @imports) = @_;
    my $basename = basename($filename);

    for (my $n = 0; $n < @imports; $n++) {
	my ($modver, $symbol) = @{$imports[$n]};
	$modver =~ s/^0x0*//;
	if (exists $modver{$symbol}) {
	    if ($modver ne $modver{$symbol}) {
		print "$basename: $symbol($modver) mismatch with $symbol" .
		      "($modver{$symbol}) from $where{$symbol}\n"
		    unless $keep_silent;
		$status = 1;
	    } elsif ($report_matching_symbols) {
		print "$basename: $symbol($modver) matches\n";
	    }
	} elsif (!$ignore_undefined_symbols) {
	    print "$basename: $symbol($modver) undefined\n"
		unless $keep_silent;
	    $status = 1;
	}
    }
}

sub check_kernel($) {
    my ($filename) = @_;
    my $file = expand($filename);
    my $filetype = filetype($file);
    if ($filetype =~ /^ELF/) {
	# Assume this is vmlinux
	check_exports
	    map { [$_->[0], $_->[1], basename $filename] }
		(version_check($file) eq "2.4"
		    ? vmlinux_24_exported_symbols($file) :
		      obj_exported_symbols($file));
	return;
    } elsif ($filetype =~ /^RPM/) {
	# Assume this is a kernel-$flavor.rpm file
	system("rpm2cpio $file | " .
	       "(cd $tmpdir; " .
	       " cpio -dim --quiet './boot/symvers-*.gz' 'boot/symvers-*.gz')");
	my @symvers = glob("$tmpdir/boot/*");
	unless (@symvers == 1) {
	    print STDERR "Failed to extract /boot/symvers-*.gz from " .
		  "$filename\n";
	    exit 1;
	}
	$file = expand($symvers[0]);
	# now we have a symvers dump file...
    }
    # Assume this is a symvers dump file
    my $fh = new FileHandle("sed -e '/^#/d' -e 's/\t+/\t/' \"$file\" |")
	or die "$file: $!\n";
    check_exports
	map { [$_->[0], $_->[1], basename($filename) .
			         (defined $_->[2] ? ":$_->[2]" : "")] }
	    map { [split(/\s+/, $_)] } <$fh>;
}

sub usage($) {
    my ($status) = @_;

    print <<EOF;
Usage: $0 {kernel symbols} {module symbols} ...
       $0 --dump-modversions {module binary}

Check a kernel's exported symbols against a set of modules, or create
a module symbol dump file (--dump-modversions). The kernel symbols can
be given as a kernel rpm, symvers dump, or vmlinux binary (with 2.6
kernels). Module symbols can either be given as a module binary or a
module symbol dump file as produced by the --dump-modversions option.

Supports checking a set of modules against a kernel: in this case,
internal dependencies between those modules are resolved as well.

--ignore-undefined-symbols
    Ignore symbols the module uses which are undefined. This is useful
    when checking a single module against a kernel and this module uses
    symbols from other modules that are not part of the kernel.

--check-for-duplicate-symbols
    Report when multiple modules define the same symbols.

--report-matching-symbols
    Report matching symbols that are found as well.

--silent
    Do not report ABI changes; only set the exit status accordingly.

EOF

    exit $status;
}

my $dump_modversions;

GetOptions(
    "d|dump-modversions" => \$dump_modversions,
    "u|ignore-undefined-symbols" => \$ignore_undefined_symbols,
    "D|check-for-duplicate-symbols" => \$check_for_duplicate_symbols,
    "report-matching-symbols" => \$report_matching_symbols,
    "silent" => \$keep_silent,
    "h|help" => sub { usage(0); },

) or usage(1);

if ($dump_modversions) {
    usage(1) if @ARGV != 1;
} else {
    usage(1) if @ARGV < 1;
}

$tmpdir = tempdir(CLEANUP => 1);

if ($dump_modversions) {
    map { print "$_->[0]\t+$_->[1]\n" } obj_exported_symbols($ARGV[0]);
    map { print "$_->[0]\t$_->[1]\n" } obj_imported_symbols($ARGV[0]);
} else {
    my @file;
    my @filetype;
    for (my $n = 1; $n < @ARGV; $n++) {
	$file[$n] = expand($ARGV[$n]);
	$filetype[$n] = filetype($file[$n]);
    }

    # Grab all the symbols the kernel exports
    check_kernel $ARGV[0];

    # Module pass 1: exported symbols
    for (my $n = 1; $n < @ARGV; $n++) {
	if ($filetype[$n] =~ /^ELF/) {
	    # Assume a *.ko file
	    check_exports
		map { [$_->[0], $_->[1], basename $ARGV[$n]] }
		    obj_exported_symbols($file[$n]);
	} else {
	    # Assume a ``--dump-modversions'' file
	    my $fh = new FileHandle("sed -n -e '/^#/d' -e 's/\t+/\t/p' " .
				    "\"$file[$n]\" |")
	        or die "$file[$n]: $!\n";
	    check_exports
		map { [split(/\s+/, $_), basename $ARGV[$n]] } <$fh>;
	}
    }
    # Module pass 2: imported symbols
    for (my $n = 1; $n < @ARGV; $n++) {
	if ($filetype[$n] =~ /^ELF/) {
	    # Assume a *.ko file
	    check_imports $ARGV[$n],
		obj_imported_symbols($file[$n]);
	} else {
	    # Assume a ``--dump-modversions'' file
	    my $fh = new FileHandle("sed -e '/^#/d' -e '/\t+/d' " .
				    "\"$file[$n]\" |")
	        or die "$file[$n]: $!\n";
	    check_imports $ARGV[$n], map { [split(/\s+/, $_)] } <$fh>;
	}
    }
}

exit $status;

# vim:shiftwidth=4 softtabstop=4