blob: bccf61044eda85a74dd8a8b0fe992d54313170a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#!/usr/bin/perl -w
2#
3# namespace.pl. Mon Aug 30 2004
4#
5# Perform a name space analysis on the linux kernel.
6#
7# Copyright Keith Owens <kaos@ocs.com.au>. GPL.
8#
9# Invoke by changing directory to the top of the kernel object
10# tree then namespace.pl, no parameters.
11#
12# Tuned for 2.1.x kernels with the new module handling, it will
13# work with 2.0 kernels as well.
14#
15# Last change 2.6.9-rc1, adding support for separate source and object
16# trees.
17#
18# The source must be compiled/assembled first, the object files
19# are the primary input to this script. Incomplete or missing
20# objects will result in a flawed analysis. Compile both vmlinux
21# and modules.
22#
23# Even with complete objects, treat the result of the analysis
24# with caution. Some external references are only used by
25# certain architectures, others with certain combinations of
26# configuration parameters. Ideally the source should include
27# something like
28#
29# #ifndef CONFIG_...
30# static
31# #endif
32# symbol_definition;
33#
34# so the symbols are defined as static unless a particular
35# CONFIG_... requires it to be external.
36#
37# A symbol that is suffixed with '(export only)' has these properties
38#
39# * It is global.
40# * It is marked EXPORT_SYMBOL or EXPORT_SYMBOL_GPL, either in the same
41# source file or a different source file.
42# * Given the current .config, nothing uses the symbol.
43#
44# The symbol is a candidate for conversion to static, plus removal of the
45# export. But be careful that a different .config might use the symbol.
46#
47#
48# Name space analysis and cleanup is an iterative process. You cannot
49# expect to find all the problems in a single pass.
50#
51# * Identify possibly unnecessary global declarations, verify that they
52# really are unnecessary and change them to static.
53# * Compile and fix up gcc warnings about static, removing dead symbols
54# as necessary.
55# * make clean and rebuild with different configs (especially
56# CONFIG_MODULES=n) to see which symbols are being defined when the
57# config does not require them. These symbols bloat the kernel object
58# for no good reason, which is frustrating for embedded systems.
59# * Wrap config sensitive symbols in #ifdef CONFIG_foo, as long as the
60# code does not get too ugly.
61# * Repeat the name space analysis until you can live with with the
62# result.
63#
64
65require 5; # at least perl 5
66use strict;
67use File::Find;
68
Aaron Brooks3a25f0b2006-02-26 22:25:46 -050069my $nm = ($ENV{'NM'} || "nm") . " -p";
70my $objdump = ($ENV{'OBJDUMP'} || "objdump") . " -s -j .comment";
Linus Torvalds1da177e2005-04-16 15:20:36 -070071my $srctree = "";
72my $objtree = "";
73$srctree = "$ENV{'srctree'}/" if (exists($ENV{'srctree'}));
74$objtree = "$ENV{'objtree'}/" if (exists($ENV{'objtree'}));
75
76if ($#ARGV != -1) {
77 print STDERR "usage: $0 takes no parameters\n";
78 die("giving up\n");
79}
80
81my %nmdata = (); # nm data for each object
82my %def = (); # all definitions for each name
83my %ksymtab = (); # names that appear in __ksymtab_
84my %ref = (); # $ref{$name} exists if there is a true external reference to $name
85my %export = (); # $export{$name} exists if there is an EXPORT_... of $name
86
Stephen Hemminger43f683c2010-10-27 12:42:00 -070087my %nmexception = (
88 'fs/ext3/bitmap' => 1,
89 'fs/ext4/bitmap' => 1,
90 'arch/x86/lib/thunk_32' => 1,
91 'arch/x86/lib/cmpxchg' => 1,
92 'arch/x86/vdso/vdso32/note' => 1,
93 'lib/irq_regs' => 1,
94 'usr/initramfs_data' => 1,
95 'drivers/scsi/aic94xx/aic94xx_dump' => 1,
96 'drivers/scsi/libsas/sas_dump' => 1,
97 'lib/dec_and_lock' => 1,
98 'drivers/ide/ide-probe-mini' => 1,
99 'usr/initramfs_data' => 1,
100 'drivers/acpi/acpia/exdump' => 1,
101 'drivers/acpi/acpia/rsdump' => 1,
102 'drivers/acpi/acpia/nsdumpdv' => 1,
103 'drivers/acpi/acpia/nsdump' => 1,
104 'arch/ia64/sn/kernel/sn2/io' => 1,
105 'arch/ia64/kernel/gate-data' => 1,
106 'security/capability' => 1,
107 'fs/ntfs/sysctl' => 1,
108 'fs/jfs/jfs_debug' => 1,
109);
110
111my %nameexception = (
112 'mod_use_count_' => 1,
113 '__initramfs_end' => 1,
114 '__initramfs_start' => 1,
115 '_einittext' => 1,
116 '_sinittext' => 1,
117 'kallsyms_names' => 1,
118 'kallsyms_num_syms' => 1,
119 'kallsyms_addresses'=> 1,
120 '__this_module' => 1,
121 '_etext' => 1,
122 '_edata' => 1,
123 '_end' => 1,
124 '__bss_start' => 1,
125 '_text' => 1,
126 '_stext' => 1,
127 '__gp' => 1,
128 'ia64_unw_start' => 1,
129 'ia64_unw_end' => 1,
130 '__init_begin' => 1,
131 '__init_end' => 1,
132 '__bss_stop' => 1,
133 '__nosave_begin' => 1,
134 '__nosave_end' => 1,
135 'pg0' => 1,
136);
137
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139&find(\&linux_objects, '.'); # find the objects and do_nm on them
140&list_multiply_defined();
141&resolve_external_references();
142&list_extra_externals();
143
144exit(0);
145
146sub linux_objects
147{
148 # Select objects, ignoring objects which are only created by
149 # merging other objects. Also ignore all of modules, scripts
150 # and compressed. Most conglomerate objects are handled by do_nm,
151 # this list only contains the special cases. These include objects
152 # that are linked from just one other object and objects for which
153 # there is really no permanent source file.
154 my $basename = $_;
155 $_ = $File::Find::name;
156 s:^\./::;
157 if (/.*\.o$/ &&
158 ! (
159 m:/built-in.o$:
Thomas Gleixnerd2c75f22007-10-11 17:56:02 +0200160 || m:arch/x86/kernel/vsyscall-syms.o$:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 || m:arch/ia64/ia32/ia32.o$:
162 || m:arch/ia64/kernel/gate-syms.o$:
163 || m:arch/ia64/lib/__divdi3.o$:
164 || m:arch/ia64/lib/__divsi3.o$:
165 || m:arch/ia64/lib/__moddi3.o$:
166 || m:arch/ia64/lib/__modsi3.o$:
167 || m:arch/ia64/lib/__udivdi3.o$:
168 || m:arch/ia64/lib/__udivsi3.o$:
169 || m:arch/ia64/lib/__umoddi3.o$:
170 || m:arch/ia64/lib/__umodsi3.o$:
171 || m:arch/ia64/scripts/check_gas_for_hint.o$:
172 || m:arch/ia64/sn/kernel/xp.o$:
173 || m:boot/bbootsect.o$:
174 || m:boot/bsetup.o$:
175 || m:/bootsect.o$:
176 || m:/boot/setup.o$:
177 || m:/compressed/:
178 || m:drivers/cdrom/driver.o$:
179 || m:drivers/char/drm/tdfx_drv.o$:
180 || m:drivers/ide/ide-detect.o$:
181 || m:drivers/ide/pci/idedriver-pci.o$:
182 || m:drivers/media/media.o$:
183 || m:drivers/scsi/sd_mod.o$:
184 || m:drivers/video/video.o$:
185 || m:fs/devpts/devpts.o$:
186 || m:fs/exportfs/exportfs.o$:
187 || m:fs/hugetlbfs/hugetlbfs.o$:
188 || m:fs/msdos/msdos.o$:
189 || m:fs/nls/nls.o$:
190 || m:fs/ramfs/ramfs.o$:
191 || m:fs/romfs/romfs.o$:
192 || m:fs/vfat/vfat.o$:
193 || m:init/mounts.o$:
194 || m:^modules/:
195 || m:net/netlink/netlink.o$:
196 || m:net/sched/sched.o$:
197 || m:/piggy.o$:
198 || m:^scripts/:
199 || m:sound/.*/snd-:
200 || m:^.*/\.tmp_:
201 || m:^\.tmp_:
202 || m:/vmlinux-obj.o$:
203 )
204 ) {
205 do_nm($basename, $_);
206 }
207 $_ = $basename; # File::Find expects $_ untouched (undocumented)
208}
209
210sub do_nm
211{
212 my ($basename, $fullname) = @_;
213 my ($source, $type, $name);
214 if (! -e $basename) {
215 printf STDERR "$basename does not exist\n";
216 return;
217 }
218 if ($fullname !~ /\.o$/) {
219 printf STDERR "$fullname is not an object file\n";
220 return;
221 }
Amerigo Wangc25f4152010-10-27 12:42:00 -0700222 ($source = $basename) =~ s/\.o$//;
223 if (-e "$source.c" || -e "$source.S") {
224 $source = "$objtree$File::Find::dir/$source";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 } else {
Amerigo Wangc25f4152010-10-27 12:42:00 -0700226 $source = "$srctree$File::Find::dir/$source";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 if (! -e "$source.c" && ! -e "$source.S") {
229 # No obvious source, exclude the object if it is conglomerate
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800230 open(my $objdumpdata, "$objdump $basename|")
231 or die "$objdump $fullname failed $!\n";
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 my $comment;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800234 while (<$objdumpdata>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 chomp();
236 if (/^In archive/) {
237 # Archives are always conglomerate
238 $comment = "GCC:GCC:";
239 last;
240 }
241 next if (! /^[ 0-9a-f]{5,} /);
242 $comment .= substr($_, 43);
243 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800244 close($objdumpdata);
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
247 printf STDERR "No source file found for $fullname\n";
248 }
249 return;
250 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800251 open (my $nmdata, "$nm $basename|")
252 or die "$nm $fullname failed $!\n";
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 my @nmdata;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800255 while (<$nmdata>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 chop;
257 ($type, $name) = (split(/ +/, $_, 3))[1..2];
258 # Expected types
259 # A absolute symbol
260 # B weak external reference to data that has been resolved
261 # C global variable, uninitialised
262 # D global variable, initialised
263 # G global variable, initialised, small data section
264 # R global array, initialised
265 # S global variable, uninitialised, small bss
266 # T global label/procedure
267 # U external reference
268 # W weak external reference to text that has been resolved
Amerigo Wange8cf9812010-10-27 12:42:01 -0700269 # V similar to W, but the value of the weak symbol becomes zero with no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 # a assembler equate
271 # b static variable, uninitialised
272 # d static variable, initialised
273 # g static variable, initialised, small data section
274 # r static array, initialised
275 # s static variable, uninitialised, small bss
276 # t static label/procedures
277 # w weak external reference to text that has not been resolved
Amerigo Wange8cf9812010-10-27 12:42:01 -0700278 # v similar to w
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 # ? undefined type, used a lot by modules
Amerigo Wange8cf9812010-10-27 12:42:01 -0700280 if ($type !~ /^[ABCDGRSTUWVabdgrstwv?]$/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 printf STDERR "nm output for $fullname contains unknown type '$_'\n";
282 }
283 elsif ($name =~ /\./) {
284 # name with '.' is local static
285 }
286 else {
287 $type = 'R' if ($type eq '?'); # binutils replaced ? with R at one point
288 # binutils keeps changing the type for exported symbols, force it to R
289 $type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
290 $name =~ s/_R[a-f0-9]{8}$//; # module versions adds this
Amerigo Wange8cf9812010-10-27 12:42:01 -0700291 if ($type =~ /[ABCDGRSTWV]/ &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 $name ne 'init_module' &&
293 $name ne 'cleanup_module' &&
294 $name ne 'Using_Versions' &&
295 $name !~ /^Version_[0-9]+$/ &&
296 $name !~ /^__parm_/ &&
297 $name !~ /^__kstrtab/ &&
298 $name !~ /^__ksymtab/ &&
299 $name !~ /^__kcrctab_/ &&
300 $name !~ /^__exitcall_/ &&
301 $name !~ /^__initcall_/ &&
302 $name !~ /^__kdb_initcall_/ &&
303 $name !~ /^__kdb_exitcall_/ &&
304 $name !~ /^__module_/ &&
305 $name !~ /^__mod_/ &&
306 $name !~ /^__crc_/ &&
307 $name ne '__this_module' &&
308 $name ne 'kernel_version') {
309 if (!exists($def{$name})) {
310 $def{$name} = [];
311 }
312 push(@{$def{$name}}, $fullname);
313 }
314 push(@nmdata, "$type $name");
315 if ($name =~ /^__ksymtab_/) {
316 $name = substr($name, 10);
317 if (!exists($ksymtab{$name})) {
318 $ksymtab{$name} = [];
319 }
320 push(@{$ksymtab{$name}}, $fullname);
321 }
322 }
323 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800324 close($nmdata);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if ($#nmdata < 0) {
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700327 printf "No nm data for $fullname\n"
328 unless $nmexception{$fullname};
329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 $nmdata{$fullname} = \@nmdata;
332}
333
334sub drop_def
335{
336 my ($object, $name) = @_;
337 my $nmdata = $nmdata{$object};
338 my ($i, $j);
339 for ($i = 0; $i <= $#{$nmdata}; ++$i) {
340 if ($name eq (split(' ', $nmdata->[$i], 2))[1]) {
341 splice(@{$nmdata{$object}}, $i, 1);
342 my $def = $def{$name};
343 for ($j = 0; $j < $#{$def{$name}}; ++$j) {
344 if ($def{$name}[$j] eq $object) {
345 splice(@{$def{$name}}, $j, 1);
346 }
347 }
348 last;
349 }
350 }
351}
352
353sub list_multiply_defined
354{
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800355 foreach my $name (keys(%def)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if ($#{$def{$name}} > 0) {
357 # Special case for cond_syscall
Amerigo Wange8cf9812010-10-27 12:42:01 -0700358 if ($#{$def{$name}} == 1 && $name =~ /^sys_/) {
359 if($def{$name}[0] eq "kernel/sys_ni.o" ||
360 $def{$name}[1] eq "kernel/sys_ni.o") {
361 &drop_def("kernel/sys_ni.o", $name);
362 next;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 # Special case for i386 entry code
366 if ($#{$def{$name}} == 1 && $name =~ /^__kernel_/ &&
Thomas Gleixnerd2c75f22007-10-11 17:56:02 +0200367 $def{$name}[0] eq "arch/x86/kernel/vsyscall-int80_32.o" &&
368 $def{$name}[1] eq "arch/x86/kernel/vsyscall-sysenter_32.o") {
369 &drop_def("arch/x86/kernel/vsyscall-sysenter_32.o", $name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 next;
371 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 printf "$name is multiply defined in :-\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800374 foreach my $module (@{$def{$name}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 printf "\t$module\n";
376 }
377 }
378 }
379}
380
381sub resolve_external_references
382{
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800383 my ($kstrtab, $ksymtab, $export);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 printf "\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800386 foreach my $object (keys(%nmdata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 my $nmdata = $nmdata{$object};
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800388 for (my $i = 0; $i <= $#{$nmdata}; ++$i) {
389 my ($type, $name) = split(' ', $nmdata->[$i], 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if ($type eq "U" || $type eq "w") {
391 if (exists($def{$name}) || exists($ksymtab{$name})) {
392 # add the owning object to the nmdata
393 $nmdata->[$i] = "$type $name $object";
394 # only count as a reference if it is not EXPORT_...
395 $kstrtab = "R __kstrtab_$name";
396 $ksymtab = "R __ksymtab_$name";
397 $export = 0;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800398 for (my $j = 0; $j <= $#{$nmdata}; ++$j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if ($nmdata->[$j] eq $kstrtab ||
400 $nmdata->[$j] eq $ksymtab) {
401 $export = 1;
402 last;
403 }
404 }
405 if ($export) {
406 $export{$name} = "";
407 }
408 else {
409 $ref{$name} = ""
410 }
411 }
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700412 elsif ( ! $nameexception{$name}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 && $name !~ /^__sched_text_/
414 && $name !~ /^__start_/
415 && $name !~ /^__end_/
416 && $name !~ /^__stop_/
417 && $name !~ /^__scheduling_functions_.*_here/
418 && $name !~ /^__.*initcall_/
419 && $name !~ /^__.*per_cpu_start/
420 && $name !~ /^__.*per_cpu_end/
421 && $name !~ /^__alt_instructions/
422 && $name !~ /^__setup_/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 && $name !~ /^__mod_timer/
424 && $name !~ /^__mod_page_state/
425 && $name !~ /^init_module/
426 && $name !~ /^cleanup_module/
427 ) {
428 printf "Cannot resolve ";
429 printf "weak " if ($type eq "w");
430 printf "reference to $name from $object\n";
431 }
432 }
433 }
434 }
435}
436
437sub list_extra_externals
438{
439 my %noref = ();
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800440
441 foreach my $name (keys(%def)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (! exists($ref{$name})) {
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800443 my @module = @{$def{$name}};
444 foreach my $module (@module) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (! exists($noref{$module})) {
446 $noref{$module} = [];
447 }
448 push(@{$noref{$module}}, $name);
449 }
450 }
451 }
452 if (%noref) {
453 printf "\nExternally defined symbols with no external references\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800454 foreach my $module (sort(keys(%noref))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printf " $module\n";
456 foreach (sort(@{$noref{$module}})) {
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800457 my $export;
458 if (exists($export{$_})) {
459 $export = " (export only)";
460 } else {
461 $export = "";
462 }
463 printf " $_$export\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 }
466 }
467}