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