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 |
| 17 | l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/'` |
| 18 | echo "Trying libraries: $BBOX_LIB_LIST" |
| 19 | try "-Wl,--start-group $l_list -Wl,--end-group" "$@" \ |
| 20 | || { |
| 21 | echo "Failed: $* -Wl,--start-group $l_list -Wl,--end-group" |
| 22 | cat busybox_ld.err |
| 23 | exit 1 |
| 24 | } |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 25 | |
| 26 | #### Hack disabled: conflicts with ld --verbose flag in last link phase |
| 27 | |
| 28 | ##### Hack: we are not supposed to know executable name, |
| 29 | ##### but this hack cuts down link time |
| 30 | ####mv busybox_unstripped busybox_unstripped.tmp |
| 31 | ####mv busybox.map busybox.map.tmp |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 33 | # Now try to remove each lib and build without it. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 34 | # Stop when no lib can be removed. |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 35 | ####ever_discarded=false |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 36 | while test "$BBOX_LIB_LIST"; do |
| 37 | $debug && echo "Trying libraries: $BBOX_LIB_LIST" |
| 38 | all_needed=true |
| 39 | for one in $BBOX_LIB_LIST; do |
| 40 | without_one=`echo " $BBOX_LIB_LIST " | sed "s/ $one / /g" | xargs` |
| 41 | l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/'` |
| 42 | $debug && echo "Trying -l options: $l_list" |
| 43 | if try "-Wl,--start-group $l_list -Wl,--end-group" "$@"; then |
| 44 | echo "Library $one is not needed" |
| 45 | BBOX_LIB_LIST="$without_one" |
| 46 | all_needed=false |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 47 | #### ever_discarded=true |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 48 | else |
| 49 | echo "Library $one is needed" |
| 50 | fi |
| 51 | done |
| 52 | # All libs were needed, can't remove any |
| 53 | $all_needed && break |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 54 | # If there is no space char, the list has just one lib. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 55 | # I'm not sure that in this case lib really is 100% needed. |
| 56 | # Let's try linking without it anyway... thus commented out. |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 57 | #{ echo "$BBOX_LIB_LIST" | grep -q ' '; } || break |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 58 | done |
| 59 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 60 | ####mv busybox_unstripped.tmp busybox_unstripped |
| 61 | ####mv busybox.map.tmp busybox.map |
| 62 | ####$ever_discarded && { |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 63 | # Make the binary with final, minimal list of libs |
Denis Vlasenko | 21d1014 | 2007-07-20 21:28:41 +0000 | [diff] [blame] | 64 | echo "Final link with: $BBOX_LIB_LIST" |
| 65 | l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/'` |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 66 | # --verbose gives us gobs of info to stdout (e.g. linker script used) |
| 67 | try "-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose" "$@" >busybox_ld.out ####|| exit 1 |
| 68 | # |
| 69 | # Add SORT_BY_ALIGNMENT to linker script (found in busybox_ld.out): |
| 70 | # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) } |
| 71 | # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) |
| 72 | # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) |
| 73 | # This will eliminate most of the data padding. Use linker script |
| 74 | # by commenting "try" above and uncommenting this one (tested on i386): |
| 75 | #try "-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose -Wl,-T -Wl,busybox_ldscript" "$@" >busybox_ld.out |
| 76 | # |
| 77 | ####} |
| 78 | ####rm busybox_ld.err |
| 79 | ####exit 0 # Ensure "success" exit code |