summaryrefslogtreecommitdiffhomepage
path: root/examples/depmod.pl
blob: ea29e8883f020dddf47bb902b51e005ee545c38e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/perl -w
# vi: set ts=4:
# Copyright (c) 2001 David Schleef <ds@schleef.org>
# Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
# Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
# Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
# This program is free software; you can redistribute it and/or modify it 
# under the same terms as Perl itself.

# TODO -- use strict mode...
#use strict;

use Getopt::Long;
use File::Find;


# Set up some default values

my $basedir="";
my $kernel;
my $kernelsyms;
my $stdout=0;
my $verbose=0;


# get command-line options

my %opt;

GetOptions(
	\%opt,
	"help|h",
	"basedir|b=s" => \$basedir,
	"kernel|k=s" => \$kernel,
	"kernelsyms|F=s" => \$kernelsyms,
	"stdout|n" => \$stdout,
	"verbose|v" => \$verbose,
);

if (defined $opt{help}) {
	print
	"  $0 [OPTION]... [basedir]\n",
	"     -h --help\t\tShow this help screen\n",
	"     -b --basedir\tModules base directory (defaults to /lib/modules)\n",
	"     -k --kernel\tKernel binary for the target\n",
	"     -F --kernelsyms\tKernel symbol file\n",
	"     -n --stdout\tWrite to stdout instead of <basedir>/modules.dep\n",
	"     -v --verbose\tPrint out lots of debugging stuff\n",
	;
	exit 1;
}

if($basedir !~ m-/lib/modules-) {
    warn "WARNING: base directory does not match ..../lib/modules\n";
}

# Find the list of .o files living under $basedir 
#if ($verbose) { printf "Locating all modules\n"; }
my($ofile) = "";
my($file) = "";
my(@liblist) = ();
find sub { 
	if ( -f $_  && ! -d $_ ) { 
		$file = $File::Find::name;
		if ( $file =~ /.o$/ ) {
			push(@liblist, $file);
			if ($verbose) { printf "$file\n"; }
		}
	}
}, $basedir;
if ($verbose) { printf "Finished locating modules\n"; }

foreach $obj ( @liblist, $kernel ){
    # turn the input file name into a target tag name
    # vmlinux is a special that is only used to resolve symbols
    if($obj =~ /vmlinux/) {
        $tgtname = "vmlinux";
    } else {
        ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
    }

    warn "MODULE = $tgtname\n" if $verbose;

    # get a list of symbols
	@output=`nm $obj`;
	$ksymtab=grep m/ __ksymtab/, @output;

    # gather the exported symbols
	if($ksymtab){
        # explicitly exported
        foreach ( @output ) {
            / __ksymtab_(.*)$/ and do {
                warn "sym = $1\n" if $verbose;
                $exp->{$1} = $tgtname;
            };
        }
	} else {
        # exporting all symbols
        foreach ( @output) {
            / [ABCDGRST] (.*)$/ and do {
                warn "syma = $1\n" if $verbose;
                $exp->{$1} = $tgtname;
            };
        }
	}
    # gather the unresolved symbols
    foreach ( @output ) {
        !/ __this_module/ && / U (.*)$/ and do {
            warn "und = $1\n" if $verbose;
            push @{$dep->{$tgtname}}, $1;
        };
    }
}


# reduce dependancies: remove unresolvable and resolved from vmlinux
# remove duplicates
foreach $module (keys %$dep) {
    $mod->{$module} = {};
    foreach (@{$dep->{$module}}) {
        if( $exp->{$_} ) { 
            warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
            next if $exp->{$_} =~ /vmlinux/;
            $mod->{$module}{$exp->{$_}} = 1;
        } else {
            warn "unresolved symbol $_ in file $module\n";
        }
    } 
}

# resolve the dependancies for each module
if ($stdout == 1) {
	foreach $module ( keys %$mod ) {
		print "$module:\t";
		@sorted = sort bydep keys %{$mod->{$module}};
		print join(" \\\n\t",@sorted);
		print "\n\n";
	}
} else {
	open(OFILE, ">$basedir/modules.dep");
	foreach $module ( keys %$mod ) {
		print OFILE "$module:\t";
		@sorted = sort bydep keys %{$mod->{$module}};
		print OFILE join(" \\\n\t",@sorted);
		print OFILE "\n\n";
	}
}


sub bydep
{
    foreach my $f ( keys %{$mod->{$b}} ) {
        if($f eq $a) {
            return 1;
        }
    }
    return -1;
}



__END__

=head1 NAME

depmod.pl - a cross platform script to generate kernel module dependency
		lists which can then be used by modprobe.

=head1 SYNOPSIS

depmod.pl [OPTION]... [basedir]...

Example:

	depmod.pl -F linux/System.map target/lib/modules

=head1 DESCRIPTION

The purpose of this script is to automagically generate a list of of kernel
module dependancies.  This script produces dependancy lists that should be
identical to the depmod program from the modutils package.  Unlike the depmod
binary, however, depmod.pl is designed to be run on your host system, not
on your target system.

This script was written by David Schleef <ds@schleef.org> to be used in
conjunction with the BusyBox modprobe applet.

=head1 OPTIONS

=over 4

=item B<-h --help>

This displays the help message.

=item B<-b --basedir>

The base directory uner which the target's modules will be found.  This
defaults to the /lib/modules directory.

=item B<-k --kernel>

Kernel binary for the target.  You must either supply a kernel binary
or a kernel symbol file (using the -F option).

=item B<-F --kernelsyms>

Kernel symbol file for the target.  You must supply either a kernel symbol file
kernel binary for the target (using the -k option).

=item B<-n --stdout>

Write to stdout instead of modules.dep.  This is currently hard coded...
kernel binary for the target (using the -k option).

=item B<--verbose>

Be verbose (not implemented)

=back

=head1 COPYRIGHT

Copyright (c) 2001 David Schleef <ds@schleef.org>
Copyright (c) 2001 Erik Andersen <andersen@lineo.com>
Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
This program is free software; you can redistribute it and/or modify it 
under the same terms as Perl itself.

=head1 AUTHOR

David Schleef <ds@schleef.org>

=cut

# $Id: depmod.pl,v 1.3 2003/07/14 21:20:51 andersen Exp $