xref: /linux/tools/include/nolibc/compiler.h (revision 7a5f1cd22d47f8ca4b760b6334378ae42c1bd24b)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * NOLIBC compiler support header
4  * Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
5  */
6 #ifndef _NOLIBC_COMPILER_H
7 #define _NOLIBC_COMPILER_H
8 
9 #if defined(__has_attribute)
10 #  define __nolibc_has_attribute(attr) __has_attribute(attr)
11 #else
12 #  define __nolibc_has_attribute(attr) 0
13 #endif
14 
15 #if defined(__has_feature)
16 #  define __nolibc_has_feature(feature) __has_feature(feature)
17 #else
18 #  define __nolibc_has_feature(feature) 0
19 #endif
20 
21 #define __nolibc_aligned(alignment) __attribute__((aligned(alignment)))
22 #define __nolibc_aligned_as(type) __nolibc_aligned(__alignof__(type))
23 
24 #if __nolibc_has_attribute(naked)
25 #  define __nolibc_entrypoint __attribute__((naked))
26 #  define __nolibc_entrypoint_epilogue()
27 #else
28 #  define __nolibc_entrypoint __attribute__((optimize("Os", "omit-frame-pointer")))
29 #  define __nolibc_entrypoint_epilogue() __builtin_unreachable()
30 #endif /* __nolibc_has_attribute(naked) */
31 
32 #if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__)
33 
34 #define _NOLIBC_STACKPROTECTOR
35 
36 #endif /* defined(__SSP__) ... */
37 
38 #if __nolibc_has_attribute(no_stack_protector)
39 #  define __no_stack_protector __attribute__((no_stack_protector))
40 #else
41 #  define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
42 #endif /* __nolibc_has_attribute(no_stack_protector) */
43 
44 #if __nolibc_has_attribute(__fallthrough__)
45 #  define __nolibc_fallthrough do { } while (0); __attribute__((__fallthrough__))
46 #else
47 #  define __nolibc_fallthrough do { } while (0)
48 #endif /* __nolibc_has_attribute(fallthrough) */
49 
50 #if defined(__STDC_VERSION__)
51 #  define __nolibc_stdc_version __STDC_VERSION__
52 #else
53 #  define __nolibc_stdc_version 0
54 #endif
55 
56 #define __nolibc_version(_major, _minor, _patch) ((_major) * 10000 + (_minor) * 100 + (_patch))
57 
58 #ifdef __GNUC__
59 #  define __nolibc_gnuc_version \
60 		__nolibc_version(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
61 #else
62 #  define __nolibc_gnuc_version 0
63 #endif /* __GNUC__ */
64 
65 #ifdef __clang__
66 #  define __nolibc_clang_version \
67 		__nolibc_version(__clang_major__, __clang_minor__, __clang_patchlevel__)
68 #else
69 #  define __nolibc_clang_version 0
70 #endif /* __clang__ */
71 
72 #if __nolibc_stdc_version >= 201112L || \
73 	__nolibc_gnuc_version >= __nolibc_version(4, 6, 0) || \
74 	__nolibc_clang_version >= __nolibc_version(3, 0, 0)
75 #  define __nolibc_static_assert(_t) _Static_assert(_t, "")
76 #else
77 #  define __nolibc_static_assert(_t)
78 #endif
79 
80 /* Make the optimizer believe the variable can be manipulated arbitrarily. */
81 #define _NOLIBC_OPTIMIZER_HIDE_VAR(var)	__asm__ ("" : "+r" (var))
82 
83 #if __nolibc_has_feature(undefined_behavior_sanitizer)
84 #  if defined(__clang__)
85 #    define __nolibc_no_sanitize_undefined __attribute__((no_sanitize("function")))
86 #  else
87 #    define __nolibc_no_sanitize_undefined __attribute__((no_sanitize_undefined))
88 #  endif
89 #else
90 #  define __nolibc_no_sanitize_undefined
91 #endif
92 
93 #endif /* _NOLIBC_COMPILER_H */
94