14
# --sort-common reduces padding
15
# --sort-section alignment reduces padding
16
# --gc-sections throws out unused sections,
17
# does not work for shared libs
18
# -On Not used, maybe useful?
20
# List of files to link:
21
# $l_list == --start-group -llib1 -llib2 --end-group
22
# --start-group $O_FILES $A_FILES --end-group
24
# Shared library link:
25
# -shared self-explanatory
26
# -fPIC position-independent code
27
# --enable-new-dtags ?
29
# -soname="libbusybox.so.$BB_VER"
30
# --undefined=lbb_main Seed name to start pulling from
31
# (otherwise we'll need --whole-archive)
32
# -static Not used, but may be useful! manpage:
33
# "... This option can be used with -shared.
34
# Doing so means that a shared library
35
# is being created but that all of the library's
36
# external references must be resolved by pulling
37
# in entries from static libraries."
41
printf "%s\n" "Output of:" >$EXE.out
42
printf "%s\n" "$*" >>$EXE.out
43
printf "%s\n" "==========" >>$EXE.out
44
$debug && echo "Trying: $*"
50
tempname="$(mktemp tmp.XXXXXXXXXX)"
51
echo "int main(int argc,char**argv){return argv?argc:0;}" >"$tempname".c
52
# Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :(
53
# Was using "-xc /dev/null", but we need a valid C program.
54
$CC $CFLAGS $LDFLAGS $1 "$tempname".c -o "$tempname" >/dev/null 2>&1
56
rm -f "$tempname" "$tempname".c "$tempname".o
60
check_libc_is_glibc() {
61
tempname="$(mktemp tmp.XXXXXXXXXX)"
64
/* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
65
#if defined(__GLIBC__) && !defined(__UCLIBC__)
69
! $CC $CFLAGS "$tempname".c -c -o "$tempname".o >/dev/null 2>&1
71
rm -f "$tempname" "$tempname".c "$tempname".o
81
# We try to drop libraries from LDLIBS if build works without them,
82
# but ones from CONFIG_EXTRA_LDLIBS are always linked in.
83
# (For example, musl has stub utmp implementation, and if you link with
84
# a real utmp library in LDLIBS, dropping it "works" but resulting binary
85
# does not work properly).
87
CONFIG_EXTRA_LDLIBS="$8"
89
# The --sort-section option is not supported by older versions of ld
90
SORT_SECTION="-Wl,--sort-section,alignment"
91
if ! check_cc "-Wl,--sort-section,alignment"; then
92
echo "Your linker does not support --sort-section,alignment"
96
START_GROUP="-Wl,--start-group"
97
END_GROUP="-Wl,--end-group"
99
echo "-Wl,--warn-common -Wl,-Map,$EXE.map -Wl,--verbose"
102
# gold may not support --sort-common (yet)
103
SORT_COMMON="-Wl,--sort-common"
104
if ! check_cc "-Wl,--sort-common"; then
105
echo "Your linker does not support --sort-common"
109
# Static linking against glibc produces buggy executables
110
# (glibc does not cope well with ld --gc-sections).
111
# See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
112
# Note that glibc is unsuitable for static linking anyway.
113
# We are removing -Wl,--gc-sections from link command line.
114
GC_SECTIONS="-Wl,--gc-sections"
115
if (. ./.config && test x"$CONFIG_STATIC" = x"y") then
116
if check_libc_is_glibc; then
117
echo "Static linking against glibc, can't use --gc-sections"
121
# The --gc-sections option is not supported by older versions of ld
122
if test -n "$GC_SECTIONS"; then
123
if ! check_cc "$GC_SECTIONS"; then
124
echo "Your linker does not support $GC_SECTIONS"
129
# Sanitize lib list (dups, extra spaces etc)
130
LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
132
# First link with all libs. If it fails, bail out
133
echo "Trying libraries: $LDLIBS"
134
# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
135
l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
136
test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
137
try $CC $CFLAGS $LDFLAGS \
142
$START_GROUP $O_FILES $A_FILES $END_GROUP \
145
echo "Failed: $l_list"
147
echo 'Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.'
148
echo 'Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"'
152
# Now try to remove each lib and build without it.
153
# Stop when no lib can be removed.
154
while test "$LDLIBS"; do
155
$debug && echo "Trying libraries: $LDLIBS"
156
dropped_non_first_lib=false
158
for one in $LDLIBS; do
159
without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
160
# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
161
l_list=`echo " $without_one $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
162
test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
163
$debug && echo "Trying -l options: '$l_list'"
164
try $CC $CFLAGS $LDFLAGS \
169
$START_GROUP $O_FILES $A_FILES $END_GROUP \
172
echo " Library $one is not needed, excluding it"
173
LDLIBS="$without_one"
174
$first_lib || dropped_non_first_lib=true
176
echo " Library $one is needed, can't exclude it (yet)"
180
# We can stop trying to drop libs if either all libs were needed,
181
# or we excluded only the _first_ few.
182
# (else: we dropped some intermediate lib(s), maybe now we can succeed
183
# in dropping some of the preceding ones)
184
$dropped_non_first_lib || break
187
# Make the binary with final, minimal list of libs
188
echo "Final link with: ${LDLIBS:-<none>}"
189
l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
190
test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
191
# --verbose gives us gobs of info to stdout (e.g. linker script used)
192
if ! test -f busybox_ldscript; then
193
try $CC $CFLAGS $LDFLAGS \
198
$START_GROUP $O_FILES $A_FILES $END_GROUP \
206
echo "Custom linker script 'busybox_ldscript' found, using it"
207
# Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
208
# .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
209
# *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
210
# *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
211
# This will eliminate most of the padding (~3kb).
212
# Hmm, "ld --sort-section alignment" should do it too.
214
# There is a ld hack which is meant to decrease disk usage
215
# at the cost of more RAM usage (??!!) in standard ld script:
216
# /* Adjust the address for the data segment. We want to adjust up to
217
# the same address within the page on the next page up. */
218
# . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
220
# . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
221
# to unconditionally align .data to the next page boundary,
222
# instead of "next page, plus current offset in this page"
223
try $CC $CFLAGS $LDFLAGS \
228
-Wl,-T,busybox_ldscript \
229
$START_GROUP $O_FILES $A_FILES $END_GROUP \
240
sharedlib_dir="0_lib"
242
if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
243
mkdir "$sharedlib_dir" 2>/dev/null
244
test -d "$sharedlib_dir" || {
245
echo "Cannot make directory $sharedlib_dir"
248
ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
250
# Yes, "ld -shared -static" is a thing. It's a shared library which is itself static.
252
test "$CONFIG_FEATURE_LIBBUSYBOX_STATIC" = y && LBB_STATIC="-Wl,-static"
254
EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
255
try $CC $CFLAGS $LDFLAGS \
257
-shared -fPIC $LBB_STATIC \
258
-Wl,--enable-new-dtags \
260
-Wl,-soname="libbusybox.so.$BB_VER" \
261
-Wl,--undefined=lbb_main \
264
$START_GROUP $A_FILES $END_GROUP \
268
echo "Linking $EXE failed"
272
$STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
273
chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
274
echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
277
if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
278
EXE="$sharedlib_dir/busybox_unstripped"
279
try $CC $CFLAGS $LDFLAGS \
284
$START_GROUP $O_FILES $END_GROUP \
285
-L"$sharedlib_dir" -lbusybox \
289
echo "Linking $EXE failed"
293
$STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
294
echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
297
if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
298
echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
299
gcc -DNAME_MAIN -E -include include/autoconf.h include/applets.h \
303
while read name main junk; do
306
void lbb_prepare(const char *applet, char **argv);
307
int $main(int argc, char **argv);
309
int main(int argc, char **argv)
311
lbb_prepare(\"$name\", argv);
312
return $main(argc, argv);
314
" >"$sharedlib_dir/applet.c"
316
EXE="$sharedlib_dir/$name"
317
try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
322
-L"$sharedlib_dir" -lbusybox \
325
echo "Linking $EXE failed"
329
rm -- "$sharedlib_dir/applet.c" $EXE.out
330
$STRIP -s --remove-section=.note --remove-section=.comment $EXE
331
# Let user see that we do something - list the names of created binaries:
337
# libbusybox.so is needed only for -lbusybox at link time,
338
# it is not needed at runtime. Deleting to reduce confusion.
339
rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
340
exit 0 # or else we may confuse make