Skip to content

Commit 815f0dd

Browse files
nickdesaulnierstorvalds
authored andcommitted
include/linux/compiler*.h: make compiler-*.h mutually exclusive
Commit cafa001 ("Raise the minimum required gcc version to 4.6") recently exposed a brittle part of the build for supporting non-gcc compilers. Both Clang and ICC define __GNUC__, __GNUC_MINOR__, and __GNUC_PATCHLEVEL__ for quick compatibility with code bases that haven't added compiler specific checks for __clang__ or __INTEL_COMPILER. This is brittle, as they happened to get compatibility by posing as a certain version of GCC. This broke when upgrading the minimal version of GCC required to build the kernel, to a version above what ICC and Clang claim to be. Rather than always including compiler-gcc.h then undefining or redefining macros in compiler-intel.h or compiler-clang.h, let's separate out the compiler specific macro definitions into mutually exclusive headers, do more proper compiler detection, and keep shared definitions in compiler_types.h. Fixes: cafa001 ("Raise the minimum required gcc version to 4.6") Reported-by: Masahiro Yamada <[email protected]> Suggested-by: Eli Friedman <[email protected]> Suggested-by: Joe Perches <[email protected]> Signed-off-by: Nick Desaulniers <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 899fbc3 commit 815f0dd

File tree

9 files changed

+133
-253
lines changed

9 files changed

+133
-253
lines changed

arch/arm/kernel/asm-offsets.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,14 @@
3838
#error Sorry, your compiler targets APCS-26 but this kernel requires APCS-32
3939
#endif
4040
/*
41-
* GCC 3.0, 3.1: general bad code generation.
42-
* GCC 3.2.0: incorrect function argument offset calculation.
43-
* GCC 3.2.x: miscompiles NEW_AUX_ENT in fs/binfmt_elf.c
44-
* (http://gcc.gnu.org/PR8896) and incorrect structure
45-
* initialisation in fs/jffs2/erase.c
4641
* GCC 4.8.0-4.8.2: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58854
4742
* miscompiles find_get_entry(), and can result in EXT3 and EXT4
4843
* filesystem corruption (possibly other FS too).
4944
*/
50-
#ifdef __GNUC__
51-
#if (__GNUC__ == 3 && __GNUC_MINOR__ < 3)
52-
#error Your compiler is too buggy; it is known to miscompile kernels.
53-
#error Known good compilers: 3.3, 4.x
54-
#endif
55-
#if GCC_VERSION >= 40800 && GCC_VERSION < 40803
45+
#if defined(GCC_VERSION) && GCC_VERSION >= 40800 && GCC_VERSION < 40803
5646
#error Your compiler is too buggy; it is known to miscompile kernels
5747
#error and result in filesystem corruption and oopses.
5848
#endif
59-
#endif
6049

6150
int main(void)
6251
{

drivers/gpu/drm/i915/i915_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
4444
__stringify(x), (long)(x))
4545

46-
#if GCC_VERSION >= 70000
46+
#if defined(GCC_VERSION) && GCC_VERSION >= 70000
4747
#define add_overflows(A, B) \
4848
__builtin_add_overflow_p((A), (B), (typeof((A) + (B)))0)
4949
#else

