Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 2 | # vi: set sw=4 ts=4: |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 3 | # Copyright (c) 2001 David Schleef <ds@schleef.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 4 | # Copyright (c) 2001 Erik Andersen <andersen@codepoet.org> |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 5 | # Copyright (c) 2001 Stuart Hughes <seh@zee2.com> |
"Steven J. Hill" | adb058b | 2002-10-08 21:33:51 +0000 | [diff] [blame] | 6 | # Copyright (c) 2002 Steven J. Hill <shill@broadcom.com> |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 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 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 14 | # This program is free software; you can redistribute it and/or modify it |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 15 | # under the same terms as Perl itself. |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 16 | use Getopt::Long; |
| 17 | use File::Find; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 18 | use strict; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 19 | |
| 20 | # Set up some default values |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 21 | my $kdir=""; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 22 | my $basedir=""; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 23 | my $kernel=""; |
| 24 | my $kernelsyms=""; |
Mike Frysinger | d611ccc | 2007-07-13 04:37:12 +0000 | [diff] [blame] | 25 | my $symprefix=""; |
"Steven J. Hill" | adb058b | 2002-10-08 21:33:51 +0000 | [diff] [blame] | 26 | my $stdout=0; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 27 | my $verbose=0; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 28 | my $help=0; |
Rob Landley | 9a990aa | 2006-06-02 21:30:40 +0000 | [diff] [blame] | 29 | my $nm = $ENV{'NM'} || "nm"; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 30 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 31 | # more globals |
| 32 | my (@liblist) = (); |
| 33 | my $exp = {}; |
| 34 | my $dep = {}; |
| 35 | my $mod = {}; |
| 36 | |
| 37 | my $usage = <<TXT; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 38 | $0 -b basedir { -k <vmlinux> | -F <System.map> } [options]... |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 39 | Where: |
Mike Frysinger | d611ccc | 2007-07-13 04:37:12 +0000 | [diff] [blame] | 40 | -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 Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 47 | TXT |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 48 | |
| 49 | # get command-line options |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 50 | GetOptions( |
Mike Frysinger | d611ccc | 2007-07-13 04:37:12 +0000 | [diff] [blame] | 51 | "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 Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 58 | ); |
| 59 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 60 | die $usage if $help; |
| 61 | die $usage unless $basedir && ( $kernel || $kernelsyms ); |
| 62 | die "can't use both -k and -F\n\n$usage" if $kernel && $kernelsyms; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 63 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 64 | # Strip any trailing or multiple slashes from basedir |
| 65 | $basedir =~ s-(/)\1+-/-g; |
| 66 | |
| 67 | # The base directory should contain /lib/modules somewhere |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 68 | if($basedir !~ m-/lib/modules-) { |
| 69 | warn "WARNING: base directory does not match ..../lib/modules\n"; |
| 70 | } |
| 71 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 72 | # if no kernel version is contained in the basedir, try to find one |
| 73 | if($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 |
| 91 | warn "**** Locating all modules\n" if $verbose; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 92 | find sub { |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 93 | my $file; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 94 | if ( -f $_ && ! -d $_ ) { |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 95 | $file = $File::Find::name; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 96 | if ( $file =~ /\.k?o$/ ) { |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 97 | push(@liblist, $file); |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 98 | warn "$file\n" if $verbose; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | }, $basedir; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 102 | warn "**** Finished locating modules\n" if $verbose; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 103 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 104 | foreach my $obj ( @liblist ){ |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 105 | # turn the input file name into a target tag name |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 106 | my ($tgtname) = $obj =~ m-(/lib/modules/.*)$-; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 107 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 108 | warn "\nMODULE = $tgtname\n" if $verbose; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 109 | |
| 110 | # get a list of symbols |
Rob Landley | 9a990aa | 2006-06-02 21:30:40 +0000 | [diff] [blame] | 111 | my @output=`$nm $obj`; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 112 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 113 | build_ref_tables($tgtname, \@output, $exp, $dep); |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 117 | # vmlinux is a special name that is only used to resolve symbols |
| 118 | my $tgtname = 'vmlinux'; |
Rob Landley | 9a990aa | 2006-06-02 21:30:40 +0000 | [diff] [blame] | 119 | my @output = $kernelsyms ? `cat $kernelsyms` : `$nm $kernel`; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 120 | warn "\nMODULE = $tgtname\n" if $verbose; |
| 121 | build_ref_tables($tgtname, \@output, $exp, $dep); |
| 122 | |
Mike Frysinger | fa6c484 | 2006-05-26 01:48:17 +0000 | [diff] [blame] | 123 | # resolve the dependencies for each module |
| 124 | # reduce dependencies: remove unresolvable and resolved from vmlinux/System.map |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 125 | # remove duplicates |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 126 | foreach my $module (keys %$dep) { |
| 127 | warn "reducing module: $module\n" if $verbose; |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 128 | $mod->{$module} = {}; |
| 129 | foreach (@{$dep->{$module}}) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 130 | if( $exp->{$_} ) { |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 131 | 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 137 | } |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 140 | # figure out where the output should go |
| 141 | if ($stdout == 0) { |
| 142 | open(STDOUT, ">$basedir/modules.dep") |
| 143 | or die "cannot open $basedir/modules.dep: $!"; |
| 144 | } |
| 145 | my $kseries = $basedir =~ m,/2\.6\.[^/]*, ? '2.6' : '2.4'; |
| 146 | |
| 147 | foreach 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 Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 159 | } |
| 160 | |
"Steven J. Hill" | adb058b | 2002-10-08 21:33:51 +0000 | [diff] [blame] | 161 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 162 | sub 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 Frysinger | d611ccc | 2007-07-13 04:37:12 +0000 | [diff] [blame] | 188 | push @{$dep->{$name}}, $symprefix . 'printk' unless $name eq 'vmlinux'; |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 189 | |
| 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 Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 199 | sub 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 Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 215 | depmod.pl - a cross platform script to generate kernel module |
| 216 | dependency lists (modules.conf) which can then be used by modprobe |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 217 | on the target platform. |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 218 | |
| 219 | It supports Linux 2.4 and 2.6 styles of modules.conf (auto-detected) |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 220 | |
| 221 | =head1 SYNOPSIS |
| 222 | |
"Steven J. Hill" | adb058b | 2002-10-08 21:33:51 +0000 | [diff] [blame] | 223 | depmod.pl [OPTION]... [basedir]... |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 224 | |
| 225 | Example: |
| 226 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 227 | depmod.pl -F linux/System.map -b target/lib/modules/2.6.11 |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 228 | |
| 229 | =head1 DESCRIPTION |
| 230 | |
| 231 | The purpose of this script is to automagically generate a list of of kernel |
Mike Frysinger | fa6c484 | 2006-05-26 01:48:17 +0000 | [diff] [blame] | 232 | module dependencies. This script produces dependency lists that should be |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 233 | identical to the depmod program from the modutils package. Unlike the depmod |
| 234 | binary, however, depmod.pl is designed to be run on your host system, not |
| 235 | on your target system. |
| 236 | |
| 237 | This script was written by David Schleef <ds@schleef.org> to be used in |
| 238 | conjunction with the BusyBox modprobe applet. |
| 239 | |
| 240 | =head1 OPTIONS |
| 241 | |
| 242 | =over 4 |
| 243 | |
| 244 | =item B<-h --help> |
| 245 | |
| 246 | This displays the help message. |
| 247 | |
| 248 | =item B<-b --basedir> |
| 249 | |
| 250 | The base directory uner which the target's modules will be found. This |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 251 | defaults to the /lib/modules directory. |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 252 | |
| 253 | If you don't specify the kernel version, this script will search for |
| 254 | one under the specified based directory and use the first thing that |
| 255 | looks like a kernel version. |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 256 | |
| 257 | =item B<-k --kernel> |
| 258 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 259 | Kernel binary for the target (vmlinux). You must either supply a kernel binary |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 260 | or a kernel symbol file (using the -F option). |
| 261 | |
| 262 | =item B<-F --kernelsyms> |
| 263 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 264 | Kernel symbol file for the target (System.map). |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 265 | |
| 266 | =item B<-n --stdout> |
| 267 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 268 | Write to stdout instead of modules.dep |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 269 | kernel binary for the target (using the -k option). |
| 270 | |
| 271 | =item B<--verbose> |
| 272 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 273 | Verbose (debug) output |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 274 | |
| 275 | =back |
| 276 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 277 | =head1 COPYRIGHT AND LICENSE |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 278 | |
Rob Landley | d049812 | 2006-03-21 16:35:50 +0000 | [diff] [blame] | 279 | 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 285 | This program is free software; you can redistribute it and/or modify it |
Eric Andersen | c126f8f | 2001-07-30 19:32:03 +0000 | [diff] [blame] | 286 | under the same terms as Perl itself. |
| 287 | |
| 288 | =head1 AUTHOR |
| 289 | |
| 290 | David Schleef <ds@schleef.org> |
| 291 | |
| 292 | =cut |