Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | debug=false |
| 4 | |
Denis Vlasenko | 018e085 | 2007-02-25 00:40:37 +0000 | [diff] [blame] | 5 | try() { |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 6 | added="$1" |
| 7 | shift |
| 8 | $debug && echo "Trying: $* $added" |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 9 | "$@" $added 2>busybox_ld.err |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 10 | } |
| 11 | |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 12 | # Sanitize lib list (dups, extra spaces etc) |
| 13 | #echo "BBOX_LIB_LIST=$BBOX_LIB_LIST" |
| 14 | BBOX_LIB_LIST=`echo "$BBOX_LIB_LIST" | xargs -n1 | sort | uniq | xargs` |
| 15 | |
| 16 | # First link with all libs. If it fails, bail out |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 17 | echo "Trying libraries: $BBOX_LIB_LIST" |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 18 | l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` |
Bernhard Reutner-Fischer | d2bb2d3 | 2007-09-02 15:28:30 +0000 | [diff] [blame] | 19 | test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" |
| 20 | try "$l_list" "$@" \ |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 21 | || { |
| 22 | echo "Failed: $* -Wl,--start-group $l_list -Wl,--end-group" |
| 23 | cat busybox_ld.err |
| 24 | exit 1 |
| 25 | } |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 27 | # Now try to remove each lib and build without it. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 28 | # Stop when no lib can be removed. |
| 29 | while test "$BBOX_LIB_LIST"; do |
| 30 | $debug && echo "Trying libraries: $BBOX_LIB_LIST" |
| 31 | all_needed=true |
| 32 | for one in $BBOX_LIB_LIST; do |
| 33 | without_one=`echo " $BBOX_LIB_LIST " | sed "s/ $one / /g" | xargs` |
Bernhard Reutner-Fischer | d2bb2d3 | 2007-09-02 15:28:30 +0000 | [diff] [blame] | 34 | l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` |
| 35 | test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" |
Bernhard Reutner-Fischer | 8d91c13 | 2007-09-02 14:51:54 +0000 | [diff] [blame] | 36 | $debug && echo "Trying -l options: '$l_list'" |
| 37 | if try "$l_list" "$@"; then |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 38 | echo "Library $one is not needed" |
| 39 | BBOX_LIB_LIST="$without_one" |
| 40 | all_needed=false |
| 41 | else |
| 42 | echo "Library $one is needed" |
| 43 | fi |
| 44 | done |
| 45 | # All libs were needed, can't remove any |
| 46 | $all_needed && break |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 47 | # If there is no space char, the list has just one lib. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 48 | # I'm not sure that in this case lib really is 100% needed. |
| 49 | # Let's try linking without it anyway... thus commented out. |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 50 | #{ echo "$BBOX_LIB_LIST" | grep -q ' '; } || break |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 51 | done |
| 52 | |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 53 | # Make the binary with final, minimal list of libs |
| 54 | echo "Final link with: $BBOX_LIB_LIST" |
| 55 | l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` |
| 56 | test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose" |
| 57 | # --verbose gives us gobs of info to stdout (e.g. linker script used) |
| 58 | if ! test -f busybox_ldscript; then |
| 59 | try "$l_list -Wl,--verbose" "$@" >busybox_ld.out |
| 60 | else |
| 61 | echo "Custom linker script 'busybox_ldscript' found, using it" |
| 62 | # Add SORT_BY_ALIGNMENT to linker script (found in busybox_ld.out): |
| 63 | # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) } |
| 64 | # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) |
| 65 | # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) |
| 66 | # This will eliminate most of the data padding (~3kb). |
| 67 | try "$l_list -Wl,--verbose -Wl,-T -Wl,busybox_ldscript" "$@" >busybox_ld.out |
| 68 | fi |