From: Behan Webster <[email protected]>
These patches add initial support for compiling the x86 kernel with clang. More
patches to the kernel code are required to actually compile the kernel with
clang. The intent of these patches are just to get things started with kbuild.
The LLVMLinux project aims to fully build the Linux kernel using both gcc and
clang (the C front end for the LLVM compiler infrastructure project).
Behan Webster (1):
kbuild: LLVMLinux: Add Kbuild support for building kernel with Clang
Jan-Simon Möller (3):
kbuild: LLVMLinux: Adapt warnings for compilation with clang
kbuild: LLVMLinux: Fix LINUX_COMPILER definition script for
compilation with clang
x86 kbuild: LLVMLinux: More cc-options added for clang
Mark Charlebois (1):
LLVMLinux: Add support for clang to compiler.h and new
compiler-clang.h
Makefile | 32 +++++++++++++++++++++++++++++++-
arch/x86/Makefile | 8 +++++++-
include/linux/compiler-clang.h | 12 ++++++++++++
include/linux/compiler.h | 7 +++++++
scripts/Makefile.build | 13 ++++++++++++-
scripts/mkcompile_h | 2 +-
6 files changed, 70 insertions(+), 4 deletions(-)
create mode 100644 include/linux/compiler-clang.h
--
1.8.3.2
From: Behan Webster <[email protected]>
Add support to toplevel Makefile for compiling with clang, both for
HOSTCC and CC. Use cc-option to prevent gcc option from breaking clang, and
from clang options from breaking gcc.
Clang 3.4 semantics are the same as gcc semantics for unsupported flags. For
unsupported warnings clang 3.4 returns true but shows a warning and gcc shows
a warning and returns false.
Signed-off-by: Behan Webster <[email protected]>
Signed-off-by: Jan-Simon Möller <[email protected]>
Signed-off-by: Mark Charlebois <[email protected]>
Cc: PaX Team <[email protected]>
---
Makefile | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 831b36a..c4ab30d 100644
--- a/Makefile
+++ b/Makefile
@@ -247,6 +247,15 @@ HOSTCXX = g++
HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer
HOSTCXXFLAGS = -O2
+ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
+HOSTCOMPILER := clang
+HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \
+ -Wno-missing-field-initializers -fno-delete-null-pointer-checks
+else
+HOSTCOMPILER := gcc
+endif
+export HOSTCOMPILER
+
# Decide whether to build built-in, modular, or both.
# Normally, just do built-in.
@@ -323,6 +332,12 @@ endif
export quiet Q KBUILD_VERBOSE
+ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
+COMPILER := clang
+else
+COMPILER := gcc
+endif
+export COMPILER
# Look for make include files relative to root of kernel src
MAKEFLAGS += --include-dir=$(srctree)
@@ -382,7 +397,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
-fno-strict-aliasing -fno-common \
-Werror-implicit-function-declaration \
-Wno-format-security \
- -fno-delete-null-pointer-checks
+ $(call cc-option,-fno-delete-null-pointer-checks,)
KBUILD_AFLAGS_KERNEL :=
KBUILD_CFLAGS_KERNEL :=
KBUILD_AFLAGS := -D__ASSEMBLY__
@@ -620,9 +635,24 @@ else
endif
KBUILD_CFLAGS += $(stackp-flag)
+ifeq ($(COMPILER),clang)
+KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
+KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
+KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
+KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
+KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
+# Quiet clang warning: comparison of unsigned expression < 0 is always false
+KBUILD_CFLAGS += $(call cc-disable-warning, tautological-compare)
+# CLANG uses a _MergedGlobals as optimization, but this breaks modpost, as the
+# source of a reference will be _MergedGlobals and not on of the whitelisted names.
+# See modpost pattern 2
+KBUILD_CFLAGS += $(call cc-option, -mno-global-merge,)
+else
+
# This warning generated too much noise in a regular build.
# Use make W=1 to enable this warning (see scripts/Makefile.build)
KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
+endif
ifdef CONFIG_FRAME_POINTER
KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
--
1.8.3.2
From: Jan-Simon Möller <[email protected]>
When compiling kernel with clang, disable warnings which are too noisy, and
add the clang flag catch-undefined-behavior.
Signed-off-by: Jan-Simon Möller <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Signed-off-by: Mark Charlebois <[email protected]>
Cc: PaX Team <[email protected]>
---
scripts/Makefile.build | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index d5d859c..27c4098 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -65,12 +65,23 @@ warning- := $(empty)
warning-1 := -Wextra -Wunused -Wno-unused-parameter
warning-1 += -Wmissing-declarations
warning-1 += -Wmissing-format-attribute
-warning-1 += -Wmissing-prototypes
+warning-1 += $(call cc-option, -Wmissing-prototypes)
warning-1 += -Wold-style-definition
warning-1 += $(call cc-option, -Wmissing-include-dirs)
warning-1 += $(call cc-option, -Wunused-but-set-variable)
warning-1 += $(call cc-disable-warning, missing-field-initializers)
+# Clang
+warning-1 += $(call cc-disable-warning, initializer-overrides)
+warning-1 += $(call cc-disable-warning, unused-value)
+warning-1 += $(call cc-disable-warning, format)
+warning-1 += $(call cc-disable-warning, unknown-warning-option)
+warning-1 += $(call cc-disable-warning, self-assign)
+warning-1 += $(call cc-disable-warning, sign-compare)
+warning-1 += $(call cc-disable-warning, format-zero-length)
+warning-1 += $(call cc-disable-warning, uninitialized)
+warning-1 += $(call cc-option, -fcatch-undefined-behavior)
+
warning-2 := -Waggregate-return
warning-2 += -Wcast-align
warning-2 += -Wdisabled-optimization
--
1.8.3.2
From: Jan-Simon Möller <[email protected]>
Protect more options for x86 with cc-option so that we don't get errors when
using clang instead of gcc. Add more or different options when using clang as
well. Also need to enforce that SSE is off for clang and the stack is 8-byte
aligned.
Signed-off-by: Jan-Simon Möller <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Signed-off-by: Mark Charlebois <[email protected]>
---
arch/x86/Makefile | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index eeda43a..852d8f0 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -108,7 +108,7 @@ else
# this works around some issues with generating unwind tables in older gccs
# newer gccs do it by default
- KBUILD_CFLAGS += -maccumulate-outgoing-args
+ KBUILD_CFLAGS += $(call cc-option,-maccumulate-outgoing-args)
endif
# Make sure compiler does not have buggy stack-protector support.
@@ -144,6 +144,12 @@ endif
sp-$(CONFIG_X86_32) := esp
sp-$(CONFIG_X86_64) := rsp
+# enforce no-sse for clang
+ifneq ($(COMPILER),clang)
+ KBUILD_CFLAGS += $(call cc-option,-mno-sse)
+ KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)
+endif
+
# do binutils support CFI?
cfi := $(call as-instr,.cfi_startproc\n.cfi_rel_offset $(sp-y)$(comma)0\n.cfi_endproc,-DCONFIG_AS_CFI=1)
# is .cfi_signal_frame supported too?
--
1.8.3.2
From: Jan-Simon Möller <[email protected]>
When building the LINUX_COMPILER definition, instead of merely taking the last
line from "$(CC) -v", grep for ' version ' in the output. This supports both
gcc and clang.
Signed-off-by: Jan-Simon Möller <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Signed-off-by: Mark Charlebois <[email protected]>
Cc: PaX Team <[email protected]>
---
scripts/mkcompile_h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h
index f221ddf..cfb8440 100755
--- a/scripts/mkcompile_h
+++ b/scripts/mkcompile_h
@@ -76,7 +76,7 @@ UTS_TRUNCATE="cut -b -$UTS_LEN"
echo \#define LINUX_COMPILE_BY \"`echo $LINUX_COMPILE_BY | $UTS_TRUNCATE`\"
echo \#define LINUX_COMPILE_HOST \"`echo $LINUX_COMPILE_HOST | $UTS_TRUNCATE`\"
- echo \#define LINUX_COMPILER \"`$CC -v 2>&1 | tail -n 1`\"
+ echo \#define LINUX_COMPILER \"`$CC -v 2>&1 | grep ' version '`\"
) > .tmpcompile
# Only replace the real compile.h if the new one is different,
--
1.8.3.2
From: Mark Charlebois <[email protected]>
Add a compiler-clang.h file to add specific macros needed for compiling the
kernel with clang.
Initially the only override required is the macro for silencing the
compiler for a purposefully uninintialized variable.
Author: Mark Charlebois <[email protected]>
Signed-off-by: Mark Charlebois <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
---
include/linux/compiler-clang.h | 12 ++++++++++++
include/linux/compiler.h | 7 +++++++
2 files changed, 19 insertions(+)
create mode 100644 include/linux/compiler-clang.h
diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
new file mode 100644
index 0000000..d1e49d5
--- /dev/null
+++ b/include/linux/compiler-clang.h
@@ -0,0 +1,12 @@
+#ifndef __LINUX_COMPILER_H
+#error "Please don't include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
+#endif
+
+/* Some compiler specific definitions are overwritten here
+ * for Clang compiler
+ */
+
+#ifdef uninitialized_var
+#undef uninitialized_var
+#define uninitialized_var(x) x = *(&(x))
+#endif
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 2472740..ee7239e 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -63,6 +63,13 @@ extern void __chk_io_ptr(const volatile void __iomem *);
# include <linux/compiler-intel.h>
#endif
+/* Clang compiler defines __GNUC__. So we will overwrite implementations
+ * coming from above header files here
+ */
+#ifdef __clang__
+#include <linux/compiler-clang.h>
+#endif
+
/*
* Generic compiler-dependent macros required for kernel
* build go below this comment. Actual compiler/compiler version
--
1.8.3.2
On Tue, Feb 25, 2014 at 05:08:40PM -0800, [email protected] wrote:
> When compiling kernel with clang, disable warnings which are too noisy, and
> add the clang flag catch-undefined-behavior.
>
> +# Clang
> +warning-1 += $(call cc-disable-warning, initializer-overrides)
> +warning-1 += $(call cc-disable-warning, unused-value)
> +warning-1 += $(call cc-disable-warning, format)
> +warning-1 += $(call cc-disable-warning, unknown-warning-option)
> +warning-1 += $(call cc-disable-warning, self-assign)
> +warning-1 += $(call cc-disable-warning, sign-compare)
> +warning-1 += $(call cc-disable-warning, format-zero-length)
> +warning-1 += $(call cc-disable-warning, uninitialized)
> +warning-1 += $(call cc-option, -fcatch-undefined-behavior)
Do you have a pointer to an example log-file from before this change ?
I'm curious for eg, which self-assign warnings are showing up,
because I've been fixing up the ones that Coverity found, of which
there are only a dozen or so left iirc.
Some of the others may also be interesting.
Dave
On 02/25/14 17:17, Dave Jones wrote:
> On Tue, Feb 25, 2014 at 05:08:40PM -0800, [email protected] wrote:
> > +warning-1 += $(call cc-disable-warning, self-assign)
>
> Do you have a pointer to an example log-file from before this change ?
> I'm curious for eg, which self-assign warnings are showing up,
> because I've been fixing up the ones that Coverity found, of which
> there are only a dozen or so left iirc.
>
> Some of the others may also be interesting.
I don't have one now, but I can generate a log and send it to you. We've
had that option set from near the beginning of the porting effort.
This are primarily off due to the amount of noise these warnings produce
at this time (clang produces a LOT of warnings straight out of the box).
I'd love to eventually turn some of these back on again.
Thanks,
Behan
--
Behan Webster
[email protected]
On 02/25/2014 05:08 PM, [email protected] wrote:
>
> +# enforce no-sse for clang
> +ifneq ($(COMPILER),clang)
> + KBUILD_CFLAGS += $(call cc-option,-mno-sse)
> + KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)
> +endif
> +
I'm *very* confused. You're doing ifneq here but you're talking about
it as if you are *adding* them for Clang, also these options are already
added elsewhere (lines 57, 64, 86, 89) so why add them here?
-hpa
On 02/25/2014 05:31 PM, Behan Webster wrote:
>
> This are primarily off due to the amount of noise these warnings produce
> at this time (clang produces a LOT of warnings straight out of the box).
> I'd love to eventually turn some of these back on again.
>
It would also be nice if someone would be willing to look at the sparse
warnings the kernel output -- there are tens of thousands of them it
seems, although some are from header files.
-hpa
On 02/25/14 17:32, H. Peter Anvin wrote:
> On 02/25/2014 05:08 PM, [email protected] wrote:
>>
>> +# enforce no-sse for clang
>> +ifneq ($(COMPILER),clang)
>> + KBUILD_CFLAGS += $(call cc-option,-mno-sse)
>> + KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)
>> +endif
>> +
> I'm *very* confused. You're doing ifneq here but you're talking about
> it as if you are *adding* them for Clang, also these options are already
> added elsewhere (lines 57, 64, 86, 89) so why add them here?
Because when David Woodhouse's recent .code16 changes made it upstream
(which invalidated most of our original patch) it seems I didn't remove
this properly from our patch as well. :)
Thanks David for your patches. Thanks Peter for your sharp eyes!
Another example of why reviewing code on the mailing list works so well.
I will fix and resend.
Behan
--
Behan Webster
[email protected]
On 02/25/14 17:17, Dave Jones wrote:
> On Tue, Feb 25, 2014 at 05:08:40PM -0800, [email protected] wrote:
>
> > When compiling kernel with clang, disable warnings which are too noisy, and
> > add the clang flag catch-undefined-behavior.
> >
> > +# Clang
> > +warning-1 += $(call cc-disable-warning, initializer-overrides)
> > +warning-1 += $(call cc-disable-warning, unused-value)
> > +warning-1 += $(call cc-disable-warning, format)
> > +warning-1 += $(call cc-disable-warning, unknown-warning-option)
> > +warning-1 += $(call cc-disable-warning, self-assign)
> > +warning-1 += $(call cc-disable-warning, sign-compare)
> > +warning-1 += $(call cc-disable-warning, format-zero-length)
> > +warning-1 += $(call cc-disable-warning, uninitialized)
> > +warning-1 += $(call cc-option, -fcatch-undefined-behavior)
>
> Do you have a pointer to an example log-file from before this change ?
> I'm curious for eg, which self-assign warnings are showing up,
> because I've been fixing up the ones that Coverity found, of which
> there are only a dozen or so left iirc.
I count 22 in this particular x86 kernel build (some of which you may
well have fixed already). Enjoy.
http://buildbot.llvm.linuxfoundation.org/self-assign-build.log.txt
Perhaps I should re-enable that warning, considering the number has
dropped so dramatically from when I last checked over a year ago.
Thanks,
Behan
--
Behan Webster
[email protected]