blob: ca2e8f76d8b912ec6ac3bb884f036351c40dc889 [file] [log] [blame]
Eric Andersenc126f8f2001-07-30 19:32:03 +00001#!/usr/bin/perl -w
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00002# vi: set sw=4 ts=4:
Eric Andersenc126f8f2001-07-30 19:32:03 +00003# Copyright (c) 2001 David Schleef <ds@schleef.org>
Eric Andersencb81e642003-07-14 21:21:08 +00004# Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
Rob Landleyd0498122006-03-21 16:35:50 +00005# Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
"Steven J. Hill"adb058b2002-10-08 21:33:51 +00006# Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
Rob Landleyd0498122006-03-21 16:35:50 +00007# 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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000014# This program is free software; you can redistribute it and/or modify it
Eric Andersenc126f8f2001-07-30 19:32:03 +000015# under the same terms as Perl itself.
Eric Andersenc126f8f2001-07-30 19:32:03 +000016use Getopt::Long;
17use File::Find;
Rob Landleyd0498122006-03-21 16:35:50 +000018use strict;
Eric Andersenc126f8f2001-07-30 19:32:03 +000019
20# Set up some default values
Rob Landleyd0498122006-03-21 16:35:50 +000021my $kdir="";
Eric Andersenc126f8f2001-07-30 19:32:03 +000022my $basedir="";
Rob Landleyd0498122006-03-21 16:35:50 +000023my $kernel="";
24my $kernelsyms="";
Mike Frysingerd611ccc2007-07-13 04:37:12 +000025my $symprefix="";
"Steven J. Hill"adb058b2002-10-08 21:33:51 +000026my $stdout=0;
Eric Andersenc126f8f2001-07-30 19:32:03 +000027my $verbose=0;
Rob Landleyd0498122006-03-21 16:35:50 +000028my $help=0;
Rob Landley9a990aa2006-06-02 21:30:40 +000029my $nm = $ENV{'NM'} || "nm";
Eric Andersenc126f8f2001-07-30 19:32:03 +000030
Rob Landleyd0498122006-03-21 16:35:50 +000031# more globals
32my (@liblist) = ();
33my $exp = {};
34my $dep = {};
35my $mod = {};
36
37my $usage = <<TXT;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000038$0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
Rob Landleyd0498122006-03-21 16:35:50 +000039 Where:
Mike Frysingerd611ccc2007-07-13 04:37:12 +000040 -h --help : Show this help screen
41 -b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
42 -k --kernel : Kernel binary for the target (e.g. vmlinux)
43 -F --kernelsyms : Kernel symbol file (e.g. System.map)
44 -n --stdout : Write to stdout instead of <basedir>/modules.dep
45 -v --verbose : Print out lots of debugging stuff
46 -P --symbol-prefix : Symbol prefix
Rob Landleyd0498122006-03-21 16:35:50 +000047TXT
Eric Andersenc126f8f2001-07-30 19:32:03 +000048
49# get command-line options
Eric Andersenc126f8f2001-07-30 19:32:03 +000050GetOptions(
Mike Frysingerd611ccc2007-07-13 04:37:12 +000051 "help|h" => \$help,
52 "basedir|b=s" => \$basedir,
53 "kernel|k=s" => \$kernel,
54 "kernelsyms|F=s" => \$kernelsyms,
55 "stdout|n" => \$stdout,
56 "verbose|v" => \$verbose,
57 "symbol-prefix|P=s" => \$symprefix,
Eric Andersenc126f8f2001-07-30 19:32:03 +000058);
59
Rob Landleyd0498122006-03-21 16:35:50 +000060die $usage if $help;
61die $usage unless $basedir && ( $kernel || $kernelsyms );
62die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
Eric Andersenc126f8f2001-07-30 19:32:03 +000063
Rob Landleyd0498122006-03-21 16:35:50 +000064# Strip any trailing or multiple slashes from basedir
65$basedir =~ s-(/)\1+-/-g;
66
67# The base directory should contain /lib/modules somewhere
Eric Andersenc126f8f2001-07-30 19:32:03 +000068if($basedir !~ m-/lib/modules-) {
69 warn "WARNING: base directory does not match ..../lib/modules\n";
70}
71
Rob Landleyd0498122006-03-21 16:35:50 +000072# if no kernel version is contained in the basedir, try to find one
73if($basedir !~ m-/lib/modules/\d\.\d-) {
74 opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
75 foreach ( readdir(BD) ) {
76 next if /^\.\.?$/;
77 next unless -d "$basedir/$_";
78 warn "dir = $_\n" if $verbose;
79 if( /^\d\.\d/ ) {
80 $kdir = $_;
81 warn("Guessed module directory as $basedir/$kdir\n");
82 last;
83 }
84 }
85 closedir(BD);
86 die "Cannot find a kernel version under $basedir\n" unless $kdir;
87 $basedir = "$basedir/$kdir";
88}
89
90# Find the list of .o or .ko files living under $basedir
91warn "**** Locating all modules\n" if $verbose;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092find sub {
Rob Landleyd0498122006-03-21 16:35:50 +000093 my $file;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094 if ( -f $_ && ! -d $_ ) {
Eric Andersenc126f8f2001-07-30 19:32:03 +000095 $file = $File::Find::name;
Rob Landleyd0498122006-03-21 16:35:50 +000096 if ( $file =~ /\.k?o$/ ) {
Eric Andersenc126f8f2001-07-30 19:32:03 +000097 push(@liblist, $file);
Rob Landleyd0498122006-03-21 16:35:50 +000098 warn "$file\n" if $verbose;
Eric Andersenc126f8f2001-07-30 19:32:03 +000099 }
100 }
101}, $basedir;
Rob Landleyd0498122006-03-21 16:35:50 +0000102warn "**** Finished locating modules\n" if $verbose;
Eric Andersenc126f8f2001-07-30 19:32:03 +0000103
Rob Landleyd0498122006-03-21 16:35:50 +0000104foreach my $obj ( @liblist ){
Eric Andersenc126f8f2001-07-30 19:32:03 +0000105 # turn the input file name into a target tag name
Rob Landleyd0498122006-03-21 16:35:50 +0000106 my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
Eric Andersenc126f8f2001-07-30 19:32:03 +0000107
Rob Landleyd0498122006-03-21 16:35:50 +0000108 warn "\nMODULE = $tgtname\n" if $verbose;
Eric Andersenc126f8f2001-07-30 19:32:03 +0000109
110 # get a list of symbols
Rob Landley9a990aa2006-06-02 21:30:40 +0000111 my @output=`$nm $obj`;
Eric Andersenc126f8f2001-07-30 19:32:03 +0000112
Rob Landleyd0498122006-03-21 16:35:50 +0000113 build_ref_tables($tgtname, \@output, $exp, $dep);
Eric Andersenc126f8f2001-07-30 19:32:03 +0000114}
115
116
Rob Landleyd0498122006-03-21 16:35:50 +0000117# vmlinux is a special name that is only used to resolve symbols
118my $tgtname = 'vmlinux';
Rob Landley9a990aa2006-06-02 21:30:40 +0000119my @output = $kernelsyms ? `cat $kernelsyms` : `$nm $kernel`;
Rob Landleyd0498122006-03-21 16:35:50 +0000120warn "\nMODULE = $tgtname\n" if $verbose;
121build_ref_tables($tgtname, \@output, $exp, $dep);
122
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000123# resolve the dependencies for each module
124# reduce dependencies: remove unresolvable and resolved from vmlinux/System.map
Eric Andersenc126f8f2001-07-30 19:32:03 +0000125# remove duplicates
Rob Landleyd0498122006-03-21 16:35:50 +0000126foreach my $module (keys %$dep) {
127 warn "reducing module: $module\n" if $verbose;
Eric Andersenc126f8f2001-07-30 19:32:03 +0000128 $mod->{$module} = {};
129 foreach (@{$dep->{$module}}) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130 if( $exp->{$_} ) {
Eric Andersenc126f8f2001-07-30 19:32:03 +0000131 warn "resolved symbol $_ in file $exp->{$_}\n" if $verbose;
132 next if $exp->{$_} =~ /vmlinux/;
133 $mod->{$module}{$exp->{$_}} = 1;
134 } else {
135 warn "unresolved symbol $_ in file $module\n";
136 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137 }
Eric Andersenc126f8f2001-07-30 19:32:03 +0000138}
139
Rob Landleyd0498122006-03-21 16:35:50 +0000140# figure out where the output should go
141if ($stdout == 0) {
142 open(STDOUT, ">$basedir/modules.dep")
143 or die "cannot open $basedir/modules.dep: $!";
144}
145my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
146
147foreach my $module ( keys %$mod ) {
148 if($kseries eq '2.4') {
149 print "$module:\t";
150 my @sorted = sort bydep keys %{$mod->{$module}};
151 print join(" \\\n\t",@sorted);
152 print "\n\n";
153 } else {
154 print "$module: ";
155 my @sorted = sort bydep keys %{$mod->{$module}};
156 print join(" ",@sorted);
157 print "\n";
158 }
Eric Andersenc126f8f2001-07-30 19:32:03 +0000159}
160
"Steven J. Hill"adb058b2002-10-08 21:33:51 +0000161
Rob Landleyd0498122006-03-21 16:35:50 +0000162sub build_ref_tables
163{
164 my ($name, $sym_ar, $exp, $dep) = @_;
165
166 my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
167
168 # gather the exported symbols
169 if($ksymtab){
170 # explicitly exported
171 foreach ( @$sym_ar ) {
172 / __ksymtab_(.*)$/ and do {
173 warn "sym = $1\n" if $verbose;
174 $exp->{$1} = $name;
175 };
176 }
177 } else {
178 # exporting all symbols
179 foreach ( @$sym_ar ) {
180 / [ABCDGRST] (.*)$/ and do {
181 warn "syma = $1\n" if $verbose;
182 $exp->{$1} = $name;
183 };
184 }
185 }
186
187 # this takes makes sure modules with no dependencies get listed
Mike Frysingerd611ccc2007-07-13 04:37:12 +0000188 push @{$dep->{$name}}, $symprefix . 'printk' unless $name eq 'vmlinux';
Rob Landleyd0498122006-03-21 16:35:50 +0000189
190 # gather the unresolved symbols
191 foreach ( @$sym_ar ) {
192 !/ __this_module/ && / U (.*)$/ and do {
193 warn "und = $1\n" if $verbose;
194 push @{$dep->{$name}}, $1;
195 };
196 }
197}
198
Eric Andersenc126f8f2001-07-30 19:32:03 +0000199sub bydep
200{
201 foreach my $f ( keys %{$mod->{$b}} ) {
202 if($f eq $a) {
203 return 1;
204 }
205 }
206 return -1;
207}
208
209
210
211__END__
212
213=head1 NAME
214
Rob Landleyd0498122006-03-21 16:35:50 +0000215depmod.pl - a cross platform script to generate kernel module
216dependency lists (modules.conf) which can then be used by modprobe
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000217on the target platform.
Rob Landleyd0498122006-03-21 16:35:50 +0000218
219It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
Eric Andersenc126f8f2001-07-30 19:32:03 +0000220
221=head1 SYNOPSIS
222
"Steven J. Hill"adb058b2002-10-08 21:33:51 +0000223depmod.pl [OPTION]... [basedir]...
Eric Andersenc126f8f2001-07-30 19:32:03 +0000224
225Example:
226
Rob Landleyd0498122006-03-21 16:35:50 +0000227 depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
Eric Andersenc126f8f2001-07-30 19:32:03 +0000228
229=head1 DESCRIPTION
230
231The purpose of this script is to automagically generate a list of of kernel
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000232module dependencies. This script produces dependency lists that should be
Eric Andersenc126f8f2001-07-30 19:32:03 +0000233identical to the depmod program from the modutils package. Unlike the depmod
234binary, however, depmod.pl is designed to be run on your host system, not
235on your target system.
236
237This script was written by David Schleef <ds@schleef.org> to be used in
238conjunction with the BusyBox modprobe applet.
239
240=head1 OPTIONS
241
242=over 4
243
244=item B<-h --help>
245
246This displays the help message.
247
248=item B<-b --basedir>
249
250The base directory uner which the target's modules will be found. This
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000251defaults to the /lib/modules directory.
Rob Landleyd0498122006-03-21 16:35:50 +0000252
253If you don't specify the kernel version, this script will search for
254one under the specified based directory and use the first thing that
255looks like a kernel version.
Eric Andersenc126f8f2001-07-30 19:32:03 +0000256
257=item B<-k --kernel>
258
Rob Landleyd0498122006-03-21 16:35:50 +0000259Kernel binary for the target (vmlinux). You must either supply a kernel binary
Eric Andersenc126f8f2001-07-30 19:32:03 +0000260or a kernel symbol file (using the -F option).
261
262=item B<-F --kernelsyms>
263
Rob Landleyd0498122006-03-21 16:35:50 +0000264Kernel symbol file for the target (System.map).
Eric Andersenc126f8f2001-07-30 19:32:03 +0000265
266=item B<-n --stdout>
267
Rob Landleyd0498122006-03-21 16:35:50 +0000268Write to stdout instead of modules.dep
Eric Andersenc126f8f2001-07-30 19:32:03 +0000269kernel binary for the target (using the -k option).
270
271=item B<--verbose>
272
Rob Landleyd0498122006-03-21 16:35:50 +0000273Verbose (debug) output
Eric Andersenc126f8f2001-07-30 19:32:03 +0000274
275=back
276
Rob Landleyd0498122006-03-21 16:35:50 +0000277=head1 COPYRIGHT AND LICENSE
Eric Andersenc126f8f2001-07-30 19:32:03 +0000278
Rob Landleyd0498122006-03-21 16:35:50 +0000279 Copyright (c) 2001 David Schleef <ds@schleef.org>
280 Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
281 Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
282 Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
283 Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
284
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000285This program is free software; you can redistribute it and/or modify it
Eric Andersenc126f8f2001-07-30 19:32:03 +0000286under the same terms as Perl itself.
287
288=head1 AUTHOR
289
290David Schleef <ds@schleef.org>
291
292=cut