<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl
#
#	namespace.pl.  Mon Aug 30 2004
#
#	Perform a name space analysis on the linux kernel.
#
#	Copyright Keith Owens &lt;kaos@ocs.com.au&gt;.  GPL.
#
#	Invoke by changing directory to the top of the kernel object
#	tree then namespace.pl, no parameters.
#
#	Tuned for 2.1.x kernels with the new module handling, it will
#	work with 2.0 kernels as well.
#
#	Last change 2.6.9-rc1, adding support for separate source and object
#	trees.
#
#	The source must be compiled/assembled first, the object files
#	are the primary input to this script.  Incomplete or missing
#	objects will result in a flawed analysis.  Compile both vmlinux
#	and modules.
#
#	Even with complete objects, treat the result of the analysis
#	with caution.  Some external references are only used by
#	certain architectures, others with certain combinations of
#	configuration parameters.  Ideally the source should include
#	something like
#
#	#ifndef CONFIG_...
#	static
#	#endif
#	symbol_definition;
#
#	so the symbols are defined as static unless a particular
#	CONFIG_... requires it to be external.
#
#	A symbol that is suffixed with '(export only)' has these properties
#
#	* It is global.
#	* It is marked EXPORT_SYMBOL or EXPORT_SYMBOL_GPL, either in the same
#	  source file or a different source file.
#	* Given the current .config, nothing uses the symbol.
#
#	The symbol is a candidate for conversion to static, plus removal of the
#	export.  But be careful that a different .config might use the symbol.
#
#
#	Name space analysis and cleanup is an iterative process.  You cannot
#	expect to find all the problems in a single pass.
#
#	* Identify possibly unnecessary global declarations, verify that they
#	  really are unnecessary and change them to static.
#	* Compile and fix up gcc warnings about static, removing dead symbols
#	  as necessary.
#	* make clean and rebuild with different configs (especially
#	  CONFIG_MODULES=n) to see which symbols are being defined when the
#	  config does not require them.  These symbols bloat the kernel object
#	  for no good reason, which is frustrating for embedded systems.
#	* Wrap config sensitive symbols in #ifdef CONFIG_foo, as long as the
#	  code does not get too ugly.
#	* Repeat the name space analysis until you can live with with the
#	  result.
#

use warnings;
use strict;
use File::Find;
use File::Spec;

my $nm = ($ENV{'NM'} || "nm") . " -p";
my $objdump = ($ENV{'OBJDUMP'} || "objdump") . " -s -j .comment";
my $srctree = File::Spec-&gt;curdir();
my $objtree = File::Spec-&gt;curdir();
$srctree = File::Spec-&gt;rel2abs($ENV{'srctree'}) if (exists($ENV{'srctree'}));
$objtree = File::Spec-&gt;rel2abs($ENV{'objtree'}) if (exists($ENV{'objtree'}));

if ($#ARGV != -1) {
	print STDERR "usage: $0 takes no parameters\n";
	die("giving up\n");
}

my %nmdata = ();	# nm data for each object
my %def = ();		# all definitions for each name
my %ksymtab = ();	# names that appear in __ksymtab_
my %ref = ();		# $ref{$name} exists if there is a true external reference to $name
my %export = ();	# $export{$name} exists if there is an EXPORT_... of $name

my %nmexception = (
    'fs/ext3/bitmap'			=&gt; 1,
    'fs/ext4/bitmap'			=&gt; 1,
    'arch/x86/lib/thunk_32'		=&gt; 1,
    'arch/x86/lib/cmpxchg'		=&gt; 1,
    'arch/x86/vdso/vdso32/note'		=&gt; 1,
    'lib/irq_regs'			=&gt; 1,
    'usr/initramfs_data'		=&gt; 1,
    'drivers/scsi/aic94xx/aic94xx_dump'	=&gt; 1,
    'drivers/scsi/libsas/sas_dump'	=&gt; 1,
    'lib/dec_and_lock'			=&gt; 1,
    'drivers/ide/ide-probe-mini'	=&gt; 1,
    'usr/initramfs_data'		=&gt; 1,
    'drivers/acpi/acpia/exdump'		=&gt; 1,
    'drivers/acpi/acpia/rsdump'		=&gt; 1,
    'drivers/acpi/acpia/nsdumpdv'	=&gt; 1,
    'drivers/acpi/acpia/nsdump'		=&gt; 1,
    'arch/ia64/sn/kernel/sn2/io'	=&gt; 1,
    'arch/ia64/kernel/gate-data'	=&gt; 1,
    'security/capability'		=&gt; 1,
    'fs/ntfs/sysctl'			=&gt; 1,
    'fs/jfs/jfs_debug'			=&gt; 1,
);

