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
12use Getopt::Long;
13use File::Find;
14
15
16# Set up some default values
17
18my $basedir="";
19my $kernel;
20my $kernelsyms;
21my $stdout=1;
22my $verbose=0;
23
24
25# get command-line options
26
27my %opt;
28
29GetOptions(
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
39if (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
52if($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"; }
58my($file) = "";
59my(@liblist) = ();
60find 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;
69if ($verbose) { printf "Finished locating modules\n"; }
70
71foreach $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
116foreach $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
130foreach $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
140sub 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
156depmod.pl - a cross platform script to generate kernel module dependency
157		lists which can then be used by modprobe.
158
159=head1 SYNOPSIS
160
161depmod.pl [OPTION]... [FILE]...
162
163Example:
164
165	depmod.pl -F linux/System.map target/lib/modules
166
167=head1 DESCRIPTION
168
169The purpose of this script is to automagically generate a list of of kernel
170module dependancies.  This script produces dependancy lists that should be
171identical to the depmod program from the modutils package.  Unlike the depmod
172binary, however, depmod.pl is designed to be run on your host system, not
173on your target system.
174
175This script was written by David Schleef <ds@schleef.org> to be used in
176conjunction with the BusyBox modprobe applet.
177
178=head1 OPTIONS
179
180=over 4
181
182=item B<-h --help>
183
184This displays the help message.
185
186=item B<-b --basedir>
187
188The base directory uner which the target's modules will be found.  This
189defaults to the /lib/modules directory.
190
191=item B<-k --kernel>
192
193Kernel binary for the target.  You must either supply a kernel binary
194or a kernel symbol file (using the -F option).
195
196=item B<-F --kernelsyms>
197
198Kernel symbol file for the target.  You must supply either a kernel symbol file
199kernel binary for the target (using the -k option).
200
201=item B<-n --stdout>
202
203Write to stdout instead of modules.dep.  This is currently hard coded...
204kernel binary for the target (using the -k option).
205
206=item B<--verbose>
207
208Be verbose (not implemented)
209
210=back
211
212=head1 COPYRIGHT
213
214Copyright (c) 2001 David Schleef <ds@schleef.org>
215Copyright (c) 2001 Erik Andersen <andersen@lineo.com>
216Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
217This program is free software; you can redistribute it and/or modify it
218under the same terms as Perl itself.
219
220=head1 AUTHOR
221
222David Schleef <ds@schleef.org>
223
224=cut
225
226# $Id: depmod.pl,v 1.1.1.1 2008/10/15 03:28:35 james26_jang Exp $
227
228