depmod.pl 9.5 KB

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