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
17 # -On Not used, maybe useful?
19 # List of files to link:
20 # $l_list == --start-group -llib1 -llib2 --end-group
21 # --start-group $O_FILES $A_FILES --end-group
23 # --start-group --whole-archive $A_FILES --no-whole-archive --end-group
24 # otherwise we don't pull in all parts of *.a files. Now we pull in too much.
25 # FIXME: make it work without --whole-archive, or better, with --gc-sections.
27 # Shared library link:
28 # -shared self-explanatory
29 # -fPIC position-independent code
30 # -Wl,--enable-new-dtags ?
32 # -Wl,-soname="libbusybox.so.$BB_VER"
33 # -static Not used, but may be useful! manpage:
34 # "... This option can be used with -shared.
35 # Doing so means that a shared library
36 # is being created but that all of the library's
37 # external references must be resolved by pulling
38 # in entries from static libraries."
42 printf "%s\n" "Output of:" >$EXE.out
43 printf "%s\n" "$*" >>$EXE.out
44 printf "%s\n" "==========" >>$EXE.out
45 $debug && echo "Trying: $*"
58 # Sanitize lib list (dups, extra spaces etc)
59 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
61 # First link with all libs. If it fails, bail out
62 echo "Trying libraries: $LDLIBS"
63 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
64 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
65 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
69 -Wl,--sort-section -Wl,alignment \
71 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
74 echo "Failed: $* $l_list"
79 # Now try to remove each lib and build without it.
80 # Stop when no lib can be removed.
81 while test "$LDLIBS"; do
82 $debug && echo "Trying libraries: $LDLIBS"
84 for one in $LDLIBS; do
85 without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
86 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
87 l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
88 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
89 $debug && echo "Trying -l options: '$l_list'"
93 -Wl,--sort-section -Wl,alignment \
95 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
98 echo "Library $one is not needed"
102 echo "Library $one is needed"
105 # All libs were needed, can't remove any
107 # If there is no space char, the list has just one lib.
108 # I'm not sure that in this case lib really is 100% needed.
109 # Let's try linking without it anyway... thus commented out.
110 #{ echo "$LDLIBS" | grep -q ' '; } || break
113 # Make the binary with final, minimal list of libs
114 echo "Final link with: $LDLIBS"
115 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
116 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
117 # --verbose gives us gobs of info to stdout (e.g. linker script used)
118 if ! test -f busybox_ldscript; then
122 -Wl,--sort-section -Wl,alignment \
124 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
127 -Wl,-Map -Wl,$EXE.map \
134 echo "Custom linker script 'busybox_ldscript' found, using it"
135 # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
136 # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
137 # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
138 # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
139 # This will eliminate most of the padding (~3kb).
140 # Hmm, "ld --sort-section alignment" should do it too.
144 -Wl,--sort-section -Wl,alignment \
146 -Wl,-T -Wl,busybox_ldscript \
147 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
150 -Wl,-Map -Wl,$EXE.map \
160 sharedlib_dir="0_lib"
162 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
163 mkdir "$sharedlib_dir" 2>/dev/null
164 test -d "$sharedlib_dir" || {
165 echo "Cannot make directory $sharedlib_dir"
168 ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
170 EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
174 -Wl,--enable-new-dtags \
176 -Wl,-soname="libbusybox.so.$BB_VER" \
178 -Wl,--sort-section -Wl,alignment \
179 -Wl,--start-group -Wl,--whole-archive $A_FILES -Wl,--no-whole-archive -Wl,--end-group \
182 -Wl,-Map -Wl,$EXE.map \
185 echo "Linking $EXE failed"
189 strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
190 chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
191 echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
194 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
195 EXE="$sharedlib_dir/busybox_unstripped"
199 -Wl,--sort-section -Wl,alignment \
201 -Wl,--start-group $O_FILES -Wl,--end-group \
202 -L"$sharedlib_dir" -lbusybox \
204 -Wl,-Map -Wl,$EXE.map \
207 echo "Linking $EXE failed"
211 strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
212 echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
215 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
216 echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
217 gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
221 while read name main junk; do
224 void bbox_prepare_main(char **argv);
225 int $main(int argc, char **argv);
227 const char *applet_name = \"$name\";
229 int main(int argc, char **argv)
231 bbox_prepare_main(argv);
232 return $main(argc, argv);
234 " >"$sharedlib_dir/applet.c"
236 EXE="$sharedlib_dir/$name"
237 try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
240 -Wl,--sort-section -Wl,alignment \
242 -L"$sharedlib_dir" -lbusybox \
245 echo "Linking $EXE failed"
249 rm -- "$sharedlib_dir/applet.c" $EXE.out
250 strip -s --remove-section=.note --remove-section=.comment $EXE
255 # libbusybox.so is needed only for -lbusybox at link time,
256 # it is not needed at runtime. Deleting to reduce confusion.
257 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
258 exit 0 # or else we may confuse make