#!/usr/bin/perl -w
# vi: set ts=4:

# Libstrip - A utility to optimize libraries for specific executables
# Copyright (C) 2001  David A. Schleef <ds@schleef.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This is a surprisingly simple script that gets a list of
# unresolved symbols in a list of executables specified on the
# command line, and then relinks the uClibc shared object file
# with only the those symbols and their dependencies.  This
# results in a shared object that is optimized for the executables
# listed, and thus may not work with other executables.
#
# Example: optimizing uClibc for BusyBox
#  Compile uClibc and BusyBox as normal.  Then, in this
#  directory, run:
#    libstrip path/to/busybox
#  After the script completes, there should be a new
#  libuClibc-0.9.5.so in the current directory, which
#  is optimized for busybox.
#
# How it works:
#  The uClibc Makefiles create libuClibc.so by first creating
#  the ar archive libc.a with all the object files, then links
#  the final libuClibc.so by using 'ld --shared --whole-archive'.
#  We take advantage of the linker command line option --undefined,
#  which pulls in a symbol and all its dependencies, and so relink
#  the library using --undefined for each symbol in place of
#  --whole-archive.  The linker script is used only to avoid
#  having very long command lines.

#$topdir="../..";

# This is the name of the default ldscript for shared libs.  The
# file name will be different for other architectures.
#$ldscript="/usr/lib/ldscripts/elf_i386.xs";
$ldscript="/usr/local/gcc333/lexra-nnop-v5/lib/ldscripts/elf32btsmip.xs";

my @syms;
my @allsyms;
my $s;
my @usedlib;

$usedlib[0] = 'libuClibc-0.9.26.so';
$usedlib[1] = 'libcrypt-0.9.26.so';
$usedlib[2] = 'libgmp.so.3';

while($exec = shift @ARGV){
	#print "$exec\n";
	@syms=`mips-uclibc-nm --dynamic $exec`;
	for $s (@syms){
		chomp $s;
		if($s =~ m/^.{8} [BUV] (.+)/){
			my $x = $1;
			if(!grep { m/^$x$/; } @allsyms){
				unshift @allsyms, $x;
			}
		}
	}
}

open(LDSCRIPT, ">ldscript");
print LDSCRIPT "INCLUDE $ldscript\n";
for $s (@allsyms) {
	print LDSCRIPT "EXTERN($s)\n";
}

$libc="/usr/local/gcc333/lexra-nnop-v5/mips-linux-uclibc/lib/libc.a";
if( -e "$libc"){
`mips-uclibc-gcc -s -nostdlib -Wl,-warn-common -shared -o \\
		libuClibc-0.9.26.so -Wl,-soname,libc.so.0 -Wl,--script=ldscript \\
		$libc\\
		 /usr/local/gcc333/lexra-nnop-v5/lib/gcc-lib/mips-linux-uclibc/3.3.3/libgcc.a\\
		 `;
}

$libcrypt="/usr/local/gcc333/lexra-nnop-v5/mips-linux-uclibc/lib/libcrypt.a";
if( -e "$libcrypt"){
`mips-uclibc-gcc -s -nostdlib -Wl,-warn-common -shared -o \\
	libcrypt-0.9.26.so  -Wl,-soname,libcrypt.so -Wl,--script=ldscript \\
	$libcrypt\\
	/usr/local/gcc333/lexra-nnop-v5/lib/gcc-lib/mips-linux-uclibc/3.3.3/libgcc.a
	`;
}
$libgmp="../gmp-4.1.2/.libs/libgmp.a";
if( -e "$libgmp"){
`mips-uclibc-gcc -s -nostdlib -Wl,-warn-common -shared -o \\
	libgmp.so.3  -Wl,-soname,libgmp.so  -Wl,--script=ldscript \\
	 $libgmp\\
	/usr/local/gcc333/lexra-nnop-v5/lib/gcc-lib/mips-linux-uclibc/3.3.3/libgcc.a\\
	`;
}

