root/floppyfw/scripts/depmod.pl

Revision 217, 7.7 KB (checked in by root, 5 years ago)

Better have them here so we can hack them if we want to.

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