Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Fri, 29 May 2015 18:48:45 +0200
From: Shiz <hi@...z.me>
To: musl@...ts.openwall.com
Subject: [PATCH 1/2] add musl-clang, a wrapper for system clang installs

musl-clang allows the user to compile musl-powered programs using their
already existent clang install, without needing a special cross compiler.
it does this by wrapping around both the system clang install and the linker
and passing them special flags to re-target musl at runtime. it does only
affect invocations done through the special musl-clang wrapper script,
so that the user setup remains fully intact otherwise.

the clang wrapper consists of the compiler frontend wrapper script,
musl-clang, and the linker wrapper script, ld.musl-clang.
musl-clang makes sure clang invokes ld.musl-clang to link objects;
neither script needs to be in PATH for the wrapper to work.
---
 .gitignore             |  1 +
 Makefile               |  6 +++++-
 tools/ld.musl-clang.in | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tools/musl-clang.in    | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 tools/ld.musl-clang.in
 create mode 100644 tools/musl-clang.in

diff --git a/.gitignore b/.gitignore
index 070f549..00e4d0a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,6 @@ arch/*/bits/alltypes.h
 config.mak
 include/bits
 tools/musl-gcc
+tools/*musl-clang
 lib/musl-gcc.specs
 src/internal/version.h
diff --git a/Makefile b/Makefile
index 3bd7b4d..0537c6a 100644
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,7 @@ STATIC_LIBS = lib/libc.a
 SHARED_LIBS = lib/libc.so
 TOOL_LIBS = lib/musl-gcc.specs
 ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
-ALL_TOOLS = tools/musl-gcc
+ALL_TOOLS = tools/musl-gcc tools/musl-clang tools/ld.musl-clang
 
 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
 
@@ -156,6 +156,10 @@ tools/musl-gcc: config.mak
 	printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
 	chmod +x $@
 
+tools/%-clang: tools/%-clang.in config.mak
+	sed -e 's!@...DIR@!$(includedir)!g' -e 's!@...DIR@!$(libdir)!g' -e 's!@...O@!$(LDSO_PATHNAME)!g' $< > $@
+	chmod +x $@
+
 $(DESTDIR)$(bindir)/%: tools/%
 	$(INSTALL) -D $< $@
 
diff --git a/tools/ld.musl-clang.in b/tools/ld.musl-clang.in
new file mode 100644
index 0000000..bb77bb1
--- /dev/null
+++ b/tools/ld.musl-clang.in
@@ -0,0 +1,45 @@
+#!/bin/sh
+cc="@CC@"
+libc_lib="@LIBDIR@"
+ldso="@LDSO@"
+cleared=
+shared=
+crt=
+userlinkdir=
+userlink=
+
+for x ; do
+    test "$cleared" || set -- ; cleared=1
+
+    case "$x" in
+        -L-user-start)
+            userlinkdir=1
+            ;;
+        -L-user-end)
+            userlinkdir=
+            ;;
+        -L*)
+            test "$userlinkdir" && set -- "$@" "$x"
+            ;;
+        -l-user-start)
+            test "$shared" || set -- "$@" "$libc_lib/Scrt1.o" "$libc_lib/crti.o"
+            userlink=1
+            ;;
+        -l-user-end)
+            userlink=
+            ;;
+        -l*)
+            test "$crt" -o "$shared" || set -- "$@" "$libc_lib/crtn.o" ; crt=1
+            test "$userlink" && set -- "$@" "$x"
+            ;;
+        -shared)
+            shared=1
+            set -- "$@" -shared
+            ;;
+        *)
+            set -- "$@" "$x"
+            ;;
+    esac
+done
+
+exec $($cc -print-prog-name=ld) -nostdlib "$@" -dynamic-linker "$ldso"
diff --git a/tools/musl-clang.in b/tools/musl-clang.in
new file mode 100644
index 0000000..51e0196
--- /dev/null
+++ b/tools/musl-clang.in
@@ -0,0 +1,35 @@
+#!/bin/sh
+cc="@CC@"
+libc_inc="@INCDIR@"
+libc_lib="@LIBDIR@"
+thisdir="`cd "$(dirname "$0")"; pwd`"
+
+# prevent clang from running the linker (and erroring) on no input.
+sflags=
+eflags=
+for x ; do
+    case "$x" in
+        -l*) input=1 ;;
+        -*) input= ;;
+        *) input=1 ;;
+    esac
+    if test "$input" ; then
+        sflags="-l-user-start"
+        eflags="-lc -l-user-end"
+        break
+    fi
+done
+
+exec $cc \
+    -B"$thisdir" \
+    -fuse-ld=musl-clang \
+    -rtlib=compiler-rt \
+    -nostdinc \
+    -nostartfiles \
+    -isystem "$libc_inc" \
+    -L-user-start \
+    $sflags \
+    "$@" \
+    $eflags \
+    -L"$libc_lib" \
+    -L-user-end
-- 
2.3.6

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.