Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250209052239.10224-1-lhr@disroot.org>
Date: Sun,  9 Feb 2025 05:21:15 +0000
From: remph <lhr@...root.org>
To: musl@...ts.openwall.com
Cc: remph <lhr@...root.org>
Subject: [PATCH] musl-clang: handle user's -fuse-ld

musl-clang takes advantage of clang's -fuse-ld option to hijack the
linking process via ld.musl-clang and filter out non-musl system search
paths. However, if the user tries to pass a -fuse-ld of their own,
ld.musl-clang is not called and the linking typically errors out.
---
 tools/ld.musl-clang.in |  5 ++++-
 tools/musl-clang.in    | 28 ++++++++++++++++++----------
 2 files changed, 22 insertions(+), 11 deletions(-)
 mode change 100644 => 100755 tools/ld.musl-clang.in
 mode change 100644 => 100755 tools/musl-clang.in

diff --git a/tools/ld.musl-clang.in b/tools/ld.musl-clang.in
old mode 100644
new mode 100755
index 93763d6b..f87c5742
--- a/tools/ld.musl-clang.in
+++ b/tools/ld.musl-clang.in
@@ -26,6 +26,9 @@ for x ; do
         -l-user-end)
             userlink=
             ;;
+        -fuse-ld=*)
+            use_ld=.${x#*=} # Passed through by musl-clang
+            ;;
         crtbegin*.o|crtend*.o)
             set -- "$@" $($cc -print-file-name=$x)
             ;;
@@ -48,4 +51,4 @@ for x ; do
     esac
 done
 
-exec $($cc -print-prog-name=ld) -nostdlib "$@" -lc -dynamic-linker "$ldso"
+exec "$($cc -print-prog-name=ld$use_ld)" -nostdlib "$@" -lc -dynamic-linker "$ldso"
diff --git a/tools/musl-clang.in b/tools/musl-clang.in
old mode 100644
new mode 100755
index 623de6f6..985b31e3
--- a/tools/musl-clang.in
+++ b/tools/musl-clang.in
@@ -5,19 +5,27 @@ libc_inc="@INCDIR@"
 libc_lib="@LIBDIR@"
 thisdir="`cd "$(dirname "$0")"; pwd`"
 
-# prevent clang from running the linker (and erroring) on no input.
-sflags=
-eflags=
+input= cleared= sflags= eflags=
 for x ; do
+    test "$cleared" || set -- ; cleared=1
+
+    # prevent clang from running the linker (and erroring) on no input.
+    if test -z "$input" ; then
+        case "$x" in
+            -l*)
+                input=1
+                sflags="-l-user-start"
+                eflags="-l-user-end"
+                ;;
+        esac
+    fi
+
+    # Preserve the user's -fuse-ld
     case "$x" in
-        -l*) input=1 ;;
-        *) input= ;;
+        -fuse-ld=*) x=-Wl,$x ;; # Pass through to ld.musl-clang
     esac
-    if test "$input" ; then
-        sflags="-l-user-start"
-        eflags="-l-user-end"
-        break
-    fi
+
+    set -- "$@" "$x"
 done
 
 exec $cc \
-- 
2.48.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.