fd2d946fae23f6b98c12002c40ea34bd9fc1ca8d
[linux-2.6.git] / scripts / namespace.pl
1 #!/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
65 require 5;      # at least perl 5
66 use strict;
67 use File::Find;
68
69 my $nm = ($ENV{'NM'} || "nm") . " -p";
70 my $objdump = ($ENV{'OBJDUMP'} || "objdump") . " -s -j .comment";
71 my $srctree = "";
72 my $objtree = "";
73 $srctree = "$ENV{'srctree'}/" if (exists($ENV{'srctree'}));
74 $objtree = "$ENV{'objtree'}/" if (exists($ENV{'objtree'}));
75
76 if ($#ARGV != -1) {
77         print STDERR "usage: $0 takes no parameters\n";
78         die("giving up\n");
79 }
80
81 my %nmdata = ();        # nm data for each object
82 my %def = ();           # all definitions for each name
83 my %ksymtab = ();       # names that appear in __ksymtab_
84 my %ref = ();           # $ref{$name} exists if there is a true external reference to $name
85 my %export = ();        # $export{$name} exists if there is an EXPORT_... of $name
86
87 my %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
111 my %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     'vdso_enabled'      => 1,
137     '__stack_chk_fail'  => 1,
138     'VDSO32_PRELINK'    => 1,
139     'VDSO32_vsyscall'   => 1,
140     'VDSO32_rt_sigreturn'=>1,
141     'VDSO32_sigreturn'  => 1,
142 );
143
144 # Files with exceptions to source file location
145 my %sourceloc = (
146     'net/dccp/dccp_probe.o'     => 'probe',
147     'net/dccp/dccp_ipv4.o'      => 'ipv4',
148     'net/dccp/dccp_ipv6.o'      => 'ipv6',
149     'net/dccp/dccp_diag.o'      => 'diag',
150     'drivers/char/hw_random/rng-core.o' => 'core',
151     'fs/fat/msdos.o'            => 'namei_msdos',
152     'fs/fat/vfat.o'             => 'namei_vfat',
153     'fs/nfs_common/nfs_acl.o'   => 'nfsacl',
154     'sound/soundcore.o'         => 'sound_core',
155     'drivers/md/dm-mirror.o'    => 'dm-raid1',
156     'drivers/message/i2o/i2o_bus.o' => 'bus-osm',
157     'arch/x86/kvm/kvm-amd.o'    => 'svm',
158     'arch/x86/kvm/kvm-intel.o'  => 'vmx',
159     'arch/x86/crypto/twofish-x86_64.o' => 'twofish-x86_64-asm_64',
160     'arch/x86/crypto/aes-x86_64.o' => 'aes-x86_64-asm_64',
161     'arch/x86/crypto/aesni-intel.o' => 'aesni-intel_asm',
162     'arch/x86/crypto/salsa20-x86_64.o' => 'salsa20-x86_64-asm_64',
163 );
164
165 &find(\&linux_objects, '.');    # find the objects and do_nm on them
166 &list_multiply_defined();
167 &resolve_external_references();
168 &list_extra_externals();
169
170 exit(0);
171
172 sub linux_objects
173 {
174         # Select objects, ignoring objects which are only created by
175         # merging other objects.  Also ignore all of modules, scripts
176         # and compressed.  Most conglomerate objects are handled by do_nm,
177         # this list only contains the special cases.  These include objects
178         # that are linked from just one other object and objects for which
179         # there is really no permanent source file.
180         my $basename = $_;
181         $_ = $File::Find::name;
182         s:^\./::;
183         if (/.*\.o$/ &&
184                 ! (
185                 m:/built-in.o$:
186                 || m:arch/x86/vdso/:
187                 || m:arch/x86/boot/:
188                 || m:arch/ia64/ia32/ia32.o$:
189                 || m:arch/ia64/kernel/gate-syms.o$:
190                 || m:arch/ia64/lib/__divdi3.o$:
191                 || m:arch/ia64/lib/__divsi3.o$:
192                 || m:arch/ia64/lib/__moddi3.o$:
193                 || m:arch/ia64/lib/__modsi3.o$:
194                 || m:arch/ia64/lib/__udivdi3.o$:
195                 || m:arch/ia64/lib/__udivsi3.o$:
196                 || m:arch/ia64/lib/__umoddi3.o$:
197                 || m:arch/ia64/lib/__umodsi3.o$:
198                 || m:arch/ia64/scripts/check_gas_for_hint.o$:
199                 || m:arch/ia64/sn/kernel/xp.o$:
200                 || m:boot/bbootsect.o$:
201                 || m:boot/bsetup.o$:
202                 || m:/bootsect.o$:
203                 || m:/boot/setup.o$:
204                 || m:/compressed/:
205                 || m:drivers/cdrom/driver.o$:
206                 || m:drivers/char/drm/tdfx_drv.o$:
207                 || m:drivers/ide/ide-detect.o$:
208                 || m:drivers/ide/pci/idedriver-pci.o$:
209                 || m:drivers/media/media.o$:
210                 || m:drivers/scsi/sd_mod.o$:
211                 || m:drivers/video/video.o$:
212                 || m:fs/devpts/devpts.o$:
213                 || m:fs/exportfs/exportfs.o$:
214                 || m:fs/hugetlbfs/hugetlbfs.o$:
215                 || m:fs/msdos/msdos.o$:
216                 || m:fs/nls/nls.o$:
217                 || m:fs/ramfs/ramfs.o$:
218                 || m:fs/romfs/romfs.o$:
219                 || m:fs/vfat/vfat.o$:
220                 || m:init/mounts.o$:
221                 || m:^modules/:
222                 || m:net/netlink/netlink.o$:
223                 || m:net/sched/sched.o$:
224                 || m:/piggy.o$:
225                 || m:^scripts/:
226                 || m:sound/.*/snd-:
227                 || m:^.*/\.tmp_:
228                 || m:^\.tmp_:
229                 || m:/vmlinux-obj.o$:
230                 || m:^tools/:
231                 )
232         ) {
233                 do_nm($basename, $_);
234         }
235         $_ = $basename;         # File::Find expects $_ untouched (undocumented)
236 }
237
238 sub do_nm
239 {
240         my ($basename, $fullname) = @_;
241         my ($source, $type, $name);
242         if (! -e $basename) {
243                 printf STDERR "$basename does not exist\n";
244                 return;
245         }
246         if ($fullname !~ /\.o$/) {
247                 printf STDERR "$fullname is not an object file\n";
248                 return;
249         }
250         ($source = $basename) =~ s/\.o$//;
251
252         $source = $sourceloc{$fullname} if ($sourceloc{$fullname});
253
254         if (-e "$source.c" || -e "$source.S") {
255                 $source = "$objtree$File::Find::dir/$source";
256         } else {
257                 $source = "$srctree$File::Find::dir/$source";
258         }
259
260         if (! -e "$source.c" && ! -e "$source.S") {
261                 # No obvious source, exclude the object if it is conglomerate
262                 open(my $objdumpdata, "$objdump $basename|")
263                     or die "$objdump $fullname failed $!\n";
264
265                 my $comment;
266                 while (<$objdumpdata>) {
267                         chomp();
268                         if (/^In archive/) {
269                                 # Archives are always conglomerate
270                                 $comment = "GCC:GCC:";
271                                 last;
272                         }
273                         next if (! /^[ 0-9a-f]{5,} /);
274                         $comment .= substr($_, 43);
275                 }
276                 close($objdumpdata);
277
278                 if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
279                         printf STDERR "No source file found for $fullname\n";
280                 }
281                 return;
282         }
283         open (my $nmdata, "$nm $basename|")
284             or die "$nm $fullname failed $!\n";
285
286         my @nmdata;
287         while (<$nmdata>) {
288                 chop;
289                 ($type, $name) = (split(/ +/, $_, 3))[1..2];
290                 # Expected types
291                 # A absolute symbol
292                 # B weak external reference to data that has been resolved
293                 # C global variable, uninitialised
294                 # D global variable, initialised
295                 # G global variable, initialised, small data section
296                 # R global array, initialised
297                 # S global variable, uninitialised, small bss
298                 # T global label/procedure
299                 # U external reference
300                 # W weak external reference to text that has been resolved
301                 # V similar to W, but the value of the weak symbol becomes zero with no error.
302                 # a assembler equate
303                 # b static variable, uninitialised
304                 # d static variable, initialised
305                 # g static variable, initialised, small data section
306                 # r static array, initialised
307                 # s static variable, uninitialised, small bss
308                 # t static label/procedures
309                 # w weak external reference to text that has not been resolved
310                 # v similar to w
311                 # ? undefined type, used a lot by modules
312                 if ($type !~ /^[ABCDGRSTUWVabdgrstwv?]$/) {
313                         printf STDERR "nm output for $fullname contains unknown type '$_'\n";
314                 }
315                 elsif ($name =~ /\./) {
316                         # name with '.' is local static
317                 }
318                 else {
319                         $type = 'R' if ($type eq '?');  # binutils replaced ? with R at one point
320                         # binutils keeps changing the type for exported symbols, force it to R
321                         $type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
322                         $name =~ s/_R[a-f0-9]{8}$//;    # module versions adds this
323                         if ($type =~ /[ABCDGRSTWV]/ &&
324                                 $name ne 'init_module' &&
325                                 $name ne 'cleanup_module' &&
326                                 $name ne 'Using_Versions' &&
327                                 $name !~ /^Version_[0-9]+$/ &&
328                                 $name !~ /^__parm_/ &&
329                                 $name !~ /^__kstrtab/ &&
330                                 $name !~ /^__ksymtab/ &&
331                                 $name !~ /^__kcrctab_/ &&
332                                 $name !~ /^__exitcall_/ &&
333                                 $name !~ /^__initcall_/ &&
334                                 $name !~ /^__kdb_initcall_/ &&
335                                 $name !~ /^__kdb_exitcall_/ &&
336                                 $name !~ /^__module_/ &&
337                                 $name !~ /^__mod_/ &&
338                                 $name !~ /^__crc_/ &&
339                                 $name ne '__this_module' &&
340                                 $name ne 'kernel_version') {
341                                 if (!exists($def{$name})) {
342                                         $def{$name} = [];
343                                 }
344                                 push(@{$def{$name}}, $fullname);
345                         }
346                         push(@nmdata, "$type $name");
347                         if ($name =~ /^__ksymtab_/) {
348                                 $name = substr($name, 10);
349                                 if (!exists($ksymtab{$name})) {
350                                         $ksymtab{$name} = [];
351                                 }
352                                 push(@{$ksymtab{$name}}, $fullname);
353                         }
354                 }
355         }
356         close($nmdata);
357
358         if ($#nmdata < 0) {
359             printf "No nm data for $fullname\n"
360                 unless $nmexception{$fullname};
361             return;
362         }
363         $nmdata{$fullname} = \@nmdata;
364 }
365
366 sub drop_def
367 {
368         my ($object, $name) = @_;
369         my $nmdata = $nmdata{$object};
370         my ($i, $j);
371         for ($i = 0; $i <= $#{$nmdata}; ++$i) {
372                 if ($name eq (split(' ', $nmdata->[$i], 2))[1]) {
373                         splice(@{$nmdata{$object}}, $i, 1);
374                         my $def = $def{$name};
375                         for ($j = 0; $j < $#{$def{$name}}; ++$j) {
376                                 if ($def{$name}[$j] eq $object) {
377                                         splice(@{$def{$name}}, $j, 1);
378                                 }
379                         }
380                         last;
381                 }
382         }
383 }
384
385 sub list_multiply_defined
386 {
387         foreach my $name (keys(%def)) {
388                 if ($#{$def{$name}} > 0) {
389                         # Special case for cond_syscall
390                         if ($#{$def{$name}} == 1 &&
391                            ($name =~ /^sys_/ || $name =~ /^compat_sys_/ ||
392                             $name =~ /^sys32_/)) {
393                                 if($def{$name}[0] eq "kernel/sys_ni.o" ||
394                                    $def{$name}[1] eq "kernel/sys_ni.o") {
395                                         &drop_def("kernel/sys_ni.o", $name);
396                                         next;
397                                 }
398                         }
399
400                         printf "$name is multiply defined in :-\n";
401                         foreach my $module (@{$def{$name}}) {
402                                 printf "\t$module\n";
403                         }
404                 }
405         }
406 }
407
408 sub resolve_external_references
409 {
410         my ($kstrtab, $ksymtab, $export);
411
412         printf "\n";
413         foreach my $object (keys(%nmdata)) {
414                 my $nmdata = $nmdata{$object};
415                 for (my $i = 0; $i <= $#{$nmdata}; ++$i) {
416                         my ($type, $name) = split(' ', $nmdata->[$i], 2);
417                         if ($type eq "U" || $type eq "w") {
418                                 if (exists($def{$name}) || exists($ksymtab{$name})) {
419                                         # add the owning object to the nmdata
420                                         $nmdata->[$i] = "$type $name $object";
421                                         # only count as a reference if it is not EXPORT_...
422                                         $kstrtab = "R __kstrtab_$name";
423                                         $ksymtab = "R __ksymtab_$name";
424                                         $export = 0;
425                                         for (my $j = 0; $j <= $#{$nmdata}; ++$j) {
426                                                 if ($nmdata->[$j] eq $kstrtab ||
427                                                     $nmdata->[$j] eq $ksymtab) {
428                                                         $export = 1;
429                                                         last;
430                                                 }
431                                         }
432                                         if ($export) {
433                                                 $export{$name} = "";
434                                         }
435                                         else {
436                                                 $ref{$name} = ""
437                                         }
438                                 }
439                                 elsif ( ! $nameexception{$name}
440                                         && $name !~ /^__sched_text_/
441                                         && $name !~ /^__start_/
442                                         && $name !~ /^__end_/
443                                         && $name !~ /^__stop_/
444                                         && $name !~ /^__scheduling_functions_.*_here/
445                                         && $name !~ /^__.*initcall_/
446                                         && $name !~ /^__.*per_cpu_start/
447                                         && $name !~ /^__.*per_cpu_end/
448                                         && $name !~ /^__alt_instructions/
449                                         && $name !~ /^__setup_/
450                                         && $name !~ /^__mod_timer/
451                                         && $name !~ /^__mod_page_state/
452                                         && $name !~ /^init_module/
453                                         && $name !~ /^cleanup_module/
454                                 ) {
455                                         printf "Cannot resolve ";
456                                         printf "weak " if ($type eq "w");
457                                         printf "reference to $name from $object\n";
458                                 }
459                         }
460                 }
461         }
462 }
463
464 sub list_extra_externals
465 {
466         my %noref = ();
467
468         foreach my $name (keys(%def)) {
469                 if (! exists($ref{$name})) {
470                         my @module = @{$def{$name}};
471                         foreach my $module (@module) {
472                                 if (! exists($noref{$module})) {
473                                         $noref{$module} = [];
474                                 }
475                                 push(@{$noref{$module}}, $name);
476                         }
477                 }
478         }
479         if (%noref) {
480                 printf "\nExternally defined symbols with no external references\n";
481                 foreach my $module (sort(keys(%noref))) {
482                         printf "  $module\n";
483                         foreach (sort(@{$noref{$module}})) {
484                             my $export;
485                             if (exists($export{$_})) {
486                                 $export = " (export only)";
487                             } else {
488                                 $export = "";
489                             }
490                             printf "    $_$export\n";
491                         }
492                 }
493         }
494 }