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