libstrip 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/perl -w
  2. # vi: set ts=4:
  3. # Libstrip - A utility to optimize libraries for specific executables
  4. # Copyright (C) 2001 David A. Schleef <ds@schleef.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of version 2 of the GNU General Public License as
  8. # published by the Free Software Foundation.
  9. #
  10. # This is a surprisingly simple script that gets a list of
  11. # unresolved symbols in a list of executables specified on the
  12. # command line, and then relinks the uClibc shared object file
  13. # with only the those symbols and their dependencies. This
  14. # results in a shared object that is optimized for the executables
  15. # listed, and thus may not work with other executables.
  16. #
  17. # Example: optimizing uClibc for BusyBox
  18. # Compile uClibc and BusyBox as normal. Then, in this
  19. # directory, run:
  20. # libstrip path/to/busybox
  21. # After the script completes, there should be a new
  22. # libuClibc-0.9.5.so in the current directory, which
  23. # is optimized for busybox.
  24. #
  25. # How it works:
  26. # The uClibc Makefiles create libuClibc.so by first creating
  27. # the ar archive libc.a with all the object files, then links
  28. # the final libuClibc.so by using 'ld --shared --whole-archive'.
  29. # We take advantage of the linker command line option --undefined,
  30. # which pulls in a symbol and all its dependencies, and so relink
  31. # the library using --undefined for each symbol in place of
  32. # --whole-archive. The linker script is used only to avoid
  33. # having very long command lines.
  34. $topdir="../..";
  35. # This is the name of the default ldscript for shared libs. The
  36. # file name will be different for other architectures.
  37. $ldscript="/usr/lib/ldscripts/elf_i386.xs";
  38. my @syms;
  39. my @allsyms;
  40. my $s;
  41. while($exec = shift @ARGV){
  42. #print "$exec\n";
  43. @syms=`nm --dynamic $exec`;
  44. for $s (@syms){
  45. chomp $s;
  46. if($s =~ m/^.{8} [BUV] (.+)/){
  47. my $x = $1;
  48. if(!grep { m/^$x$/; } @allsyms){
  49. unshift @allsyms, $x;
  50. }
  51. }
  52. }
  53. }
  54. open(LDSCRIPT, ">ldscript");
  55. print LDSCRIPT "INCLUDE $ldscript\n";
  56. for $s (@allsyms) {
  57. print LDSCRIPT "EXTERN($s)\n";
  58. }
  59. `gcc -s -nostdlib -Wl,-warn-common -shared \\
  60. -o libuClibc-0.9.5.so \\
  61. -Wl,-soname,libc.so.0 -Wl,--script=ldscript \\
  62. $topdir/libc/libc.a \\
  63. $topdir/libc/tmp/libgcc-need.a`