my %nameexception = (
    'mod_use_count_'	 =&gt; 1,
    '__initramfs_end'	=&gt; 1,
    '__initramfs_start'	=&gt; 1,
    '_einittext'	=&gt; 1,
    '_sinittext'	=&gt; 1,
    'kallsyms_names'	=&gt; 1,
    'kallsyms_num_syms'	=&gt; 1,
    'kallsyms_addresses'=&gt; 1,
    'kallsyms_offsets'	=&gt; 1,
    'kallsyms_relative_base'=&gt; 1,
    '__this_module'	=&gt; 1,
    '_etext'		=&gt; 1,
    '_edata'		=&gt; 1,
    '_end'		=&gt; 1,
    '__bss_start'	=&gt; 1,
    '_text'		=&gt; 1,
    '_stext'		=&gt; 1,
    '__gp'		=&gt; 1,
    'ia64_unw_start'	=&gt; 1,
    'ia64_unw_end'	=&gt; 1,
    '__init_begin'	=&gt; 1,
    '__init_end'	=&gt; 1,
    '__bss_stop'	=&gt; 1,
    '__nosave_begin'	=&gt; 1,
    '__nosave_end'	=&gt; 1,
    'pg0'		=&gt; 1,
    'vdso_enabled'	=&gt; 1,
    '__stack_chk_fail'  =&gt; 1,
    'VDSO32_PRELINK'	=&gt; 1,
    'VDSO32_vsyscall'	=&gt; 1,
    'VDSO32_rt_sigreturn'=&gt;1,
    'VDSO32_sigreturn'	=&gt; 1,
);


&amp;find(\&amp;linux_objects, '.');	# find the objects and do_nm on them
&amp;list_multiply_defined();
&amp;resolve_external_references();
&amp;list_extra_externals();

exit(0);

sub linux_objects
{
	# Select objects, ignoring objects which are only created by
	# merging other objects.  Also ignore all of modules, scripts
	# and compressed.  Most conglomerate objects are handled by do_nm,
	# this list only contains the special cases.  These include objects
	# that are linked from just one other object and objects for which
	# there is really no permanent source file.
	my $basename = $_;
	$_ = $File::Find::name;
	s:^\./::;
	if (/.*\.o$/ &amp;&amp;
		! (
		m:/built-in.o$:
		|| m:arch/x86/vdso/:
		|| m:arch/x86/boot/:
		|| m:arch/ia64/ia32/ia32.o$:
		|| m:arch/ia64/kernel/gate-syms.o$:
		|| m:arch/ia64/lib/__divdi3.o$:
		|| m:arch/ia64/lib/__divsi3.o$:
		|| m:arch/ia64/lib/__moddi3.o$:
		|| m:arch/ia64/lib/__modsi3.o$:
		|| m:arch/ia64/lib/__udivdi3.o$:
		|| m:arch/ia64/lib/__udivsi3.o$:
		|| m:arch/ia64/lib/__umoddi3.o$:
		|| m:arch/ia64/lib/__umodsi3.o$:
		|| m:arch/ia64/scripts/check_gas_for_hint.o$:
		|| m:arch/ia64/sn/kernel/xp.o$:
		|| m:boot/bbootsect.o$:
		|| m:boot/bsetup.o$:
		|| m:/bootsect.o$:
		|| m:/boot/setup.o$:
		|| m:/compressed/:
		|| m:drivers/cdrom/driver.o$:
		|| m:drivers/char/drm/tdfx_drv.o$:
		|| m:drivers/ide/ide-detect.o$:
		|| m:drivers/ide/pci/idedriver-pci.o$:
		|| m:drivers/media/media.o$:
		|| m:drivers/scsi/sd_mod.o$:
		|| m:drivers/video/video.o$:
		|| m:fs/devpts/devpts.o$:
		|| m:fs/exportfs/exportfs.o$:
		|| m:fs/hugetlbfs/hugetlbfs.o$:
		|| m:fs/msdos/msdos.o$:
		|| m:fs/nls/nls.o$:
		|| m:fs/ramfs/ramfs.o$:
		|| m:fs/romfs/romfs.o$:
		|| m:fs/vfat/vfat.o$:
		|| m:init/mounts.o$:
		|| m:^modules/:
		|| m:net/netlink/netlink.o$:
		|| m:net/sched/sched.o$:
		|| m:/piggy.o$:
		|| m:^scripts/:
		|| m:sound/.*/snd-:
		|| m:^.*/\.tmp_:
		|| m:^\.tmp_:
		|| m:/vmlinux-obj.o$:
		|| m:^tools/:
		)
	) {
		do_nm($basename, $_);
	}
	$_ = $basename;		# File::Find expects $_ untouched (undocumented)
}

