e65f07b686534b68faed26a4159d05af508ece5f
[platform/upstream/busybox.git] / examples / depmod.pl
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@lineo.com>
5 # Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
6 # This program is free software; you can redistribute it and/or modify it 
7 # under the same terms as Perl itself.
8
9 # TODO -- use strict mode...
10 #use strict;
11
12 use Getopt::Long;
13 use File::Find;
14
15
16 # Set up some default values
17
18 my $basedir="";
19 my $kernel;
20 my $kernelsyms;
21 my $stdout=1;
22 my $verbose=0;
23
24
25 # get command-line options
26
27 my %opt;
28
29 GetOptions(
30         \%opt,
31         "help|h",
32         "basedir|b=s" => \$basedir,
33         "kernel|k=s" => \$kernel,
34         "kernelsyms|F=s" => \$kernelsyms,
35         "stdout|n" => \$stdout,
36         "verbose|v" => \$verbose,
37 );
38
39 if (defined $opt{help}) {
40         print
41                 "$0 [OPTION]... [basedir]\n",
42                 "\t-h --help\t\tShow this help screen\n",
43                 "\t-b --basedir\t\tModules base directory (defaults to /lib/modules)\n",
44                 "\t-k --kernel\t\tKernel binary for the target\n",
45                 "\t-F --kernelsyms\t\tKernel symbol file\n",
46                 "\t-n --stdout\t\tWrite to stdout instead of modules.dep\n",
47                 "\t-v --verbose\t\tPrint out lots of debugging stuff\n",
48         ;
49         exit 1;
50 }
51
52 if($basedir !~ m-/lib/modules-) {
53     warn "WARNING: base directory does not match ..../lib/modules\n";
54 }
55
56 # Find the list of .o files living under $basedir 
57 #if ($verbose) { printf "Locating all modules\n"; }
58 my($file) = "";
59 my(@liblist) = ();
60 find sub { 
61         if ( -f $_  && ! -d $_ ) { 
62                 $file = $File::Find::name;
63                 if ( $file =~ /.o$/ ) {
64                         push(@liblist, $file);
65                         if ($verbose) { printf "$file\n"; }
66                 }
67         }
68 }, $basedir;
69 if ($verbose) { printf "Finished locating modules\n"; }
70
71 foreach $obj ( @liblist, $kernel ){
72     # turn the input file name into a target tag name
73     # vmlinux is a special that is only used to resolve symbols
74     if($obj =~ /vmlinux/) {
75         $tgtname = "vmlinux";
76     } else {
77         ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
78     }
79
80     warn "MODULE = $tgtname\n" if $verbose;
81
82     # get a list of symbols
83         @output=`nm $obj`;
84         $ksymtab=grep m/ __ksymtab/, @output;
85
86     # gather the exported symbols
87         if($ksymtab){
88         # explicitly exported
89         foreach ( @output ) {
90             / __ksymtab_(.*)$/ and do {
91                 warn "sym = $1\n" if $verbose;
92                 $exp->{$1} = $tgtname;
93             };
94         }
95         } else {
96         # exporting all symbols
97         foreach ( @output) {
98             / [ABCDGRST] (.*)$/ and do {
99                 warn "syma = $1\n" if $verbose;
100                 $exp->{$1} = $tgtname;
101             };
102         }
103         }
104     # gather the unresolved symbols
105     foreach ( @output ) {
106         !/ __this_module/ && / U (.*)$/ and do {
107             warn "und = $1\n" if $verbose;
108             push @{$dep->{$tgtname}}, $1;
109         };
110     }
111 }
112
113
114 # reduce dependancies: remove unresolvable and resolved from vmlinux
115 # remove duplicates
116 foreach $module (keys %$dep) {
117     $mod->{$module} = {};
118     foreach (@{$dep->{$module}}) {
119         if( $exp->{$_} ) { 
120             warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
121             next if $exp->{$_} =~ /vmlinux/;
122             $mod->{$module}{$exp->{$_}} = 1;
123         } else {
124             warn "unresolved symbol $_ in file $module\n";
125         }
126     } 
127 }
128
129 # resolve the dependancies for each module
130 foreach $module ( keys %$mod )  {
131     print "$module:\t";
132     @sorted = sort bydep keys %{$mod->{$module}};
133     print join(" \\\n\t",@sorted);
134 #    foreach $m (@sorted ) {
135 #        print "\t$m\n";
136 #    }
137     print "\n\n";
138 }
139
140 sub bydep
141 {
142     foreach my $f ( keys %{$mod->{$b}} ) {
143         if($f eq $a) {
144             return 1;
145         }
146     }
147     return -1;
148 }
149
150
151
152 __END__
153
154 =head1 NAME
155
156 depmod.pl - a cross platform script to generate kernel module dependency
157                 lists which can then be used by modprobe.
158
159 =head1 SYNOPSIS
160
161 depmod.pl [OPTION]... [FILE]...
162
163 Example:
164
165         depmod.pl -F linux/System.map target/lib/modules
166
167 =head1 DESCRIPTION
168
169 The purpose of this script is to automagically generate a list of of kernel
170 module dependancies.  This script produces dependancy lists that should be
171 identical to the depmod program from the modutils package.  Unlike the depmod
172 binary, however, depmod.pl is designed to be run on your host system, not
173 on your target system.
174
175 This script was written by David Schleef <ds@schleef.org> to be used in
176 conjunction with the BusyBox modprobe applet.
177
178 =head1 OPTIONS
179
180 =over 4
181
182 =item B<-h --help>
183
184 This displays the help message.
185
186 =item B<-b --basedir>
187
188 The base directory uner which the target's modules will be found.  This
189 defaults to the /lib/modules directory.
190
191 =item B<-k --kernel>
192
193 Kernel binary for the target.  You must either supply a kernel binary
194 or a kernel symbol file (using the -F option).
195
196 =item B<-F --kernelsyms>
197
198 Kernel symbol file for the target.  You must supply either a kernel symbol file
199 kernel binary for the target (using the -k option).
200
201 =item B<-n --stdout>
202
203 Write to stdout instead of modules.dep.  This is currently hard coded...
204 kernel binary for the target (using the -k option).
205
206 =item B<--verbose>
207
208 Be verbose (not implemented)
209
210 =back
211
212 =head1 COPYRIGHT
213
214 Copyright (c) 2001 David Schleef <ds@schleef.org>
215 Copyright (c) 2001 Erik Andersen <andersen@lineo.com>
216 Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
217 This program is free software; you can redistribute it and/or modify it 
218 under the same terms as Perl itself.
219
220 =head1 AUTHOR
221
222 David Schleef <ds@schleef.org>
223
224 =cut
225
226 # $Id: depmod.pl,v 1.1 2001/07/30 19:32:03 andersen Exp $
227