Samba: version choice
[tomato.git] / release / src / btools / libfoo.pl
blob6d103afb313b06984370ebf75c357098921577bf
1 #!/usr/bin/perl
3 # libfoo.pl
4 # Copyright (C) 2006-2008 Jonathan Zarate
6 # - strip un-needed objects
7 # - create xref of symbols used
10 sub error
12 print STDERR "\n*** ERROR: " . (shift) . "\n\n";
13 exit 1;
16 sub basename
18 my $fn = shift;
19 if ($fn =~ /([^\/]+)$/) {
20 return $1;
22 return $fn;
25 sub load
27 my $fname = shift;
29 if ((-l $fname) ||
30 ($fname =~ /\/lib\/modules\/\d+\.\d+\.\d+/) ||
31 ($fname =~ /\.(asp|gif|png|svg|js|jsx|css|txt|pat|sh)$/)) {
32 return;
35 if (-d $fname) {
36 my $d;
37 if (opendir($d, $fname)) {
38 foreach (readdir($d)) {
39 if ($_ !~ /^\./) {
40 load($fname . "/" . $_);
43 closedir($d);
45 return;
49 my $f;
50 my $base;
51 my $ok;
52 my $s;
54 $base = basename($fname);
55 print LOG "\n\nreadelf $base:\n";
57 open($f, "mipsel-linux-readelf -WhsdD ${fname} 2>&1 |") || error("readelf - $!\n");
59 while (<$f>) {
60 print LOG;
62 if (/\s+Type:\s+(\w+)/) {
63 $elf_type{$base} = $1;
64 $ok = 1;
65 last;
69 if (!$ok) {
70 close($f);
71 return;
74 print "$elf_type{$base} $base", " " x 30, "\r";
76 push(@elfs, $base);
78 while (<$f>) {
79 print LOG;
81 if (/\(NEEDED\)\s+Shared library: \[(.+)\]/) {
82 push(@{$elf_lib{$base}}, $1);
84 elsif (/Symbol table for image:/) {
85 last;
89 while (<$f>) {
90 print LOG;
92 if (/\s+(WEAK|GLOBAL)\s+(?:DEFAULT|VISIBLE)\s+(\w+)\s+(\w+)/) {
93 $s = $3;
94 if ($2 eq 'UND') {
95 if ($1 eq 'GLOBAL') {
96 $elf_ext{$base}{$s} = 1;
98 else {
99 print LOG "*** not GLOBAL\n";
102 elsif ($2 eq 'ABS') {
104 elsif ($2 =~ /^\d+$/) {
105 $elf_exp{$base}{$s} = 1;
107 else {
108 print LOG "*** unknown type\n";
111 elsif (!/Num Buc:/) {
112 print LOG "*** strange line\n";
116 close($f);
119 sub fixDynDep
121 my ($user, $dep) = @_;
123 if (!defined $elf_dyn{$user}{$dep}) {
124 push(@{$elf_lib{$user}}, $dep);
125 $elf_dyn{$user}{$dep} = 1;
127 print LOG "FixDynDep: $user = $dep\n";
131 sub fixDyn
133 my $s;
135 foreach (@elfs) {
136 if (/^libipt_.+\.so$/) {
137 fixDynDep("iptables", $_);
139 elsif (/^CP\d+\.so$/) {
140 fixDynDep("smbd", $_);
144 fixDynDep("l2tpd", "cmd.so");
145 fixDynDep("l2tpd", "sync-pppd.so");
147 # fixDynDep("libbcm.so", "libshared.so");
148 # fixDynDep("libbcm.so", "libc.so.0");
150 #!!TB - Updated Broadcom WL driver
151 fixDynDep("libbcmcrypto.so", "libc.so.0");
152 fixDynDep("nas", "libbcmcrypto.so");
153 fixDynDep("wl", "libbcmcrypto.so");
154 fixDynDep("nas", "libc.so.0");
155 fixDynDep("wl", "libc.so.0");
158 sub usersOf
160 my $name = shift;
161 my $sym = shift;
162 my @x;
163 my $e;
164 my $l;
166 @x = ();
167 foreach $e (@elfs) {
168 foreach $l (@{$elf_lib{$e}}) {
169 if ($l eq $name) {
170 if ((!defined $sym) || (defined $elf_ext{$e}{$sym})) {
171 push(@x, $e);
173 last;
177 return @x;
180 sub resolve
182 my $name = shift;
183 my $sym = shift;
184 my $l;
186 foreach $l (@{$elf_lib{$name}}) {
187 # print "\n$l $sym ", $elf_exp{$l}{$sym}, "\n";
188 return $l if (defined $elf_exp{$l}{$sym});
190 return "*** unresolved ***";
193 sub fillGaps
195 my $name;
196 my $sym;
197 my @users;
198 my $u;
199 my $t;
200 my $found;
202 # print "Resolving implicit links...\n";
204 foreach $name (@elfs) {
205 foreach $sym (keys %{$elf_ext{$name}}) {
206 $found = 0;
208 if ($sym eq '__uClibc_start_main') {
209 $sym = '__uClibc_main';
212 if (resolve($name, $sym) eq "*** unresolved ***") {
213 @users = usersOf($name);
214 foreach $u (@users) {
215 # if exported by $u
216 if (defined $elf_exp{$u}{$sym}) {
217 fixDynDep($name, $u);
218 $found = 1;
220 # if exported by shared libs of $u
221 if (($t = resolve($u, $sym)) ne "*** unresolved ***") {
222 fixDynDep($name, $t);
223 $found = 1;
227 if ($found == 0) {
228 print "Unable to resolve $sym used by $name\n", @users;
229 exit 1;
236 sub tab
238 my $current = shift;
239 my $target = shift;
240 my $s = "";
241 my $n;
243 while (1) {
244 $n = $current + (4 - ($current % 4));
245 last if ($n > $target);
246 $s = $s . "\t";
247 $current = $n;
249 while ($current < $target) {
250 $s = $s . " ";
251 $current++;
253 return $s;
256 sub genXref
258 my $f;
259 my $fname;
260 my $s;
261 my @u;
263 # print "Generating Xref Report...\n";
265 open($f, ">libfoo_xref.txt");
266 foreach $fname (sort keys %elf_type) {
267 print $f "$fname:\n";
269 if (scalar(@{$elf_lib{$fname}}) > 0) {
270 print $f "Dependency:\n";
271 foreach $s (sort @{$elf_lib{$fname}}) {
272 print $f "\t$s", defined $elf_dyn{$fname}{$s} ? " (dyn)\n" : "\n";
276 if (scalar(keys %{$elf_exp{$fname}}) > 0) {
277 print $f "Export:\n";
278 foreach $s (sort keys %{$elf_exp{$fname}}) {
279 @u = usersOf($fname, $s);
280 if (scalar(@u) > 0) {
281 print $f "\t$s", tab(length($s) + 4, 40), " > ", join(",", @u), "\n";
283 else {
284 print $f "\t$s\n";
289 if (scalar(keys %{$elf_ext{$fname}}) > 0) {
290 print $f "External:\n";
291 foreach $s (sort keys %{$elf_ext{$fname}}) {
292 print $f "\t$s", tab(length($s) + 4, 40), " < ", resolve($fname, $s), "\n";
296 print $f "\n";
298 close($f);
302 sub genSO
304 my ($so, $arc, $opt) = @_;
305 my $name = basename($so);
306 my $sym;
307 my $fn;
308 my $inuse;
309 my @used;
310 my @unused;
311 my $cmd;
312 my $before, $after;
314 if (!-f $so) {
315 print "$name: not found, skipping...\n";
316 return 0;
319 #!!TB
320 if (!-f $arc) {
321 print "$arc: not found, skipping...\n";
322 return 0;
325 foreach $sym (sort keys %{$elf_exp{$name}}) {
326 if (scalar(usersOf($name, $sym)) > 0) {
327 push(@used, $sym);
329 else {
330 push(@unused, $sym);
334 # print "\n$name: Attempting to link ", scalar(@used), " and remove ", scalar(@unused), " objects...\n";
336 print LOG "\n\n${base}\n";
338 $cmd = "mipsel-uclibc-ld -shared -s -z combreloc --warn-common --fatal-warnings ${opt} -soname ${name} -o ${so}";
339 foreach (@{$elf_lib{$name}}) {
340 if ((!$elf_dyn{$name}{$_}) && (/^lib(.+)\.so/)) {
341 $cmd .= " -l$1";
343 else {
344 # print LOG "Not marking for linkage: $_\n";
347 # print "$cmd -u... ${arc}\n";
348 if (scalar(@used) == 0) {
349 print "$name: WARNING: Library is not used by anything, deleting...\n";
350 unlink $so;
351 # <>;
352 return 0;
354 $cmd .= " -u " . join(" -u ", @used) . " ". $arc;
356 print LOG "Command: $cmd\n";
357 print LOG "Used: ", join(",", @used), "\n";
358 print LOG "Unused: ", join(",", @unused), "\n";
360 $before = -s $so;
362 system($cmd);
363 if ($? != 0) {
364 error("ld returned $?");
367 $after = -s $so;
369 print "$name: Attempted to remove ", scalar(@unused), "/", scalar(@unused) + scalar(@used), " symbols. ";
370 printf "%.2fK - %.2fK = %.2fK\n", $before / 1024, $after / 1024, ($before - $after) / 1024;
372 # print "\n$name: Attempting to link ", scalar(@used), " and remove ", scalar(@unused), " objects...\n";
373 # printf "Before: %.2fK / After: %.2fK / Removed: %.2fK\n\n", $before / 1024, $after / 1024, ($before - $after) / 1024;
375 return ($before > $after)
383 # print "\nlibfoo.pl - fooify shared libraries\n";
384 # print "Copyright (C) 2006-2007 Jonathan Zarate\n\n";
386 $root = $ENV{"TARGETDIR"};
387 $uclibc = $ENV{"TOOLCHAIN"};
388 $router = $ENV{"SRCBASE"} . "/router";
390 if ((!-d $root) || (!-d $uclibc) || (!-d $router)) {
391 print "Missing or invalid environment variables\n";
392 exit(1);
395 #open(LOG, ">libfoo.debug");
396 open(LOG, ">/dev/null");
398 print "Loading...\r";
399 load($root);
400 print "Finished loading files.", " " x 30, "\r";
402 fixDyn();
403 fillGaps();
405 genXref();
408 genSO("${root}/lib/libc.so.0", "${uclibc}/lib/libc.a", "-init __uClibc_init ${uclibc}/lib/optinfo/interp.os");
409 genSO("${root}/lib/libresolv.so.0", "${uclibc}/lib/libresolv.a");
410 genSO("${root}/lib/libcrypt.so.0", "${uclibc}/lib/libcrypt.a");
411 genSO("${root}/lib/libm.so.0", "${uclibc}/lib/libm.a");
412 genSO("${root}/lib/libpthread.so.0", "${uclibc}/lib/libpthread.a");
413 genSO("${root}/lib/libutil.so.0", "${uclibc}/lib/libutil.a");
414 # genSO("${root}/lib/libdl.so.0", "${uclibc}/lib/libdl.a");
415 # genSO("${root}/lib/libnsl.so.0", "${uclibc}/lib/libnsl.a");
417 genSO("${root}/usr/lib/libcrypto.so", "${router}/openssl/libcrypto.a");
418 genSO("${root}/usr/lib/libzebra.so", "${router}/zebra/lib/libzebra.a");
420 #!!TB - Samba
421 genSO("${root}/usr/lib/libsmb.so", "${router}/samba/source/bin/libsmb.a");
422 genSO("${root}/usr/lib/libbigballofmud.so", "${router}/samba3/source/bin/libbigballofmud.a");
423 #!!TB - FTP SSL
424 genSO("${root}/usr/lib/libssl.so", "${router}/openssl/libssl.a");
426 # genSO("${root}/usr/lib/libtamba.so", "${router}/samba3/source/bin/libtamba.a");
427 # genSO("${root}/usr/lib/libiptc.so", "${router}/iptables/libiptc/libiptc.a");
428 # genSO("${root}/usr/lib/libshared.so", "${router}/shared/libshared.a");
429 # genSO("${root}/usr/lib/libnvram.so", "${router}/nvram/libnvram.a");
431 #!!TB - Updated Broadcom WL driver
432 genSO("${root}/usr/lib/libbcmcrypto.so", "${router}/libbcmcrypto/libbcmcrypto.a");
434 print "\n";
436 close(LOG);
437 exit(0);