trylink: do not drop libs from CONFIG_EXTRA_LDLIBS

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/scripts/trylink b/scripts/trylink
index 6b74f09..2255dee 100755
--- a/scripts/trylink
+++ b/scripts/trylink
@@ -1,5 +1,6 @@
 #!/bin/sh
 
+#debug=true
 debug=false
 
 # Linker flags used:
@@ -77,7 +78,13 @@
 LDFLAGS="$4"
 O_FILES="$5"
 A_FILES="$6"
+# We try to drop libraries from LDLIBS if build works without them,
+# but ones from CONFIG_EXTRA_LDLIBS are always linked in.
+# (For example, musl has stub utmp implementation, and if you link with
+# a real utmp library in LDLIBS, dropping it "works" but resulting binary
+# does not work properly).
 LDLIBS="$7"
+CONFIG_EXTRA_LDLIBS="$8"
 
 # The --sort-section option is not supported by older versions of ld
 SORT_SECTION="-Wl,--sort-section,alignment"
@@ -125,8 +132,8 @@
 # First link with all libs. If it fails, bail out
 echo "Trying libraries: $LDLIBS"
 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
-l_list=`echo " $LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
-test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
+l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
+test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
 try $CC $CFLAGS $LDFLAGS \
 	-o $EXE \
 	$SORT_COMMON \
@@ -151,7 +158,7 @@
     for one in $LDLIBS; do
 	without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
 	# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
-	l_list=`echo " $without_one " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
+	l_list=`echo " $without_one $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
 	test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
 	$debug && echo "Trying -l options: '$l_list'"
 	try $CC $CFLAGS $LDFLAGS \
@@ -179,8 +186,8 @@
 
 # Make the binary with final, minimal list of libs
 echo "Final link with: ${LDLIBS:-<none>}"
-l_list=`echo " $LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
-test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
+l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
+test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
 # --verbose gives us gobs of info to stdout (e.g. linker script used)
 if ! test -f busybox_ldscript; then
     try $CC $CFLAGS $LDFLAGS \