depmod.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if (length($depth) > 50) {
  158. die "too much recursion (circular dependencies in modules?)";
  159. }
  160. foreach my $md (keys %{$mod->{$this_module}}) {
  161. add_mod_deps ($depth, $mod, $mod2, $module, $md);
  162. warn "${depth} outputting $md\n" if $verbose;
  163. maybe_unshift (\@{$$mod2->{$module}}, $md);
  164. }
  165. if (!%{$mod->{$this_module}}) {
  166. warn "${depth} no deps\n" if $verbose;
  167. }
  168. }
  169. foreach my $module (keys %$mod) {
  170. warn "filling out module: $module\n" if $verbose;
  171. @{$mod2->{$module}} = ();
  172. add_mod_deps ("", $mod, \$mod2, $module, $module);
  173. }
  174. # figure out where the output should go
  175. if ($stdout == 0) {
  176. warn "writing $basedir/modules.dep\n" if $verbose;
  177. open(STDOUT, ">$basedir/modules.dep")
  178. or die "cannot open $basedir/modules.dep: $!";
  179. }
  180. my $kseries = $basedir =~ m,/2\.4\.[^/]*, ? '2.4' : 'others';
  181. foreach my $module ( keys %$mod ) {
  182. if($kseries eq '2.4') {
  183. print "$module:\t";
  184. my @sorted = sort bydep keys %{$mod->{$module}};
  185. print join(" \\\n\t",@sorted);
  186. print "\n\n";
  187. } else {
  188. my $shortmod = $module;
  189. $shortmod =~ s:^/lib/modules/[^/]*/::;
  190. print "$shortmod:";
  191. my @sorted = @{$mod2->{$module}};
  192. printf " " if @sorted;
  193. print join(" ",@sorted);
  194. print "\n";
  195. }
  196. }
  197. sub build_ref_tables
  198. {
  199. my ($name, $sym_ar, $exp, $dep) = @_;
  200. my $ksymtab = grep m/ ${symprefix}__ksymtab/, @$sym_ar;
  201. # gather the exported symbols
  202. if($ksymtab){
  203. # explicitly exported
  204. foreach ( @$sym_ar ) {
  205. / ${symprefix}__ksymtab_(.*)$/ and do {
  206. my $sym = ${symprefix} . $1;
  207. warn "sym = $sym\n" if $verbose;
  208. $exp->{$sym} = $name;
  209. };
  210. }
  211. } else {
  212. # exporting all symbols
  213. foreach ( @$sym_ar ) {
  214. / [ABCDGRSTW] (.*)$/ and do {
  215. warn "syma = $1\n" if $verbose;
  216. $exp->{$1} = $name;
  217. };
  218. }
  219. }
  220. # this takes makes sure modules with no dependencies get listed
  221. push @{$dep->{$name}}, $symprefix . 'printk' unless $name eq 'vmlinux';
  222. # gather the unresolved symbols
  223. foreach ( @$sym_ar ) {
  224. !/ ${symprefix}__this_module/ && / U (.*)$/ and do {
  225. warn "und = $1\n" if $verbose;
  226. push @{$dep->{$name}}, $1;
  227. };
  228. }
  229. }
  230. sub bydep
  231. {
  232. foreach my $f ( keys %{$mod->{$b}} ) {
  233. if($f eq $a) {
  234. return 1;
  235. }
  236. }
  237. return -1;
  238. }
  239. __END__
  240. =head1 NAME
  241. depmod.pl - a cross platform script to generate kernel module
  242. dependency lists (modules.conf) which can then be used by modprobe
  243. on the target platform.
  244. It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
  245. =head1 SYNOPSIS
  246. depmod.pl [OPTION]... [basedir]...
  247. Example:
  248. depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
  249. =head1 DESCRIPTION
  250. The purpose of this script is to automagically generate a list of of kernel
  251. module dependencies. This script produces dependency lists that should be
  252. identical to the depmod program from the modutils package. Unlike the depmod
  253. binary, however, depmod.pl is designed to be run on your host system, not
  254. on your target system.
  255. This script was written by David Schleef <ds@schleef.org> to be used in
  256. conjunction with the BusyBox modprobe applet.
  257. =head1 OPTIONS
  258. =over 4
  259. =item B<-h --help>
  260. This displays the help message.
  261. =item B<-b --basedir>
  262. The base directory uner which the target's modules will be found. This
  263. defaults to the /lib/modules directory.
  264. If you don't specify the kernel version, this script will search for
  265. one under the specified based directory and use the first thing that
  266. looks like a kernel version.
  267. =item B<-k --kernel>
  268. Kernel binary for the target (vmlinux). You must either supply a kernel binary
  269. or a kernel symbol file (using the -F option).
  270. =item B<-F --kernelsyms>
  271. Kernel symbol file for the target (System.map).
  272. =item B<-n --stdout>
  273. Write to stdout instead of modules.dep
  274. kernel binary for the target (using the -k option).
  275. =item B<--verbose>
  276. Verbose (debug) output
  277. =back
  278. =head1 COPYRIGHT AND LICENSE
  279. Copyright (c) 2001 David Schleef <ds@schleef.org>
  280. Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
  281. Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
  282. Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
  283. Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
  284. This program is free software; you can redistribute it and/or modify it
  285. under the same terms as Perl itself.
  286. =head1 AUTHOR
  287. David Schleef <ds@schleef.org>
  288. =cut