compiler.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #if defined(__has_attribute)
  9. # define __nolibc_has_attribute(attr) __has_attribute(attr)
  10. #else
  11. # define __nolibc_has_attribute(attr) 0
  12. #endif
  13. #if defined(__has_feature)
  14. # define __nolibc_has_feature(feature) __has_feature(feature)
  15. #else
  16. # define __nolibc_has_feature(feature) 0
  17. #endif
  18. #define __nolibc_aligned(alignment) __attribute__((aligned(alignment)))
  19. #define __nolibc_aligned_as(type) __nolibc_aligned(__alignof__(type))
  20. #if __nolibc_has_attribute(naked)
  21. # define __nolibc_entrypoint __attribute__((naked))
  22. # define __nolibc_entrypoint_epilogue()
  23. #else
  24. # define __nolibc_entrypoint __attribute__((optimize("Os", "omit-frame-pointer")))
  25. # define __nolibc_entrypoint_epilogue() __builtin_unreachable()
  26. #endif /* __nolibc_has_attribute(naked) */
  27. #if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__)
  28. #define _NOLIBC_STACKPROTECTOR
  29. #endif /* defined(__SSP__) ... */
  30. #if __nolibc_has_attribute(no_stack_protector)
  31. # define __no_stack_protector __attribute__((no_stack_protector))
  32. #else
  33. # define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
  34. #endif /* __nolibc_has_attribute(no_stack_protector) */
  35. #if __nolibc_has_attribute(__fallthrough__)
  36. # define __nolibc_fallthrough do { } while (0); __attribute__((__fallthrough__))
  37. #else
  38. # define __nolibc_fallthrough do { } while (0)
  39. #endif /* __nolibc_has_attribute(fallthrough) */
  40. #define __nolibc_version(_major, _minor, _patch) ((_major) * 10000 + (_minor) * 100 + (_patch))
  41. #ifdef __GNUC__
  42. # define __nolibc_gnuc_version \
  43. __nolibc_version(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  44. #else
  45. # define __nolibc_gnuc_version 0
  46. #endif /* __GNUC__ */
  47. #ifdef __clang__
  48. # define __nolibc_clang_version \
  49. __nolibc_version(__clang_major__, __clang_minor__, __clang_patchlevel__)
  50. #else
  51. # define __nolibc_clang_version 0
  52. #endif /* __clang__ */
  53. #if __STDC_VERSION__ >= 201112L || \
  54. __nolibc_gnuc_version >= __nolibc_version(4, 6, 0) || \
  55. __nolibc_clang_version >= __nolibc_version(3, 0, 0)
  56. # define __nolibc_static_assert(_t) _Static_assert(_t, "")
  57. #else
  58. # define __nolibc_static_assert(_t)
  59. #endif
  60. #endif /* _NOLIBC_COMPILER_H */