namespace.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. require 5; # at least perl 5
  65. use strict;
  66. use File::Find;
  67. my $nm = ($ENV{'NM'} || "nm") . " -p";
  68. my $objdump = ($ENV{'OBJDUMP'} || "objdump") . " -s -j .comment";
  69. my $srctree = "";
  70. my $objtree = "";
  71. $srctree = "$ENV{'srctree'}/" if (exists($ENV{'srctree'}));
  72. $objtree = "$ENV{'objtree'}/" if (exists($ENV{'objtree'}));
  73. if ($#ARGV != -1) {
  74. print STDERR "usage: $0 takes no parameters\n";
  75. die("giving up\n");
  76. }
  77. my %nmdata = (); # nm data for each object
  78. my %def = (); # all definitions for each name
  79. my %ksymtab = (); # names that appear in __ksymtab_
  80. my %ref = (); # $ref{$name} exists if there is a true external reference to $name
  81. my %export = (); # $export{$name} exists if there is an EXPORT_... of $name
  82. &find(\&linux_objects, '.'); # find the objects and do_nm on them
  83. &list_multiply_defined();
  84. &resolve_external_references();
  85. &list_extra_externals();
  86. exit(0);
  87. sub linux_objects
  88. {
  89. # Select objects, ignoring objects which are only created by
  90. # merging other objects. Also ignore all of modules, scripts
  91. # and compressed. Most conglomerate objects are handled by do_nm,
  92. # this list only contains the special cases. These include objects
  93. # that are linked from just one other object and objects for which
  94. # there is really no permanent source file.
  95. my $basename = $_;
  96. $_ = $File::Find::name;
  97. s:^\./::;
  98. if (/.*\.o$/ &&
  99. ! (
  100. m:/built-in.o$:
  101. || m:arch/i386/kernel/vsyscall-syms.o$:
  102. || m:arch/ia64/ia32/ia32.o$:
  103. || m:arch/ia64/kernel/gate-syms.o$:
  104. || m:arch/ia64/lib/__divdi3.o$:
  105. || m:arch/ia64/lib/__divsi3.o$:
  106. || m:arch/ia64/lib/__moddi3.o$:
  107. || m:arch/ia64/lib/__modsi3.o$:
  108. || m:arch/ia64/lib/__udivdi3.o$:
  109. || m:arch/ia64/lib/__udivsi3.o$:
  110. || m:arch/ia64/lib/__umoddi3.o$:
  111. || m:arch/ia64/lib/__umodsi3.o$:
  112. || m:arch/ia64/scripts/check_gas_for_hint.o$:
  113. || m:arch/ia64/sn/kernel/xp.o$:
  114. || m:boot/bbootsect.o$:
  115. || m:boot/bsetup.o$:
  116. || m:/bootsect.o$:
  117. || m:/boot/setup.o$:
  118. || m:/compressed/:
  119. || m:drivers/cdrom/driver.o$:
  120. || m:drivers/char/drm/tdfx_drv.o$:
  121. || m:drivers/ide/ide-detect.o$:
  122. || m:drivers/ide/pci/idedriver-pci.o$:
  123. || m:drivers/media/media.o$:
  124. || m:drivers/scsi/sd_mod.o$:
  125. || m:drivers/video/video.o$:
  126. || m:fs/devpts/devpts.o$:
  127. || m:fs/exportfs/exportfs.o$:
  128. || m:fs/hugetlbfs/hugetlbfs.o$:
  129. || m:fs/msdos/msdos.o$:
  130. || m:fs/nls/nls.o$:
  131. || m:fs/ramfs/ramfs.o$:
  132. || m:fs/romfs/romfs.o$:
  133. || m:fs/vfat/vfat.o$:
  134. || m:init/mounts.o$:
  135. || m:^modules/:
  136. || m:net/netlink/netlink.o$:
  137. || m:net/sched/sched.o$:
  138. || m:/piggy.o$:
  139. || m:^scripts/:
  140. || m:sound/.*/snd-:
  141. || m:^.*/\.tmp_:
  142. || m:^\.tmp_:
  143. || m:/vmlinux-obj.o$:
  144. )
  145. ) {
  146. do_nm($basename, $_);
  147. }
  148. $_ = $basename; # File::Find expects $_ untouched (undocumented)
  149. }
  150. sub do_nm
  151. {
  152. my ($basename, $fullname) = @_;
  153. my ($source, $type, $name);
  154. if (! -e $basename) {
  155. printf STDERR "$basename does not exist\n";
  156. return;
  157. }
  158. if ($fullname !~ /\.o$/) {
  159. printf STDERR "$fullname is not an object file\n";
  160. return;
  161. }
  162. ($source = $fullname) =~ s/\.o$//;
  163. if (-e "$objtree$source.c" || -e "$objtree$source.S") {
  164. $source = "$objtree$source";
  165. } else {
  166. $source = "$srctree$source";
  167. }
  168. if (! -e "$source.c" && ! -e "$source.S") {
  169. # No obvious source, exclude the object if it is conglomerate
  170. if (! open(OBJDUMPDATA, "$objdump $basename|")) {
  171. printf STDERR "$objdump $fullname failed $!\n";
  172. return;
  173. }
  174. my $comment;
  175. while (<OBJDUMPDATA>) {
  176. chomp();
  177. if (/^In archive/) {
  178. # Archives are always conglomerate
  179. $comment = "GCC:GCC:";
  180. last;
  181. }
  182. next if (! /^[ 0-9a-f]{5,} /);
  183. $comment .= substr($_, 43);
  184. }
  185. close(OBJDUMPDATA);
  186. if (!defined($comment) || $comment !~ /GCC\:.*GCC\:/m) {
  187. printf STDERR "No source file found for $fullname\n";
  188. }
  189. return;
  190. }
  191. if (! open(NMDATA, "$nm $basename|")) {
  192. printf STDERR "$nm $fullname failed $!\n";
  193. return;
  194. }
  195. my @nmdata;
  196. while (<NMDATA>) {
  197. chop;
  198. ($type, $name) = (split(/ +/, $_, 3))[1..2];
  199. # Expected types
  200. # A absolute symbol
  201. # B weak external reference to data that has been resolved
  202. # C global variable, uninitialised
  203. # D global variable, initialised
  204. # G global variable, initialised, small data section
  205. # R global array, initialised
  206. # S global variable, uninitialised, small bss
  207. # T global label/procedure
  208. # U external reference
  209. # W weak external reference to text that has been resolved
  210. # a assembler equate
  211. # b static variable, uninitialised
  212. # d static variable, initialised
  213. # g static variable, initialised, small data section
  214. # r static array, initialised
  215. # s static variable, uninitialised, small bss
  216. # t static label/procedures
  217. # w weak external reference to text that has not been resolved
  218. # ? undefined type, used a lot by modules
  219. if ($type !~ /^[ABCDGRSTUWabdgrstw?]$/) {
  220. printf STDERR "nm output for $fullname contains unknown type '$_'\n";
  221. }
  222. elsif ($name =~ /\./) {
  223. # name with '.' is local static
  224. }
  225. else {
  226. $type = 'R' if ($type eq '?'); # binutils replaced ? with R at one point
  227. # binutils keeps changing the type for exported symbols, force it to R
  228. $type = 'R' if ($name =~ /^__ksymtab/ || $name =~ /^__kstrtab/);
  229. $name =~ s/_R[a-f0-9]{8}$//; # module versions adds this
  230. if ($type =~ /[ABCDGRSTW]/ &&
  231. $name ne 'init_module' &&
  232. $name ne 'cleanup_module' &&
  233. $name ne 'Using_Versions' &&
  234. $name !~ /^Version_[0-9]+$/ &&
  235. $name !~ /^__parm_/ &&
  236. $name !~ /^__kstrtab/ &&
  237. $name !~ /^__ksymtab/ &&
  238. $name !~ /^__kcrctab_/ &&
  239. $name !~ /^__exitcall_/ &&
  240. $name !~ /^__initcall_/ &&
  241. $name !~ /^__kdb_initcall_/ &&
  242. $name !~ /^__kdb_exitcall_/ &&
  243. $name !~ /^__module_/ &&
  244. $name !~ /^__mod_/ &&
  245. $name !~ /^__crc_/ &&
  246. $name ne '__this_module' &&
  247. $name ne 'kernel_version') {
  248. if (!exists($def{$name})) {
  249. $def{$name} = [];
  250. }
  251. push(@{$def{$name}}, $fullname);
  252. }
  253. push(@nmdata, "$type $name");
  254. if ($name =~ /^__ksymtab_/) {
  255. $name = substr($name, 10);
  256. if (!exists($ksymtab{$name})) {
  257. $ksymtab{$name} = [];
  258. }
  259. push(@{$ksymtab{$name}}, $fullname);
  260. }
  261. }
  262. }
  263. close(NMDATA);
  264. if ($#nmdata < 0) {
  265. if (
  266. $fullname ne "lib/brlock.o"
  267. && $fullname ne "lib/dec_and_lock.o"
  268. && $fullname ne "fs/xfs/xfs_macros.o"
  269. && $fullname ne "drivers/ide/ide-probe-mini.o"
  270. && $fullname ne "usr/initramfs_data.o"
  271. && $fullname ne "drivers/acpi/executer/exdump.o"
  272. && $fullname ne "drivers/acpi/resources/rsdump.o"
  273. && $fullname ne "drivers/acpi/namespace/nsdumpdv.o"
  274. && $fullname ne "drivers/acpi/namespace/nsdump.o"
  275. && $fullname ne "arch/ia64/sn/kernel/sn2/io.o"
  276. && $fullname ne "arch/ia64/kernel/gate-data.o"
  277. && $fullname ne "drivers/ieee1394/oui.o"
  278. && $fullname ne "security/capability.o"
  279. && $fullname ne "sound/core/wrappers.o"
  280. && $fullname ne "fs/ntfs/sysctl.o"
  281. && $fullname ne "fs/jfs/jfs_debug.o"
  282. ) {
  283. printf "No nm data for $fullname\n";
  284. }
  285. return;
  286. }
  287. $nmdata{$fullname} = \@nmdata;
  288. }
  289. sub drop_def
  290. {
  291. my ($object, $name) = @_;
  292. my $nmdata = $nmdata{$object};
  293. my ($i, $j);
  294. for ($i = 0; $i <= $#{$nmdata}; ++$i) {
  295. if ($name eq (split(' ', $nmdata->[$i], 2))[1]) {
  296. splice(@{$nmdata{$object}}, $i, 1);
  297. my $def = $def{$name};
  298. for ($j = 0; $j < $#{$def{$name}}; ++$j) {
  299. if ($def{$name}[$j] eq $object) {
  300. splice(@{$def{$name}}, $j, 1);
  301. }
  302. }
  303. last;
  304. }
  305. }
  306. }
  307. sub list_multiply_defined
  308. {
  309. my ($name, $module);
  310. foreach $name (keys(%def)) {
  311. if ($#{$def{$name}} > 0) {
  312. # Special case for cond_syscall
  313. if ($#{$def{$name}} == 1 && $name =~ /^sys_/ &&
  314. ($def{$name}[0] eq "kernel/sys.o" ||
  315. $def{$name}[1] eq "kernel/sys.o")) {
  316. &drop_def("kernel/sys.o", $name);
  317. next;
  318. }
  319. # Special case for i386 entry code
  320. if ($#{$def{$name}} == 1 && $name =~ /^__kernel_/ &&
  321. $def{$name}[0] eq "arch/i386/kernel/vsyscall-int80.o" &&
  322. $def{$name}[1] eq "arch/i386/kernel/vsyscall-sysenter.o") {
  323. &drop_def("arch/i386/kernel/vsyscall-sysenter.o", $name);
  324. next;
  325. }
  326. printf "$name is multiply defined in :-\n";
  327. foreach $module (@{$def{$name}}) {
  328. printf "\t$module\n";
  329. }
  330. }
  331. }
  332. }
  333. sub resolve_external_references
  334. {
  335. my ($object, $type, $name, $i, $j, $kstrtab, $ksymtab, $export);
  336. printf "\n";
  337. foreach $object (keys(%nmdata)) {
  338. my $nmdata = $nmdata{$object};
  339. for ($i = 0; $i <= $#{$nmdata}; ++$i) {
  340. ($type, $name) = split(' ', $nmdata->[$i], 2);
  341. if ($type eq "U" || $type eq "w") {
  342. if (exists($def{$name}) || exists($ksymtab{$name})) {
  343. # add the owning object to the nmdata
  344. $nmdata->[$i] = "$type $name $object";
  345. # only count as a reference if it is not EXPORT_...
  346. $kstrtab = "R __kstrtab_$name";
  347. $ksymtab = "R __ksymtab_$name";
  348. $export = 0;
  349. for ($j = 0; $j <= $#{$nmdata}; ++$j) {
  350. if ($nmdata->[$j] eq $kstrtab ||
  351. $nmdata->[$j] eq $ksymtab) {
  352. $export = 1;
  353. last;
  354. }
  355. }
  356. if ($export) {
  357. $export{$name} = "";
  358. }
  359. else {
  360. $ref{$name} = ""
  361. }
  362. }
  363. elsif ( $name ne "mod_use_count_"
  364. && $name ne "__initramfs_end"
  365. && $name ne "__initramfs_start"
  366. && $name ne "_einittext"
  367. && $name ne "_sinittext"
  368. && $name ne "kallsyms_names"
  369. && $name ne "kallsyms_num_syms"
  370. && $name ne "kallsyms_addresses"
  371. && $name ne "__this_module"
  372. && $name ne "_etext"
  373. && $name ne "_edata"
  374. && $name ne "_end"
  375. && $name ne "__bss_start"
  376. && $name ne "_text"
  377. && $name ne "_stext"
  378. && $name ne "__gp"
  379. && $name ne "ia64_unw_start"
  380. && $name ne "ia64_unw_end"
  381. && $name ne "__init_begin"
  382. && $name ne "__init_end"
  383. && $name ne "__bss_stop"
  384. && $name ne "__nosave_begin"
  385. && $name ne "__nosave_end"
  386. && $name ne "pg0"
  387. && $name ne "__module_text_address"
  388. && $name !~ /^__sched_text_/
  389. && $name !~ /^__start_/
  390. && $name !~ /^__end_/
  391. && $name !~ /^__stop_/
  392. && $name !~ /^__scheduling_functions_.*_here/
  393. && $name !~ /^__.*initcall_/
  394. && $name !~ /^__.*per_cpu_start/
  395. && $name !~ /^__.*per_cpu_end/
  396. && $name !~ /^__alt_instructions/
  397. && $name !~ /^__setup_/
  398. && $name !~ /^jiffies/
  399. && $name !~ /^__mod_timer/
  400. && $name !~ /^__mod_page_state/
  401. && $name !~ /^init_module/
  402. && $name !~ /^cleanup_module/
  403. ) {
  404. printf "Cannot resolve ";
  405. printf "weak " if ($type eq "w");
  406. printf "reference to $name from $object\n";
  407. }
  408. }
  409. }
  410. }
  411. }
  412. sub list_extra_externals
  413. {
  414. my %noref = ();
  415. my ($name, @module, $module, $export);
  416. foreach $name (keys(%def)) {
  417. if (! exists($ref{$name})) {
  418. @module = @{$def{$name}};
  419. foreach $module (@module) {
  420. if (! exists($noref{$module})) {
  421. $noref{$module} = [];
  422. }
  423. push(@{$noref{$module}}, $name);
  424. }
  425. }
  426. }
  427. if (%noref) {
  428. printf "\nExternally defined symbols with no external references\n";
  429. foreach $module (sort(keys(%noref))) {
  430. printf " $module\n";
  431. foreach (sort(@{$noref{$module}})) {
  432. if (exists($export{$_})) {
  433. $export = " (export only)";
  434. }
  435. else {
  436. $export = "";
  437. }
  438. printf " $_$export\n";
  439. }
  440. }
  441. }
  442. }