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 #define __nolibc_version(_major, _minor, _patch) ((_major) * 10000 + (_minor) * 100 + (_patch)) 51 52 #ifdef __GNUC__ 53 # define __nolibc_gnuc_version \ 54 __nolibc_version(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) 55 #else 56 # define __nolibc_gnuc_version 0 57 #endif /* __GNUC__ */ 58 59 #ifdef __clang__ 60 # define __nolibc_clang_version \ 61 __nolibc_version(__clang_major__, __clang_minor__, __clang_patchlevel__) 62 #else 63 # define __nolibc_clang_version 0 64 #endif /* __clang__ */ 65 66 #if __STDC_VERSION__ >= 201112L || \ 67 __nolibc_gnuc_version >= __nolibc_version(4, 6, 0) || \ 68 __nolibc_clang_version >= __nolibc_version(3, 0, 0) 69 # define __nolibc_static_assert(_t) _Static_assert(_t, "") 70 #else 71 # define __nolibc_static_assert(_t) 72 #endif 73 74 #endif /* _NOLIBC_COMPILER_H */ 75