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>
10 # March 2006: Stuart Hughes <stuarth@freescale.com>.
11 # Significant updates, including implementing the '-F' option
12 # and adding support for 2.6 kernels.
14 # This program is free software; you can redistribute it and/or modify it
15 # under the same terms as Perl itself.
20 # Set up some default values
36 $0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
38 -h --help : Show this help screen
39 -b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
40 -k --kernel : Kernel binary for the target (e.g. vmlinux)
41 -F --kernelsyms : Kernel symbol file (e.g. System.map)
42 -n --stdout : Write to stdout instead of <basedir>/modules.dep
43 -v --verbose : Print out lots of debugging stuff
46 # get command-line options
49 "basedir|b=s" => \$basedir,
50 "kernel|k=s" => \$kernel,
51 "kernelsyms|F=s" => \$kernelsyms,
52 "stdout|n" => \$stdout,
53 "verbose|v" => \$verbose,
57 die $usage unless $basedir && ( $kernel || $kernelsyms );
58 die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
60 # Strip any trailing or multiple slashes from basedir
61 $basedir =~ s-(/)\1+-/-g;
63 # The base directory should contain /lib/modules somewhere
64 if($basedir !~ m-/lib/modules-) {
65 warn "WARNING: base directory does not match ..../lib/modules\n";
68 # if no kernel version is contained in the basedir, try to find one
69 if($basedir !~ m-/lib/modules/\d\.\d-) {
70 opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
71 foreach ( readdir(BD) ) {
73 next unless -d "$basedir/$_";
74 warn "dir = $_\n" if $verbose;
77 warn("Guessed module directory as $basedir/$kdir\n");
82 die "Cannot find a kernel version under $basedir\n" unless $kdir;
83 $basedir = "$basedir/$kdir";
86 # Find the list of .o or .ko files living under $basedir
87 warn "**** Locating all modules\n" if $verbose;
90 if ( -f $_ && ! -d $_ ) {
91 $file = $File::Find::name;
92 if ( $file =~ /\.k?o$/ ) {
93 push(@liblist, $file);
94 warn "$file\n" if $verbose;
98 warn "**** Finished locating modules\n" if $verbose;
100 foreach my $obj ( @liblist ){
101 # turn the input file name into a target tag name
102 my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
104 warn "\nMODULE = $tgtname\n" if $verbose;
106 # get a list of symbols
107 my @output=`nm $obj`;
109 build_ref_tables($tgtname, \@output, $exp, $dep);
113 # vmlinux is a special name that is only used to resolve symbols
114 my $tgtname = 'vmlinux';
115 my @output = $kernelsyms ? `cat $kernelsyms` : `nm $kernel`;
116 warn "\nMODULE = $tgtname\n" if $verbose;
117 build_ref_tables($tgtname, \@output, $exp, $dep);
119 # resolve the dependencies for each module
120 # reduce dependencies: remove unresolvable and resolved from vmlinux/System.map
122 foreach my $module (keys %$dep) {
123 warn "reducing module: $module\n" if $verbose;
124 $mod->{$module} = {};
125 foreach (@{$dep->{$module}}) {
127 warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
128 next if $exp->{$_} =~ /vmlinux/;
129 $mod->{$module}{$exp->{$_}} = 1;
131 warn "unresolved symbol $_ in file $module\n";
136 # figure out where the output should go
138 open(STDOUT, ">$basedir/modules.dep")
139 or die "cannot open $basedir/modules.dep: $!";
141 my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
143 foreach my $module ( keys %$mod ) {
144 if($kseries eq '2.4') {
146 my @sorted = sort bydep keys %{$mod->{$module}};
147 print join(" \\\n\t",@sorted);
151 my @sorted = sort bydep keys %{$mod->{$module}};
152 print join(" ",@sorted);
160 my ($name, $sym_ar, $exp, $dep) = @_;
162 my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
164 # gather the exported symbols
166 # explicitly exported
167 foreach ( @$sym_ar ) {
168 / __ksymtab_(.*)$/ and do {
169 warn "sym = $1\n" if $verbose;
174 # exporting all symbols
175 foreach ( @$sym_ar ) {
176 / [ABCDGRST] (.*)$/ and do {
177 warn "syma = $1\n" if $verbose;
183 # this takes makes sure modules with no dependencies get listed
184 push @{$dep->{$name}}, 'printk' unless $name eq 'vmlinux';
186 # gather the unresolved symbols
187 foreach ( @$sym_ar ) {
188 !/ __this_module/ && / U (.*)$/ and do {
189 warn "und = $1\n" if $verbose;
190 push @{$dep->{$name}}, $1;
197 foreach my $f ( keys %{$mod->{$b}} ) {
211 depmod.pl - a cross platform script to generate kernel module
212 dependency lists (modules.conf) which can then be used by modprobe
213 on the target platform.
215 It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
219 depmod.pl [OPTION]... [basedir]...
223 depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
227 The purpose of this script is to automagically generate a list of of kernel
228 module dependencies. This script produces dependency lists that should be
229 identical to the depmod program from the modutils package. Unlike the depmod
230 binary, however, depmod.pl is designed to be run on your host system, not
231 on your target system.
233 This script was written by David Schleef <ds@schleef.org> to be used in
234 conjunction with the BusyBox modprobe applet.
242 This displays the help message.
244 =item B<-b --basedir>
246 The base directory uner which the target's modules will be found. This
247 defaults to the /lib/modules directory.
249 If you don't specify the kernel version, this script will search for
250 one under the specified based directory and use the first thing that
251 looks like a kernel version.
255 Kernel binary for the target (vmlinux). You must either supply a kernel binary
256 or a kernel symbol file (using the -F option).
258 =item B<-F --kernelsyms>
260 Kernel symbol file for the target (System.map).
264 Write to stdout instead of modules.dep
265 kernel binary for the target (using the -k option).
269 Verbose (debug) output
273 =head1 COPYRIGHT AND LICENSE
275 Copyright (c) 2001 David Schleef <ds@schleef.org>
276 Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
277 Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
278 Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
279 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.
286 David Schleef <ds@schleef.org>
290 # $Id: depmod.pl,v 1.4 2004/03/15 08:28:33 andersen Exp $