sub do_nm
{
	my ($basename, $fullname) = @_;
	my ($source, $type, $name);
	if (! -e $basename) {
		printf STDERR "$basename does not exist\n";
		return;
	}
	if ($fullname !~ /\.o$/) {
		printf STDERR "$fullname is not an object file\n";
		return;
	}
	($source = $basename) =~ s/\.o$//;
	if (-e "$source.c" || -e "$source.S") {
		$source = File::Spec-&gt;catfile($objtree, $File::Find::dir, $source)
	} else {
		$source = File::Spec-&gt;catfile($srctree, $File::Find::dir, $source)
	}
	if (! -e "$source.c" &amp;&amp; ! -e "$source.S") {
		# No obvious source, exclude the object if it is conglomerate
	        open(my $objdumpdata, "$objdump $basename|")
		    or die "$objdump $fullname failed $!\n";

		my $comment;
		while (&lt;$objdumpdata&gt;) {
			chomp();
			if (/^In archive/) {
				# Archives are always conglomerate
				$comment = "GCC:GCC:";
				last;
			}
			next if (! /^[ 0-9a-f]{5,} /);
			$comment .= substr($_, 43);
		}
		close($objdumpdata);

		if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
			printf STDERR "No source file found for $fullname\n";
		}
		return;
	}
	open (my $nmdata, "$nm $basename|")
	    or die "$nm $fullname failed $!\n";

	my @nmdata;
	while (&lt;$nmdata&gt;) {
		chop;
		($type, $name) = (split(/ +/, $_, 3))[1..2];
		# Expected types
		# A absolute symbol
		# B weak external reference to data that has been resolved
		# C global variable, uninitialised
		# D global variable, initialised
		# G global variable, initialised, small data section
		# R global array, initialised
		# S global variable, uninitialised, small bss
		# T global label/procedure
		# U external reference
		# W weak external reference to text that has been resolved
		# V similar to W, but the value of the weak symbol becomes zero with no error.
		# a assembler equate
		# b static variable, uninitialised
		# d static variable, initialised
		# g static variable, initialised, small data section
		# r static array, initialised
		# s static variable, uninitialised, small bss
		# t static label/procedures
		# w weak external reference to text that has not been resolved
		# v similar to w
		# ? undefined type, used a lot by modules
		if ($type !~ /^[ABCDGRSTUWVabdgrstwv?]$/) {
			printf STDERR "nm output for $fullname contains unknown type '$_'\n";
		}
		elsif ($name =~ /\./) {
			# name with '.' is local static
		}
		else {
			$type = 'R' if ($type eq '?');	# binutils replaced ? with R at one point
			# binutils keeps changing the type for exported symbols, force it to R
			$type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
			$name =~ s/_R[a-f0-9]{8}$//;	# module versions adds this
			if ($type =~ /[ABCDGRSTWV]/ &amp;&amp;
				$name ne 'init_module' &amp;&amp;
				$name ne 'cleanup_module' &amp;&amp;
				$name ne 'Using_Versions' &amp;&amp;
				$name !~ /^Version_[0-9]+$/ &amp;&amp;
				$name !~ /^__parm_/ &amp;&amp;
				$name !~ /^__kstrtab/ &amp;&amp;
				$name !~ /^__ksymtab/ &amp;&amp;
				$name !~ /^__kcrctab_/ &amp;&amp;
				$name !~ /^__exitcall_/ &amp;&amp;
				$name !~ /^__initcall_/ &amp;&amp;
				$name !~ /^__kdb_initcall_/ &amp;&amp;
				$name !~ /^__kdb_exitcall_/ &amp;&amp;
				$name !~ /^__module_/ &amp;&amp;
				$name !~ /^__mod_/ &amp;&amp;
				$name !~ /^__crc_/ &amp;&amp;
				$name ne '__this_module' &amp;&amp;
				$name ne 'kernel_version') {
				if (!exists($def{$name})) {
					$def{$name} = [];
				}
				push(@{$def{$name}}, $fullname);
			}
			push(@nmdata, "$type $name");
			if ($name =~ /^__ksymtab_/) {
				$name = substr($name, 10);
				if (!exists($ksymtab{$name})) {
					$ksymtab{$name} = [];
				}
				push(@{$ksymtab{$name}}, $fullname);
			}
		}
	}
	close($nmdata);

	if ($#nmdata &lt; 0) {
	    printf "No nm data for $fullname\n"
		unless $nmexception{$fullname};
	    return;
	}
	$nmdata{$fullname} = \@nmdata;
}

