depmod.pl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/perl -w
  2. # vi: set sw=4 ts=4:
  3. # Copyright (c) 2001 David Schleef <ds@schleef.org>
  4. # Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
  5. # Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
  6. # Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
  7. # Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
  8. #
  9. # History:
  10. # March 2006: Stuart Hughes <stuarth@freescale.com>.
  11. # Significant updates, including implementing the '-F' option
  12. # and adding support for 2.6 kernels.
  13. # This program is free software; you can redistribute it and/or modify it
  14. # under the same terms as Perl itself.
  15. use Getopt::Long;
  16. use File::Find;
  17. use strict;
  18. # Set up some default values
  19. my $kdir="";
  20. my $basedir="";
  21. my $kernel="";
  22. my $kernelsyms="";
  23. my $symprefix="";
  24. my $stdout=0;
  25. my $verbose=0;
  26. my $help=0;
  27. my $nm = $ENV{'NM'} || "nm";
  28. # more globals
  29. my (@liblist) = ();
  30. my $exp = {};
  31. my $dep = {};
  32. my $mod = {};
  33. my $usage = <<TXT;
  34. $0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
  35. Where:
  36. -h --help : Show this help screen
  37. -b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
  38. -k --kernel : Kernel binary for the target (e.g. vmlinux)
  39. -F --kernelsyms : Kernel symbol file (e.g. System.map)
  40. -n --stdout : Write to stdout instead of <basedir>/modules.dep
  41. -v --verbose : Print out lots of debugging stuff
  42. -P --symbol-prefix : Symbol prefix
  43. TXT
  44. # get command-line options
  45. GetOptions(
  46. "help|h" => \$help,
  47. "basedir|b=s" => \$basedir,
  48. "kernel|k=s" => \$kernel,
  49. "kernelsyms|F=s" => \$kernelsyms,
  50. "stdout|n" => \$stdout,
  51. "verbose|v" => \$verbose,
  52. "symbol-prefix|P=s" => \$symprefix,
  53. );
  54. die $usage if $help;
  55. die $usage unless $basedir && ( $kernel || $kernelsyms );
  56. die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
  57. # Strip any trailing or multiple slashes from basedir
  58. $basedir =~ s-(/)\1+-/-g;
  59. # The base directory should contain /lib/modules somewhere
  60. if($basedir !~ m-/lib/modules-) {
  61. warn "WARNING: base directory does not match ..../lib/modules\n";
  62. }
  63. # if no kernel version is contained in the basedir, try to find one
  64. if($basedir !~ m-/lib/modules/\d\.\d-) {
  65. opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
  66. foreach ( readdir(BD) ) {
  67. next if /^\.\.?$/;
  68. next unless -d "$basedir/$_";
  69. warn "dir = $_\n" if $verbose;
  70. if( /^\d\.\d/ ) {
  71. $kdir = $_;
  72. warn("Guessed module directory as $basedir/$kdir\n");
  73. last;
  74. }
  75. }
  76. closedir(BD);
  77. die "Cannot find a kernel version under $basedir\n" unless $kdir;
  78. $basedir = "$basedir/$kdir";
  79. }
  80. # Find the list of .o or .ko files living under $basedir
  81. warn "**** Locating all modules\n" if $verbose;
  82. find sub {
  83. my $file;
  84. if ( -f $_ && ! -d $_ ) {
  85. $file = $File::Find::name;
  86. if ( $file =~ /\.k?o$/ ) {
  87. push(@liblist, $file);
  88. warn "$file\n" if $verbose;
  89. }
  90. }
  91. }, $basedir;
  92. warn "**** Finished locating modules\n" if $verbose;
  93. foreach my $obj ( @liblist ){
  94. # turn the input file name into a target tag name
  95. my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
  96. warn "\nMODULE = $tgtname\n" if $verbose;
  97. # get a list of symbols
  98. my @output=`$nm $obj`;
  99. build_ref_tables($tgtname, \@output, $exp, $dep);
  100. }
  101. # vmlinux is a special name that is only used to resolve symbols
  102. my $tgtname = 'vmlinux';
  103. my @output = $kernelsyms ? `cat $kernelsyms` : `$nm $kernel`;
  104. warn "\nMODULE = $tgtname\n" if $verbose;
  105. build_ref_tables($tgtname, \@output, $exp, $dep);
  106. # resolve the dependencies for each module
  107. # reduce dependencies: remove unresolvable and resolved from vmlinux/System.map
  108. # remove duplicates
  109. foreach my $module (keys %$dep) {
  110. warn "reducing module: $module\n" if $verbose;
  111. $mod->{$module} = {};
  112. foreach (@{$dep->{$module}}) {
  113. if( $exp->{$_} ) {
  114. warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
  115. next if $exp->{$_} =~ /vmlinux/;
  116. $mod->{$module}{$exp->{$_}} = 1;
  117. } else {
  118. warn "unresolved symbol $_ in file $module\n";
  119. }
  120. }
  121. }
  122. # figure out where the output should go
  123. if ($stdout == 0) {
  124. open(STDOUT, ">$basedir/modules.dep")
  125. or die "cannot open $basedir/modules.dep: $!";
  126. }
  127. my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
  128. foreach my $module ( keys %$mod ) {
  129. if($kseries eq '2.4') {
  130. print "$module:\t";
  131. my @sorted = sort bydep keys %{$mod->{$module}};
  132. print join(" \\\n\t",@sorted);
  133. print "\n\n";
  134. } else {
  135. print "$module: ";
  136. my @sorted = sort bydep keys %{$mod->{$module}};
  137. print join(" ",@sorted);
  138. print "\n";
  139. }
  140. }
  141. sub build_ref_tables
  142. {
  143. my ($name, $sym_ar, $exp, $dep) = @_;
  144. my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
  145. # gather the exported symbols
  146. if($ksymtab){
  147. # explicitly exported
  148. foreach ( @$sym_ar ) {
  149. / __ksymtab_(.*)$/ and do {
  150. warn "sym = $1\n" if $verbose;
  151. $exp->{$1} = $name;
  152. };
  153. }
  154. } else {
  155. # exporting all symbols
  156. foreach ( @$sym_ar ) {
  157. / [ABCDGRSTW] (.*)$/ and do {
  158. warn "syma = $1\n" if $verbose;
  159. $exp->{$1} = $name;
  160. };
  161. }
  162. }
  163. # this takes makes sure modules with no dependencies get listed
  164. push @{$dep->{$name}}, $symprefix . 'printk' unless $name eq 'vmlinux';
  165. # gather the unresolved symbols
  166. foreach ( @$sym_ar ) {
  167. !/ __this_module/ && / U (.*)$/ and do {
  168. warn "und = $1\n" if $verbose;
  169. push @{$dep->{$name}}, $1;
  170. };
  171. }
  172. }
  173. sub bydep
  174. {
  175. foreach my $f ( keys %{$mod->{$b}} ) {
  176. if($f eq $a) {
  177. return 1;
  178. }
  179. }
  180. return -1;
  181. }
  182. __END__
  183. =head1 NAME
  184. depmod.pl - a cross platform script to generate kernel module
  185. dependency lists (modules.conf) which can then be used by modprobe
  186. on the target platform.
  187. It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
  188. =head1 SYNOPSIS
  189. depmod.pl [OPTION]... [basedir]...
  190. Example:
  191. depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
  192. =head1 DESCRIPTION
  193. The purpose of this script is to automagically generate a list of of kernel
  194. module dependencies. This script produces dependency lists that should be
  195. identical to the depmod program from the modutils package. Unlike the depmod
  196. binary, however, depmod.pl is designed to be run on your host system, not
  197. on your target system.
  198. This script was written by David Schleef <ds@schleef.org> to be used in
  199. conjunction with the BusyBox modprobe applet.
  200. =head1 OPTIONS
  201. =over 4
  202. =item B<-h --help>
  203. This displays the help message.
  204. =item B<-b --basedir>
  205. The base directory uner which the target's modules will be found. This
  206. defaults to the /lib/modules directory.
  207. If you don't specify the kernel version, this script will search for
  208. one under the specified based directory and use the first thing that
  209. looks like a kernel version.
  210. =item B<-k --kernel>
  211. Kernel binary for the target (vmlinux). You must either supply a kernel binary
  212. or a kernel symbol file (using the -F option).
  213. =item B<-F --kernelsyms>
  214. Kernel symbol file for the target (System.map).
  215. =item B<-n --stdout>
  216. Write to stdout instead of modules.dep
  217. kernel binary for the target (using the -k option).
  218. =item B<--verbose>
  219. Verbose (debug) output
  220. =back
  221. =head1 COPYRIGHT AND LICENSE
  222. Copyright (c) 2001 David Schleef <ds@schleef.org>
  223. Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
  224. Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
  225. Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
  226. Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
  227. This program is free software; you can redistribute it and/or modify it
  228. under the same terms as Perl itself.
  229. =head1 AUTHOR
  230. David Schleef <ds@schleef.org>
  231. =cut