3 # namespace.pl. Mon Aug 30 2004
5 # Perform a name space analysis on the linux kernel.
7 # Copyright Keith Owens <kaos@ocs.com.au>. GPL.
9 # Invoke by changing directory to the top of the kernel object
10 # tree then namespace.pl, no parameters.
12 # Tuned for 2.1.x kernels with the new module handling, it will
13 # work with 2.0 kernels as well.
15 # Last change 2.6.9-rc1, adding support for separate source and object
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
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
34 # so the symbols are defined as static unless a particular
35 # CONFIG_... requires it to be external.
37 # A symbol that is suffixed with '(export only)' has these properties
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.
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.
48 # Name space analysis and cleanup is an iterative process. You cannot
49 # expect to find all the problems in a single pass.
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
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
65 require 5; # at least perl 5
69 my $nm = ($ENV{'NM'} || "nm") . " -p";
70 my $objdump = ($ENV{'OBJDUMP'} || "objdump") . " -s -j .comment";
73 $srctree = "$ENV{'srctree'}/" if (exists($ENV{'srctree'}));
74 $objtree = "$ENV{'objtree'}/" if (exists($ENV{'objtree'}));
77 print STDERR
"usage: $0 takes no parameters\n";
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
87 &find
(\
&linux_objects
, '.'); # find the objects and do_nm on them
88 &list_multiply_defined
();
89 &resolve_external_references
();
90 &list_extra_externals
();
96 # Select objects, ignoring objects which are only created by
97 # merging other objects. Also ignore all of modules, scripts
98 # and compressed. Most conglomerate objects are handled by do_nm,
99 # this list only contains the special cases. These include objects
100 # that are linked from just one other object and objects for which
101 # there is really no permanent source file.
103 $_ = $File::Find
::name
;
108 || m
:arch
/x86/kernel
/vsyscall
-syms
.o
$:
109 || m
:arch
/ia64/ia32/ia32
.o
$:
110 || m
:arch
/ia64/kernel
/gate
-syms
.o
$:
111 || m
:arch
/ia64/lib
/__divdi3
.o
$:
112 || m
:arch
/ia64/lib
/__divsi3
.o
$:
113 || m
:arch
/ia64/lib
/__moddi3
.o
$:
114 || m
:arch
/ia64/lib
/__modsi3
.o
$:
115 || m
:arch
/ia64/lib
/__udivdi3
.o
$:
116 || m
:arch
/ia64/lib
/__udivsi3
.o
$:
117 || m
:arch
/ia64/lib
/__umoddi3
.o
$:
118 || m
:arch
/ia64/lib
/__umodsi3
.o
$:
119 || m
:arch
/ia64/scripts
/check_gas_for_hint
.o
$:
120 || m
:arch
/ia64/sn
/kernel/xp.o
$:
121 || m
:boot
/bbootsect
.o
$:
126 || m
:drivers
/cdrom/driver
.o
$:
127 || m
:drivers
/char
/drm
/tdfx_drv
.o
$:
128 || m
:drivers
/ide/ide
-detect
.o
$:
129 || m
:drivers
/ide
/pci
/idedriver
-pci
.o
$:
130 || m
:drivers
/media/media
.o
$:
131 || m
:drivers
/scsi/sd_mod
.o
$:
132 || m
:drivers
/video/video
.o
$:
133 || m
:fs
/devpts/devpts
.o
$:
134 || m
:fs
/exportfs/exportfs
.o
$:
135 || m
:fs
/hugetlbfs/hugetlbfs
.o
$:
136 || m
:fs
/msdos/msdos.o
$:
138 || m
:fs
/ramfs/ramfs
.o
$:
139 || m
:fs
/romfs/romfs
.o
$:
140 || m
:fs
/vfat/vfat
.o
$:
143 || m
:net
/netlink/netlink
.o
$:
144 || m
:net
/sched/sched
.o
$:
150 || m
:/vmlinux
-obj
.o
$:
153 do_nm
($basename, $_);
155 $_ = $basename; # File::Find expects $_ untouched (undocumented)
160 my ($basename, $fullname) = @_;
161 my ($source, $type, $name);
162 if (! -e
$basename) {
163 printf STDERR
"$basename does not exist\n";
166 if ($fullname !~ /\.o$/) {
167 printf STDERR
"$fullname is not an object file\n";
170 ($source = $fullname) =~ s/\.o$//;
171 if (-e
"$objtree$source.c" || -e
"$objtree$source.S") {
172 $source = "$objtree$source";
174 $source = "$srctree$source";
176 if (! -e
"$source.c" && ! -e
"$source.S") {
177 # No obvious source, exclude the object if it is conglomerate
178 open(my $objdumpdata, "$objdump $basename|")
179 or die "$objdump $fullname failed $!\n";
182 while (<$objdumpdata>) {
185 # Archives are always conglomerate
186 $comment = "GCC:GCC:";
189 next if (! /^[ 0-9a-f]{5,} /);
190 $comment .= substr($_, 43);
194 if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
195 printf STDERR
"No source file found for $fullname\n";
199 open (my $nmdata, "$nm $basename|")
200 or die "$nm $fullname failed $!\n";
205 ($type, $name) = (split(/ +/, $_, 3))[1..2];
208 # B weak external reference to data that has been resolved
209 # C global variable, uninitialised
210 # D global variable, initialised
211 # G global variable, initialised, small data section
212 # R global array, initialised
213 # S global variable, uninitialised, small bss
214 # T global label/procedure
215 # U external reference
216 # W weak external reference to text that has been resolved
218 # b static variable, uninitialised
219 # d static variable, initialised
220 # g static variable, initialised, small data section
221 # r static array, initialised
222 # s static variable, uninitialised, small bss
223 # t static label/procedures
224 # w weak external reference to text that has not been resolved
225 # ? undefined type, used a lot by modules
226 if ($type !~ /^[ABCDGRSTUWabdgrstw?]$/) {
227 printf STDERR
"nm output for $fullname contains unknown type '$_'\n";
229 elsif ($name =~ /\./) {
230 # name with '.' is local static
233 $type = 'R' if ($type eq '?'); # binutils replaced ? with R at one point
234 # binutils keeps changing the type for exported symbols, force it to R
235 $type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
236 $name =~ s/_R[a-f0-9]{8}$//; # module versions adds this
237 if ($type =~ /[ABCDGRSTW]/ &&
238 $name ne 'init_module' &&
239 $name ne 'cleanup_module' &&
240 $name ne 'Using_Versions' &&
241 $name !~ /^Version_[0-9]+$/ &&
242 $name !~ /^__parm_/ &&
243 $name !~ /^__kstrtab/ &&
244 $name !~ /^__ksymtab/ &&
245 $name !~ /^__kcrctab_/ &&
246 $name !~ /^__exitcall_/ &&
247 $name !~ /^__initcall_/ &&
248 $name !~ /^__kdb_initcall_/ &&
249 $name !~ /^__kdb_exitcall_/ &&
250 $name !~ /^__module_/ &&
251 $name !~ /^__mod_/ &&
252 $name !~ /^__crc_/ &&
253 $name ne '__this_module' &&
254 $name ne 'kernel_version') {
255 if (!exists($def{$name})) {
258 push(@
{$def{$name}}, $fullname);
260 push(@nmdata, "$type $name");
261 if ($name =~ /^__ksymtab_/) {
262 $name = substr($name, 10);
263 if (!exists($ksymtab{$name})) {
264 $ksymtab{$name} = [];
266 push(@
{$ksymtab{$name}}, $fullname);
274 $fullname ne "lib/brlock.o"
275 && $fullname ne "lib/dec_and_lock.o"
276 && $fullname ne "fs/xfs/xfs_macros.o"
277 && $fullname ne "drivers/ide/ide-probe-mini.o"
278 && $fullname ne "usr/initramfs_data.o"
279 && $fullname ne "drivers/acpi/executer/exdump.o"
280 && $fullname ne "drivers/acpi/resources/rsdump.o"
281 && $fullname ne "drivers/acpi/namespace/nsdumpdv.o"
282 && $fullname ne "drivers/acpi/namespace/nsdump.o"
283 && $fullname ne "arch/ia64/sn/kernel/sn2/io.o"
284 && $fullname ne "arch/ia64/kernel/gate-data.o"
285 && $fullname ne "drivers/ieee1394/oui.o"
286 && $fullname ne "security/capability.o"
287 && $fullname ne "sound/core/wrappers.o"
288 && $fullname ne "fs/ntfs/sysctl.o"
289 && $fullname ne "fs/jfs/jfs_debug.o"
291 printf "No nm data for $fullname\n";
295 $nmdata{$fullname} = \
@nmdata;
300 my ($object, $name) = @_;
301 my $nmdata = $nmdata{$object};
303 for ($i = 0; $i <= $#{$nmdata}; ++$i) {
304 if ($name eq (split(' ', $nmdata->[$i], 2))[1]) {
305 splice(@
{$nmdata{$object}}, $i, 1);
306 my $def = $def{$name};
307 for ($j = 0; $j < $#{$def{$name}}; ++$j) {
308 if ($def{$name}[$j] eq $object) {
309 splice(@
{$def{$name}}, $j, 1);
317 sub list_multiply_defined
319 foreach my $name (keys(%def)) {
320 if ($#{$def{$name}} > 0) {
321 # Special case for cond_syscall
322 if ($#{$def{$name}} == 1 && $name =~ /^sys_/ &&
323 ($def{$name}[0] eq "kernel/sys.o" ||
324 $def{$name}[1] eq "kernel/sys.o")) {
325 &drop_def
("kernel/sys.o", $name);
328 # Special case for i386 entry code
329 if ($#{$def{$name}} == 1 && $name =~ /^__kernel_/ &&
330 $def{$name}[0] eq "arch/x86/kernel/vsyscall-int80_32.o" &&
331 $def{$name}[1] eq "arch/x86/kernel/vsyscall-sysenter_32.o") {
332 &drop_def
("arch/x86/kernel/vsyscall-sysenter_32.o", $name);
336 printf "$name is multiply defined in :-\n";
337 foreach my $module (@
{$def{$name}}) {
338 printf "\t$module\n";
344 sub resolve_external_references
346 my ($kstrtab, $ksymtab, $export);
349 foreach my $object (keys(%nmdata)) {
350 my $nmdata = $nmdata{$object};
351 for (my $i = 0; $i <= $#{$nmdata}; ++$i) {
352 my ($type, $name) = split(' ', $nmdata->[$i], 2);
353 if ($type eq "U" || $type eq "w") {
354 if (exists($def{$name}) || exists($ksymtab{$name})) {
355 # add the owning object to the nmdata
356 $nmdata->[$i] = "$type $name $object";
357 # only count as a reference if it is not EXPORT_...
358 $kstrtab = "R __kstrtab_$name";
359 $ksymtab = "R __ksymtab_$name";
361 for (my $j = 0; $j <= $#{$nmdata}; ++$j) {
362 if ($nmdata->[$j] eq $kstrtab ||
363 $nmdata->[$j] eq $ksymtab) {
375 elsif ( $name ne "mod_use_count_"
376 && $name ne "__initramfs_end"
377 && $name ne "__initramfs_start"
378 && $name ne "_einittext"
379 && $name ne "_sinittext"
380 && $name ne "kallsyms_names"
381 && $name ne "kallsyms_num_syms"
382 && $name ne "kallsyms_addresses"
383 && $name ne "__this_module"
387 && $name ne "__bss_start"
391 && $name ne "ia64_unw_start"
392 && $name ne "ia64_unw_end"
393 && $name ne "__init_begin"
394 && $name ne "__init_end"
395 && $name ne "__bss_stop"
396 && $name ne "__nosave_begin"
397 && $name ne "__nosave_end"
399 && $name ne "__module_text_address"
400 && $name !~ /^__sched_text_/
401 && $name !~ /^__start_/
402 && $name !~ /^__end_/
403 && $name !~ /^__stop_/
404 && $name !~ /^__scheduling_functions_.*_here/
405 && $name !~ /^__.*initcall_/
406 && $name !~ /^__.*per_cpu_start/
407 && $name !~ /^__.*per_cpu_end/
408 && $name !~ /^__alt_instructions/
409 && $name !~ /^__setup_/
410 && $name !~ /^jiffies/
411 && $name !~ /^__mod_timer/
412 && $name !~ /^__mod_page_state/
413 && $name !~ /^init_module/
414 && $name !~ /^cleanup_module/
416 printf "Cannot resolve ";
417 printf "weak " if ($type eq "w");
418 printf "reference to $name from $object\n";
425 sub list_extra_externals
429 foreach my $name (keys(%def)) {
430 if (! exists($ref{$name})) {
431 my @module = @
{$def{$name}};
432 foreach my $module (@module) {
433 if (! exists($noref{$module})) {
434 $noref{$module} = [];
436 push(@
{$noref{$module}}, $name);
441 printf "\nExternally defined symbols with no external references\n";
442 foreach my $module (sort(keys(%noref))) {
444 foreach (sort(@
{$noref{$module}})) {
446 if (exists($export{$_})) {
447 $export = " (export only)";
451 printf " $_$export\n";