021374aa9e1f3c3a6b81b9dc695e0a943f8a8304
[oweals/busybox.git] / scripts / trylink
1 #!/bin/sh
2
3 debug=false
4
5 # 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
17 # -On                           Not used, maybe useful?
18 #
19 # List of files to link:
20 # $l_list                       == --start-group -llib1 -llib2 --end-group
21 # --start-group $O_FILES $A_FILES --end-group
22 #
23 # Shared library link:
24 # -shared                       self-explanatory
25 # -fPIC                         position-independent code
26 # --enable-new-dtags            ?
27 # -z,combreloc                  ?
28 # -soname="libbusybox.so.$BB_VER"
29 # --undefined=lbb_main          Seed name to start pulling from
30 #                               (otherwise we'll need --whole-archive)
31 # -static                       Not used, but may be useful! manpage:
32 #                               "... 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
39 try() {
40     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     return $?
46 }
47
48 check_cc() {
49     local tempname="/tmp/temp.$$.$RANDOM"
50     # Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :(
51     # "-xc": C language. "/dev/null" is an empty source file.
52     if $CC $1 -shared -xc /dev/null -o "$tempname".o >/dev/null 2>&1; then
53         echo "$1";
54     else
55         echo "$2";
56     fi
57     rm "$tempname".o 2>/dev/null
58 }
59
60 check_libc_is_glibc() {
61     local tempname="/tmp/temp.$$.$RANDOM"
62     echo "\
63         #include <stdlib.h>
64         /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
65         #if defined(__GLIBC__) && !defined(__UCLIBC__)
66         syntax error here
67         #endif
68         " >"$tempname".c
69     if $CC "$tempname".c -c -o "$tempname".o >/dev/null 2>&1; then
70         echo "$2";
71     else
72         echo "$1";
73     fi
74     rm "$tempname".c "$tempname".o 2>/dev/null
75 }
76
77 EXE="$1"
78 CC="$2"
79 CFLAGS="$3"
80 LDFLAGS="$4"
81 O_FILES="$5"
82 A_FILES="$6"
83 LDLIBS="$7"
84
85 # The --sort-section option is not supported by older versions of ld
86 SORT_SECTION=`check_cc "-Wl,--sort-section,alignment" ""`
87
88 START_GROUP="-Wl,--start-group"
89 END_GROUP="-Wl,--end-group"
90
91 INFO_OPTS="-Wl,--warn-common -Wl,-Map,$EXE.map -Wl,--verbose"
92
93 # gold may not support --sort-common (yet)
94 SORT_COMMON=`check_cc "-Wl,--sort-common" ""`
95
96 # Static linking against glibc produces buggy executables
97 # (glibc does not cope well with ld --gc-sections).
98 # See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
99 # Note that glibc is unsuitable for static linking anyway.
100 # We are removing -Wl,--gc-sections from link command line.
101 GC_SECTIONS=`(
102 . ./.config
103 if test x"$CONFIG_STATIC" = x"y"; then
104     check_libc_is_glibc "" "-Wl,--gc-sections"
105 else
106     echo "-Wl,--gc-sections"
107 fi
108 )`
109
110 # The --gc-sections option is not supported by older versions of ld
111 if test -n "$GC_SECTIONS"; then
112     GC_SECTIONS=`check_cc "$GC_SECTIONS" ""`
113 fi
114
115 # Sanitize lib list (dups, extra spaces etc)
116 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
117
118 # First link with all libs. If it fails, bail out
119 echo "Trying libraries: $LDLIBS"
120 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
121 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
122 test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
123 try $CC $CFLAGS $LDFLAGS \
124         -o $EXE \
125         $SORT_COMMON \
126         $SORT_SECTION \
127         $GC_SECTIONS \
128         $START_GROUP $O_FILES $A_FILES $END_GROUP \
129         $l_list \
130 || {
131     echo "Failed: $l_list"
132     cat $EXE.out
133     exit 1
134 }
135
136 # Now try to remove each lib and build without it.
137 # Stop when no lib can be removed.
138 while test "$LDLIBS"; do
139     $debug && echo "Trying libraries: $LDLIBS"
140     all_needed=true
141     last_needed=false
142     for one in $LDLIBS; do
143         without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
144         # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
145         l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
146         test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
147         $debug && echo "Trying -l options: '$l_list'"
148         try $CC $CFLAGS $LDFLAGS \
149                 -o $EXE \
150                 $SORT_COMMON \
151                 $SORT_SECTION \
152                 $GC_SECTIONS \
153                 $START_GROUP $O_FILES $A_FILES $END_GROUP \
154                 $l_list
155         if test $? = 0; then
156             echo " Library $one is not needed, excluding it"
157             LDLIBS="$without_one"
158             all_needed=false
159             last_needed=false
160         else
161             echo " Library $one is needed, can't exclude it (yet)"
162             last_needed=true
163         fi
164     done
165     # All libs were needed, can't remove any
166     $all_needed && break
167     # Optimization: was the last tried lib needed?
168     if $last_needed; then
169         # Was it the only one lib left? Don't test again then.
170         { echo "$LDLIBS" | grep -q ' '; } || break
171     fi
172 done
173
174 # Make the binary with final, minimal list of libs
175 echo "Final link with: ${LDLIBS:-<none>}"
176 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
177 test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
178 # --verbose gives us gobs of info to stdout (e.g. linker script used)
179 if ! test -f busybox_ldscript; then
180     try $CC $CFLAGS $LDFLAGS \
181             -o $EXE \
182             $SORT_COMMON \
183             $SORT_SECTION \
184             $GC_SECTIONS \
185             $START_GROUP $O_FILES $A_FILES $END_GROUP \
186             $l_list \
187             $INFO_OPTS \
188     || {
189         cat $EXE.out
190         exit 1
191     }
192 else
193     echo "Custom linker script 'busybox_ldscript' found, using it"
194     # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
195     #  .rodata         : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
196     #  *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
197     #  *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
198     # This will eliminate most of the padding (~3kb).
199     # Hmm, "ld --sort-section alignment" should do it too.
200     try $CC $CFLAGS $LDFLAGS \
201             -o $EXE \
202             $SORT_COMMON \
203             $SORT_SECTION \
204             $GC_SECTIONS \
205             -Wl,-T,busybox_ldscript \
206             $START_GROUP $O_FILES $A_FILES $END_GROUP \
207             $l_list \
208             $INFO_OPTS \
209     || {
210         cat $EXE.out
211         exit 1
212     }
213 fi
214
215 . ./.config
216
217 sharedlib_dir="0_lib"
218
219 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
220     mkdir "$sharedlib_dir" 2>/dev/null
221     test -d "$sharedlib_dir" || {
222         echo "Cannot make directory $sharedlib_dir"
223         exit 1
224     }
225     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
226
227     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
228     try $CC $CFLAGS $LDFLAGS \
229             -o $EXE \
230             -shared -fPIC \
231             -Wl,--enable-new-dtags \
232             -Wl,-z,combreloc \
233             -Wl,-soname="libbusybox.so.$BB_VER" \
234             -Wl,--undefined=lbb_main \
235             $SORT_COMMON \
236             $SORT_SECTION \
237             $START_GROUP $A_FILES $END_GROUP \
238             $l_list \
239             $INFO_OPTS \
240     || {
241         echo "Linking $EXE failed"
242         cat $EXE.out
243         exit 1
244     }
245     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
246     chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
247     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
248 fi
249
250 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
251     EXE="$sharedlib_dir/busybox_unstripped"
252     try $CC $CFLAGS $LDFLAGS \
253             -o $EXE \
254             $SORT_COMMON \
255             $SORT_SECTION \
256             $GC_SECTIONS \
257             $START_GROUP $O_FILES $END_GROUP \
258             -L"$sharedlib_dir" -lbusybox \
259             $INFO_OPTS \
260     || {
261         echo "Linking $EXE failed"
262         cat $EXE.out
263         exit 1
264     }
265     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
266     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
267 fi
268
269 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
270     echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
271     gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
272     | grep -v "^#" \
273     | grep -v "^$" \
274     > applet_lst.tmp
275     while read name main junk; do
276
277         echo "\
278 void lbb_prepare(const char *applet, char **argv);
279 int $main(int argc, char **argv);
280
281 int main(int argc, char **argv)
282 {
283         lbb_prepare(\"$name\", argv);
284         return $main(argc, argv);
285 }
286 " >"$sharedlib_dir/applet.c"
287
288         EXE="$sharedlib_dir/$name"
289         try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
290                 -o $EXE \
291                 $SORT_COMMON \
292                 $SORT_SECTION \
293                 $GC_SECTIONS \
294                 -L"$sharedlib_dir" -lbusybox \
295                 -Wl,--warn-common \
296         || {
297             echo "Linking $EXE failed"
298             cat $EXE.out
299             exit 1
300         }
301         rm -- "$sharedlib_dir/applet.c" $EXE.out
302         $STRIP -s --remove-section=.note --remove-section=.comment $EXE
303
304     done <applet_lst.tmp
305 fi
306
307 # libbusybox.so is needed only for -lbusybox at link time,
308 # it is not needed at runtime. Deleting to reduce confusion.
309 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
310 exit 0 # or else we may confuse make