Zephyr Project API  3.3.0
A Scalable Open Source RTOS
gcc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014,2017 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
9
17#define TOOLCHAIN_GCC_VERSION \
18 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
19
20/* GCC supports #pragma diagnostics since 4.6.0 */
21#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (TOOLCHAIN_GCC_VERSION >= 40600)
22#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
23#endif
24
25#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (TOOLCHAIN_GCC_VERSION >= 40900)
26#define TOOLCHAIN_HAS_C_GENERIC 1
27#endif
28
29#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (TOOLCHAIN_GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
31#endif
32
33/*
34 * Older versions of GCC do not define __BYTE_ORDER__, so it must be manually
35 * detected and defined using arch-specific definitions.
36 */
37
38#ifndef _LINKER
39
40#ifndef __ORDER_BIG_ENDIAN__
41#define __ORDER_BIG_ENDIAN__ (1)
42#endif
43
44#ifndef __ORDER_LITTLE_ENDIAN__
45#define __ORDER_LITTLE_ENDIAN__ (2)
46#endif
47
48#ifndef __BYTE_ORDER__
49#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
50 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
51 defined(__MIPSEB__) || defined(__TC32EB__)
52
53#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
54
55#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
56 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
57 defined(__MIPSEL__) || defined(__TC32EL__)
58
59#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
60
61#else
62#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
63#endif
64#endif
65
66
67#undef BUILD_ASSERT /* clear out common version */
68/* C++11 has static_assert built in */
69#if defined(__cplusplus) && (__cplusplus >= 201103L)
70#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
71
72/*
73 * GCC 4.6 and higher have the C11 _Static_assert built in, and its
74 * output is easier to understand than the common BUILD_ASSERT macros.
75 */
76#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
77 (__STDC_VERSION__) >= 201100
78#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
79#else
80#define BUILD_ASSERT(EXPR, MSG...)
81#endif
82
83#ifdef __cplusplus
84#define ZRESTRICT __restrict
85#else
86#define ZRESTRICT restrict
87#endif
88
90#include <stdbool.h>
91
92#define ALIAS_OF(of) __attribute__((alias(#of)))
93
94#define FUNC_ALIAS(real_func, new_alias, return_type) \
95 return_type new_alias() ALIAS_OF(real_func)
96
97#if defined(CONFIG_ARCH_POSIX)
99
100/*let's not segfault if this were to happen for some reason*/
101#define CODE_UNREACHABLE \
102{\
103 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
104 __FILE__, __LINE__);\
105 __builtin_unreachable(); \
106}
107#else
108#define CODE_UNREACHABLE __builtin_unreachable()
109#endif
110#define FUNC_NORETURN __attribute__((__noreturn__))
111
112/* The GNU assembler for Cortex-M3 uses # for immediate values, not
113 * comments, so the @nobits# trick does not work.
114 */
115#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
116#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
117#else
118#define _NODATA_SECTION(segment) \
119 __attribute__((section(#segment ",\"wa\",@nobits#")))
120#endif
121
122/* Unaligned access */
123#define UNALIGNED_GET(p) \
124__extension__ ({ \
125 struct __attribute__((__packed__)) { \
126 __typeof__(*(p)) __v; \
127 } *__p = (__typeof__(__p)) (p); \
128 __p->__v; \
129})
130
131
132#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
133
134/* Version of UNALIGNED_PUT() which issues a compiler_barrier() after
135 * the store. It is required to workaround an apparent optimization
136 * bug in GCC for ARM Cortex-M3 and higher targets, when multiple
137 * byte, half-word and word stores (strb, strh, str instructions),
138 * which support unaligned access, can be coalesced into store double
139 * (strd) instruction, which doesn't support unaligned access (the
140 * compilers in question do this optimization ignoring __packed__
141 * attribute).
142 */
143#define UNALIGNED_PUT(v, p) \
144do { \
145 struct __attribute__((__packed__)) { \
146 __typeof__(*p) __v; \
147 } *__p = (__typeof__(__p)) (p); \
148 __p->__v = (v); \
149 compiler_barrier(); \
150} while (false)
151
152#else
153
154#define UNALIGNED_PUT(v, p) \
155do { \
156 struct __attribute__((__packed__)) { \
157 __typeof__(*p) __v; \
158 } *__p = (__typeof__(__p)) (p); \
159 __p->__v = (v); \
160} while (false)
161
162#endif
163
164/* Double indirection to ensure section names are expanded before
165 * stringification
166 */
167#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
168#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
169
170#define __GENERIC_DOT_SECTION(segment) \
171 __attribute__((section("." STRINGIFY(segment))))
172#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
173
174#define ___in_section(a, b, c) \
175 __attribute__((section("." Z_STRINGIFY(a) \
176 "." Z_STRINGIFY(b) \
177 "." Z_STRINGIFY(c))))
178#define __in_section(a, b, c) ___in_section(a, b, c)
179
180#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
181
182#define __in_section_unique_named(seg, name) \
183 ___in_section(seg, __FILE__, name)
184
185/* When using XIP, using '__ramfunc' places a function into RAM instead
186 * of FLASH. Make sure '__ramfunc' is defined only when
187 * CONFIG_ARCH_HAS_RAMFUNC_SUPPORT is defined, so that the compiler can
188 * report an error if '__ramfunc' is used but the architecture does not
189 * support it.
190 */
191#if !defined(CONFIG_XIP)
192#define __ramfunc
193#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
194#define __ramfunc __attribute__((noinline)) \
195 __attribute__((long_call, section(".ramfunc")))
196#endif /* !CONFIG_XIP */
197
198#ifndef __fallthrough
199#if __GNUC__ >= 7
200#define __fallthrough __attribute__((fallthrough))
201#else
202#define __fallthrough
203#endif /* __GNUC__ >= 7 */
204#endif
205
206#ifndef __packed
207#define __packed __attribute__((__packed__))
208#endif
209
210#ifndef __aligned
211#define __aligned(x) __attribute__((__aligned__(x)))
212#endif
213
214#define __may_alias __attribute__((__may_alias__))
215
216#ifndef __printf_like
217#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
218#define __printf_like(f, a) __attribute__((format (printf, f, a)))
219#else
220/*
221 * The Zephyr stdint convention enforces int32_t = int, int64_t = long long,
222 * and intptr_t = long so that short string format length modifiers can be
223 * used universally across ILP32 and LP64 architectures. Without that it
224 * is possible for ILP32 toolchains to have int32_t = long and intptr_t = int
225 * clashing with the Zephyr convention and generating pointless warnings
226 * as they're still the same size. Inhibit the format argument type
227 * validation in that case and let the other configs do it.
228 */
229#define __printf_like(f, a)
230#endif
231#endif
232
233#define __used __attribute__((__used__))
234#define __unused __attribute__((__unused__))
235#define __maybe_unused __attribute__((__unused__))
236
237#ifndef __deprecated
238#define __deprecated __attribute__((deprecated))
239#endif
240
241#ifndef __attribute_const__
242#define __attribute_const__ __attribute__((__const__))
243#endif
244
245#ifndef __must_check
246#define __must_check __attribute__((warn_unused_result))
247#endif
248
249#define ARG_UNUSED(x) (void)(x)
250
251#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
252#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
253#define POPCOUNT(x) __builtin_popcount(x)
254
255#ifndef __no_optimization
256#define __no_optimization __attribute__((optimize("-O0")))
257#endif
258
259#ifndef __weak
260#define __weak __attribute__((__weak__))
261#endif
262
263/* Builtins with availability that depend on the compiler version. */
264#if __GNUC__ >= 5
265#define HAS_BUILTIN___builtin_add_overflow 1
266#define HAS_BUILTIN___builtin_sub_overflow 1
267#define HAS_BUILTIN___builtin_mul_overflow 1
268#define HAS_BUILTIN___builtin_div_overflow 1
269#endif
270#if __GNUC__ >= 4
271#define HAS_BUILTIN___builtin_clz 1
272#define HAS_BUILTIN___builtin_clzl 1
273#define HAS_BUILTIN___builtin_clzll 1
274#define HAS_BUILTIN___builtin_ctz 1
275#define HAS_BUILTIN___builtin_ctzl 1
276#define HAS_BUILTIN___builtin_ctzll 1
277#endif
278
279/*
280 * Be *very* careful with these. You cannot filter out __DEPRECATED_MACRO with
281 * -wno-deprecated, which has implications for -Werror.
282 */
283
284/*
285 * Expands to nothing and generates a warning. Used like
286 *
287 * #define FOO __WARN("Please use BAR instead") ...
288 *
289 * The warning points to the location where the macro is expanded.
290 */
291#define __WARN(msg) __WARN1(GCC warning msg)
292#define __WARN1(s) _Pragma(#s)
293
294/* Generic message */
295#ifndef __DEPRECATED_MACRO
296#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
297#endif
298
299/* These macros allow having ARM asm functions callable from thumb */
300
301#if defined(_ASMLANGUAGE)
302
303#if defined(CONFIG_ARM)
304
305#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
306
307#define FUNC_CODE() .thumb;
308#define FUNC_INSTR(a)
309
310#else
311
312#define FUNC_CODE() .code 32
313#define FUNC_INSTR(a)
314
315#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
316
317#else
318
319#define FUNC_CODE()
320#define FUNC_INSTR(a)
321
322#endif /* CONFIG_ARM */
323
324#endif /* _ASMLANGUAGE */
325
326/*
327 * These macros are used to declare assembly language symbols that need
328 * to be typed properly(func or data) to be visible to the OMF tool.
329 * So that the build tool could mark them as an entry point to be linked
330 * correctly. This is an elfism. Use #if 0 for a.out.
331 */
332
333#if defined(_ASMLANGUAGE)
334
335#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
336 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
337 || defined(CONFIG_MIPS)
338#define GTEXT(sym) .global sym; .type sym, %function
339#define GDATA(sym) .global sym; .type sym, %object
340#define WTEXT(sym) .weak sym; .type sym, %function
341#define WDATA(sym) .weak sym; .type sym, %object
342#elif defined(CONFIG_ARC)
343/*
344 * Need to use assembly macros because ';' is interpreted as the start of
345 * a single line comment in the ARC assembler.
346 */
347
348.macro glbl_text symbol
349 .globl \symbol
350 .type \symbol, %function
351.endm
352
353.macro glbl_data symbol
354 .globl \symbol
355 .type \symbol, %object
356.endm
357
358.macro weak_data symbol
359 .weak \symbol
360 .type \symbol, %object
361.endm
362
363#define GTEXT(sym) glbl_text sym
364#define GDATA(sym) glbl_data sym
365#define WDATA(sym) weak_data sym
366
367#else /* !CONFIG_ARM && !CONFIG_ARC */
368#define GTEXT(sym) .globl sym; .type sym, @function
369#define GDATA(sym) .globl sym; .type sym, @object
370#endif
371
372/*
373 * These macros specify the section in which a given function or variable
374 * resides.
375 *
376 * - SECTION_FUNC allows only one function to reside in a sub-section
377 * - SECTION_SUBSEC_FUNC allows multiple functions to reside in a sub-section
378 * This ensures that garbage collection only discards the section
379 * if all functions in the sub-section are not referenced.
380 */
381
382#if defined(CONFIG_ARC)
383/*
384 * Need to use assembly macros because ';' is interpreted as the start of
385 * a single line comment in the ARC assembler.
386 *
387 * Also, '\‍()' is needed in the .section directive of these macros for
388 * correct substitution of the 'section' variable.
389 */
390
391.macro section_var section, symbol
392 .section .\section\‍().\symbol
393 \symbol :
394.endm
395
396.macro section_func section, symbol
397 .section .\section\().\symbol, "ax"
398 FUNC_CODE()
399 PERFOPT_ALIGN
400 \symbol :
401 FUNC_INSTR(\symbol)
402.endm
403
404.macro section_subsec_func section, subsection, symbol
405 .section .\section\().\subsection, "ax"
406 PERFOPT_ALIGN
407 \symbol :
408.endm
409
410#define SECTION_VAR(sect, sym) section_var sect, sym
411#define SECTION_FUNC(sect, sym) section_func sect, sym
412#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
413 section_subsec_func sect, subsec, sym
414#else /* !CONFIG_ARC */
415
416#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
417#define SECTION_FUNC(sect, sym) \
418 .section .sect.sym, "ax"; \
419 FUNC_CODE() \
420 PERFOPT_ALIGN; sym : \
421 FUNC_INSTR(sym)
422#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
423 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
424
425#endif /* CONFIG_ARC */
426
427#endif /* _ASMLANGUAGE */
428
429#if defined(_ASMLANGUAGE)
430#if defined(CONFIG_ARM)
431#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
432/* '.syntax unified' is a gcc-ism used in thumb-2 asm files */
433#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
434#else
435#define _ASM_FILE_PROLOGUE .text; .code 32
436#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
437#elif defined(CONFIG_ARM64)
438#define _ASM_FILE_PROLOGUE .text
439#endif /* CONFIG_ARM64 || CONFIG_ARM */
440#endif /* _ASMLANGUAGE */
441
442/*
443 * These macros generate absolute symbols for GCC
444 */
445
446/* create an extern reference to the absolute symbol */
447
448#define GEN_OFFSET_EXTERN(name) extern const char name[]
449
450#define GEN_ABS_SYM_BEGIN(name) \
451 EXTERN_C void name(void); \
452 void name(void) \
453 {
454
455#define GEN_ABS_SYM_END }
456
457/*
458 * Note that GEN_ABSOLUTE_SYM(), depending on the architecture
459 * and toolchain, may restrict the range of values permitted
460 * for assignment to the named symbol.
461 *
462 * For example, on x86, "value" is interpreted as signed
463 * 32-bit integer. Passing in an unsigned 32-bit integer
464 * with MSB set would result in a negative integer.
465 * Moreover, GCC would error out if an integer larger
466 * than 2^32-1 is passed as "value".
467 */
468
469/*
470 * GEN_ABSOLUTE_SYM_KCONFIG() is outputted by the build system
471 * to generate named symbol/value pairs for kconfigs.
472 */
473
474#if defined(CONFIG_ARM)
475
476/*
477 * GNU/ARM backend does not have a proper operand modifier which does not
478 * produces prefix # followed by value, such as %0 for PowerPC, Intel, and
479 * MIPS. The workaround performed here is using %B0 which converts
480 * the value to ~(value). Thus "n"(~(value)) is set in operand constraint
481 * to output (value) in the ARM specific GEN_OFFSET macro.
482 */
483
484#define GEN_ABSOLUTE_SYM(name, value) \
485 __asm__(".globl\t" #name "\n\t.equ\t" #name \
486 ",%B0" \
487 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
488
489#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
490 __asm__(".globl\t" #name \
491 "\n\t.equ\t" #name "," #value \
492 "\n\t.type\t" #name ",%object")
493
494#elif defined(CONFIG_X86)
495
496#define GEN_ABSOLUTE_SYM(name, value) \
497 __asm__(".globl\t" #name "\n\t.equ\t" #name \
498 ",%c0" \
499 "\n\t.type\t" #name ",@object" : : "n"(value))
500
501#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
502 __asm__(".globl\t" #name \
503 "\n\t.equ\t" #name "," #value \
504 "\n\t.type\t" #name ",@object")
505
506#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
507
508#define GEN_ABSOLUTE_SYM(name, value) \
509 __asm__(".globl\t" #name "\n\t.equ\t" #name \
510 ",%c0" \
511 "\n\t.type\t" #name ",@object" : : "n"(value))
512
513#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
514 __asm__(".globl\t" #name \
515 "\n\t.equ\t" #name "," #value \
516 "\n\t.type\t" #name ",@object")
517
518#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
519 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
520
521/* No special prefixes necessary for constants in this arch AFAICT */
522#define GEN_ABSOLUTE_SYM(name, value) \
523 __asm__(".globl\t" #name "\n\t.equ\t" #name \
524 ",%0" \
525 "\n\t.type\t" #name ",%%object" : : "n"(value))
526
527#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
528 __asm__(".globl\t" #name \
529 "\n\t.equ\t" #name "," #value \
530 "\n\t.type\t" #name ",%object")
531
532#elif defined(CONFIG_ARCH_POSIX)
533#define GEN_ABSOLUTE_SYM(name, value) \
534 __asm__(".globl\t" #name "\n\t.equ\t" #name \
535 ",%c0" \
536 "\n\t.type\t" #name ",@object" : : "n"(value))
537
538#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
539 __asm__(".globl\t" #name \
540 "\n\t.equ\t" #name "," #value \
541 "\n\t.type\t" #name ",@object")
542
543#elif defined(CONFIG_SPARC)
544#define GEN_ABSOLUTE_SYM(name, value) \
545 __asm__(".global\t" #name "\n\t.equ\t" #name \
546 ",%0" \
547 "\n\t.type\t" #name ",#object" : : "n"(value))
548
549#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
550 __asm__(".globl\t" #name \
551 "\n\t.equ\t" #name "," #value \
552 "\n\t.type\t" #name ",#object")
553
554#else
555#error processor architecture not supported
556#endif
557
558#define compiler_barrier() do { \
559 __asm__ __volatile__ ("" ::: "memory"); \
560} while (false)
561
571#define Z_MAX(a, b) ({ \
572 /* random suffix to avoid naming conflict */ \
573 __typeof__(a) _value_a_ = (a); \
574 __typeof__(b) _value_b_ = (b); \
575 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
576 })
577
583#define Z_MIN(a, b) ({ \
584 /* random suffix to avoid naming conflict */ \
585 __typeof__(a) _value_a_ = (a); \
586 __typeof__(b) _value_b_ = (b); \
587 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
588 })
589
595#define Z_CLAMP(val, low, high) ({ \
596 /* random suffix to avoid naming conflict */ \
597 __typeof__(val) _value_val_ = (val); \
598 __typeof__(low) _value_low_ = (low); \
599 __typeof__(high) _value_high_ = (high); \
600 (_value_val_ < _value_low_) ? _value_low_ : \
601 (_value_val_ > _value_high_) ? _value_high_ : \
602 _value_val_; \
603 })
604
611#define Z_POW2_CEIL(x) \
612 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
613
620#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
621
622#if defined(CONFIG_ASAN) && defined(__clang__)
623#define __noasan __attribute__((no_sanitize("address")))
624#else
625#define __noasan
626#endif
627
633#if (TOOLCHAIN_GCC_VERSION >= 110000) || (TOOLCHAIN_CLANG_VERSION >= 70000)
634#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
635#else
636#define FUNC_NO_STACK_PROTECTOR
637#endif
638
639#endif /* !_LINKER */
640#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
Common toolchain abstraction.