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