drivers/watchdog/kempld_wdt.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,7 @@ static int kempld_wdt_set_stage_timeout(struct kempld_wdt_data *wdt_data,
146146
u32 remainder;
147147
u8 stage_cfg;
148148

149-
#if GCC_VERSION < 40400
150-
/* work around a bug compiling do_div() */
151-
prescaler = READ_ONCE(kempld_prescaler[PRESCALER_21]);
152-
#else
153149
prescaler = kempld_prescaler[PRESCALER_21];
154-
#endif
155150

156151
if (!stage)
157152
return -EINVAL;

include/linux/compiler-clang.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
/* Some compiler specific definitions are overwritten here
77
* for Clang compiler
88
*/
9-
10-
#ifdef uninitialized_var
11-
#undef uninitialized_var
129
#define uninitialized_var(x) x = *(&(x))
13-
#endif
1410

1511
/* same as gcc, this was present in clang-2.6 so we can assume it works
1612
* with any version that can compile the kernel
@@ -25,24 +21,26 @@
2521
#define __SANITIZE_ADDRESS__
2622
#endif
2723

28-
#undef __no_sanitize_address
2924
#define __no_sanitize_address __attribute__((no_sanitize("address")))
3025

31-
/* Clang doesn't have a way to turn it off per-function, yet. */
32-
#ifdef __noretpoline
33-
#undef __noretpoline
34-
#endif
35-
3626
/*
3727
* Not all versions of clang implement the the type-generic versions
3828
* of the builtin overflow checkers. Fortunately, clang implements
3929
* __has_builtin allowing us to avoid awkward version
4030
* checks. Unfortunately, we don't know which version of gcc clang
4131
* pretends to be, so the macro may or may not be defined.
4232
*/
43-
#undef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
4433
#if __has_builtin(__builtin_mul_overflow) && \
4534
__has_builtin(__builtin_add_overflow) && \
4635
__has_builtin(__builtin_sub_overflow)
4736
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
4837
#endif
38+
39+
/* The following are for compatibility with GCC, from compiler-gcc.h,
40+
* and may be redefined here because they should not be shared with other
41+
* compilers, like ICC.
42+
*/
43+
#define barrier() __asm__ __volatile__("" : : : "memory")
44+
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
45+
#define __assume_aligned(a, ...) \
46+
__attribute__((__assume_aligned__(a, ## __VA_ARGS__)))

include/linux/compiler-gcc.h

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -75,48 +75,6 @@
7575
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
7676
#endif
7777

78-
/*
79-
* Feature detection for gnu_inline (gnu89 extern inline semantics). Either
80-
* __GNUC_STDC_INLINE__ is defined (not using gnu89 extern inline semantics,
81-
* and we opt in to the gnu89 semantics), or __GNUC_STDC_INLINE__ is not
82-
* defined so the gnu89 semantics are the default.
83-
*/
84-
#ifdef __GNUC_STDC_INLINE__
85-
# define __gnu_inline __attribute__((gnu_inline))
86-
#else
87-
# define __gnu_inline
88-
#endif
89-
90-
/*
91-
* Force always-inline if the user requests it so via the .config,
92-
* or if gcc is too old.
93-
* GCC does not warn about unused static inline functions for
94-
* -Wunused-function. This turns out to avoid the need for complex #ifdef
95-
* directives. Suppress the warning in clang as well by using "unused"
96-
* function attribute, which is redundant but not harmful for gcc.
97-
* Prefer gnu_inline, so that extern inline functions do not emit an
98-
* externally visible function. This makes extern inline behave as per gnu89
99-
* semantics rather than c99. This prevents multiple symbol definition errors
100-
* of extern inline functions at link time.
101-
* A lot of inline functions can cause havoc with function tracing.
102-
*/
103-
#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
104-
!defined(CONFIG_OPTIMIZE_INLINING)
105-
#define inline \
106-
inline __attribute__((always_inline, unused)) notrace __gnu_inline
107-
#else
108-
#define inline inline __attribute__((unused)) notrace __gnu_inline
109-
#endif
110-
111-
#define __inline__ inline
112-
#define __inline inline
113-
#define __always_inline inline __attribute__((always_inline))
114-
#define noinline __attribute__((noinline))
115-
116-
#define __packed __attribute__((packed))
117-
#define __weak __attribute__((weak))
118-
#define __alias(symbol) __attribute__((alias(#symbol)))
119-
12078
#ifdef RETPOLINE
12179
#define __noretpoline __attribute__((indirect_branch("keep")))
12280
#endif
@@ -135,55 +93,9 @@
13593
*/
13694
#define __naked __attribute__((naked)) noinline __noclone notrace
13795

138-
#define __noreturn __attribute__((noreturn))
139-
140-
/*
141-
* From the GCC manual:
142-
*
143-
* Many functions have no effects except the return value and their
144-
* return value depends only on the parameters and/or global
145-
* variables. Such a function can be subject to common subexpression
146-
* elimination and loop optimization just as an arithmetic operator
147-
* would be.
148-
* [...]
149-
*/
150-
#define __pure __attribute__((pure))
151-
#define __aligned(x) __attribute__((aligned(x)))
152-
#define __aligned_largest __attribute__((aligned))
153-
#define __printf(a, b) __attribute__((format(printf, a, b)))
154-
#define __scanf(a, b) __attribute__((format(scanf, a, b)))
155-
#define __attribute_const__ __attribute__((__const__))
156-
#define __maybe_unused __attribute__((unused))
157-
#define __always_unused __attribute__((unused))
158-
#define __mode(x) __attribute__((mode(x)))
159-
160-
#define __must_check __attribute__((warn_unused_result))
161-
#define __malloc __attribute__((__malloc__))
162-
163-
#define __used __attribute__((__used__))
164-
#define __compiler_offsetof(a, b) \
165-
__builtin_offsetof(a, b)
166-
167-
/* Mark functions as cold. gcc will assume any path leading to a call
168-
* to them will be unlikely. This means a lot of manual unlikely()s
169-
* are unnecessary now for any paths leading to the usual suspects
170-
* like BUG(), printk(), panic() etc. [but let's keep them for now for
171-
* older compilers]
172-
*
173-
* Early snapshots of gcc 4.3 don't support this and we can't detect this
174-
* in the preprocessor, but we can live with this because they're unreleased.
175-
* Maketime probing would be overkill here.
176-
*
177-
* gcc also has a __attribute__((__hot__)) to move hot functions into
178-
* a special section, but I don't see any sense in this right now in
179-
* the kernel context
180-
*/
181-
#define __cold __attribute__((__cold__))
182-
18396
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
18497

18598
#define __optimize(level) __attribute__((__optimize__(level)))
186-
#define __nostackprotector __optimize("no-stack-protector")
18799

188100
#define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
189101

include/linux/compiler-intel.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
/* Intel ECC compiler doesn't support gcc specific asm stmts.
1515
* It uses intrinsics to do the equivalent things.
1616
*/
17-
#undef barrier
18-
#undef barrier_data
19-
#undef RELOC_HIDE
20-
#undef OPTIMIZER_HIDE_VAR
2117

2218
#define barrier() __memory_barrier()
2319
#define barrier_data(ptr) barrier()
@@ -38,13 +34,12 @@
3834

3935
#endif
4036

41-
#ifndef __HAVE_BUILTIN_BSWAP16__
4237
/* icc has this, but it's called _bswap16 */
4338
#define __HAVE_BUILTIN_BSWAP16__
4439
#define __builtin_bswap16 _bswap16
45-
#endif
4640

47-
/*
48-
* icc defines __GNUC__, but does not implement the builtin overflow checkers.
41+
/* The following are for compatibility with GCC, from compiler-gcc.h,
42+
* and may be redefined here because they should not be shared with other
43+
* compilers, like clang.
4944
*/
50-
#undef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
45+
#define __visible __attribute__((externally_visible))

0 commit comments

Comments
 (0)