depmod.pl 7.6 KB

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