<div dir="ltr"><div><div>Hi,<br></div>I am trying to use compiler-rt as a replacement for libgcc in a compiler for the Rust language. In the process I came up with as set of patches, which I hope will be useful to other people as well. Specifically, these patches:<br>
</div><div></div><div><div>- enable building compiler-rt with GCC (because we don't want to require installation or building of clang),<br>
</div><div>- enable building it for an arbitrary LLVM target triple (just the runtime part for now, excluding sanitizers),<br></div><div>- enable building it on Windows with mingw (assembly syntax variations),<br></div><div>
- enable building it for arm-linux-androideabi (assembly syntax variations).<br></div><br></div><div></div><div><div></div><div></div></div><div>Vadim<br></div><div><div><div><br>----------<br><br>From 5d0ef64e0e35779af23c9ebedc2692f16f7e2e6d Mon Sep 17 00:00:00 2001<br>
From: Vadim Chugunov <<a href="mailto:vadimcn@gmail.com">vadimcn@gmail.com</a>><br>Date: Sun, 2 Feb 2014 22:43:10 -0800<br>Subject: [PATCH 1/5] Windows does not have the ".const" directive and the<br> function type must be declared differently.<br>
<br>---<br> lib/assembly.h | 4 ++++<br> lib/i386/floatdidf.S | 4 +---<br> lib/i386/floatundidf.S | 4 +---<br> lib/i386/floatundisf.S | 8 ++++----<br> lib/i386/floatundixf.S | 4 +---<br> lib/x86_64/floatundidf.S | 4 +---<br>
lib/x86_64/floatundixf.S | 6 ++----<br> 7 files changed, 14 insertions(+), 20 deletions(-)<br><br>diff --git a/lib/assembly.h b/lib/assembly.h<br>index 1a9f79c..b950d06 100644<br>--- a/lib/assembly.h<br>+++ b/lib/assembly.h<br>
@@ -27,12 +27,16 @@<br> #define LOCAL_LABEL(name) L_##name<br> #define FILE_LEVEL_DIRECTIVE .subsections_via_symbols<br> #define SYMBOL_IS_FUNC(name)<br>+#define CONST_SECTION .const<br> #else<br> #define HIDDEN_DIRECTIVE .hidden<br>
#define LOCAL_LABEL(name) .L_##name<br> #define FILE_LEVEL_DIRECTIVE<br>+#define CONST_SECTION .text<br> # if defined(__arm__)<br> # define SYMBOL_IS_FUNC(name) .type name, %function<br>+# elif __WIN32__<br>+# define SYMBOL_IS_FUNC(name) .def name; .scl 3; .type 32; .endef<br>
# else<br> # define SYMBOL_IS_FUNC(name) .type name, @function<br> # endif<br>diff --git a/lib/i386/floatdidf.S b/lib/i386/floatdidf.S<br>index 4936088..b0fb7c8 100644<br>--- a/lib/i386/floatdidf.S<br>+++ b/lib/i386/floatdidf.S<br>
@@ -7,9 +7,7 @@<br> <br> #ifdef __i386__<br> <br>-#ifndef __ELF__<br>-.const<br>-#endif<br>+CONST_SECTION<br> .align 4<br> twop52: .quad 0x4330000000000000<br> twop32: .quad 0x41f0000000000000<br>diff --git a/lib/i386/floatundidf.S b/lib/i386/floatundidf.S<br>
index 5b8787b..b2dcf3a 100644<br>--- a/lib/i386/floatundidf.S<br>+++ b/lib/i386/floatundidf.S<br>@@ -17,9 +17,7 @@<br> <br> #ifdef __i386__<br> <br>-#ifndef __ELF__<br>-.const<br>-#endif<br>+CONST_SECTION<br> .align 4<br>
twop52: .quad 0x4330000000000000<br> twop84_plus_twop52:<br>diff --git a/lib/i386/floatundisf.S b/lib/i386/floatundisf.S<br>index 2253021..3d29a62 100644<br>--- a/lib/i386/floatundisf.S<br>+++ b/lib/i386/floatundisf.S<br>
@@ -18,7 +18,7 @@<br> <br> #ifdef __i386__<br> <br>-.const<br>+CONST_SECTION<br> .align 3<br> <br> .quad 0x43f0000000000000<br>@@ -52,8 +52,8 @@ END_COMPILERRT_FUNCTION(__floatundisf)<br> <br> #ifdef __i386__<br>
<br>-#ifndef __ELF__<br>-.const<br>+CONST_SECTION<br>+#if !defined(__ELF__) && !defined(__WIN32__)<br> .align 3<br> #else<br> .align 8<br>@@ -74,7 +74,7 @@ DEFINE_COMPILERRT_FUNCTION(__floatundisf)<br> movd 8(%esp), %xmm1<br>
movd 4(%esp), %xmm0<br> punpckldq %xmm1, %xmm0<br>- <br>+<br> calll 0f<br> 0: popl %ecx<br> shrl %eax // high 31 bits of input as sint32<br>
diff --git a/lib/i386/floatundixf.S b/lib/i386/floatundixf.S<br>index b728d06..63ef3aa 100644<br>--- a/lib/i386/floatundixf.S<br>+++ b/lib/i386/floatundixf.S<br>@@ -7,9 +7,7 @@<br> <br> #ifdef __i386__<br> <br>-#ifndef __ELF__<br>
-.const<br>-#endif<br>+CONST_SECTION<br> .align 4<br> twop52: .quad 0x4330000000000000<br> twop84_plus_twop52_neg:<br>diff --git a/lib/x86_64/floatundidf.S b/lib/x86_64/floatundidf.S<br>index 1df3d74..7975e1a 100644<br>--- a/lib/x86_64/floatundidf.S<br>
+++ b/lib/x86_64/floatundidf.S<br>@@ -17,9 +17,7 @@<br> <br> #ifdef __x86_64__<br> <br>-#ifndef __ELF__<br>-.const<br>-#endif<br>+CONST_SECTION<br> .align 4<br> twop52: .quad 0x4330000000000000<br> twop84_plus_twop52:<br>
diff --git a/lib/x86_64/floatundixf.S b/lib/x86_64/floatundixf.S<br>index b05954a..037c2a7 100644<br>--- a/lib/x86_64/floatundixf.S<br>+++ b/lib/x86_64/floatundixf.S<br>@@ -7,9 +7,7 @@<br> <br> #ifdef __x86_64__<br> <br>-#ifndef __ELF__<br>
-.const<br>-#endif<br>+CONST_SECTION<br> .align 4<br> twop64: .quad 0x43f0000000000000<br> <br>@@ -35,7 +33,7 @@ END_COMPILERRT_FUNCTION(__floatundixf)<br> <br> #ifdef __x86_64__<br> <br>-.const<br>+CONST_SECTION<br> .align 4<br>
twop52: .quad 0x4330000000000000<br> twop84_plus_twop52_neg:<br>-- <br>1.8.5.2.msysgit.0<br><br>From deb3972b20fcb0679122730cdd752dc0a4519003 Mon Sep 17 00:00:00 2001<br>From: Vadim Chugunov <<a href="mailto:vadimcn@gmail.com">vadimcn@gmail.com</a>><br>
Date: Mon, 3 Feb 2014 18:43:13 -0800<br>Subject: [PATCH 2/5] Only Macs use .subsections_via_symbols<br><br>---<br> lib/arm/restore_vfp_d8_d15_regs.S | 4 ++--<br> lib/arm/save_vfp_d8_d15_regs.S | 4 ++--<br> lib/arm/switch16.S | 4 ++--<br>
lib/arm/switch32.S | 4 ++--<br> lib/arm/switch8.S | 4 ++--<br> lib/arm/switchu8.S | 4 ++--<br> lib/arm/sync_synchronize.S | 4 ++--<br> 7 files changed, 14 insertions(+), 14 deletions(-)<br>
<br>diff --git a/lib/arm/restore_vfp_d8_d15_regs.S b/lib/arm/restore_vfp_d8_d15_regs.S<br>index 5d55474..dc57655 100644<br>--- a/lib/arm/restore_vfp_d8_d15_regs.S<br>+++ b/lib/arm/restore_vfp_d8_d15_regs.S<br>@@ -31,5 +31,5 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__restore_vfp_d8_d15_regs)<br>
bx lr // return to prolog<br> END_COMPILERRT_FUNCTION(__restore_vfp_d8_d15_regs)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br>diff --git a/lib/arm/save_vfp_d8_d15_regs.S b/lib/arm/save_vfp_d8_d15_regs.S<br>index 4be3ff3..233772b 100644<br>--- a/lib/arm/save_vfp_d8_d15_regs.S<br>+++ b/lib/arm/save_vfp_d8_d15_regs.S<br>@@ -31,5 +31,5 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__save_vfp_d8_d15_regs)<br>
bx lr // return to prolog<br> END_COMPILERRT_FUNCTION(__save_vfp_d8_d15_regs)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br>diff --git a/lib/arm/switch16.S b/lib/arm/switch16.S<br>index 0dedc59..e0ca444 100644<br>--- a/lib/arm/switch16.S<br>+++ b/lib/arm/switch16.S<br>@@ -42,5 +42,5 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch16)<br>
bx ip // jump to computed label<br> END_COMPILERRT_FUNCTION(__switch16)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br>diff --git a/lib/arm/switch32.S b/lib/arm/switch32.S<br>index 64d558e..54812af 100644<br>--- a/lib/arm/switch32.S<br>+++ b/lib/arm/switch32.S<br>@@ -42,6 +42,6 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch32)<br>
bx ip // jump to computed label<br> END_COMPILERRT_FUNCTION(__switch32)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br> <br>diff --git a/lib/arm/switch8.S b/lib/arm/switch8.S<br>index b500884..e0d089d 100644<br>--- a/lib/arm/switch8.S<br>+++ b/lib/arm/switch8.S<br>@@ -40,6 +40,6 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch8)<br>
bx ip // jump to computed label<br> END_COMPILERRT_FUNCTION(__switch8)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br> <br>diff --git a/lib/arm/switchu8.S b/lib/arm/switchu8.S<br>index 488d4e7..daaf605 100644<br>--- a/lib/arm/switchu8.S<br>+++ b/lib/arm/switchu8.S<br>@@ -40,6 +40,6 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switchu8)<br>
bx ip // jump to computed label<br> END_COMPILERRT_FUNCTION(__switchu8)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br> <br>diff --git a/lib/arm/sync_synchronize.S b/lib/arm/sync_synchronize.S<br>index aa18f04..96971cd 100644<br>--- a/lib/arm/sync_synchronize.S<br>+++ b/lib/arm/sync_synchronize.S<br>@@ -29,7 +29,7 @@ DEFINE_COMPILERRT_PRIVATE_FUNCTION(__sync_synchronize)<br>
ldmfd sp!, {r7, pc}<br> END_COMPILERRT_FUNCTION(__sync_synchronize)<br> <br>- // tell linker it can break up file at label boundaries<br>- .subsections_via_symbols<br>+// tell linker it can break up file at label boundaries<br>
+FILE_LEVEL_DIRECTIVE<br> <br> #endif<br>-- <br>1.8.5.2.msysgit.0<br><br>From 47f3beebb666dcbc3c0e7653a7a808dffd4fa066 Mon Sep 17 00:00:00 2001<br>From: Vadim Chugunov <<a href="mailto:vadimcn@gmail.com">vadimcn@gmail.com</a>><br>
Date: Sun, 2 Feb 2014 22:44:28 -0800<br>Subject: [PATCH 3/5] -fPIC switch does not apply on Windows.<br><br>---<br> make/<a href="http://config.mk">config.mk</a> | 4 ++--<br> make/platform/<a href="http://clang_darwin.mk">clang_darwin.mk</a> | 2 +-<br>
make/platform/<a href="http://clang_linux.mk">clang_linux.mk</a> | 2 +-<br> make/platform/<a href="http://darwin_bni.mk">darwin_bni.mk</a> | 2 +-<br> make/platform/<a href="http://darwin_fat.mk">darwin_fat.mk</a> | 2 +-<br>
5 files changed, 6 insertions(+), 6 deletions(-)<br><br>diff --git a/make/<a href="http://config.mk">config.mk</a> b/make/<a href="http://config.mk">config.mk</a><br>index 2838b60..9364c74 100644<br>--- a/make/<a href="http://config.mk">config.mk</a><br>
+++ b/make/<a href="http://config.mk">config.mk</a><br>@@ -44,6 +44,6 @@ endif<br> ###<br> # Common compiler options<br> COMMON_INCLUDES=-I${ProjSrcRoot}/lib -I${ProjSrcRoot}/include<br>-COMMON_CXXFLAGS=-fno-exceptions -fPIC -funwind-tables $(COMMON_INCLUDES)<br>
-COMMON_CFLAGS=-fPIC $(COMMON_INCLUDES)<br>+COMMON_CXXFLAGS=-fno-exceptions -funwind-tables $(COMMON_INCLUDES)<br>+COMMON_CFLAGS=$(COMMON_INCLUDES)<br> COMMON_ASMFLAGS=$(COMMON_INCLUDES)<br>diff --git a/make/platform/<a href="http://clang_darwin.mk">clang_darwin.mk</a> b/make/platform/<a href="http://clang_darwin.mk">clang_darwin.mk</a><br>
index 3dd18e1..534fae9 100644<br>--- a/make/platform/<a href="http://clang_darwin.mk">clang_darwin.mk</a><br>+++ b/make/platform/<a href="http://clang_darwin.mk">clang_darwin.mk</a><br>@@ -136,7 +136,7 @@ endif<br> override CC := $(subst -arch ,-arch_,$(CC))<br>
override CC := $(patsubst -arch_%,,$(CC))<br> <br>-CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer<br>+CFLAGS := -fPIC -Wall -Werror -O3 -fomit-frame-pointer<br> <br> # Always set deployment target arguments for every build, these libraries should<br>
# never depend on the environmental overrides. We simply set them to minimum<br>diff --git a/make/platform/<a href="http://clang_linux.mk">clang_linux.mk</a> b/make/platform/<a href="http://clang_linux.mk">clang_linux.mk</a><br>
index fe39a2d..b6cbf32 100644<br>--- a/make/platform/<a href="http://clang_linux.mk">clang_linux.mk</a><br>+++ b/make/platform/<a href="http://clang_linux.mk">clang_linux.mk</a><br>@@ -85,7 +85,7 @@ endif<br> <br> ###<br>
<br>-CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer<br>+CFLAGS := -fPIC -Wall -Werror -O3 -fomit-frame-pointer<br> SANITIZER_CFLAGS := -fPIE -fno-builtin -gline-tables-only<br> <br> CFLAGS.full-i386 := $(CFLAGS) -m32<br>
diff --git a/make/platform/<a href="http://darwin_bni.mk">darwin_bni.mk</a> b/make/platform/<a href="http://darwin_bni.mk">darwin_bni.mk</a><br>index 03e8d29..aa1319b 100644<br>--- a/make/platform/<a href="http://darwin_bni.mk">darwin_bni.mk</a><br>
+++ b/make/platform/<a href="http://darwin_bni.mk">darwin_bni.mk</a><br>@@ -29,7 +29,7 @@ ifneq (,$(SDKROOT))<br> DEPLOYMENT_FLAGS += -isysroot $(SDKROOT)<br> endif<br> <br>-CFLAGS := -Wall -Os -fomit-frame-pointer -g $(DEPLOYMENT_FLAGS)<br>
+CFLAGS := -fPIC -Wall -Os -fomit-frame-pointer -g $(DEPLOYMENT_FLAGS)<br> CFLAGS.Static := $(CFLAGS) -static <br> DYLIB_FLAGS := $(DEPLOYMENT_FLAGS) \<br> -Xarch_arm -Wl,-alias_list,$(SRCROOT)/lib/arm/softfloat-alias.list<br>
diff --git a/make/platform/<a href="http://darwin_fat.mk">darwin_fat.mk</a> b/make/platform/<a href="http://darwin_fat.mk">darwin_fat.mk</a><br>index 54936a3..eaf55ff 100644<br>--- a/make/platform/<a href="http://darwin_fat.mk">darwin_fat.mk</a><br>
+++ b/make/platform/<a href="http://darwin_fat.mk">darwin_fat.mk</a><br>@@ -39,7 +39,7 @@ UniversalArchs := i386 x86_64<br> <br> CC := clang<br> <br>-CFLAGS := -Wall -Werror<br>+CFLAGS := -fPIC -Wall -Werror<br> CFLAGS.Debug := $(CFLAGS) -g<br>
CFLAGS.Release := $(CFLAGS) -O3 -fomit-frame-pointer<br> CFLAGS.Profile := $(CFLAGS) -pg -g<br>-- <br>1.8.5.2.msysgit.0<br><br>From 4036b71dc47ad734ba1130e54b07f00958841b11 Mon Sep 17 00:00:00 2001<br>From: Vadim Chugunov <<a href="mailto:vadimcn@gmail.com">vadimcn@gmail.com</a>><br>
Date: Mon, 3 Feb 2014 18:54:25 -0800<br>Subject: [PATCH 4/5] This file doesn't need to be executable.<br><br>---<br> lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc | 0<br> 1 file changed, 0 insertions(+), 0 deletions(-)<br>
mode change 100755 => 100644 lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc<br><br>diff --git a/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc b/lib/sanitizer_common/sanitizer_common_interceptors_ioctl.inc<br>
old mode 100755<br>new mode 100644<br>-- <br>1.8.5.2.msysgit.0<br><br>From d4606f1818dd8dfeaa3e509cd1cbac4482c3513e Mon Sep 17 00:00:00 2001<br>From: Vadim Chugunov <<a href="mailto:vadimcn@gmail.com">vadimcn@gmail.com</a>><br>
Date: Mon, 3 Feb 2014 00:03:01 -0800<br>Subject: [PATCH 5/5] Enable building compiler-rt for a specific LLVM triple<br> with GCC.<br><br>---<br> make/platform/<a href="http://triple.mk">triple.mk</a> | 27 +++++++++++++++++++++++++++<br>
1 file changed, 27 insertions(+)<br> create mode 100644 make/platform/<a href="http://triple.mk">triple.mk</a><br><br>diff --git a/make/platform/<a href="http://triple.mk">triple.mk</a> b/make/platform/<a href="http://triple.mk">triple.mk</a><br>
new file mode 100644<br>index 0000000..6a3eb99<br>--- /dev/null<br>+++ b/make/platform/<a href="http://triple.mk">triple.mk</a><br>@@ -0,0 +1,27 @@<br>+# This "platform" file is intended for building compiler-rt using gcc.<br>
+# The actual target platform is selected by setting the TargetTriple variable to the corresponding LLVM triple.<br>+<br>+Description := Static runtime libraries for platforms selected by 'TargetTriple'<br>+<br>+# Provide defaults for the required vars<br>
+ifndef CC<br>+ CC := gcc<br>+endif<br>+ifndef CFLAGS<br>+ CFLAGS := -Wall -O3<br>+endif<br>+<br>+Configs := runtime<br>+<br>+Arch := $(word 1,$(subst -, ,$(TargetTriple)))<br>+ifeq ($(Arch),i686)<br>+ Arch := i386<br>
+else ifeq ($(Arch),arm)<br>+ Arch := armv7<br>+endif<br>+<br>+# Filter out stuff that gcc cannot compile (these are only needed for clang-generated code anywasys).<br>+CommonFunctions_gcc := $(filter-out atomic enable_execute_stack,$(CommonFunctions))<br>
+<br>+FUNCTIONS.runtime := $(CommonFunctions_gcc) $(value ArchFunctions.$(Arch))<br>+<br>-- <br>1.8.5.2.msysgit.0<br><br><br></div></div></div></div>