Patch from Stuart Hughes upgrading depmod.pl

This commit is contained in:
Rob Landley 2006-03-21 16:35:50 +00:00
parent 8dd4ca787a
commit d049812571

View File

@ -2,120 +2,125 @@
# vi: set ts=4: # vi: set ts=4:
# Copyright (c) 2001 David Schleef <ds@schleef.org> # Copyright (c) 2001 David Schleef <ds@schleef.org>
# Copyright (c) 2001 Erik Andersen <andersen@codepoet.org> # Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
# Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com> # Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
# Copyright (c) 2002 Steven J. Hill <shill@broadcom.com> # Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
# Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
#
# History:
# March 2006: Stuart Hughes <stuarth@freescale.com>.
# Significant updates, including implementing the '-F' option
# and adding support for 2.6 kernels.
# This program is free software; you can redistribute it and/or modify it # This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself. # under the same terms as Perl itself.
# TODO -- use strict mode...
#use strict;
use Getopt::Long; use Getopt::Long;
use File::Find; use File::Find;
use strict;
# Set up some default values # Set up some default values
my $kdir="";
my $basedir=""; my $basedir="";
my $kernel; my $kernel="";
my $kernelsyms; my $kernelsyms="";
my $stdout=0; my $stdout=0;
my $verbose=0; my $verbose=0;
my $help=0;
# more globals
my (@liblist) = ();
my $exp = {};
my $dep = {};
my $mod = {};
my $usage = <<TXT;
$0 -b basedir { -k <vmlinux> | -F <System.map> } [options]...
Where:
-h --help : Show this help screen
-b --basedir : Modules base directory (e.g /lib/modules/<2.x.y>)
-k --kernel : Kernel binary for the target (e.g. vmlinux)
-F --kernelsyms : Kernel symbol file (e.g. System.map)
-n --stdout : Write to stdout instead of <basedir>/modules.dep
-v --verbose : Print out lots of debugging stuff
TXT
# get command-line options # get command-line options
my %opt;
GetOptions( GetOptions(
\%opt, "help|h" => \$help,
"help|h", "basedir|b=s" => \$basedir,
"basedir|b=s" => \$basedir, "kernel|k=s" => \$kernel,
"kernel|k=s" => \$kernel,
"kernelsyms|F=s" => \$kernelsyms, "kernelsyms|F=s" => \$kernelsyms,
"stdout|n" => \$stdout, "stdout|n" => \$stdout,
"verbose|v" => \$verbose, "verbose|v" => \$verbose,
); );
if (defined $opt{help}) { die $usage if $help;
print die $usage unless $basedir && ( $kernel || $kernelsyms );
" $0 [OPTION]... [basedir]\n", die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms;
" -h --help\t\tShow this help screen\n",
" -b --basedir\tModules base directory (defaults to /lib/modules)\n",
" -k --kernel\tKernel binary for the target\n",
" -F --kernelsyms\tKernel symbol file\n",
" -n --stdout\tWrite to stdout instead of <basedir>/modules.dep\n",
" -v --verbose\tPrint out lots of debugging stuff\n",
;
exit 1;
}
# Strip any trailing or multiple slashes from basedir
$basedir =~ s-(/)\1+-/-g;
# The base directory should contain /lib/modules somewhere
if($basedir !~ m-/lib/modules-) { if($basedir !~ m-/lib/modules-) {
warn "WARNING: base directory does not match ..../lib/modules\n"; warn "WARNING: base directory does not match ..../lib/modules\n";
} }
# Find the list of .o files living under $basedir # if no kernel version is contained in the basedir, try to find one
#if ($verbose) { printf "Locating all modules\n"; } if($basedir !~ m-/lib/modules/\d\.\d-) {
my($ofile) = ""; opendir(BD, $basedir) or die "can't open basedir $basedir : $!\n";
my($file) = ""; foreach ( readdir(BD) ) {
my(@liblist) = (); next if /^\.\.?$/;
next unless -d "$basedir/$_";
warn "dir = $_\n" if $verbose;
if( /^\d\.\d/ ) {
$kdir = $_;
warn("Guessed module directory as $basedir/$kdir\n");
last;
}
}
closedir(BD);
die "Cannot find a kernel version under $basedir\n" unless $kdir;
$basedir = "$basedir/$kdir";
}
# Find the list of .o or .ko files living under $basedir
warn "**** Locating all modules\n" if $verbose;
find sub { find sub {
my $file;
if ( -f $_ && ! -d $_ ) { if ( -f $_ && ! -d $_ ) {
$file = $File::Find::name; $file = $File::Find::name;
if ( $file =~ /.o$/ ) { if ( $file =~ /\.k?o$/ ) {
push(@liblist, $file); push(@liblist, $file);
if ($verbose) { printf "$file\n"; } warn "$file\n" if $verbose;
} }
} }
}, $basedir; }, $basedir;
if ($verbose) { printf "Finished locating modules\n"; } warn "**** Finished locating modules\n" if $verbose;
foreach $obj ( @liblist, $kernel ){ foreach my $obj ( @liblist ){
# turn the input file name into a target tag name # turn the input file name into a target tag name
# vmlinux is a special that is only used to resolve symbols my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
if($obj =~ /vmlinux/) {
$tgtname = "vmlinux";
} else {
($tgtname) = $obj =~ m-(/lib/modules/.*)$-;
}
warn "MODULE = $tgtname\n" if $verbose; warn "\nMODULE = $tgtname\n" if $verbose;
# get a list of symbols # get a list of symbols
@output=`nm $obj`; my @output=`nm $obj`;
$ksymtab=grep m/ __ksymtab/, @output;
# gather the exported symbols build_ref_tables($tgtname, \@output, $exp, $dep);
if($ksymtab){
# explicitly exported
foreach ( @output ) {
/ __ksymtab_(.*)$/ and do {
warn "sym = $1\n" if $verbose;
$exp->{$1} = $tgtname;
};
}
} else {
# exporting all symbols
foreach ( @output) {
/ [ABCDGRST] (.*)$/ and do {
warn "syma = $1\n" if $verbose;
$exp->{$1} = $tgtname;
};
}
}
# gather the unresolved symbols
foreach ( @output ) {
!/ __this_module/ && / U (.*)$/ and do {
warn "und = $1\n" if $verbose;
push @{$dep->{$tgtname}}, $1;
};
}
} }
# reduce dependancies: remove unresolvable and resolved from vmlinux # vmlinux is a special name that is only used to resolve symbols
my $tgtname = 'vmlinux';
my @output = $kernelsyms ? `cat $kernelsyms` : `nm $kernel`;
warn "\nMODULE = $tgtname\n" if $verbose;
build_ref_tables($tgtname, \@output, $exp, $dep);
# resolve the dependancies for each module
# reduce dependancies: remove unresolvable and resolved from vmlinux/System.map
# remove duplicates # remove duplicates
foreach $module (keys %$dep) { foreach my $module (keys %$dep) {
warn "reducing module: $module\n" if $verbose;
$mod->{$module} = {}; $mod->{$module} = {};
foreach (@{$dep->{$module}}) { foreach (@{$dep->{$module}}) {
if( $exp->{$_} ) { if( $exp->{$_} ) {
@ -128,25 +133,65 @@ foreach $module (keys %$dep) {
} }
} }
# resolve the dependancies for each module # figure out where the output should go
if ($stdout == 1) { if ($stdout == 0) {
foreach $module ( keys %$mod ) { open(STDOUT, ">$basedir/modules.dep")
print "$module:\t"; or die "cannot open $basedir/modules.dep: $!";
@sorted = sort bydep keys %{$mod->{$module}}; }
print join(" \\\n\t",@sorted); my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4';
print "\n\n";
} foreach my $module ( keys %$mod ) {
} else { if($kseries eq '2.4') {
open(OFILE, ">$basedir/modules.dep"); print "$module:\t";
foreach $module ( keys %$mod ) { my @sorted = sort bydep keys %{$mod->{$module}};
print OFILE "$module:\t"; print join(" \\\n\t",@sorted);
@sorted = sort bydep keys %{$mod->{$module}}; print "\n\n";
print OFILE join(" \\\n\t",@sorted); } else {
print OFILE "\n\n"; print "$module: ";
} my @sorted = sort bydep keys %{$mod->{$module}};
print join(" ",@sorted);
print "\n";
}
} }
sub build_ref_tables
{
my ($name, $sym_ar, $exp, $dep) = @_;
my $ksymtab = grep m/ __ksymtab/, @$sym_ar;
# gather the exported symbols
if($ksymtab){
# explicitly exported
foreach ( @$sym_ar ) {
/ __ksymtab_(.*)$/ and do {
warn "sym = $1\n" if $verbose;
$exp->{$1} = $name;
};
}
} else {
# exporting all symbols
foreach ( @$sym_ar ) {
/ [ABCDGRST] (.*)$/ and do {
warn "syma = $1\n" if $verbose;
$exp->{$1} = $name;
};
}
}
# this takes makes sure modules with no dependencies get listed
push @{$dep->{$name}}, 'printk' unless $name eq 'vmlinux';
# gather the unresolved symbols
foreach ( @$sym_ar ) {
!/ __this_module/ && / U (.*)$/ and do {
warn "und = $1\n" if $verbose;
push @{$dep->{$name}}, $1;
};
}
}
sub bydep sub bydep
{ {
foreach my $f ( keys %{$mod->{$b}} ) { foreach my $f ( keys %{$mod->{$b}} ) {
@ -163,8 +208,11 @@ __END__
=head1 NAME =head1 NAME
depmod.pl - a cross platform script to generate kernel module dependency depmod.pl - a cross platform script to generate kernel module
lists which can then be used by modprobe. dependency lists (modules.conf) which can then be used by modprobe
on the target platform.
It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected)
=head1 SYNOPSIS =head1 SYNOPSIS
@ -172,7 +220,7 @@ depmod.pl [OPTION]... [basedir]...
Example: Example:
depmod.pl -F linux/System.map target/lib/modules depmod.pl -F linux/System.map -b target/lib/modules/2.6.11
=head1 DESCRIPTION =head1 DESCRIPTION
@ -196,34 +244,40 @@ This displays the help message.
=item B<-b --basedir> =item B<-b --basedir>
The base directory uner which the target's modules will be found. This The base directory uner which the target's modules will be found. This
defaults to the /lib/modules directory. defaults to the /lib/modules directory.
If you don't specify the kernel version, this script will search for
one under the specified based directory and use the first thing that
looks like a kernel version.
=item B<-k --kernel> =item B<-k --kernel>
Kernel binary for the target. You must either supply a kernel binary Kernel binary for the target (vmlinux). You must either supply a kernel binary
or a kernel symbol file (using the -F option). or a kernel symbol file (using the -F option).
=item B<-F --kernelsyms> =item B<-F --kernelsyms>
Kernel symbol file for the target. You must supply either a kernel symbol file Kernel symbol file for the target (System.map).
kernel binary for the target (using the -k option).
=item B<-n --stdout> =item B<-n --stdout>
Write to stdout instead of modules.dep. This is currently hard coded... Write to stdout instead of modules.dep
kernel binary for the target (using the -k option). kernel binary for the target (using the -k option).
=item B<--verbose> =item B<--verbose>
Be verbose (not implemented) Verbose (debug) output
=back =back
=head1 COPYRIGHT =head1 COPYRIGHT AND LICENSE
Copyright (c) 2001 David Schleef <ds@schleef.org>
Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
Copyright (c) 2001 Stuart Hughes <seh@zee2.com>
Copyright (c) 2002 Steven J. Hill <shill@broadcom.com>
Copyright (c) 2006 Freescale Semiconductor, Inc <stuarth@freescale.com>
Copyright (c) 2001 David Schleef <ds@schleef.org>
Copyright (c) 2001 Erik Andersen <andersen@codepoet.org>
Copyright (c) 2001 Stuart Hughes <stuarth@lineo.com>
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. under the same terms as Perl itself.