sub drop_def
{
	my ($object, $name) = @_;
	my $nmdata = $nmdata{$object};
	my ($i, $j);
	for ($i = 0; $i &lt;= $#{$nmdata}; ++$i) {
		if ($name eq (split(' ', $nmdata-&gt;[$i], 2))[1]) {
			splice(@{$nmdata{$object}}, $i, 1);
			my $def = $def{$name};
			for ($j = 0; $j &lt; $#{$def{$name}}; ++$j) {
				if ($def{$name}[$j] eq $object) {
					splice(@{$def{$name}}, $j, 1);
				}
			}
			last;
		}
	}
}

sub list_multiply_defined
{
	foreach my $name (keys(%def)) {
		if ($#{$def{$name}} &gt; 0) {
			# Special case for cond_syscall
			if ($#{$def{$name}} == 1 &amp;&amp;
			   ($name =~ /^sys_/ || $name =~ /^compat_sys_/ ||
			    $name =~ /^sys32_/)) {
				if($def{$name}[0] eq "kernel/sys_ni.o" ||
				   $def{$name}[1] eq "kernel/sys_ni.o") {
					&amp;drop_def("kernel/sys_ni.o", $name);
					next;
				}
			}

			printf "$name is multiply defined in :-\n";
			foreach my $module (@{$def{$name}}) {
				printf "\t$module\n";
			}
		}
	}
}

sub resolve_external_references
{
	my ($kstrtab, $ksymtab, $export);

	printf "\n";
	foreach my $object (keys(%nmdata)) {
		my $nmdata = $nmdata{$object};
		for (my $i = 0; $i &lt;= $#{$nmdata}; ++$i) {
			my ($type, $name) = split(' ', $nmdata-&gt;[$i], 2);
			if ($type eq "U" || $type eq "w") {
				if (exists($def{$name}) || exists($ksymtab{$name})) {
					# add the owning object to the nmdata
					$nmdata-&gt;[$i] = "$type $name $object";
					# only count as a reference if it is not EXPORT_...
					$kstrtab = "R __kstrtab_$name";
					$ksymtab = "R __ksymtab_$name";
					$export = 0;
					for (my $j = 0; $j &lt;= $#{$nmdata}; ++$j) {
						if ($nmdata-&gt;[$j] eq $kstrtab ||
						    $nmdata-&gt;[$j] eq $ksymtab) {
							$export = 1;
							last;
						}
					}
					if ($export) {
						$export{$name} = "";
					}
					else {
						$ref{$name} = ""
					}
				}
				elsif ( ! $nameexception{$name}
					&amp;&amp; $name !~ /^__sched_text_/
					&amp;&amp; $name !~ /^__start_/
					&amp;&amp; $name !~ /^__end_/
					&amp;&amp; $name !~ /^__stop_/
					&amp;&amp; $name !~ /^__scheduling_functions_.*_here/
					&amp;&amp; $name !~ /^__.*initcall_/
					&amp;&amp; $name !~ /^__.*per_cpu_start/
					&amp;&amp; $name !~ /^__.*per_cpu_end/
					&amp;&amp; $name !~ /^__alt_instructions/
					&amp;&amp; $name !~ /^__setup_/
					&amp;&amp; $name !~ /^__mod_timer/
					&amp;&amp; $name !~ /^__mod_page_state/
					&amp;&amp; $name !~ /^init_module/
					&amp;&amp; $name !~ /^cleanup_module/
				) {
					printf "Cannot resolve ";
					printf "weak " if ($type eq "w");
					printf "reference to $name from $object\n";
				}
			}
		}
	}
}

sub list_extra_externals
{
	my %noref = ();

	foreach my $name (keys(%def)) {
		if (! exists($ref{$name})) {
			my @module = @{$def{$name}};
			foreach my $module (@module) {
				if (! exists($noref{$module})) {
					$noref{$module} = [];
				}
				push(@{$noref{$module}}, $name);
			}
		}
	}
	if (%noref) {
		printf "\nExternally defined symbols with no external references\n";
		foreach my $module (sort(keys(%noref))) {
			printf "  $module\n";
			foreach (sort(@{$noref{$module}})) {
			    my $export;
			    if (exists($export{$_})) {
				$export = " (export only)";
			    } else {
				$export = "";
			    }
			    printf "    $_$export\n";
			}
		}
	}
}
</pre></body></html>