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
29 my $nm = $ENV{'NM'} || "nm";
38 $0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
40 -h --help : Show this help screen
41 -b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
42 -k --kernel : Kernel binary for the target (e.g. vmlinux)
43 -F --kernelsyms : Kernel symbol file (e.g. System.map)
44 -n --stdout : Write to stdout instead of <basedir>/modules.dep
45 -v --verbose : Print out lots of debugging stuff
46 -P --symbol-prefix : Symbol prefix
49 # get command-line options
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,
61 die $usage unless $basedir && ( $kernel || $kernelsyms );
62 die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
64 # Strip any trailing or multiple slashes from basedir
65 $basedir =~ s-(/)\1+-/-g;
67 # The base directory should contain /lib/modules somewhere
68 if($basedir !~ m-/lib/modules-) {
69 warn "WARNING: base directory does not match ..../lib/modules\n";
72 # if no kernel version is contained in the basedir, try to find one
73 if($basedir !~ m-/lib/modules/\d\.\d-) {
74 opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
75 foreach ( readdir(BD) ) {
77 next unless -d "$basedir/$_";
78 warn "dir = $_\n" if $verbose;
81 warn("Guessed module directory as $basedir/$kdir\n");
86 die "Cannot find a kernel version under $basedir\n" unless $kdir;
87 $basedir = "$basedir/$kdir";
90 # Find the list of .o or .ko files living under $basedir
91 warn "**** Locating all modules\n" if $verbose;
94 if ( -f $_ && ! -d $_ ) {
95 $file = $File::Find::name;
96 if ( $file =~ /\.k?o$/ ) {
97 push(@liblist, $file);
98 warn "$file\n" if $verbose;
102 warn "**** Finished locating modules\n" if $verbose;
104 foreach my $obj ( @liblist ){
105 # turn the input file name into a target tag name
106 my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
108 warn "\nMODULE = $tgtname\n" if $verbose;
110 # get a list of symbols
111 my @output=`$nm $obj`;
113 build_ref_tables($tgtname, \@output, $exp, $dep);
117 # vmlinux is a special name that is only used to resolve symbols
118 my $tgtname = 'vmlinux';
119 my @output = $kernelsyms ? `cat $kernelsyms` : `$nm $kernel`;
120 warn "\nMODULE = $tgtname\n" if $verbose;
121 build_ref_tables($tgtname, \@output, $exp, $dep);
123 # resolve the dependencies for each module
124 # reduce dependencies: remove unresolvable and resolved from vmlinux/System.map
126 foreach my $module (keys %$dep) {
127 warn "reducing module: $module\n" if $verbose;
128 $mod->{$module} = {};
129 foreach (@{$dep->{$module}}) {
131 warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
132 next if $exp->{$_} =~ /vmlinux/;
133 $mod->{$module}{$exp->{$_}} = 1;
135 warn "unresolved symbol $_ in file $module\n";
140 # figure out where the output should go
142 open(STDOUT, ">$basedir/modules.dep")
143 or die "cannot open $basedir/modules.dep: $!";
145 my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
147 foreach my $module ( keys %$mod ) {
148 if($kseries eq '2.4') {
150 my @sorted = sort bydep keys %{$mod->{$module}};
151 print join(" \\\n\t",@sorted);
155 my @sorted = sort bydep keys %{$mod->{$module}};
156 print join(" ",@sorted);
164 my ($name, $sym_ar, $exp, $dep) = @_;
166 my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
168 # gather the exported symbols
170 # explicitly exported
171 foreach ( @$sym_ar ) {
172 / __ksymtab_(.*)$/ and do {
173 warn "sym = $1\n" if $verbose;
178 # exporting all symbols
179 foreach ( @$sym_ar ) {
180 / [ABCDGRSTW] (.*)$/ and do {
181 warn "syma = $1\n" if $verbose;
187 # this takes makes sure modules with no dependencies get listed
188 push @{$dep->{$name}}, $symprefix . 'printk' unless $name eq 'vmlinux';
190 # gather the unresolved symbols
191 foreach ( @$sym_ar ) {
192 !/ __this_module/ && / U (.*)$/ and do {
193 warn "und = $1\n" if $verbose;
194 push @{$dep->{$name}}, $1;
201 foreach my $f ( keys %{$mod->{$b}} ) {
215 depmod.pl - a cross platform script to generate kernel module
216 dependency lists (modules.conf) which can then be used by modprobe
217 on the target platform.
219 It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
223 depmod.pl [OPTION]... [basedir]...
227 depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
231 The purpose of this script is to automagically generate a list of of kernel
232 module dependencies. This script produces dependency lists that should be
233 identical to the depmod program from the modutils package. Unlike the depmod
234 binary, however, depmod.pl is designed to be run on your host system, not
235 on your target system.
237 This script was written by David Schleef <ds@schleef.org> to be used in
238 conjunction with the BusyBox modprobe applet.
246 This displays the help message.
248 =item B<-b --basedir>
250 The base directory uner which the target's modules will be found. This
251 defaults to the /lib/modules directory.
253 If you don't specify the kernel version, this script will search for
254 one under the specified based directory and use the first thing that
255 looks like a kernel version.
259 Kernel binary for the target (vmlinux). You must either supply a kernel binary
260 or a kernel symbol file (using the -F option).
262 =item B<-F --kernelsyms>
264 Kernel symbol file for the target (System.map).
268 Write to stdout instead of modules.dep
269 kernel binary for the target (using the -k option).
273 Verbose (debug) output
277 =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>
285 This program is free software; you can redistribute it and/or modify it
286 under the same terms as Perl itself.
290 David Schleef <ds@schleef.org>