summaryrefslogtreecommitdiff
path: root/examples/depmod.pl
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-03-21 16:35:50 +0000
committerRob Landley <rob@landley.net>2006-03-21 16:35:50 +0000
commitd0498125711c1d26d57a5565d5f4c573e5bbfac7 (patch)
tree5969dcd47159df27587666ebc161825d1f5150af /examples/depmod.pl
parent8dd4ca787ab4606424d634ef94246a658ee6c6bf (diff)
downloadbusybox-w32-d0498125711c1d26d57a5565d5f4c573e5bbfac7.tar.gz
busybox-w32-d0498125711c1d26d57a5565d5f4c573e5bbfac7.tar.bz2
busybox-w32-d0498125711c1d26d57a5565d5f4c573e5bbfac7.zip
Patch from Stuart Hughes upgrading depmod.pl
Diffstat (limited to 'examples/depmod.pl')
-rwxr-xr-xexamples/depmod.pl258
1 files changed, 156 insertions, 102 deletions
diff --git a/examples/depmod.pl b/examples/depmod.pl
index 9af192208..3211c8a64 100755
--- a/examples/depmod.pl
+++ b/examples/depmod.pl
@@ -2,151 +2,196 @@
2# vi: set ts=4: 2# vi: set ts=4:
3# Copyright (c) 2001 David Schleef <ds@schleef.org> 3# Copyright (c) 2001 David Schleef <ds@schleef.org>
4# Copyright (c) 2001 Erik Andersen <andersen@codepoet.org> 4# Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
5# Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com> 5# Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
6# Copyright (c) 2002 Steven J. Hill <shill@broadcom.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
7# This program is free software; you can redistribute it and/or modify it 14# This program is free software; you can redistribute it and/or modify it
8# under the same terms as Perl itself. 15# under the same terms as Perl itself.
9
10# TODO -- use strict mode...
11#use strict;
12
13use Getopt::Long; 16use Getopt::Long;
14use File::Find; 17use File::Find;
15 18use strict;
16 19
17# Set up some default values 20# Set up some default values
18 21my $kdir="";
19my $basedir=""; 22my $basedir="";
20my $kernel; 23my $kernel="";
21my $kernelsyms; 24my $kernelsyms="";
22my $stdout=0; 25my $stdout=0;
23my $verbose=0; 26my $verbose=0;
24 27my $help=0;
28
29# more globals
30my (@liblist) = ();
31my $exp = {};
32my $dep = {};
33my $mod = {};
34
35my $usage = <<TXT;
36$0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
37 Where:
38 -h --help : Show this help screen
39 -b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
40 -k --kernel : Kernel binary for the target (e.g. vmlinux)
41 -F --kernelsyms : Kernel symbol file (e.g. System.map)
42 -n --stdout : Write to stdout instead of <basedir>/modules.dep
43 -v --verbose : Print out lots of debugging stuff
44TXT
25 45
26# get command-line options 46# get command-line options
27
28my %opt;
29
30GetOptions( 47GetOptions(
31 \%opt, 48 "help|h" => \$help,
32 "help|h", 49 "basedir|b=s" => \$basedir,
33 "basedir|b=s" => \$basedir, 50 "kernel|k=s" => \$kernel,
34 "kernel|k=s" => \$kernel,
35 "kernelsyms|F=s" => \$kernelsyms, 51 "kernelsyms|F=s" => \$kernelsyms,
36 "stdout|n" => \$stdout, 52 "stdout|n" => \$stdout,
37 "verbose|v" => \$verbose, 53 "verbose|v" => \$verbose,
38); 54);
39 55
40if (defined $opt{help}) { 56die $usage if $help;
41 print 57die $usage unless $basedir && ( $kernel || $kernelsyms );
42 " $0 [OPTION]... [basedir]\n", 58die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
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",
49 ;
50 exit 1;
51}
52 59
60# Strip any trailing or multiple slashes from basedir
61$basedir =~ s-(/)\1+-/-g;
62
63# The base directory should contain /lib/modules somewhere
53if($basedir !~ m-/lib/modules-) { 64if($basedir !~ m-/lib/modules-) {
54 warn "WARNING: base directory does not match ..../lib/modules\n"; 65 warn "WARNING: base directory does not match ..../lib/modules\n";
55} 66}
56 67
57# Find the list of .o files living under $basedir 68# if no kernel version is contained in the basedir, try to find one
58#if ($verbose) { printf "Locating all modules\n"; } 69if($basedir !~ m-/lib/modules/\d\.\d-) {
59my($ofile) = ""; 70 opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
60my($file) = ""; 71 foreach ( readdir(BD) ) {
61my(@liblist) = (); 72 next if /^\.\.?$/;
73 next unless -d "$basedir/$_";
74 warn "dir = $_\n" if $verbose;
75 if( /^\d\.\d/ ) {
76 $kdir = $_;
77 warn("Guessed module directory as $basedir/$kdir\n");
78 last;
79 }
80 }
81 closedir(BD);
82 die "Cannot find a kernel version under $basedir\n" unless $kdir;
83 $basedir = "$basedir/$kdir";
84}
85
86# Find the list of .o or .ko files living under $basedir
87warn "**** Locating all modules\n" if $verbose;
62find sub { 88find sub {
89 my $file;
63 if ( -f $_ && ! -d $_ ) { 90 if ( -f $_ && ! -d $_ ) {
64 $file = $File::Find::name; 91 $file = $File::Find::name;
65 if ( $file =~ /.o$/ ) { 92 if ( $file =~ /\.k?o$/ ) {
66 push(@liblist, $file); 93 push(@liblist, $file);
67 if ($verbose) { printf "$file\n"; } 94 warn "$file\n" if $verbose;
68 } 95 }
69 } 96 }
70}, $basedir; 97}, $basedir;
71if ($verbose) { printf "Finished locating modules\n"; } 98warn "**** Finished locating modules\n" if $verbose;
72 99
73foreach $obj ( @liblist, $kernel ){ 100foreach my $obj ( @liblist ){
74 # turn the input file name into a target tag name 101 # turn the input file name into a target tag name
75 # vmlinux is a special that is only used to resolve symbols 102 my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
76 if($obj =~ /vmlinux/) { 103
77 $tgtname = "vmlinux"; 104 warn "\nMODULE = $tgtname\n" if $verbose;
105
106 # get a list of symbols
107 my @output=`nm $obj`;
108
109 build_ref_tables($tgtname, \@output, $exp, $dep);
110}
111
112
113# vmlinux is a special name that is only used to resolve symbols
114my $tgtname = 'vmlinux';
115my @output = $kernelsyms ? `cat $kernelsyms` : `nm $kernel`;
116warn "\nMODULE = $tgtname\n" if $verbose;
117build_ref_tables($tgtname, \@output, $exp, $dep);
118
119# resolve the dependancies for each module
120# reduce dependancies: remove unresolvable and resolved from vmlinux/System.map
121# remove duplicates
122foreach my $module (keys %$dep) {
123 warn "reducing module: $module\n" if $verbose;
124 $mod->{$module} = {};
125 foreach (@{$dep->{$module}}) {
126 if( $exp->{$_} ) {
127 warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
128 next if $exp->{$_} =~ /vmlinux/;
129 $mod->{$module}{$exp->{$_}} = 1;
130 } else {
131 warn "unresolved symbol $_ in file $module\n";
132 }
133 }
134}
135
136# figure out where the output should go
137if ($stdout == 0) {
138 open(STDOUT, ">$basedir/modules.dep")
139 or die "cannot open $basedir/modules.dep: $!";
140}
141my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
142
143foreach my $module ( keys %$mod ) {
144 if($kseries eq '2.4') {
145 print "$module:\t";
146 my @sorted = sort bydep keys %{$mod->{$module}};
147 print join(" \\\n\t",@sorted);
148 print "\n\n";
78 } else { 149 } else {
79 ($tgtname) = $obj =~ m-(/lib/modules/.*)$-; 150 print "$module: ";
151 my @sorted = sort bydep keys %{$mod->{$module}};
152 print join(" ",@sorted);
153 print "\n";
80 } 154 }
155}
81 156
82 warn "MODULE = $tgtname\n" if $verbose;
83 157
84 # get a list of symbols 158sub build_ref_tables
85 @output=`nm $obj`; 159{
86 $ksymtab=grep m/ __ksymtab/, @output; 160 my ($name, $sym_ar, $exp, $dep) = @_;
161
162 my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
87 163
88 # gather the exported symbols 164 # gather the exported symbols
89 if($ksymtab){ 165 if($ksymtab){
90 # explicitly exported 166 # explicitly exported
91 foreach ( @output ) { 167 foreach ( @$sym_ar ) {
92 / __ksymtab_(.*)$/ and do { 168 / __ksymtab_(.*)$/ and do {
93 warn "sym = $1\n" if $verbose; 169 warn "sym = $1\n" if $verbose;
94 $exp->{$1} = $tgtname; 170 $exp->{$1} = $name;
95 }; 171 };
96 } 172 }
97 } else { 173 } else {
98 # exporting all symbols 174 # exporting all symbols
99 foreach ( @output) { 175 foreach ( @$sym_ar ) {
100 / [ABCDGRST] (.*)$/ and do { 176 / [ABCDGRST] (.*)$/ and do {
101 warn "syma = $1\n" if $verbose; 177 warn "syma = $1\n" if $verbose;
102 $exp->{$1} = $tgtname; 178 $exp->{$1} = $name;
103 }; 179 };
104 } 180 }
105 } 181 }
182
183 # this takes makes sure modules with no dependencies get listed
184 push @{$dep->{$name}}, 'printk' unless $name eq 'vmlinux';
185
106 # gather the unresolved symbols 186 # gather the unresolved symbols
107 foreach ( @output ) { 187 foreach ( @$sym_ar ) {
108 !/ __this_module/ && / U (.*)$/ and do { 188 !/ __this_module/ && / U (.*)$/ and do {
109 warn "und = $1\n" if $verbose; 189 warn "und = $1\n" if $verbose;
110 push @{$dep->{$tgtname}}, $1; 190 push @{$dep->{$name}}, $1;
111 }; 191 };
112 } 192 }
113} 193}
114 194
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
132if ($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 }
147}
148
149
150sub bydep 195sub bydep
151{ 196{
152 foreach my $f ( keys %{$mod->{$b}} ) { 197 foreach my $f ( keys %{$mod->{$b}} ) {
@@ -163,8 +208,11 @@ __END__
163 208
164=head1 NAME 209=head1 NAME
165 210
166depmod.pl - a cross platform script to generate kernel module dependency 211depmod.pl - a cross platform script to generate kernel module
167 lists which can then be used by modprobe. 212dependency lists (modules.conf) which can then be used by modprobe
213on the target platform.
214
215It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
168 216
169=head1 SYNOPSIS 217=head1 SYNOPSIS
170 218
@@ -172,7 +220,7 @@ depmod.pl [OPTION]... [basedir]...
172 220
173Example: 221Example:
174 222
175 depmod.pl -F linux/System.map target/lib/modules 223 depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
176 224
177=head1 DESCRIPTION 225=head1 DESCRIPTION
178 226
@@ -196,34 +244,40 @@ This displays the help message.
196=item B<-b --basedir> 244=item B<-b --basedir>
197 245
198The base directory uner which the target's modules will be found. This 246The base directory uner which the target's modules will be found. This
199defaults to the /lib/modules directory. 247defaults to the /lib/modules directory.
248
249If you don't specify the kernel version, this script will search for
250one under the specified based directory and use the first thing that
251looks like a kernel version.
200 252
201=item B<-k --kernel> 253=item B<-k --kernel>
202 254
203Kernel binary for the target. You must either supply a kernel binary 255Kernel binary for the target (vmlinux). You must either supply a kernel binary
204or a kernel symbol file (using the -F option). 256or a kernel symbol file (using the -F option).
205 257
206=item B<-F --kernelsyms> 258=item B<-F --kernelsyms>
207 259
208Kernel symbol file for the target. You must supply either a kernel symbol file 260Kernel symbol file for the target (System.map).
209kernel binary for the target (using the -k option).
210 261
211=item B<-n --stdout> 262=item B<-n --stdout>
212 263
213Write to stdout instead of modules.dep. This is currently hard coded... 264Write to stdout instead of modules.dep
214kernel binary for the target (using the -k option). 265kernel binary for the target (using the -k option).
215 266
216=item B<--verbose> 267=item B<--verbose>
217 268
218Be verbose (not implemented) 269Verbose (debug) output
219 270
220=back 271=back
221 272
222=head1 COPYRIGHT 273=head1 COPYRIGHT AND LICENSE
274
275 Copyright (c) 2001 David Schleef <ds@schleef.org>
276 Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
277 Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
278 Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
279 Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
223 280
224Copyright (c) 2001 David Schleef <ds@schleef.org>
225Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
226Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
227This program is free software; you can redistribute it and/or modify it 281This program is free software; you can redistribute it and/or modify it
228under the same terms as Perl itself. 282under the same terms as Perl itself.
229 283