add libjpeg, add jpeg support for php
[tomato.git] / release / src / btools / libfoo.pl
blob6d0426b5384d75c0f25168ec58fe9eca6db61afc
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 $root = $ENV{"TARGETDIR"};
11 $uclibc = $ENV{"TOOLCHAIN"};
12 $router = $ENV{"SRCBASE"} . "/router";
14 sub error
16 print STDERR "\n*** ERROR: " . (shift) . "\n\n";
17 exit 1;
20 sub basename
22 my $fn = shift;
23 if ($fn =~ /([^\/]+)$/) {
24 return $1;
26 return $fn;
29 sub load
31 my $fname = shift;
33 if ((-l $fname) ||
34 ($fname =~ /\/lib\/modules\/\d+\.\d+\.\d+/) ||
35 ($fname =~ /\.(asp|gif|png|svg|js|jsx|css|txt|pat|sh)$/)) {
36 return;
39 if (-d $fname) {
40 my $d;
41 if (opendir($d, $fname)) {
42 foreach (readdir($d)) {
43 if ($_ !~ /^\./) {
44 load($fname . "/" . $_);
47 closedir($d);
49 return;
53 my $f;
54 my $base;
55 my $ok;
56 my $s;
58 $base = basename($fname);
59 print LOG "\n\nreadelf $base:\n";
61 open($f, "mipsel-linux-readelf -WhsdD ${fname} 2>&1 |") || error("readelf - $!\n");
63 while (<$f>) {
64 print LOG;
66 if (/\s+Type:\s+(\w+)/) {
67 $elf_type{$base} = $1;
68 $ok = 1;
69 last;
73 if (!$ok) {
74 close($f);
75 return;
78 print "$elf_type{$base} $base", " " x 30, "\r";
80 push(@elfs, $base);
82 while (<$f>) {
83 print LOG;
85 if (/\(NEEDED\)\s+Shared library: \[(.+)\]/) {
86 push(@{$elf_lib{$base}}, $1);
88 elsif (/Symbol table for image:/) {
89 last;
93 while (<$f>) {
94 print LOG;
96 if (/\s+(WEAK|GLOBAL)\s+(?:DEFAULT|VISIBLE)\s+(\w+)\s+(\w+)/) {
97 $s = $3;
98 if ($2 eq 'UND') {
99 if ($1 eq 'GLOBAL') {
100 $elf_ext{$base}{$s} = 1;
102 else {
103 print LOG "*** not GLOBAL\n";
106 elsif ($2 eq 'ABS') {
108 elsif ($2 =~ /^\d+$/) {
109 $elf_exp{$base}{$s} = 1;
111 else {
112 print LOG "*** unknown type\n";
115 elsif (!/Num Buc:/) {
116 print LOG "*** strange line\n";
120 close($f);
123 sub fixDynDep
125 my ($user, $dep) = @_;
127 if (!defined $elf_dyn{$user}{$dep}) {
128 push(@{$elf_lib{$user}}, $dep);
129 $elf_dyn{$user}{$dep} = 1;
131 print LOG "FixDynDep: $user = $dep\n";
135 sub fixDyn
137 my $s;
139 foreach (@elfs) {
140 if (/^libipt_.+\.so$/) {
141 fixDynDep("iptables", $_);
143 elsif (/^libip6t_.+\.so$/) {
144 fixDynDep("ip6tables", $_);
146 elsif (/^CP\d+\.so$/) {
147 fixDynDep("smbd", $_);
151 fixDynDep("l2tpd", "cmd.so");
152 fixDynDep("l2tpd", "sync-pppd.so");
153 fixDynDep("pppd", "pppol2tp.so");
154 fixDynDep("pppd", "pptp.so");
155 fixDynDep("pppd", "rp-pppoe.so");
157 fixDynDep("libcrypto.so.1.0.0", "libssl.so.1.0.0");
159 #shibby
160 fixDynDep("transmission-daemon", "libevent-2.0.so.5");
161 fixDynDep("transmission-daemon", "libcurl.so.4.3.0");
162 # fixDynDep("transmission-daemon", "libiconv.so.2.4.0");
163 fixDynDep("transmission-remote", "libevent-2.0.so.5");
164 fixDynDep("transmission-remote", "libcurl.so.4.3.0");
165 # fixDynDep("transmission-remote", "libiconv.so.2.4.0");
166 fixDynDep("radvd", "libdaemon.so.0.5.0");
167 fixDynDep("miniupnpd", "libnfnetlink.so.0.2.0");
168 fixDynDep("dnscrypt-proxy", "libsodium.so.13.0.0");
170 #minidlna module, bwq518
171 fixDynDep("minidlna", "libz.so.1");
172 fixDynDep("minidlna", "libstdc.so.6");
173 fixDynDep("minidlna", "libiconv.so.2.4.0");
174 fixDynDep("minidlna", "libssl.so.1.0.0");
175 fixDynDep("minidlna", "libjpeg.so");
176 fixDynDep("minidlna", "libogg.so.0");
177 fixDynDep("minidlna", "libvorbis.so.0");
178 fixDynDep("minidlna", "libid3tag.so.0");
179 fixDynDep("minidlna", "libexif.so.12");
180 fixDynDep("minidlna", "libFLAC.so.8");
181 fixDynDep("libjepg.so", "libc.so.0");
182 fixDynDep("libavcodec.so.52", "libpthread.so.0");
184 #ipset modules
185 fixDynDep("libipset_iphash.so", "ipset");
186 fixDynDep("libipset_iptree.so", "ipset");
187 fixDynDep("libipset_ipmap.so", "ipset");
188 fixDynDep("libipset_ipporthash.so", "ipset");
189 fixDynDep("libipset_ipportiphash.so", "ipset");
190 fixDynDep("libipset_ipportnethash.so", "ipset");
191 fixDynDep("libipset_iptreemap.so", "ipset");
192 fixDynDep("libipset_macipmap.so", "ipset");
193 fixDynDep("libipset_nethash.so", "ipset");
194 fixDynDep("libipset_portmap.so", "ipset");
195 fixDynDep("libipset_setlist.so", "ipset");
197 fixDynDep("tomatodata.cgi", "libc.so.0");
198 fixDynDep("tomatoups.cgi", "libc.so.0");
199 fixDynDep("apcupsd", "libc.so.0");
200 fixDynDep("apcupsd", "libgcc_s.so.1");
201 fixDynDep("apcaccess", "libc.so.0");
202 fixDynDep("smtp", "libc.so.0");
204 # fixDynDep("libbcm.so", "libshared.so");
205 # fixDynDep("libbcm.so", "libc.so.0");
207 fixDynDep("nginx", "libpcre.so.1.2.3");
208 fixDynDep("nginx", "libpcreposix.so.0.0.2");
210 #!!TB - Updated Broadcom WL driver
211 fixDynDep("libbcmcrypto.so", "libc.so.0");
212 fixDynDep("nas", "libbcmcrypto.so");
213 fixDynDep("wl", "libbcmcrypto.so");
214 fixDynDep("nas", "libc.so.0");
215 fixDynDep("wl", "libc.so.0");
217 #Roadkill for NocatSplash
218 fixDynDep("splashd","libglib-1.2.so.0.0.10");
221 sub usersOf
223 my $name = shift;
224 my $sym = shift;
225 my @x;
226 my $e;
227 my $l;
229 @x = ();
230 foreach $e (@elfs) {
231 foreach $l (@{$elf_lib{$e}}) {
232 if ($l eq $name) {
233 if ((!defined $sym) || (defined $elf_ext{$e}{$sym})) {
234 push(@x, $e);
236 last;
240 return @x;
243 sub resolve
245 my $name = shift;
246 my $sym = shift;
247 my $l;
249 foreach $l (@{$elf_lib{$name}}) {
250 # print "\n$l $sym ", $elf_exp{$l}{$sym}, "\n";
251 return $l if (defined $elf_exp{$l}{$sym});
253 return "*** unresolved ***";
256 sub fillGaps
258 my $name;
259 my $sym;
260 my @users;
261 my $u;
262 my $t;
263 my $found;
265 # print "Resolving implicit links...\n";
267 foreach $name (@elfs) {
268 foreach $sym (keys %{$elf_ext{$name}}) {
269 $found = 0;
271 if ($sym eq '__uClibc_start_main') {
272 $sym = '__uClibc_main';
275 # __gnu_local_gp is defined specially by the linker on MIPS
276 if ($sym eq '__gnu_local_gp') {
277 $found = 1;
279 elsif (resolve($name, $sym) eq "*** unresolved ***") {
280 @users = usersOf($name);
281 foreach $u (@users) {
282 # if exported by $u
283 if (defined $elf_exp{$u}{$sym}) {
284 fixDynDep($name, $u);
285 $found = 1;
287 # if exported by shared libs of $u
288 if (($t = resolve($u, $sym)) ne "*** unresolved ***") {
289 fixDynDep($name, $t);
290 $found = 1;
294 if ($found == 0) {
295 print "Unable to resolve $sym used by $name\n", @users;
296 exit 1;
303 sub tab
305 my $current = shift;
306 my $target = shift;
307 my $s = "";
308 my $n;
310 while (1) {
311 $n = $current + (4 - ($current % 4));
312 last if ($n > $target);
313 $s = $s . "\t";
314 $current = $n;
316 while ($current < $target) {
317 $s = $s . " ";
318 $current++;
320 return $s;
323 sub genXref
325 my $f;
326 my $fname;
327 my $s;
328 my @u;
330 # print "Generating Xref Report...\n";
332 open($f, ">libfoo_xref.txt");
333 foreach $fname (sort keys %elf_type) {
334 print $f "$fname:\n";
336 if (scalar(@{$elf_lib{$fname}}) > 0) {
337 print $f "Dependency:\n";
338 foreach $s (sort @{$elf_lib{$fname}}) {
339 print $f "\t$s", defined $elf_dyn{$fname}{$s} ? " (dyn)\n" : "\n";
343 if (scalar(keys %{$elf_exp{$fname}}) > 0) {
344 print $f "Export:\n";
345 foreach $s (sort keys %{$elf_exp{$fname}}) {
346 @u = usersOf($fname, $s);
347 if (scalar(@u) > 0) {
348 print $f "\t$s", tab(length($s) + 4, 40), " > ", join(",", @u), "\n";
350 else {
351 print $f "\t$s\n";
356 if (scalar(keys %{$elf_ext{$fname}}) > 0) {
357 print $f "External:\n";
358 foreach $s (sort keys %{$elf_ext{$fname}}) {
359 print $f "\t$s", tab(length($s) + 4, 40), " < ", resolve($fname, $s), "\n";
363 print $f "\n";
365 close($f);
369 sub genSO
371 my ($so, $arc, $strip, $opt) = @_;
372 my $name = basename($so);
373 my $sym;
374 my $fn;
375 my $inuse;
376 my @used;
377 my @unused;
378 my $cmd;
379 my $before, $after;
381 if (!-f $so) {
382 print "$name: not found, skipping...\n";
383 return 0;
386 #!!TB
387 if (!-f $arc) {
388 print "$arc: not found, skipping...\n";
389 return 0;
392 foreach $sym (sort keys %{$elf_exp{$name}}) {
393 if ((scalar(usersOf($name, $sym)) > 0) || (${strip} eq "no")) {
394 push(@used, $sym);
396 else {
397 push(@unused, $sym);
401 # print "\n$name: Attempting to link ", scalar(@used), " and remove ", scalar(@unused), " objects...\n";
403 print LOG "\n\n${base}\n";
405 # $cmd = "mipsel-uclibc-ld -shared -s -z combreloc --warn-common --fatal-warnings ${opt} -soname ${name} -o ${so}";
406 $cmd = "mipsel-uclibc-gcc -shared -nostdlib -Wl,-s,-z,combreloc -Wl,--warn-common -Wl,--fatal-warnings -Wl,--gc-sections ${opt} -Wl,-soname=${name} -o ${so}";
407 foreach (@{$elf_lib{$name}}) {
408 if ((!$elf_dyn{$name}{$_}) && (/^lib(.+)\.so/)) {
409 $cmd .= " -l$1";
411 else {
412 # print LOG "Not marking for linkage: $_\n";
415 # print "$cmd -u... ${arc}\n";
416 if (scalar(@used) == 0) {
417 print "$name: WARNING: Library is not used by anything, deleting...\n";
418 unlink $so;
419 # <>;
420 return 0;
422 $cmd .= " -u " . join(" -u ", @used) . " ". $arc;
424 print LOG "Command: $cmd\n";
425 print LOG "Used: ", join(",", @used), "\n";
426 print LOG "Unused: ", join(",", @unused), "\n";
428 $before = -s $so;
430 system($cmd);
431 if ($? != 0) {
432 error("ld returned $?");
435 $after = -s $so;
437 print "$name: Attempted to remove ", scalar(@unused), "/", scalar(@unused) + scalar(@used), " symbols. ";
438 printf "%.2fK - %.2fK = %.2fK\n", $before / 1024, $after / 1024, ($before - $after) / 1024;
440 # print "\n$name: Attempting to link ", scalar(@used), " and remove ", scalar(@unused), " objects...\n";
441 # printf "Before: %.2fK / After: %.2fK / Removed: %.2fK\n\n", $before / 1024, $after / 1024, ($before - $after) / 1024;
443 return ($before > $after)
451 # print "\nlibfoo.pl - fooify shared libraries\n";
452 # print "Copyright (C) 2006-2007 Jonathan Zarate\n\n";
454 if ((!-d $root) || (!-d $uclibc) || (!-d $router)) {
455 print "Missing or invalid environment variables\n";
456 exit(1);
459 open(LOG, ">libfoo.debug");
460 #open(LOG, ">/dev/null");
462 print "Loading...\r";
463 load($root);
464 print "Finished loading files.", " " x 30, "\r";
466 fixDyn();
467 fillGaps();
469 genXref();
471 $stripshared = "yes";
472 if ($ARGV[0] eq "--noopt") {
473 $stripshared = "no";
476 genSO("${root}/lib/libc.so.0", "${uclibc}/lib/libc.a", "", "-Wl,-init=__uClibc_init ${uclibc}/lib/optinfo/interp.os");
477 genSO("${root}/lib/libresolv.so.0", "${uclibc}/lib/libresolv.a", "${stripshared}");
478 genSO("${root}/lib/libcrypt.so.0", "${uclibc}/lib/libcrypt.a", "${stripshared}");
479 genSO("${root}/lib/libm.so.0", "${uclibc}/lib/libm.a");
480 genSO("${root}/lib/libpthread.so.0", "${uclibc}/lib/libpthread.a", "${stripshared}", "-u pthread_mutexattr_init -Wl,-init=__pthread_initialize_minimal_internal");
481 genSO("${root}/lib/libutil.so.0", "${uclibc}/lib/libutil.a", "${stripshared}");
482 # genSO("${root}/lib/libdl.so.0", "${uclibc}/lib/libdl.a", "${stripshared}");
483 # genSO("${root}/lib/libnsl.so.0", "${uclibc}/lib/libnsl.a", "${stripshared}");
485 genSO("${root}/usr/lib/libcrypto.so.1.0.0", "${router}/openssl/libcrypto.a");
486 genSO("${root}/usr/lib/libssl.so.1.0.0", "${router}/openssl/libssl.a", "${stripshared}", "-L${router}/openssl");
488 genSO("${root}/usr/lib/libzebra.so", "${router}/zebra/lib/libzebra.a");
489 genSO("${root}/usr/lib/libz.so.1", "${router}/zlib/libz.a");
490 genSO("${root}/usr/lib/libjpeg.so", "${router}/jpeg/libjpeg.a");
491 genSO("${root}/usr/lib/libsqlite3.so.0.8.6", "${router}/sqlite/.libs/libsqlite3.a");
492 genSO("${root}/usr/lib/libogg.so.0", "${router}/libogg/src/.libs/libogg.a");
493 genSO("${root}/usr/lib/libvorbis.so.0", "${router}/libvorbis/lib/.libs/libvorbis.a", "", "-L${router}/libogg/src/.libs");
494 genSO("${root}/usr/lib/libid3tag.so.0", "${router}/libid3tag/.libs/libid3tag.a", "", "-L${router}/zlib");
495 genSO("${root}/usr/lib/libexif.so.12", "${router}/libexif/libexif/.libs/libexif.a");
496 genSO("${root}/usr/lib/libFLAC.so.8", "${router}/flac/src/libFLAC/.libs/libFLAC.a", "", "-L${router}/libogg/src/.libs");
497 genSO("${root}/usr/lib/libavcodec.so.52", "${router}/ffmpeg/libavcodec/libavcodec.a", "", "-L${router}/ffmpeg/libavutil -L${router}/zlib");
498 genSO("${root}/usr/lib/libavutil.so.50", "${router}/ffmpeg/libavutil/libavutil.a", "-L${router}/zlib");
499 genSO("${root}/usr/lib/libavformat.so.52", "${router}/ffmpeg/libavformat/libavformat.a", "", "-L${router}/ffmpeg/libavutil -L${router}/ffmpeg/libavcodec -L${router}/zlib");
500 genSO("${root}/usr/lib/libsmb.so", "${router}/samba/source/bin/libsmb.a");
501 genSO("${root}/usr/lib/libbigballofmud.so", "${router}/samba3/source/bin/libbigballofmud.a");
503 genSO("${root}/usr/lib/liblzo2.so.2", "${router}/lzo/src/.libs/liblzo2.a");
504 # genSO("${root}/usr/lib/libtamba.so", "${router}/samba3/source/bin/libtamba.a");
505 # genSO("${root}/usr/lib/libiptc.so", "${router}/iptables/libiptc/libiptc.a");
506 # genSO("${root}/usr/lib/libshared.so", "${router}/shared/libshared.a");
507 # genSO("${root}/usr/lib/libnvram.so", "${router}/nvram/libnvram.a");
508 # genSO("${root}/usr/lib/libusb-1.0.so.0", "${router}/libusb10/libusb/.libs/libusb-1.0.a");
509 # genSO("${root}/usr/lib/libusb-0.1.so.4", "${router}/libusb/libusb/.libs/libusb.a", "", "-L${router}/libusb10/libusb/.libs");
511 genSO("${root}/usr/lib/libbcmcrypto.so", "${router}/libbcmcrypto/libbcmcrypto.a");
513 #shibby
514 genSO("${root}/usr/lib/libcurl.so.4.3.0", "${router}/libcurl/lib/.libs/libcurl.a", "", "-L${router}/zlib");
515 genSO("${root}/usr/lib/libevent-2.0.so.5", "${router}/libevent/.libs/libevent.a");
516 genSO("${root}/usr/lib/libdaemon.so.0.5.0", "${router}/libdaemon/libdaemon/.libs/libdaemon.a");
517 genSO("${root}/usr/lib/libiconv.so.2.5.1", "${router}/libiconv/lib/.libs/libiconv.a");
518 genSO("${root}/usr/lib/libnfnetlink.so.0.2.0", "${router}/libnfnetlink/src/.libs/libnfnetlink.a");
519 genSO("${root}/usr/lib/libsodium.so.13.0.0", "${router}/libsodium/src/libsodium/.libs/libsodium.a");
520 genSO("${root}/usr/lib/libpng.so.3.51.0", "${router}/libpng/.libs/libpng.a");
521 genSO("${root}/usr/lib/libpng12.so.0.51.0", "${router}/libpng/.libs/libpng12.a");
522 genSO("${root}/usr/lib/libjpeg.so.62.0.0", "${router}/libjpeg/.libs/libjpeg.a");
523 genSO("${root}/usr/lib/libxml2.so.2.9.1", "${router}/libxml2/.libs/libxml2.a");
525 print "\n";
527 close(LOG);
528 exit(0);