depmod.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <stuarth@lineo.com>
  6. # Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the same terms as Perl itself.
  9. # TODO -- use strict mode...
  10. #use strict;
  11. use Getopt::Long;
  12. use File::Find;
  13. # Set up some default values
  14. my $basedir="";
  15. my $kernel;
  16. my $kernelsyms;
  17. my $stdout=0;
  18. my $verbose=0;
  19. # get command-line options
  20. my %opt;
  21. GetOptions(
  22. \%opt,
  23. "help|h",
  24. "basedir|b=s" => \$basedir,
  25. "kernel|k=s" => \$kernel,
  26. "kernelsyms|F=s" => \$kernelsyms,
  27. "stdout|n" => \$stdout,
  28. "verbose|v" => \$verbose,
  29. );
  30. if (defined $opt{help}) {
  31. print
  32. " $0 [OPTION]... [basedir]\n",
  33. " -h --help\t\tShow this help screen\n",
  34. " -b --basedir\tModules base directory (defaults to /lib/modules)\n",
  35. " -k --kernel\tKernel binary for the target\n",
  36. " -F --kernelsyms\tKernel symbol file\n",
  37. " -n --stdout\tWrite to stdout instead of <basedir>/modules.dep\n",
  38. " -v --verbose\tPrint out lots of debugging stuff\n",
  39. ;
  40. exit 1;
  41. }
  42. if($basedir !~ m-/lib/modules-) {
  43. warn "WARNING: base directory does not match ..../lib/modules\n";
  44. }
  45. # Find the list of .o files living under $basedir
  46. #if ($verbose) { printf "Locating all modules\n"; }
  47. my($ofile) = "";
  48. my($file) = "";
  49. my(@liblist) = ();
  50. find sub {
  51. if ( -f $_ && ! -d $_ ) {
  52. $file = $File::Find::name;
  53. if ( $file =~ /.o$/ ) {
  54. push(@liblist, $file);
  55. if ($verbose) { printf "$file\n"; }
  56. }
  57. }
  58. }, $basedir;
  59. if ($verbose) { printf "Finished locating modules\n"; }
  60. foreach $obj ( @liblist, $kernel ){
  61. # turn the input file name into a target tag name
  62. # vmlinux is a special that is only used to resolve symbols
  63. if($obj =~ /vmlinux/) {
  64. $tgtname = "vmlinux";
  65. } else {
  66. ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
  67. }
  68. warn "MODULE = $tgtname\n" if $verbose;
  69. # get a list of symbols
  70. @output=`nm $obj`;
  71. $ksymtab=grep m/ __ksymtab/, @output;
  72. # gather the exported symbols
  73. if($ksymtab){
  74. # explicitly exported
  75. foreach ( @output ) {
  76. / __ksymtab_(.*)$/ and do {
  77. warn "sym = $1\n" if $verbose;
  78. $exp->{$1} = $tgtname;
  79. };
  80. }
  81. } else {
  82. # exporting all symbols
  83. foreach ( @output) {
  84. / [ABCDGRST] (.*)$/ and do {
  85. warn "syma = $1\n" if $verbose;
  86. $exp->{$1} = $tgtname;
  87. };
  88. }
  89. }
  90. # gather the unresolved symbols
  91. foreach ( @output ) {
  92. !/ __this_module/ && / U (.*)$/ and do {
  93. warn "und = $1\n" if $verbose;
  94. push @{$dep->{$tgtname}}, $1;
  95. };
  96. }
  97. }
  98. # reduce dependancies: remove unresolvable and resolved from vmlinux
  99. # remove duplicates
  100. foreach $module (keys %$dep) {
  101. $mod->{$module} = {};
  102. foreach (@{$dep->{$module}}) {
  103. if( $exp->{$_} ) {
  104. warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
  105. next if $exp->{$_} =~ /vmlinux/;
  106. $mod->{$module}{$exp->{$_}} = 1;
  107. } else {
  108. warn "unresolved symbol $_ in file $module\n";
  109. }
  110. }
  111. }
  112. # resolve the dependancies for each module
  113. if ($stdout == 1) {
  114. foreach $module ( keys %$mod ) {
  115. print "$module:\t";
  116. @sorted = sort bydep keys %{$mod->{$module}};
  117. print join(" \\\n\t",@sorted);
  118. print "\n\n";
  119. }
  120. } else {
  121. open(OFILE, ">$basedir/modules.dep");
  122. foreach $module ( keys %$mod ) {
  123. print OFILE "$module:\t";
  124. @sorted = sort bydep keys %{$mod->{$module}};
  125. print OFILE join(" \\\n\t",@sorted);
  126. print OFILE "\n\n";
  127. }
  128. }
  129. sub bydep
  130. {
  131. foreach my $f ( keys %{$mod->{$b}} ) {
  132. if($f eq $a) {
  133. return 1;
  134. }
  135. }
  136. return -1;
  137. }
  138. __END__
  139. =head1 NAME
  140. depmod.pl - a cross platform script to generate kernel module dependency
  141. lists which can then be used by modprobe.
  142. =head1 SYNOPSIS
  143. depmod.pl [OPTION]... [basedir]...
  144. Example:
  145. depmod.pl -F linux/System.map target/lib/modules
  146. =head1 DESCRIPTION
  147. The purpose of this script is to automagically generate a list of of kernel
  148. module dependancies. This script produces dependancy lists that should be
  149. identical to the depmod program from the modutils package. Unlike the depmod
  150. binary, however, depmod.pl is designed to be run on your host system, not
  151. on your target system.
  152. This script was written by David Schleef <ds@schleef.org> to be used in
  153. conjunction with the BusyBox modprobe applet.
  154. =head1 OPTIONS
  155. =over 4
  156. =item B<-h --help>
  157. This displays the help message.
  158. =item B<-b --basedir>
  159. The base directory uner which the target's modules will be found. This
  160. defaults to the /lib/modules directory.
  161. =item B<-k --kernel>
  162. Kernel binary for the target. You must either supply a kernel binary
  163. or a kernel symbol file (using the -F option).
  164. =item B<-F --kernelsyms>
  165. Kernel symbol file for the target. You must supply either a kernel symbol file
  166. kernel binary for the target (using the -k option).
  167. =item B<-n --stdout>
  168. Write to stdout instead of modules.dep. This is currently hard coded...
  169. kernel binary for the target (using the -k option).
  170. =item B<--verbose>
  171. Be verbose (not implemented)
  172. =back
  173. =head1 COPYRIGHT
  174. Copyright (c) 2001 David Schleef <ds@schleef.org>
  175. Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
  176. Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
  177. This program is free software; you can redistribute it and/or modify it
  178. under the same terms as Perl itself.
  179. =head1 AUTHOR
  180. David Schleef <ds@schleef.org>
  181. =cut
  182. # $Id: depmod.pl,v 1.4 2004/03/15 08:28:33 andersen Exp $