f57a8a1607bbe3d0ef62d9603c09d9045903f7b9
[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     exitcode=$?
46     return $exitcode
47 }
48
49 check_cc() {
50     if $CC $1 -shared -o /dev/null -xc /dev/null > /dev/null 2>&1; then
51         echo "$1";
52     else
53         echo "$2";
54     fi
55 }
56
57 EXE="$1"
58 CC="$2"
59 LDFLAGS="$3"
60 O_FILES="$4"
61 A_FILES="$5"
62 LDLIBS="$6"
63
64 # The -Wl,--sort-section option is not supported by older versions of ld
65 SORT_SECTION=`check_cc "-Wl,--sort-section -Wl,alignment" ""`
66
67 # Sanitize lib list (dups, extra spaces etc)
68 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
69
70 # First link with all libs. If it fails, bail out
71 echo "Trying libraries: $LDLIBS"
72 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
73 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
74 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
75 try $CC $LDFLAGS \
76         -o $EXE \
77         -Wl,--sort-common \
78         $SORT_SECTION \
79         -Wl,--gc-sections \
80         -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
81         $l_list \
82 || {
83     echo "Failed: $* $l_list"
84     cat $EXE.out
85     exit 1
86 }
87
88 # Now try to remove each lib and build without it.
89 # Stop when no lib can be removed.
90 while test "$LDLIBS"; do
91     $debug && echo "Trying libraries: $LDLIBS"
92     all_needed=true
93     for one in $LDLIBS; do
94         without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
95         # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
96         l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
97         test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
98         $debug && echo "Trying -l options: '$l_list'"
99         try $CC $LDFLAGS \
100                 -o $EXE \
101                 -Wl,--sort-common \
102                 $SORT_SECTION \
103                 -Wl,--gc-sections \
104                 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
105                 $l_list
106         if test $? = 0; then
107             echo " Library $one is not needed"
108             LDLIBS="$without_one"
109             all_needed=false
110         else
111             echo " Library $one is needed"
112         fi
113     done
114     # All libs were needed, can't remove any
115     $all_needed && break
116     # If there is no space char, the list has just one lib.
117     # I'm not sure that in this case lib really is 100% needed.
118     # Let's try linking without it anyway... thus commented out.
119     #{ echo "$LDLIBS" | grep -q ' '; } || break
120 done
121
122 # Make the binary with final, minimal list of libs
123 echo "Final link with: ${LDLIBS:-<none>}"
124 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
125 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
126 # --verbose gives us gobs of info to stdout (e.g. linker script used)
127 if ! test -f busybox_ldscript; then
128     try $CC $LDFLAGS \
129             -o $EXE \
130             -Wl,--sort-common \
131             $SORT_SECTION \
132             -Wl,--gc-sections \
133             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
134             $l_list \
135             -Wl,--warn-common \
136             -Wl,-Map -Wl,$EXE.map \
137             -Wl,--verbose \
138     || {
139         cat $EXE.out
140         exit 1
141     }
142 else
143     echo "Custom linker script 'busybox_ldscript' found, using it"
144     # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
145     #  .rodata         : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
146     #  *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
147     #  *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
148     # This will eliminate most of the padding (~3kb).
149     # Hmm, "ld --sort-section alignment" should do it too.
150     try $CC $LDFLAGS \
151             -o $EXE \
152             -Wl,--sort-common \
153             $SORT_SECTION \
154             -Wl,--gc-sections \
155             -Wl,-T -Wl,busybox_ldscript \
156             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
157             $l_list \
158             -Wl,--warn-common \
159             -Wl,-Map -Wl,$EXE.map \
160             -Wl,--verbose \
161     || {
162         cat $EXE.out
163         exit 1
164     }
165 fi
166
167 . ./.config
168
169 sharedlib_dir="0_lib"
170
171 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
172     mkdir "$sharedlib_dir" 2>/dev/null
173     test -d "$sharedlib_dir" || {
174         echo "Cannot make directory $sharedlib_dir"
175         exit 1
176     }
177     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
178
179     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
180     try $CC $LDFLAGS \
181             -o $EXE \
182             -shared -fPIC \
183             -Wl,--enable-new-dtags \
184             -Wl,-z,combreloc \
185             -Wl,-soname="libbusybox.so.$BB_VER" \
186             -Wl,--undefined=lbb_main \
187             -Wl,--sort-common \
188             $SORT_SECTION \
189             -Wl,--start-group $A_FILES -Wl,--end-group \
190             $l_list \
191             -Wl,--warn-common \
192             -Wl,-Map -Wl,$EXE.map \
193             -Wl,--verbose \
194     || {
195         echo "Linking $EXE failed"
196         cat $EXE.out
197         exit 1
198     }
199     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
200     chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
201     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
202 fi
203
204 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
205     EXE="$sharedlib_dir/busybox_unstripped"
206     try $CC $LDFLAGS \
207             -o $EXE \
208             -Wl,--sort-common \
209             $SORT_SECTION \
210             -Wl,--gc-sections \
211             -Wl,--start-group $O_FILES -Wl,--end-group \
212             -L"$sharedlib_dir" -lbusybox \
213             -Wl,--warn-common \
214             -Wl,-Map -Wl,$EXE.map \
215             -Wl,--verbose \
216     || {
217         echo "Linking $EXE failed"
218         cat $EXE.out
219         exit 1
220     }
221     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
222     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
223 fi
224
225 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
226     echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
227     gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
228     | grep -v "^#" \
229     | grep -v "^$" \
230     > applet_lst.tmp
231     while read name main junk; do
232
233         echo "\
234 void lbb_prepare(const char *applet, char **argv);
235 int $main(int argc, char **argv);
236
237 int main(int argc, char **argv)
238 {
239         lbb_prepare(\"$name\", argv);
240         return $main(argc, argv);
241 }
242 " >"$sharedlib_dir/applet.c"
243
244         EXE="$sharedlib_dir/$name"
245         try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
246                 -o $EXE \
247                 -Wl,--sort-common \
248                 $SORT_SECTION \
249                 -Wl,--gc-sections \
250                 -L"$sharedlib_dir" -lbusybox \
251                 -Wl,--warn-common \
252         || {
253             echo "Linking $EXE failed"
254             cat $EXE.out
255             exit 1
256         }
257         rm -- "$sharedlib_dir/applet.c" $EXE.out
258         $STRIP -s --remove-section=.note --remove-section=.comment $EXE
259
260     done <applet_lst.tmp
261 fi
262
263 # libbusybox.so is needed only for -lbusybox at link time,
264 # it is not needed at runtime. Deleting to reduce confusion.
265 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
266 exit 0 # or else we may confuse make