195ffaba3SGraeme Russ #ifndef __LINUX_COMPILER_H 295ffaba3SGraeme Russ #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." 395ffaba3SGraeme Russ #endif 495ffaba3SGraeme Russ 595ffaba3SGraeme Russ /* 695ffaba3SGraeme Russ * Common definitions for all gcc versions go here. 795ffaba3SGraeme Russ */ 8fb8ffd7cSMasahiro Yamada #define GCC_VERSION (__GNUC__ * 10000 \ 9fb8ffd7cSMasahiro Yamada + __GNUC_MINOR__ * 100 \ 10fb8ffd7cSMasahiro Yamada + __GNUC_PATCHLEVEL__) 1195ffaba3SGraeme Russ 1295ffaba3SGraeme Russ /* Optimization barrier */ 139b2c282bSTom Rini 1495ffaba3SGraeme Russ /* The "volatile" is due to gcc bugs */ 1595ffaba3SGraeme Russ #define barrier() __asm__ __volatile__("": : :"memory") 169b2c282bSTom Rini /* 179b2c282bSTom Rini * This version is i.e. to prevent dead stores elimination on @ptr 189b2c282bSTom Rini * where gcc and llvm may behave differently when otherwise using 199b2c282bSTom Rini * normal barrier(): while gcc behavior gets along with a normal 209b2c282bSTom Rini * barrier(), llvm needs an explicit input variable to be assumed 219b2c282bSTom Rini * clobbered. The issue is as follows: while the inline asm might 229b2c282bSTom Rini * access any memory it wants, the compiler could have fit all of 239b2c282bSTom Rini * @ptr into memory registers instead, and since @ptr never escaped 249b2c282bSTom Rini * from that, it proofed that the inline asm wasn't touching any of 259b2c282bSTom Rini * it. This version works well with both compilers, i.e. we're telling 269b2c282bSTom Rini * the compiler that the inline asm absolutely may see the contents 279b2c282bSTom Rini * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495 289b2c282bSTom Rini */ 299b2c282bSTom Rini #define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory") 3095ffaba3SGraeme Russ 3195ffaba3SGraeme Russ /* 3295ffaba3SGraeme Russ * This macro obfuscates arithmetic on a variable address so that gcc 3395ffaba3SGraeme Russ * shouldn't recognize the original var, and make assumptions about it. 3495ffaba3SGraeme Russ * 3595ffaba3SGraeme Russ * This is needed because the C standard makes it undefined to do 3695ffaba3SGraeme Russ * pointer arithmetic on "objects" outside their boundaries and the 3795ffaba3SGraeme Russ * gcc optimizers assume this is the case. In particular they 3895ffaba3SGraeme Russ * assume such arithmetic does not wrap. 3995ffaba3SGraeme Russ * 4095ffaba3SGraeme Russ * A miscompilation has been observed because of this on PPC. 4195ffaba3SGraeme Russ * To work around it we hide the relationship of the pointer and the object 4295ffaba3SGraeme Russ * using this macro. 4395ffaba3SGraeme Russ * 4495ffaba3SGraeme Russ * Versions of the ppc64 compiler before 4.1 had a bug where use of 4595ffaba3SGraeme Russ * RELOC_HIDE could trash r30. The bug can be worked around by changing 4695ffaba3SGraeme Russ * the inline assembly constraint from =g to =r, in this particular 4795ffaba3SGraeme Russ * case either is valid. 4895ffaba3SGraeme Russ */ 4995ffaba3SGraeme Russ #define RELOC_HIDE(ptr, off) \ 509b2c282bSTom Rini ({ \ 519b2c282bSTom Rini unsigned long __ptr; \ 5295ffaba3SGraeme Russ __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \ 539b2c282bSTom Rini (typeof(ptr)) (__ptr + (off)); \ 549b2c282bSTom Rini }) 5595ffaba3SGraeme Russ 56fb8ffd7cSMasahiro Yamada /* Make the optimizer believe the variable can be manipulated arbitrarily. */ 579b2c282bSTom Rini #define OPTIMIZER_HIDE_VAR(var) \ 589b2c282bSTom Rini __asm__ ("" : "=r" (var) : "0" (var)) 59fb8ffd7cSMasahiro Yamada 60fb8ffd7cSMasahiro Yamada #ifdef __CHECKER__ 619b2c282bSTom Rini #define __must_be_array(a) 0 62fb8ffd7cSMasahiro Yamada #else 6395ffaba3SGraeme Russ /* &a[0] degrades to a pointer: a different type from an array */ 64fb8ffd7cSMasahiro Yamada #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) 65fb8ffd7cSMasahiro Yamada #endif 6695ffaba3SGraeme Russ 6795ffaba3SGraeme Russ /* 6895ffaba3SGraeme Russ * Force always-inline if the user requests it so via the .config, 6995ffaba3SGraeme Russ * or if gcc is too old: 7095ffaba3SGraeme Russ */ 7195ffaba3SGraeme Russ #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \ 7295ffaba3SGraeme Russ !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4) 73fb8ffd7cSMasahiro Yamada #define inline inline __attribute__((always_inline)) notrace 74fb8ffd7cSMasahiro Yamada #define __inline__ __inline__ __attribute__((always_inline)) notrace 75fb8ffd7cSMasahiro Yamada #define __inline __inline __attribute__((always_inline)) notrace 76fb8ffd7cSMasahiro Yamada #else 77fb8ffd7cSMasahiro Yamada /* A lot of inline functions can cause havoc with function tracing */ 78fb8ffd7cSMasahiro Yamada #define inline inline notrace 79fb8ffd7cSMasahiro Yamada #define __inline__ __inline__ notrace 80fb8ffd7cSMasahiro Yamada #define __inline __inline notrace 8195ffaba3SGraeme Russ #endif 8295ffaba3SGraeme Russ 839b2c282bSTom Rini #define __always_inline inline __attribute__((always_inline)) 849b2c282bSTom Rini #define noinline __attribute__((noinline)) 859b2c282bSTom Rini 8695ffaba3SGraeme Russ #define __deprecated __attribute__((deprecated)) 8795ffaba3SGraeme Russ #define __packed __attribute__((packed)) 8895ffaba3SGraeme Russ #define __weak __attribute__((weak)) 899b2c282bSTom Rini #define __alias(symbol) __attribute__((alias(#symbol))) 90*cc3d6741SJoseph Chen #define __cacheline_aligned __attribute__((__aligned__(CONFIG_SYS_CACHELINE_SIZE))) 9195ffaba3SGraeme Russ 9295ffaba3SGraeme Russ /* 939b2c282bSTom Rini * it doesn't make sense on ARM (currently the only user of __naked) 949b2c282bSTom Rini * to trace naked functions because then mcount is called without 959b2c282bSTom Rini * stack and frame pointer being set up and there is no chance to 969b2c282bSTom Rini * restore the lr register to the value before mcount was called. 97fb8ffd7cSMasahiro Yamada * 989b2c282bSTom Rini * The asm() bodies of naked functions often depend on standard calling 999b2c282bSTom Rini * conventions, therefore they must be noinline and noclone. 1009b2c282bSTom Rini * 1019b2c282bSTom Rini * GCC 4.[56] currently fail to enforce this, so we must do so ourselves. 1029b2c282bSTom Rini * See GCC PR44290. 10395ffaba3SGraeme Russ */ 104fb8ffd7cSMasahiro Yamada #define __naked __attribute__((naked)) noinline __noclone notrace 10595ffaba3SGraeme Russ 10695ffaba3SGraeme Russ #define __noreturn __attribute__((noreturn)) 10795ffaba3SGraeme Russ 10895ffaba3SGraeme Russ /* 10995ffaba3SGraeme Russ * From the GCC manual: 11095ffaba3SGraeme Russ * 11195ffaba3SGraeme Russ * Many functions have no effects except the return value and their 11295ffaba3SGraeme Russ * return value depends only on the parameters and/or global 11395ffaba3SGraeme Russ * variables. Such a function can be subject to common subexpression 11495ffaba3SGraeme Russ * elimination and loop optimization just as an arithmetic operator 11595ffaba3SGraeme Russ * would be. 11695ffaba3SGraeme Russ * [...] 11795ffaba3SGraeme Russ */ 11895ffaba3SGraeme Russ #define __pure __attribute__((pure)) 11995ffaba3SGraeme Russ #define __aligned(x) __attribute__((aligned(x))) 12095ffaba3SGraeme Russ #define __printf(a, b) __attribute__((format(printf, a, b))) 121fb8ffd7cSMasahiro Yamada #define __scanf(a, b) __attribute__((format(scanf, a, b))) 12295ffaba3SGraeme Russ #define __attribute_const__ __attribute__((__const__)) 12395ffaba3SGraeme Russ #define __maybe_unused __attribute__((unused)) 12495ffaba3SGraeme Russ #define __always_unused __attribute__((unused)) 12595ffaba3SGraeme Russ 1269b2c282bSTom Rini /* gcc version specific checks */ 1279b2c282bSTom Rini 1289b2c282bSTom Rini #if GCC_VERSION < 30200 1299b2c282bSTom Rini # error Sorry, your compiler is too old - please upgrade it. 1309b2c282bSTom Rini #endif 1319b2c282bSTom Rini 1329b2c282bSTom Rini #if GCC_VERSION < 30300 1339b2c282bSTom Rini # define __used __attribute__((__unused__)) 1349b2c282bSTom Rini #else 1359b2c282bSTom Rini # define __used __attribute__((__used__)) 1369b2c282bSTom Rini #endif 1379b2c282bSTom Rini 1389b2c282bSTom Rini #ifdef CONFIG_GCOV_KERNEL 1399b2c282bSTom Rini # if GCC_VERSION < 30400 1409b2c282bSTom Rini # error "GCOV profiling support for gcc versions below 3.4 not included" 1419b2c282bSTom Rini # endif /* __GNUC_MINOR__ */ 1429b2c282bSTom Rini #endif /* CONFIG_GCOV_KERNEL */ 1439b2c282bSTom Rini 1449b2c282bSTom Rini #if GCC_VERSION >= 30400 1459b2c282bSTom Rini #define __must_check __attribute__((warn_unused_result)) 1469b2c282bSTom Rini #endif 1479b2c282bSTom Rini 1489b2c282bSTom Rini #if GCC_VERSION >= 40000 1499b2c282bSTom Rini 1509b2c282bSTom Rini /* GCC 4.1.[01] miscompiles __weak */ 1519b2c282bSTom Rini #ifdef __KERNEL__ 1529b2c282bSTom Rini # if GCC_VERSION >= 40100 && GCC_VERSION <= 40101 1539b2c282bSTom Rini # error Your version of gcc miscompiles the __weak directive 1549b2c282bSTom Rini # endif 1559b2c282bSTom Rini #endif 1569b2c282bSTom Rini 1579b2c282bSTom Rini #define __used __attribute__((__used__)) 1589b2c282bSTom Rini #define __compiler_offsetof(a, b) \ 1599b2c282bSTom Rini __builtin_offsetof(a, b) 1609b2c282bSTom Rini 1619b2c282bSTom Rini #if GCC_VERSION >= 40100 && GCC_VERSION < 40600 1629b2c282bSTom Rini # define __compiletime_object_size(obj) __builtin_object_size(obj, 0) 1639b2c282bSTom Rini #endif 1649b2c282bSTom Rini 1659b2c282bSTom Rini #if GCC_VERSION >= 40300 1669b2c282bSTom Rini /* Mark functions as cold. gcc will assume any path leading to a call 1679b2c282bSTom Rini * to them will be unlikely. This means a lot of manual unlikely()s 1689b2c282bSTom Rini * are unnecessary now for any paths leading to the usual suspects 1699b2c282bSTom Rini * like BUG(), printk(), panic() etc. [but let's keep them for now for 1709b2c282bSTom Rini * older compilers] 1719b2c282bSTom Rini * 1729b2c282bSTom Rini * Early snapshots of gcc 4.3 don't support this and we can't detect this 1739b2c282bSTom Rini * in the preprocessor, but we can live with this because they're unreleased. 1749b2c282bSTom Rini * Maketime probing would be overkill here. 1759b2c282bSTom Rini * 1769b2c282bSTom Rini * gcc also has a __attribute__((__hot__)) to move hot functions into 1779b2c282bSTom Rini * a special section, but I don't see any sense in this right now in 1789b2c282bSTom Rini * the kernel context 1799b2c282bSTom Rini */ 1809b2c282bSTom Rini #define __cold __attribute__((__cold__)) 1819b2c282bSTom Rini 1829b2c282bSTom Rini #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) 1839b2c282bSTom Rini 1849b2c282bSTom Rini #ifndef __CHECKER__ 1859b2c282bSTom Rini # define __compiletime_warning(message) __attribute__((warning(message))) 1869b2c282bSTom Rini # define __compiletime_error(message) __attribute__((error(message))) 1879b2c282bSTom Rini #endif /* __CHECKER__ */ 1889b2c282bSTom Rini #endif /* GCC_VERSION >= 40300 */ 1899b2c282bSTom Rini 1909b2c282bSTom Rini #if GCC_VERSION >= 40500 1919b2c282bSTom Rini /* 1929b2c282bSTom Rini * Mark a position in code as unreachable. This can be used to 1939b2c282bSTom Rini * suppress control flow warnings after asm blocks that transfer 1949b2c282bSTom Rini * control elsewhere. 1959b2c282bSTom Rini * 1969b2c282bSTom Rini * Early snapshots of gcc 4.5 don't support this and we can't detect 1979b2c282bSTom Rini * this in the preprocessor, but we can live with this because they're 1989b2c282bSTom Rini * unreleased. Really, we need to have autoconf for the kernel. 1999b2c282bSTom Rini */ 2009b2c282bSTom Rini #define unreachable() __builtin_unreachable() 2019b2c282bSTom Rini 2029b2c282bSTom Rini /* Mark a function definition as prohibited from being cloned. */ 2039b2c282bSTom Rini #define __noclone __attribute__((__noclone__)) 2049b2c282bSTom Rini 2059b2c282bSTom Rini #endif /* GCC_VERSION >= 40500 */ 2069b2c282bSTom Rini 2079b2c282bSTom Rini #if GCC_VERSION >= 40600 2089b2c282bSTom Rini /* 2099b2c282bSTom Rini * When used with Link Time Optimization, gcc can optimize away C functions or 2109b2c282bSTom Rini * variables which are referenced only from assembly code. __visible tells the 2119b2c282bSTom Rini * optimizer that something else uses this function or variable, thus preventing 2129b2c282bSTom Rini * this. 2139b2c282bSTom Rini */ 2149b2c282bSTom Rini #define __visible __attribute__((externally_visible)) 2159b2c282bSTom Rini #endif 2169b2c282bSTom Rini 2179b2c282bSTom Rini 2189b2c282bSTom Rini #if GCC_VERSION >= 40900 && !defined(__CHECKER__) 2199b2c282bSTom Rini /* 2209b2c282bSTom Rini * __assume_aligned(n, k): Tell the optimizer that the returned 2219b2c282bSTom Rini * pointer can be assumed to be k modulo n. The second argument is 2229b2c282bSTom Rini * optional (default 0), so we use a variadic macro to make the 2239b2c282bSTom Rini * shorthand. 2249b2c282bSTom Rini * 2259b2c282bSTom Rini * Beware: Do not apply this to functions which may return 2269b2c282bSTom Rini * ERR_PTRs. Also, it is probably unwise to apply it to functions 2279b2c282bSTom Rini * returning extra information in the low bits (but in that case the 2289b2c282bSTom Rini * compiler should see some alignment anyway, when the return value is 2299b2c282bSTom Rini * massaged by 'flags = ptr & 3; ptr &= ~3;'). 2309b2c282bSTom Rini */ 2319b2c282bSTom Rini #define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) 2329b2c282bSTom Rini #endif 2339b2c282bSTom Rini 2349b2c282bSTom Rini /* 2359b2c282bSTom Rini * GCC 'asm goto' miscompiles certain code sequences: 2369b2c282bSTom Rini * 2379b2c282bSTom Rini * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670 2389b2c282bSTom Rini * 2399b2c282bSTom Rini * Work it around via a compiler barrier quirk suggested by Jakub Jelinek. 2409b2c282bSTom Rini * 2419b2c282bSTom Rini * (asm goto is automatically volatile - the naming reflects this.) 2429b2c282bSTom Rini */ 2439b2c282bSTom Rini #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0) 2449b2c282bSTom Rini 2459b2c282bSTom Rini #ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP 2469b2c282bSTom Rini #if GCC_VERSION >= 40400 2479b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP32__ 2489b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP64__ 2499b2c282bSTom Rini #endif 2509b2c282bSTom Rini #if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600) 2519b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP16__ 2529b2c282bSTom Rini #endif 2539b2c282bSTom Rini #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */ 2549b2c282bSTom Rini 2559b2c282bSTom Rini #if GCC_VERSION >= 50000 2569b2c282bSTom Rini #define KASAN_ABI_VERSION 4 2579b2c282bSTom Rini #elif GCC_VERSION >= 40902 2589b2c282bSTom Rini #define KASAN_ABI_VERSION 3 2599b2c282bSTom Rini #endif 2609b2c282bSTom Rini 2619b2c282bSTom Rini #if GCC_VERSION >= 40902 2629b2c282bSTom Rini /* 2639b2c282bSTom Rini * Tell the compiler that address safety instrumentation (KASAN) 2649b2c282bSTom Rini * should not be applied to that function. 2659b2c282bSTom Rini * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 2669b2c282bSTom Rini */ 2679b2c282bSTom Rini #define __no_sanitize_address __attribute__((no_sanitize_address)) 2689b2c282bSTom Rini #endif 2699b2c282bSTom Rini 2709b2c282bSTom Rini #endif /* gcc version >= 40000 specific checks */ 271fb8ffd7cSMasahiro Yamada 272fb8ffd7cSMasahiro Yamada #if !defined(__noclone) 273fb8ffd7cSMasahiro Yamada #define __noclone /* not needed */ 274fb8ffd7cSMasahiro Yamada #endif 275fb8ffd7cSMasahiro Yamada 2769b2c282bSTom Rini #if !defined(__no_sanitize_address) 2779b2c282bSTom Rini #define __no_sanitize_address 2789b2c282bSTom Rini #endif 2799b2c282bSTom Rini 280fb8ffd7cSMasahiro Yamada /* 281fb8ffd7cSMasahiro Yamada * A trick to suppress uninitialized variable warning without generating any 282fb8ffd7cSMasahiro Yamada * code 283fb8ffd7cSMasahiro Yamada */ 284fb8ffd7cSMasahiro Yamada #define uninitialized_var(x) x = x 285