From 640f10d7a65b7bd588c356291a78b192d7568a9d Mon Sep 17 00:00:00 2001 From: Behan Webster Date: Tue, 3 Sep 2013 22:27:23 -0400 Subject: kbuild: LLVMLinux: Add Kbuild support for building kernel with Clang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Jan-Simon Möller Signed-off-by: Mark Charlebois Cc: PaX Team --- Makefile | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e5ac8a62e6e5..67e0ba35c731 100644 --- a/Makefile +++ b/Makefile @@ -247,6 +247,11 @@ 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) +HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \ + -Wno-missing-field-initializers -fno-delete-null-pointer-checks +endif + # Decide whether to build built-in, modular, or both. # Normally, just do built-in. @@ -323,6 +328,14 @@ endif export quiet Q KBUILD_VERBOSE +ifneq ($(CC),) +ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1) +COMPILER := clang +else +COMPILER := gcc +endif +export COMPILER +endif # Look for make include files relative to root of kernel src MAKEFLAGS += --include-dir=$(srctree) @@ -382,7 +395,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__ @@ -622,9 +635,24 @@ endif 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 -- cgit v1.2.3 From d1b49729a31e537b11f47ceb523bb1b5490aa01a Mon Sep 17 00:00:00 2001 From: Jan-Simon Möller Date: Wed, 14 Nov 2012 10:39:57 +0100 Subject: kbuild: LLVMLinux: Adapt warnings for compilation with clang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Behan Webster Signed-off-by: Mark Charlebois Cc: PaX Team --- scripts/Makefile.build | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/Makefile.build b/scripts/Makefile.build index d5d859c80729..5cd6651b247c 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -65,12 +65,22 @@ 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, 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 -- cgit v1.2.3 From 896ff880ff0758f58c01087f878cddc367b5a04d Mon Sep 17 00:00:00 2001 From: Jan-Simon Möller Date: Wed, 14 Nov 2012 10:55:00 +0100 Subject: Documentation: LLVMLinux: Update Documentation/dontdiff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clang has a few other kinds of derived files which shouldn't be added to a patch. Add them to the Documentation/dontdiff file to prevent this. Signed-off-by: Jan-Simon Möller Signed-off-by: Behan Webster Cc: PaX Team --- Documentation/dontdiff | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Documentation/dontdiff b/Documentation/dontdiff index b89a739a3276..9de9813d0ec5 100644 --- a/Documentation/dontdiff +++ b/Documentation/dontdiff @@ -1,5 +1,6 @@ *.a *.aux +*.bc *.bin *.bz2 *.cis @@ -21,6 +22,7 @@ *.i *.jpeg *.ko +*.ll *.log *.lst *.lzma @@ -35,6 +37,7 @@ *.out *.patch *.pdf +*.plist *.png *.pot *.ps -- cgit v1.2.3 From 33be7d7bce36f1d0282aa9a9bfe948b86c35bfd8 Mon Sep 17 00:00:00 2001 From: Jan-Simon Möller Date: Wed, 14 Nov 2012 10:55:48 +0100 Subject: kbuild: LLVMLinux: Fix LINUX_COMPILER definition script for compilation with clang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Behan Webster Signed-off-by: Mark Charlebois Cc: PaX Team --- scripts/mkcompile_h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h index f221ddf69080..cfb8440cc0b2 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, -- cgit v1.2.3 From e8cccb2e4baec59cc0fabcc86cc8c98a3f8f5f26 Mon Sep 17 00:00:00 2001 From: Behan Webster Date: Tue, 20 Nov 2012 21:19:50 +0100 Subject: LLVMLinux: Remove warning about returning an uninitialized variable Fix uninitialized return code in default case in cmpxchg-local.h This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang. Signed-off-by: Behan Webster Signed-off-by: Mark Charlebois Acked-by: David Howells Acked-by: Arnd Bergmann --- include/asm-generic/cmpxchg-local.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/asm-generic/cmpxchg-local.h b/include/asm-generic/cmpxchg-local.h index d8d4c898c1bb..70bef78912b7 100644 --- a/include/asm-generic/cmpxchg-local.h +++ b/include/asm-generic/cmpxchg-local.h @@ -4,7 +4,8 @@ #include #include -extern unsigned long wrong_size_cmpxchg(volatile void *ptr); +extern unsigned long wrong_size_cmpxchg(volatile void *ptr) + __noreturn; /* * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned -- cgit v1.2.3 From 5e673e92b73b671b3c3196aabd30fa0d997a972b Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Tue, 20 Nov 2012 22:13:10 +0100 Subject: LLVMLinux: Add support for clang to compiler.h and new compiler-clang.h 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 Signed-off-by: Mark Charlebois Signed-off-by: Behan Webster --- 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 000000000000..d1e49d52b640 --- /dev/null +++ b/include/linux/compiler-clang.h @@ -0,0 +1,12 @@ +#ifndef __LINUX_COMPILER_H +#error "Please don't include directly, include 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 2472740d7ab2..ee7239ea1583 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 #endif +/* Clang compiler defines __GNUC__. So we will overwrite implementations + * coming from above header files here + */ +#ifdef __clang__ +#include +#endif + /* * Generic compiler-dependent macros required for kernel * build go below this comment. Actual compiler/compiler version -- cgit v1.2.3 From 77295dd1974fa5356ac5162ad2ba81b6aa0d6867 Mon Sep 17 00:00:00 2001 From: Behan Webster Date: Tue, 12 Feb 2013 22:25:57 -0500 Subject: x86, acpi: LLVMLinux: Remove nested functions from Thinkpad ACPI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only real change is passing in event_mask to the formerly nested functions. Otherwise it's just moving around function and macro code. This is the only place in the Linux kernel where nested functions are still in use. Nested functions aren't part of the C standards, and complicate the generated code. Although the Linux Kernel has never set out to be entirely C standard compliant, it is increasingly compliant to the standard which is supported by other compilers such as Clang. The LLVMLinux project is working on being able to compile the Linux kernel with Clang. The use of nested functions blocks this effort. Signed-off-by: Behan Webster Signed-off-by: Jan-Simon Möller Acked-by: Henrique de Moraes Holschuh --- drivers/platform/x86/thinkpad_acpi.c | 86 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c index defb6afc1409..e6e068ec1ad4 100644 --- a/drivers/platform/x86/thinkpad_acpi.c +++ b/drivers/platform/x86/thinkpad_acpi.c @@ -2321,53 +2321,55 @@ static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m) } } -static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, - struct tp_nvram_state *newn, - const u32 event_mask) -{ - #define TPACPI_COMPARE_KEY(__scancode, __member) \ - do { \ - if ((event_mask & (1 << __scancode)) && \ - oldn->__member != newn->__member) \ - tpacpi_hotkey_send_key(__scancode); \ - } while (0) +do { \ + if ((event_mask & (1 << __scancode)) && \ + oldn->__member != newn->__member) \ + tpacpi_hotkey_send_key(__scancode); \ +} while (0) #define TPACPI_MAY_SEND_KEY(__scancode) \ - do { \ - if (event_mask & (1 << __scancode)) \ - tpacpi_hotkey_send_key(__scancode); \ - } while (0) +do { \ + if (event_mask & (1 << __scancode)) \ + tpacpi_hotkey_send_key(__scancode); \ +} while (0) - void issue_volchange(const unsigned int oldvol, - const unsigned int newvol) - { - unsigned int i = oldvol; +static void issue_volchange(const unsigned int oldvol, + const unsigned int newvol, + const u32 event_mask) +{ + unsigned int i = oldvol; - while (i > newvol) { - TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); - i--; - } - while (i < newvol) { - TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); - i++; - } + while (i > newvol) { + TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); + i--; + } + while (i < newvol) { + TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); + i++; } +} - void issue_brightnesschange(const unsigned int oldbrt, - const unsigned int newbrt) - { - unsigned int i = oldbrt; +static void issue_brightnesschange(const unsigned int oldbrt, + const unsigned int newbrt, + const u32 event_mask) +{ + unsigned int i = oldbrt; - while (i > newbrt) { - TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); - i--; - } - while (i < newbrt) { - TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); - i++; - } + while (i > newbrt) { + TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); + i--; } + while (i < newbrt) { + TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); + i++; + } +} + +static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, + struct tp_nvram_state *newn, + const u32 event_mask) +{ TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle); TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle); @@ -2402,7 +2404,8 @@ static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, oldn->volume_level != newn->volume_level) { /* recently muted, or repeated mute keypress, or * multiple presses ending in mute */ - issue_volchange(oldn->volume_level, newn->volume_level); + issue_volchange(oldn->volume_level, newn->volume_level, + event_mask); TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); } } else { @@ -2412,7 +2415,8 @@ static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); } if (oldn->volume_level != newn->volume_level) { - issue_volchange(oldn->volume_level, newn->volume_level); + issue_volchange(oldn->volume_level, newn->volume_level, + event_mask); } else if (oldn->volume_toggle != newn->volume_toggle) { /* repeated vol up/down keypress at end of scale ? */ if (newn->volume_level == 0) @@ -2425,7 +2429,7 @@ static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, /* handle brightness */ if (oldn->brightness_level != newn->brightness_level) { issue_brightnesschange(oldn->brightness_level, - newn->brightness_level); + newn->brightness_level, event_mask); } else if (oldn->brightness_toggle != newn->brightness_toggle) { /* repeated key presses that didn't change state */ if (newn->brightness_level == 0) -- cgit v1.2.3 From 3f9fd143c5e33166d3c00aa1c0c1fe2cb7f8e050 Mon Sep 17 00:00:00 2001 From: Jan-Simon Möller Date: Sun, 20 Oct 2013 00:36:47 +0200 Subject: x86 kbuild: LLVMLinux: More cc-options added for clang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Behan Webster Signed-off-by: Mark Charlebois --- arch/x86/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index eeda43abed6e..d5a5a914489d 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. -- cgit v1.2.3 From fbc151c073f42fd5c5f12b61a1a246ab5721e741 Mon Sep 17 00:00:00 2001 From: Behan Webster Date: Thu, 13 Feb 2014 12:21:48 -0800 Subject: x86: LLVMLinux: Fix "incomplete type const struct x86cpu_device_id" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Similar to the fix in 40413dcb7b273bda681dca38e6ff0bbb3728ef11 MODULE_DEVICE_TABLE(x86cpu, ...) expects the struct to be called struct x86cpu_device_id, and not struct x86_cpu_id which is what is used in the rest of the kernel code. Although gcc seems to ignore this error, clang fails without this define to fix the name. Code from drivers/thermal/x86_pkg_temp_thermal.c static const struct x86_cpu_id __initconst pkg_temp_thermal_ids[] = { ... }; MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids); Error from clang: drivers/thermal/x86_pkg_temp_thermal.c:577:1: error: variable has incomplete type 'const struct x86cpu_device_id' MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids); ^ include/linux/module.h:145:3: note: expanded from macro 'MODULE_DEVICE_TABLE' MODULE_GENERIC_TABLE(type##_device, name) ^ include/linux/module.h:87:32: note: expanded from macro 'MODULE_GENERIC_TABLE' extern const struct gtype##_id __mod_##gtype##_table \ ^ :143:1: note: expanded from here __mod_x86cpu_device_table ^ drivers/thermal/x86_pkg_temp_thermal.c:577:1: note: forward declaration of 'struct x86cpu_device_id' include/linux/module.h:145:3: note: expanded from macro 'MODULE_DEVICE_TABLE' MODULE_GENERIC_TABLE(type##_device, name) ^ include/linux/module.h:87:21: note: expanded from macro 'MODULE_GENERIC_TABLE' extern const struct gtype##_id __mod_##gtype##_table \ ^ :141:1: note: expanded from here x86cpu_device_id ^ 1 error generated. Signed-off-by: Behan Webster Signed-off-by: Jan-Simon Möller Acked-by: Greg Kroah-Hartman --- include/linux/mod_devicetable.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h index 45e921401b06..dbb5cf09f72f 100644 --- a/include/linux/mod_devicetable.h +++ b/include/linux/mod_devicetable.h @@ -548,6 +548,11 @@ struct amba_id { * See documentation of "x86_match_cpu" for details. */ +/* + * MODULE_DEVICE_TABLE expects this struct to be called x86cpu_device_id. + * Although gcc seems to ignore this error, clang fails without this define. + */ +#define x86cpu_device_id x86_cpu_id struct x86_cpu_id { __u16 vendor; __u16 family; -- cgit v1.2.3