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";
16 print STDERR
"\n*** ERROR: " . (shift) . "\n\n";
23 if ($fn =~ /([^\/]+)$/) {
34 ($fname =~ /\/lib\
/modules\/\d
+\
.\d
+\
.\d
+/) ||
35 ($fname =~ /\.(asp|gif|png|svg|js|jsx|css|txt|pat|sh)$/)) {
41 if (opendir($d, $fname)) {
42 foreach (readdir($d)) {
44 load
($fname . "/" . $_);
58 $base = basename
($fname);
59 print LOG
"\n\nreadelf $base:\n";
61 open($f, "mipsel-linux-readelf -WhsdD ${fname} 2>&1 |") || error
("readelf - $!\n");
66 if (/\s+Type:\s+(\w+)/) {
67 $elf_type{$base} = $1;
78 print "$elf_type{$base} $base", " " x
30, "\r";
85 if (/\(NEEDED\)\s+Shared library: \[(.+)\]/) {
86 push(@
{$elf_lib{$base}}, $1);
88 elsif (/Symbol table for image:/) {
96 if (/\s+(WEAK|GLOBAL)\s+(?:DEFAULT|VISIBLE)\s+(\w+)\s+(\w+)/) {
100 $elf_ext{$base}{$s} = 1;
103 print LOG
"*** not GLOBAL\n";
106 elsif ($2 eq 'ABS') {
108 elsif ($2 =~ /^\d+$/) {
109 $elf_exp{$base}{$s} = 1;
112 print LOG
"*** unknown type\n";
115 elsif (!/Num Buc:/) {
116 print LOG
"*** strange line\n";
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";
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");
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");
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");
231 foreach $l (@
{$elf_lib{$e}}) {
233 if ((!defined $sym) || (defined $elf_ext{$e}{$sym})) {
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 ***";
265 # print "Resolving implicit links...\n";
267 foreach $name (@elfs) {
268 foreach $sym (keys %{$elf_ext{$name}}) {
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') {
279 elsif (resolve
($name, $sym) eq "*** unresolved ***") {
280 @users = usersOf
($name);
281 foreach $u (@users) {
283 if (defined $elf_exp{$u}{$sym}) {
284 fixDynDep
($name, $u);
287 # if exported by shared libs of $u
288 if (($t = resolve
($u, $sym)) ne "*** unresolved ***") {
289 fixDynDep
($name, $t);
295 print "Unable to resolve $sym used by $name\n", @users;
311 $n = $current + (4 - ($current % 4));
312 last if ($n > $target);
316 while ($current < $target) {
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";
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";
371 my ($so, $arc, $strip, $opt) = @_;
372 my $name = basename
($so);
382 print "$name: not found, skipping...\n";
388 print "$arc: not found, skipping...\n";
392 foreach $sym (sort keys %{$elf_exp{$name}}) {
393 if ((scalar(usersOf
($name, $sym)) > 0) || (${strip
} eq "no")) {
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/)) {
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";
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";
432 error
("ld returned $?");
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";
459 open(LOG
, ">libfoo.debug");
460 #open(LOG, ">/dev/null");
462 print "Loading...\r";
464 print "Finished loading files.", " " x
30, "\r";
471 $stripshared = "yes";
472 if ($ARGV[0] eq "--noopt") {
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");
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");