blob: 7fe262baaba3be988a38b9bea752616dd699e5b9 [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001#!/bin/sh
2
3debug=false
4
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +00005# Linker flags used:
6#
7# Informational:
8# --warn-common
9# -Map $EXE.map
10# --verbose
11#
12# Optimizations:
13# --sort-common reduces padding
14# --sort-section alignment reduces padding
15# --gc-sections throws out unused sections,
16# does not work for shared libs
Denis Vlasenko42e41822007-10-09 18:01:13 +000017# -On Not used, maybe useful?
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000018#
19# List of files to link:
20# $l_list == --start-group -llib1 -llib2 --end-group
21# --start-group $O_FILES $A_FILES --end-group
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000022#
23# Shared library link:
24# -shared self-explanatory
25# -fPIC position-independent code
Denis Vlasenko724d1962007-10-10 14:41:07 +000026# --enable-new-dtags ?
27# -z,combreloc ?
28# -soname="libbusybox.so.$BB_VER"
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000029# --undefined=lbb_main Seed name to start pulling from
Denis Vlasenko724d1962007-10-10 14:41:07 +000030# (otherwise we'll need --whole-archive)
31# -static Not used, but may be useful! manpage:
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000032# "... This option can be used with -shared.
33# Doing so means that a shared library
34# is being created but that all of the library's
35# external references must be resolved by pulling
36# in entries from static libraries."
37
38
Denis Vlasenko018e0852007-02-25 00:40:37 +000039try() {
Denis Vlasenko32404742007-10-07 17:05:22 +000040 printf "%s\n" "Output of:" >$EXE.out
41 printf "%s\n" "$*" >>$EXE.out
42 printf "%s\n" "==========" >>$EXE.out
43 $debug && echo "Trying: $*"
44 "$@" >>$EXE.out 2>&1
45 exitcode=$?
Denis Vlasenko32404742007-10-07 17:05:22 +000046 return $exitcode
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000047}
48
Denis Vlasenko32404742007-10-07 17:05:22 +000049EXE="$1"
50CC="$2"
51LDFLAGS="$3"
52O_FILES="$4"
53A_FILES="$5"
54LDLIBS="$6"
55
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000056# Sanitize lib list (dups, extra spaces etc)
Denis Vlasenko76a6b232007-10-07 17:05:42 +000057LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000058
59# First link with all libs. If it fails, bail out
Denis Vlasenko76a6b232007-10-07 17:05:42 +000060echo "Trying libraries: $LDLIBS"
Denis Vlasenko32404742007-10-07 17:05:22 +000061# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000062l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000063test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Denis Vlasenko32404742007-10-07 17:05:22 +000064try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000065 -o $EXE \
66 -Wl,--sort-common \
67 -Wl,--sort-section -Wl,alignment \
68 -Wl,--gc-sections \
Denis Vlasenko32404742007-10-07 17:05:22 +000069 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
70 $l_list \
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000071|| {
Denis Vlasenko32404742007-10-07 17:05:22 +000072 echo "Failed: $* $l_list"
73 cat $EXE.out
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000074 exit 1
75}
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000076
Denis Vlasenko8274e062007-08-06 03:41:08 +000077# Now try to remove each lib and build without it.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000078# Stop when no lib can be removed.
Denis Vlasenko76a6b232007-10-07 17:05:42 +000079while test "$LDLIBS"; do
80 $debug && echo "Trying libraries: $LDLIBS"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000081 all_needed=true
Denis Vlasenko76a6b232007-10-07 17:05:42 +000082 for one in $LDLIBS; do
83 without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
Denis Vlasenko32404742007-10-07 17:05:22 +000084 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000085 l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
86 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Bernhard Reutner-Fischer8d91c132007-09-02 14:51:54 +000087 $debug && echo "Trying -l options: '$l_list'"
Denis Vlasenko32404742007-10-07 17:05:22 +000088 try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000089 -o $EXE \
90 -Wl,--sort-common \
91 -Wl,--sort-section -Wl,alignment \
92 -Wl,--gc-sections \
Denis Vlasenko32404742007-10-07 17:05:22 +000093 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +000094 $l_list
Denis Vlasenko32404742007-10-07 17:05:22 +000095 if test $? = 0; then
96 echo "Library $one is not needed"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000097 LDLIBS="$without_one"
Denis Vlasenko32404742007-10-07 17:05:22 +000098 all_needed=false
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000099 else
Denis Vlasenko32404742007-10-07 17:05:22 +0000100 echo "Library $one is needed"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000101 fi
102 done
103 # All libs were needed, can't remove any
104 $all_needed && break
Denis Vlasenko8274e062007-08-06 03:41:08 +0000105 # If there is no space char, the list has just one lib.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000106 # I'm not sure that in this case lib really is 100% needed.
107 # Let's try linking without it anyway... thus commented out.
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000108 #{ echo "$LDLIBS" | grep -q ' '; } || break
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000109done
110
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000111# Make the binary with final, minimal list of libs
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000112echo "Final link with: $LDLIBS"
113l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000114test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000115# --verbose gives us gobs of info to stdout (e.g. linker script used)
116if ! test -f busybox_ldscript; then
Denis Vlasenko32404742007-10-07 17:05:22 +0000117 try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000118 -o $EXE \
119 -Wl,--sort-common \
120 -Wl,--sort-section -Wl,alignment \
121 -Wl,--gc-sections \
Denis Vlasenko32404742007-10-07 17:05:22 +0000122 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000123 $l_list \
124 -Wl,--warn-common \
125 -Wl,-Map -Wl,$EXE.map \
126 -Wl,--verbose \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +0000127 || {
128 cat $EXE.out
129 exit 1
130 }
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000131else
132 echo "Custom linker script 'busybox_ldscript' found, using it"
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000133 # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000134 # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
135 # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
136 # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000137 # This will eliminate most of the padding (~3kb).
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000138 # Hmm, "ld --sort-section alignment" should do it too.
Denis Vlasenko32404742007-10-07 17:05:22 +0000139 try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000140 -o $EXE \
141 -Wl,--sort-common \
142 -Wl,--sort-section -Wl,alignment \
143 -Wl,--gc-sections \
Denis Vlasenko32404742007-10-07 17:05:22 +0000144 -Wl,-T -Wl,busybox_ldscript \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000145 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
146 $l_list \
147 -Wl,--warn-common \
148 -Wl,-Map -Wl,$EXE.map \
149 -Wl,--verbose \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +0000150 || {
151 cat $EXE.out
152 exit 1
153 }
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000154fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000155
Denis Vlasenko42e41822007-10-09 18:01:13 +0000156. ./.config
Denis Vlasenko32404742007-10-07 17:05:22 +0000157
Denis Vlasenkodef88982007-10-07 17:06:01 +0000158sharedlib_dir="0_lib"
159
160if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
161 mkdir "$sharedlib_dir" 2>/dev/null
162 test -d "$sharedlib_dir" || {
163 echo "Cannot make directory $sharedlib_dir"
164 exit 1
165 }
166 ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
167
168 EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
169 try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000170 -o $EXE \
171 -shared -fPIC \
172 -Wl,--enable-new-dtags \
173 -Wl,-z,combreloc \
174 -Wl,-soname="libbusybox.so.$BB_VER" \
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000175 -Wl,--undefined=lbb_main \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000176 -Wl,--sort-common \
177 -Wl,--sort-section -Wl,alignment \
Denis Vlasenko724d1962007-10-10 14:41:07 +0000178 -Wl,--start-group $A_FILES -Wl,--end-group \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000179 $l_list \
180 -Wl,--warn-common \
181 -Wl,-Map -Wl,$EXE.map \
182 -Wl,--verbose \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000183 || {
184 echo "Linking $EXE failed"
185 cat $EXE.out
186 exit 1
187 }
Denis Vlasenko141750e2007-10-10 10:05:35 +0000188 $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000189 chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
Denis Vlasenkodef88982007-10-07 17:06:01 +0000190 echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
191fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000192
Denis Vlasenkodef88982007-10-07 17:06:01 +0000193if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
194 EXE="$sharedlib_dir/busybox_unstripped"
195 try $CC $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000196 -o $EXE \
197 -Wl,--sort-common \
198 -Wl,--sort-section -Wl,alignment \
199 -Wl,--gc-sections \
200 -Wl,--start-group $O_FILES -Wl,--end-group \
201 -L"$sharedlib_dir" -lbusybox \
202 -Wl,--warn-common \
203 -Wl,-Map -Wl,$EXE.map \
204 -Wl,--verbose \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000205 || {
206 echo "Linking $EXE failed"
207 cat $EXE.out
208 exit 1
209 }
Denis Vlasenko141750e2007-10-10 10:05:35 +0000210 $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
Denis Vlasenkodef88982007-10-07 17:06:01 +0000211 echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
212fi
Denis Vlasenkof545be02007-10-07 17:06:26 +0000213
214if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000215 echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
Denis Vlasenkof545be02007-10-07 17:06:26 +0000216 gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
217 | grep -v "^#" \
218 | grep -v "^$" \
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000219 > applet_lst.tmp
Denis Vlasenko642a52d2007-10-07 21:00:41 +0000220 while read name main junk; do
Denis Vlasenkof545be02007-10-07 17:06:26 +0000221
222 echo "\
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000223void lbb_prepare(const char *applet, char **argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000224int $main(int argc, char **argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000225
Denis Vlasenkof545be02007-10-07 17:06:26 +0000226int main(int argc, char **argv)
227{
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000228 lbb_prepare(\"$name\", argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000229 return $main(argc, argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000230}
231" >"$sharedlib_dir/applet.c"
232
233 EXE="$sharedlib_dir/$name"
234 try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000235 -o $EXE \
236 -Wl,--sort-common \
237 -Wl,--sort-section -Wl,alignment \
238 -Wl,--gc-sections \
239 -L"$sharedlib_dir" -lbusybox \
240 -Wl,--warn-common \
Denis Vlasenkof545be02007-10-07 17:06:26 +0000241 || {
242 echo "Linking $EXE failed"
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000243 cat $EXE.out
Denis Vlasenkof545be02007-10-07 17:06:26 +0000244 exit 1
245 }
246 rm -- "$sharedlib_dir/applet.c" $EXE.out
Denis Vlasenko141750e2007-10-10 10:05:35 +0000247 $STRIP -s --remove-section=.note --remove-section=.comment $EXE
Denis Vlasenkof545be02007-10-07 17:06:26 +0000248
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000249 done <applet_lst.tmp
Denis Vlasenkof545be02007-10-07 17:06:26 +0000250fi
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000251
252# libbusybox.so is needed only for -lbusybox at link time,
253# it is not needed at runtime. Deleting to reduce confusion.
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000254rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
255exit 0 # or else we may confuse make