blob: c621b7c018f81131d7905ef4857dd1443620d024 [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001#!/bin/sh
2
3debug=false
4
Denis Vlasenko018e0852007-02-25 00:40:37 +00005try() {
Denis Vlasenko32404742007-10-07 17:05:22 +00006 printf "%s\n" "Output of:" >$EXE.out
7 printf "%s\n" "$*" >>$EXE.out
8 printf "%s\n" "==========" >>$EXE.out
9 $debug && echo "Trying: $*"
10 "$@" >>$EXE.out 2>&1
11 exitcode=$?
12 cat $EXE.out
13 return $exitcode
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000014}
15
Denis Vlasenko32404742007-10-07 17:05:22 +000016EXE="$1"
17CC="$2"
18LDFLAGS="$3"
19O_FILES="$4"
20A_FILES="$5"
21LDLIBS="$6"
22
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000023# Sanitize lib list (dups, extra spaces etc)
Denis Vlasenko76a6b232007-10-07 17:05:42 +000024LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000025
26# First link with all libs. If it fails, bail out
Denis Vlasenko76a6b232007-10-07 17:05:42 +000027echo "Trying libraries: $LDLIBS"
Denis Vlasenko32404742007-10-07 17:05:22 +000028# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000029l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000030test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Denis Vlasenko32404742007-10-07 17:05:22 +000031try $CC $LDFLAGS \
32 -o $EXE -Wl,-Map -Wl,$EXE.map \
33 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
34 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
35 $l_list \
36 >/dev/null \
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000037|| {
Denis Vlasenko32404742007-10-07 17:05:22 +000038 echo "Failed: $* $l_list"
39 cat $EXE.out
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000040 exit 1
41}
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000042
Denis Vlasenko8274e062007-08-06 03:41:08 +000043# Now try to remove each lib and build without it.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000044# Stop when no lib can be removed.
Denis Vlasenko76a6b232007-10-07 17:05:42 +000045while test "$LDLIBS"; do
46 $debug && echo "Trying libraries: $LDLIBS"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000047 all_needed=true
Denis Vlasenko76a6b232007-10-07 17:05:42 +000048 for one in $LDLIBS; do
49 without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
Denis Vlasenko32404742007-10-07 17:05:22 +000050 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000051 l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
52 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Bernhard Reutner-Fischer8d91c132007-09-02 14:51:54 +000053 $debug && echo "Trying -l options: '$l_list'"
Denis Vlasenko32404742007-10-07 17:05:22 +000054 try $CC $LDFLAGS \
55 -o $EXE -Wl,-Map -Wl,$EXE.map \
56 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
57 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
58 $l_list \
59 >/dev/null
60 if test $? = 0; then
61 echo "Library $one is not needed"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000062 LDLIBS="$without_one"
Denis Vlasenko32404742007-10-07 17:05:22 +000063 all_needed=false
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000064 else
Denis Vlasenko32404742007-10-07 17:05:22 +000065 echo "Library $one is needed"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000066 fi
67 done
68 # All libs were needed, can't remove any
69 $all_needed && break
Denis Vlasenko8274e062007-08-06 03:41:08 +000070 # If there is no space char, the list has just one lib.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000071 # I'm not sure that in this case lib really is 100% needed.
72 # Let's try linking without it anyway... thus commented out.
Denis Vlasenko76a6b232007-10-07 17:05:42 +000073 #{ echo "$LDLIBS" | grep -q ' '; } || break
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000074done
75
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000076# Make the binary with final, minimal list of libs
Denis Vlasenko76a6b232007-10-07 17:05:42 +000077echo "Final link with: $LDLIBS"
78l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000079test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose"
80# --verbose gives us gobs of info to stdout (e.g. linker script used)
81if ! test -f busybox_ldscript; then
Denis Vlasenko32404742007-10-07 17:05:22 +000082 try $CC $LDFLAGS \
83 -o $EXE -Wl,-Map -Wl,$EXE.map \
84 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
85 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
86 $l_list -Wl,--verbose \
87 >/dev/null
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000088else
89 echo "Custom linker script 'busybox_ldscript' found, using it"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000090 # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000091 # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
92 # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
93 # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
Denis Vlasenko76a6b232007-10-07 17:05:42 +000094 # This will eliminate most of the padding (~3kb).
Denis Vlasenko32404742007-10-07 17:05:22 +000095 try $CC $LDFLAGS \
96 -o $EXE -Wl,-Map -Wl,$EXE.map \
97 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
98 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
99 $l_list -Wl,--verbose \
100 -Wl,-T -Wl,busybox_ldscript \
101 >/dev/null
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000102fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000103
104mkdir 0lib 2>/dev/null
105test -d 0lib || exit 1
106ln -s libbusybox.so.1.8.0 0lib/libbusybox.so 2>/dev/null
107
108EXE="0lib/libbusybox.so.1.8.0"
109try $CC $LDFLAGS \
110 -o $EXE -Wl,-Map -Wl,$EXE.map \
111 -shared -fPIC -Wl,--enable-new-dtags \
112 -Wl,--start-group -Wl,--whole-archive $A_FILES -Wl,--no-whole-archive -Wl,--end-group \
113 $l_list -Wl,--verbose \
114 -Wl,-soname="libbusybox.so.1.8.0" \
115 -Wl,-z,combreloc \
116 >/dev/null \
117|| {
118 echo "Linking $EXE failed"
119 exit 1
120}
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000121strip -s --remove-section=.note --remove-section=.comment $EXE
Denis Vlasenko32404742007-10-07 17:05:22 +0000122
123EXE="0lib/busybox"
124try $CC $LDFLAGS \
125 -o $EXE -Wl,-Map -Wl,$EXE.map \
126 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
127 -Wl,--start-group $O_FILES -Wl,--end-group \
128 $l_list -Wl,--verbose \
129 -L"0lib" -lbusybox \
130 >/dev/null \
131|| {
132 echo "Linking $EXE failed"
133 exit 1
134}
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000135strip -s --remove-section=.note --remove-section=.comment $EXE