7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
10#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_
11#error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead
21#define TOOLCHAIN_GCC_VERSION \
22 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
25#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (TOOLCHAIN_GCC_VERSION >= 40600)
26#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
29#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (TOOLCHAIN_GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_GENERIC 1
33#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (TOOLCHAIN_GCC_VERSION >= 40900)
34#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
37#define TOOLCHAIN_HAS_ZLA 1
46#ifndef __ORDER_BIG_ENDIAN__
47#define __ORDER_BIG_ENDIAN__ (1)
50#ifndef __ORDER_LITTLE_ENDIAN__
51#define __ORDER_LITTLE_ENDIAN__ (2)
55#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
56 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
57 defined(__MIPSEB__) || defined(__TC32EB__)
59#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
61#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
62 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
63 defined(__MIPSEL__) || defined(__TC32EL__)
65#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
68#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
75#if defined(__cplusplus) && (__cplusplus >= 201103L)
76#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
84#elif !defined(__cplusplus) && \
85 (((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))) || \
86 (__STDC_VERSION__) >= 201100)
87#define BUILD_ASSERT(EXPR, MSG...) _Static_assert((EXPR), "" MSG)
89#define BUILD_ASSERT(EXPR, MSG...)
93#define ZRESTRICT __restrict
95#define ZRESTRICT restrict
101#define ALIAS_OF(of) __attribute__((alias(#of)))
103#define FUNC_ALIAS(real_func, new_alias, return_type) \
104 return_type new_alias() ALIAS_OF(real_func)
106#if TOOLCHAIN_GCC_VERSION < 40500
107#define __builtin_unreachable() __builtin_trap()
110#if defined(CONFIG_ARCH_POSIX) && !defined(_ASMLANGUAGE)
114#define CODE_UNREACHABLE \
116 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
117 __FILE__, __LINE__);\
118 __builtin_unreachable(); \
121#define CODE_UNREACHABLE __builtin_unreachable()
123#define FUNC_NORETURN __attribute__((__noreturn__))
128#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
129#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
131#define _NODATA_SECTION(segment) \
132 __attribute__((section(#segment ",\"wa\",@nobits#")))
136#define UNALIGNED_GET(g) \
138 struct __attribute__((__packed__)) { \
139 __typeof__(*(g)) __v; \
140 } *__g = (__typeof__(__g)) (g); \
145#if (__GNUC__ >= 7) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
156#define UNALIGNED_PUT(v, p) \
158 struct __attribute__((__packed__)) { \
159 __typeof__(*p) __v; \
160 } *__p = (__typeof__(__p)) (p); \
162 compiler_barrier(); \
167#define UNALIGNED_PUT(v, p) \
169 struct __attribute__((__packed__)) { \
170 __typeof__(*p) __v; \
171 } *__p = (__typeof__(__p)) (p); \
184#define UNALIGNED_MEMBER_ADDR(_p, _member) ((__typeof__(_p->_member) *) \
185 (((intptr_t)(_p)) + offsetof(__typeof__(*_p), _member)))
190#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
191#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
193#define __GENERIC_DOT_SECTION(segment) \
194 __attribute__((section("." STRINGIFY(segment))))
195#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
197#define ___in_section(a, b, c) \
198 __attribute__((section("." Z_STRINGIFY(a) \
200 "." Z_STRINGIFY(c))))
201#define __in_section(a, b, c) ___in_section(a, b, c)
203#define ___in_section_unique(a, b) \
204 __attribute__((section("." Z_STRINGIFY(a) \
206 "." Z_STRINGIFY(b))))
208#ifndef __in_section_unique
209#define __in_section_unique(seg) ___in_section_unique(seg, __COUNTER__)
212#ifndef __in_section_unique_named
213#define __in_section_unique_named(seg, name) ___in_section_unique(seg, name)
222#if !defined(CONFIG_XIP)
224#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
225#if defined(CONFIG_ARM)
226#if defined(__clang__)
231#define __ramfunc __attribute__((noinline)) __attribute__((section(".ramfunc")))
234#define __ramfunc __attribute__((noinline)) \
235 __attribute__((long_call, section(".ramfunc")))
238#define __ramfunc __attribute__((noinline)) \
239 __attribute__((section(".ramfunc")))
245#define __fallthrough __attribute__((fallthrough))
252#define __packed __attribute__((__packed__))
256#define __aligned(x) __attribute__((__aligned__(x)))
260#define __noinline __attribute__((noinline))
263#define __may_alias __attribute__((__may_alias__))
266#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
267#define __printf_like(f, a) __attribute__((format (printf, f, a)))
278#define __printf_like(f, a)
282#define __used __attribute__((__used__))
283#define __unused __attribute__((__unused__))
284#define __maybe_unused __attribute__((__unused__))
287#define __deprecated __attribute__((deprecated))
293#ifndef __deprecated_version
294#define __deprecated_version(version) \
295 __attribute__((deprecated("planned removal in v" #version)))
298#ifndef __attribute_const__
299#define __attribute_const__ __attribute__((__const__))
303#define __must_check __attribute__((warn_unused_result))
306#define ARG_UNUSED(x) (void)(x)
308#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
309#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
310#define POPCOUNT(x) __builtin_popcount(x)
312#ifndef __no_optimization
313#define __no_optimization __attribute__((optimize("-O0")))
317#define __weak __attribute__((__weak__))
320#ifndef __attribute_nonnull
321#define __attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
325#define __cleanup(x) __attribute__((cleanup(x)))
330#define HAS_BUILTIN___builtin_add_overflow 1
331#define HAS_BUILTIN___builtin_sub_overflow 1
332#define HAS_BUILTIN___builtin_mul_overflow 1
333#define HAS_BUILTIN___builtin_div_overflow 1
335#if TOOLCHAIN_GCC_VERSION >= 40800
336#define HAS_BUILTIN___builtin_bswap16 1
338#if TOOLCHAIN_GCC_VERSION >= 40300
339#define HAS_BUILTIN___builtin_bswap32 1
340#define HAS_BUILTIN___builtin_bswap64 1
343#define HAS_BUILTIN___builtin_clz 1
344#define HAS_BUILTIN___builtin_clzl 1
345#define HAS_BUILTIN___builtin_clzll 1
346#define HAS_BUILTIN___builtin_ctz 1
347#define HAS_BUILTIN___builtin_ctzl 1
348#define HAS_BUILTIN___builtin_ctzll 1
363#define __WARN(msg) __WARN1(GCC warning msg)
364#define __WARN1(s) _Pragma(#s)
367#if !(defined(CONFIG_DEPRECATION_TEST) || !defined(CONFIG_WARN_DEPRECATED))
368#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
373#define __DEPRECATED_MACRO
378#if defined(_ASMLANGUAGE)
380#if defined(CONFIG_ARM)
382#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
384#define FUNC_CODE() .thumb;
389#define FUNC_CODE() .code 32;
410#if defined(_ASMLANGUAGE)
412#if defined(CONFIG_ARM) || defined(CONFIG_RISCV) \
413 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
414 || defined(CONFIG_MIPS) || defined(CONFIG_RX) \
415 || defined(CONFIG_OPENRISC)
416#define GTEXT(sym) .global sym; .type sym, %function
417#define GDATA(sym) .global sym; .type sym, %object
418#define WTEXT(sym) .weak sym; .type sym, %function
419#define WDATA(sym) .weak sym; .type sym, %object
420#elif defined(CONFIG_ARC)
426.macro glbl_text symbol
428 .type \symbol, %function
431.macro glbl_data symbol
433 .type \symbol, %
object
436.macro weak_data symbol
438 .type \symbol, %
object
441#define GTEXT(sym) glbl_text sym
442#define GDATA(sym) glbl_data sym
443#define WDATA(sym) weak_data sym
446#define GTEXT(sym) .globl sym; .type sym, @function
447#define GDATA(sym) .globl sym; .type sym, @object
460#if defined(CONFIG_ARC)
469.macro section_var section, symbol
470 .section .\section\().\symbol
474.macro section_func section, symbol
475 .section .\section\().\symbol,
"ax"
482.macro section_subsec_func section, subsection, symbol
483 .section .\section\().\subsection,
"ax"
488#define SECTION_VAR(sect, sym) section_var sect, sym
489#define SECTION_FUNC(sect, sym) section_func sect, sym
490#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
491 section_subsec_func sect, subsec, sym
494#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
495#define SECTION_FUNC(sect, sym) \
496 .section .sect.sym, "ax"; \
498 PERFOPT_ALIGN; sym : \
500#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
501 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
507#if defined(_ASMLANGUAGE)
508#if defined(CONFIG_ARM)
509#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
511#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
513#define _ASM_FILE_PROLOGUE .text; .code 32
515#elif defined(CONFIG_ARM64)
516#define _ASM_FILE_PROLOGUE .text
526#define GEN_OFFSET_EXTERN(name) extern const char name[]
528#define GEN_ABS_SYM_BEGIN(name) \
529 EXTERN_C void name(void); \
533#define GEN_ABS_SYM_END }
552#if defined(CONFIG_ARM) || (defined(CONFIG_ARCH_POSIX) && defined(__arm__))
562#define GEN_ABSOLUTE_SYM(name, value) \
563 __asm__ __volatile__(".globl\t" #name "\n\t.equ\t" #name \
565 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
567#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
568 __asm__ __volatile__(".globl\t" #name \
569 "\n\t.equ\t" #name "," #value \
570 "\n\t.type\t" #name ",%object")
572#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64) \
573 || defined(CONFIG_ARCH_POSIX) \
574 || defined(CONFIG_X86)
576#define GEN_ABSOLUTE_SYM(name, value) \
577 __asm__ __volatile__(".globl\t" #name "\n\t.equ\t" #name \
579 "\n\t.type\t" #name ",@object" : : "n"(value))
581#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
582 __asm__ __volatile__(".globl\t" #name \
583 "\n\t.equ\t" #name "," #value \
584 "\n\t.type\t" #name ",@object")
586#elif defined(CONFIG_RISCV) || defined(CONFIG_XTENSA) || \
587 defined(CONFIG_MIPS) || defined(CONFIG_OPENRISC)
590#define GEN_ABSOLUTE_SYM(name, value) \
591 __asm__ __volatile__(".globl\t" #name "\n\t.equ\t" #name \
593 "\n\t.type\t" #name ",%%object" : : "n"(value))
595#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
596 __asm__ __volatile__(".globl\t" #name \
597 "\n\t.equ\t" #name "," #value \
598 "\n\t.type\t" #name ",%object")
600#elif defined(CONFIG_SPARC)
601#define GEN_ABSOLUTE_SYM(name, value) \
602 __asm__ __volatile__(".global\t" #name "\n\t.equ\t" #name \
604 "\n\t.type\t" #name ",#object" : : "n"(value))
606#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
607 __asm__ __volatile__(".globl\t" #name \
608 "\n\t.equ\t" #name "," #value \
609 "\n\t.type\t" #name ",#object")
611#elif defined(CONFIG_RX)
612#define GEN_ABSOLUTE_SYM(name, value) \
613 __asm__ __volatile__(".global\t" #name "\n\t.equ\t" #name \
615 "\n\t.type\t" #name ",%%object" : : "n"(value))
617#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
618 __asm__ __volatile__(".global\t" #name \
619 "\n\t.equ\t" #name "," #value \
620 "\n\t.type\t" #name ",#object")
623#error processor architecture not supported
626#define compiler_barrier() do { \
627 __asm__ __volatile__ ("" ::: "memory"); \
636#define Z_POW2_CEIL(x) \
637 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
645#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
647#if defined(CONFIG_ASAN) && defined(__clang__)
648#define __noasan __attribute__((no_sanitize("address")))
653#if defined(CONFIG_UBSAN)
654#define __noubsan __attribute__((no_sanitize("undefined")))
664#if (TOOLCHAIN_GCC_VERSION >= 110000) || \
665 (defined(TOOLCHAIN_CLANG_VERSION) && (TOOLCHAIN_CLANG_VERSION >= 70000))
666#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
668#define FUNC_NO_STACK_PROTECTOR
671#if defined(CONFIG_INSTRUMENTATION)
672#define __no_instrumentation__ __attribute__((__no_instrument_function__))
674#define __no_instrumentation__
679#define TOOLCHAIN_WARNING_ADDRESS_OF_PACKED_MEMBER "-Waddress-of-packed-member"
680#define TOOLCHAIN_WARNING_ARRAY_BOUNDS "-Warray-bounds"
681#define TOOLCHAIN_WARNING_ATTRIBUTES "-Wattributes"
682#define TOOLCHAIN_WARNING_DELETE_NON_VIRTUAL_DTOR "-Wdelete-non-virtual-dtor"
683#define TOOLCHAIN_WARNING_EXTRA "-Wextra"
684#define TOOLCHAIN_WARNING_NONNULL "-Wnonnull"
685#define TOOLCHAIN_WARNING_SHADOW "-Wshadow"
686#define TOOLCHAIN_WARNING_UNUSED_LABEL "-Wunused-label"
687#define TOOLCHAIN_WARNING_UNUSED_VARIABLE "-Wunused-variable"
688#define TOOLCHAIN_WARNING_CAST_QUAL "-Wcast-qual"
691#if defined(__GNUC__) && !defined(__clang__)
692#define TOOLCHAIN_WARNING_POINTER_ARITH "-Wpointer-arith"
693#define TOOLCHAIN_WARNING_STRINGOP_OVERREAD "-Wstringop-overread"
696#define _TOOLCHAIN_DISABLE_WARNING(compiler, warning) \
697 TOOLCHAIN_PRAGMA(compiler diagnostic push) \
698 TOOLCHAIN_PRAGMA(compiler diagnostic ignored warning)
700#define _TOOLCHAIN_ENABLE_WARNING(compiler, warning) TOOLCHAIN_PRAGMA(compiler diagnostic pop)
702#define TOOLCHAIN_DISABLE_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
703#define TOOLCHAIN_ENABLE_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)
705#if defined(__GNUC__) && !defined(__clang__)
706#define TOOLCHAIN_DISABLE_GCC_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
707#define TOOLCHAIN_ENABLE_GCC_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)