blob: 729c547fc9e1ef54400db1f06e5cf98c592bf290 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#
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
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020065use warnings;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066use 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,
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700120 'kallsyms_offsets' => 1,
121 'kallsyms_relative_base'=> 1,
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700122 '__this_module' => 1,
123 '_etext' => 1,
124 '_edata' => 1,
125 '_end' => 1,
126 '__bss_start' => 1,
127 '_text' => 1,
128 '_stext' => 1,
129 '__gp' => 1,
130 'ia64_unw_start' => 1,
131 'ia64_unw_end' => 1,
132 '__init_begin' => 1,
133 '__init_end' => 1,
134 '__bss_stop' => 1,
135 '__nosave_begin' => 1,
136 '__nosave_end' => 1,
137 'pg0' => 1,
Amerigo Wangabb43852010-10-27 12:42:01 -0700138 'vdso_enabled' => 1,
139 '__stack_chk_fail' => 1,
140 'VDSO32_PRELINK' => 1,
141 'VDSO32_vsyscall' => 1,
142 'VDSO32_rt_sigreturn'=>1,
143 'VDSO32_sigreturn' => 1,
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700144);
145
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147&find(\&linux_objects, '.'); # find the objects and do_nm on them
148&list_multiply_defined();
149&resolve_external_references();
150&list_extra_externals();
151
152exit(0);
153
154sub linux_objects
155{
156 # Select objects, ignoring objects which are only created by
157 # merging other objects. Also ignore all of modules, scripts
158 # and compressed. Most conglomerate objects are handled by do_nm,
159 # this list only contains the special cases. These include objects
160 # that are linked from just one other object and objects for which
161 # there is really no permanent source file.
162 my $basename = $_;
163 $_ = $File::Find::name;
164 s:^\./::;
165 if (/.*\.o$/ &&
166 ! (
167 m:/built-in.o$:
Amerigo Wangabb43852010-10-27 12:42:01 -0700168 || m:arch/x86/vdso/:
169 || m:arch/x86/boot/:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 || m:arch/ia64/ia32/ia32.o$:
171 || m:arch/ia64/kernel/gate-syms.o$:
172 || m:arch/ia64/lib/__divdi3.o$:
173 || m:arch/ia64/lib/__divsi3.o$:
174 || m:arch/ia64/lib/__moddi3.o$:
175 || m:arch/ia64/lib/__modsi3.o$:
176 || m:arch/ia64/lib/__udivdi3.o$:
177 || m:arch/ia64/lib/__udivsi3.o$:
178 || m:arch/ia64/lib/__umoddi3.o$:
179 || m:arch/ia64/lib/__umodsi3.o$:
180 || m:arch/ia64/scripts/check_gas_for_hint.o$:
181 || m:arch/ia64/sn/kernel/xp.o$:
182 || m:boot/bbootsect.o$:
183 || m:boot/bsetup.o$:
184 || m:/bootsect.o$:
185 || m:/boot/setup.o$:
186 || m:/compressed/:
187 || m:drivers/cdrom/driver.o$:
188 || m:drivers/char/drm/tdfx_drv.o$:
189 || m:drivers/ide/ide-detect.o$:
190 || m:drivers/ide/pci/idedriver-pci.o$:
191 || m:drivers/media/media.o$:
192 || m:drivers/scsi/sd_mod.o$:
193 || m:drivers/video/video.o$:
194 || m:fs/devpts/devpts.o$:
195 || m:fs/exportfs/exportfs.o$:
196 || m:fs/hugetlbfs/hugetlbfs.o$:
197 || m:fs/msdos/msdos.o$:
198 || m:fs/nls/nls.o$:
199 || m:fs/ramfs/ramfs.o$:
200 || m:fs/romfs/romfs.o$:
201 || m:fs/vfat/vfat.o$:
202 || m:init/mounts.o$:
203 || m:^modules/:
204 || m:net/netlink/netlink.o$:
205 || m:net/sched/sched.o$:
206 || m:/piggy.o$:
207 || m:^scripts/:
208 || m:sound/.*/snd-:
209 || m:^.*/\.tmp_:
210 || m:^\.tmp_:
211 || m:/vmlinux-obj.o$:
Amerigo Wangabb43852010-10-27 12:42:01 -0700212 || m:^tools/:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 )
214 ) {
215 do_nm($basename, $_);
216 }
217 $_ = $basename; # File::Find expects $_ untouched (undocumented)
218}
219
220sub do_nm
221{
222 my ($basename, $fullname) = @_;
223 my ($source, $type, $name);
224 if (! -e $basename) {
225 printf STDERR "$basename does not exist\n";
226 return;
227 }
228 if ($fullname !~ /\.o$/) {
229 printf STDERR "$fullname is not an object file\n";
230 return;
231 }
Amerigo Wangc25f4152010-10-27 12:42:00 -0700232 ($source = $basename) =~ s/\.o$//;
233 if (-e "$source.c" || -e "$source.S") {
234 $source = "$objtree$File::Find::dir/$source";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 } else {
Amerigo Wangc25f4152010-10-27 12:42:00 -0700236 $source = "$srctree$File::Find::dir/$source";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 if (! -e "$source.c" && ! -e "$source.S") {
239 # No obvious source, exclude the object if it is conglomerate
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800240 open(my $objdumpdata, "$objdump $basename|")
241 or die "$objdump $fullname failed $!\n";
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 my $comment;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800244 while (<$objdumpdata>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 chomp();
246 if (/^In archive/) {
247 # Archives are always conglomerate
248 $comment = "GCC:GCC:";
249 last;
250 }
251 next if (! /^[ 0-9a-f]{5,} /);
252 $comment .= substr($_, 43);
253 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800254 close($objdumpdata);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
257 printf STDERR "No source file found for $fullname\n";
258 }
259 return;
260 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800261 open (my $nmdata, "$nm $basename|")
262 or die "$nm $fullname failed $!\n";
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 my @nmdata;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800265 while (<$nmdata>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 chop;
267 ($type, $name) = (split(/ +/, $_, 3))[1..2];
268 # Expected types
269 # A absolute symbol
270 # B weak external reference to data that has been resolved
271 # C global variable, uninitialised
272 # D global variable, initialised
273 # G global variable, initialised, small data section
274 # R global array, initialised
275 # S global variable, uninitialised, small bss
276 # T global label/procedure
277 # U external reference
278 # W weak external reference to text that has been resolved
Amerigo Wange8cf9812010-10-27 12:42:01 -0700279 # V similar to W, but the value of the weak symbol becomes zero with no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 # a assembler equate
281 # b static variable, uninitialised
282 # d static variable, initialised
283 # g static variable, initialised, small data section
284 # r static array, initialised
285 # s static variable, uninitialised, small bss
286 # t static label/procedures
287 # w weak external reference to text that has not been resolved
Amerigo Wange8cf9812010-10-27 12:42:01 -0700288 # v similar to w
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 # ? undefined type, used a lot by modules
Amerigo Wange8cf9812010-10-27 12:42:01 -0700290 if ($type !~ /^[ABCDGRSTUWVabdgrstwv?]$/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 printf STDERR "nm output for $fullname contains unknown type '$_'\n";
292 }
293 elsif ($name =~ /\./) {
294 # name with '.' is local static
295 }
296 else {
297 $type = 'R' if ($type eq '?'); # binutils replaced ? with R at one point
298 # binutils keeps changing the type for exported symbols, force it to R
299 $type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
300 $name =~ s/_R[a-f0-9]{8}$//; # module versions adds this
Amerigo Wange8cf9812010-10-27 12:42:01 -0700301 if ($type =~ /[ABCDGRSTWV]/ &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 $name ne 'init_module' &&
303 $name ne 'cleanup_module' &&
304 $name ne 'Using_Versions' &&
305 $name !~ /^Version_[0-9]+$/ &&
306 $name !~ /^__parm_/ &&
307 $name !~ /^__kstrtab/ &&
308 $name !~ /^__ksymtab/ &&
309 $name !~ /^__kcrctab_/ &&
310 $name !~ /^__exitcall_/ &&
311 $name !~ /^__initcall_/ &&
312 $name !~ /^__kdb_initcall_/ &&
313 $name !~ /^__kdb_exitcall_/ &&
314 $name !~ /^__module_/ &&
315 $name !~ /^__mod_/ &&
316 $name !~ /^__crc_/ &&
317 $name ne '__this_module' &&
318 $name ne 'kernel_version') {
319 if (!exists($def{$name})) {
320 $def{$name} = [];
321 }
322 push(@{$def{$name}}, $fullname);
323 }
324 push(@nmdata, "$type $name");
325 if ($name =~ /^__ksymtab_/) {
326 $name = substr($name, 10);
327 if (!exists($ksymtab{$name})) {
328 $ksymtab{$name} = [];
329 }
330 push(@{$ksymtab{$name}}, $fullname);
331 }
332 }
333 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800334 close($nmdata);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if ($#nmdata < 0) {
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700337 printf "No nm data for $fullname\n"
338 unless $nmexception{$fullname};
339 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 $nmdata{$fullname} = \@nmdata;
342}
343
344sub drop_def
345{
346 my ($object, $name) = @_;
347 my $nmdata = $nmdata{$object};
348 my ($i, $j);
349 for ($i = 0; $i <= $#{$nmdata}; ++$i) {
350 if ($name eq (split(' ', $nmdata->[$i], 2))[1]) {
351 splice(@{$nmdata{$object}}, $i, 1);
352 my $def = $def{$name};
353 for ($j = 0; $j < $#{$def{$name}}; ++$j) {
354 if ($def{$name}[$j] eq $object) {
355 splice(@{$def{$name}}, $j, 1);
356 }
357 }
358 last;
359 }
360 }
361}
362
363sub list_multiply_defined
364{
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800365 foreach my $name (keys(%def)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if ($#{$def{$name}} > 0) {
367 # Special case for cond_syscall
Amerigo Wangabb43852010-10-27 12:42:01 -0700368 if ($#{$def{$name}} == 1 &&
369 ($name =~ /^sys_/ || $name =~ /^compat_sys_/ ||
370 $name =~ /^sys32_/)) {
Amerigo Wange8cf9812010-10-27 12:42:01 -0700371 if($def{$name}[0] eq "kernel/sys_ni.o" ||
372 $def{$name}[1] eq "kernel/sys_ni.o") {
373 &drop_def("kernel/sys_ni.o", $name);
374 next;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 printf "$name is multiply defined in :-\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800379 foreach my $module (@{$def{$name}}) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printf "\t$module\n";
381 }
382 }
383 }
384}
385
386sub resolve_external_references
387{
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800388 my ($kstrtab, $ksymtab, $export);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 printf "\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800391 foreach my $object (keys(%nmdata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 my $nmdata = $nmdata{$object};
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800393 for (my $i = 0; $i <= $#{$nmdata}; ++$i) {
394 my ($type, $name) = split(' ', $nmdata->[$i], 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if ($type eq "U" || $type eq "w") {
396 if (exists($def{$name}) || exists($ksymtab{$name})) {
397 # add the owning object to the nmdata
398 $nmdata->[$i] = "$type $name $object";
399 # only count as a reference if it is not EXPORT_...
400 $kstrtab = "R __kstrtab_$name";
401 $ksymtab = "R __ksymtab_$name";
402 $export = 0;
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800403 for (my $j = 0; $j <= $#{$nmdata}; ++$j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if ($nmdata->[$j] eq $kstrtab ||
405 $nmdata->[$j] eq $ksymtab) {
406 $export = 1;
407 last;
408 }
409 }
410 if ($export) {
411 $export{$name} = "";
412 }
413 else {
414 $ref{$name} = ""
415 }
416 }
Stephen Hemminger43f683c2010-10-27 12:42:00 -0700417 elsif ( ! $nameexception{$name}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 && $name !~ /^__sched_text_/
419 && $name !~ /^__start_/
420 && $name !~ /^__end_/
421 && $name !~ /^__stop_/
422 && $name !~ /^__scheduling_functions_.*_here/
423 && $name !~ /^__.*initcall_/
424 && $name !~ /^__.*per_cpu_start/
425 && $name !~ /^__.*per_cpu_end/
426 && $name !~ /^__alt_instructions/
427 && $name !~ /^__setup_/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 && $name !~ /^__mod_timer/
429 && $name !~ /^__mod_page_state/
430 && $name !~ /^init_module/
431 && $name !~ /^cleanup_module/
432 ) {
433 printf "Cannot resolve ";
434 printf "weak " if ($type eq "w");
435 printf "reference to $name from $object\n";
436 }
437 }
438 }
439 }
440}
441
442sub list_extra_externals
443{
444 my %noref = ();
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800445
446 foreach my $name (keys(%def)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (! exists($ref{$name})) {
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800448 my @module = @{$def{$name}};
449 foreach my $module (@module) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (! exists($noref{$module})) {
451 $noref{$module} = [];
452 }
453 push(@{$noref{$module}}, $name);
454 }
455 }
456 }
457 if (%noref) {
458 printf "\nExternally defined symbols with no external references\n";
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800459 foreach my $module (sort(keys(%noref))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 printf " $module\n";
461 foreach (sort(@{$noref{$module}})) {
Stephen Hemminger86d08e52010-02-22 15:17:18 -0800462 my $export;
463 if (exists($export{$_})) {
464 $export = " (export only)";
465 } else {
466 $export = "";
467 }
468 printf " $_$export\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 }
471 }
472}