3
0

depmod.pl 7.7 KB

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