compiler-gcc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_COMPILER_TYPES_H
  3. #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
  4. #endif
  5. /*
  6. * Common definitions for all gcc versions go here.
  7. */
  8. #define GCC_VERSION (__GNUC__ * 10000 \
  9. + __GNUC_MINOR__ * 100 \
  10. + __GNUC_PATCHLEVEL__)
  11. /*
  12. * This macro obfuscates arithmetic on a variable address so that gcc
  13. * shouldn't recognize the original var, and make assumptions about it.
  14. *
  15. * This is needed because the C standard makes it undefined to do
  16. * pointer arithmetic on "objects" outside their boundaries and the
  17. * gcc optimizers assume this is the case. In particular they
  18. * assume such arithmetic does not wrap.
  19. *
  20. * A miscompilation has been observed because of this on PPC.
  21. * To work around it we hide the relationship of the pointer and the object
  22. * using this macro.
  23. *
  24. * Versions of the ppc64 compiler before 4.1 had a bug where use of
  25. * RELOC_HIDE could trash r30. The bug can be worked around by changing
  26. * the inline assembly constraint from =g to =r, in this particular
  27. * case either is valid.
  28. */
  29. #define RELOC_HIDE(ptr, off) \
  30. ({ \
  31. unsigned long __ptr; \
  32. __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
  33. (typeof(ptr)) (__ptr + (off)); \
  34. })
  35. #ifdef CONFIG_RETPOLINE
  36. #define __noretpoline __attribute__((__indirect_branch__("keep")))
  37. #endif
  38. #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
  39. #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
  40. #define __latent_entropy __attribute__((latent_entropy))
  41. #endif
  42. /*
  43. * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
  44. * confuse the stack allocation in gcc, leading to overly large stack
  45. * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
  46. *
  47. * Adding an empty inline assembly before it works around the problem
  48. */
  49. #define barrier_before_unreachable() asm volatile("")
  50. /*
  51. * Mark a position in code as unreachable. This can be used to
  52. * suppress control flow warnings after asm blocks that transfer
  53. * control elsewhere.
  54. */
  55. #define unreachable() \
  56. do { \
  57. annotate_unreachable(); \
  58. barrier_before_unreachable(); \
  59. __builtin_unreachable(); \
  60. } while (0)
  61. #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
  62. #define __HAVE_BUILTIN_BSWAP32__
  63. #define __HAVE_BUILTIN_BSWAP64__
  64. #define __HAVE_BUILTIN_BSWAP16__
  65. #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
  66. #if GCC_VERSION >= 70000
  67. #define KASAN_ABI_VERSION 5
  68. #else
  69. #define KASAN_ABI_VERSION 4
  70. #endif
  71. #ifdef CONFIG_SHADOW_CALL_STACK
  72. #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
  73. #endif
  74. #if __has_attribute(__no_sanitize_address__)
  75. #define __no_sanitize_address __attribute__((no_sanitize_address))
  76. #else
  77. #define __no_sanitize_address
  78. #endif
  79. #if defined(__SANITIZE_THREAD__) && __has_attribute(__no_sanitize_thread__)
  80. #define __no_sanitize_thread __attribute__((no_sanitize_thread))
  81. #else
  82. #define __no_sanitize_thread
  83. #endif
  84. #if __has_attribute(__no_sanitize_undefined__)
  85. #define __no_sanitize_undefined __attribute__((no_sanitize_undefined))
  86. #else
  87. #define __no_sanitize_undefined
  88. #endif
  89. #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
  90. #define __no_sanitize_coverage __attribute__((no_sanitize_coverage))
  91. #else
  92. #define __no_sanitize_coverage
  93. #endif
  94. /*
  95. * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel,
  96. * matching the defines used by Clang.
  97. */
  98. #ifdef __SANITIZE_HWADDRESS__
  99. #define __SANITIZE_ADDRESS__
  100. #endif
  101. /*
  102. * GCC does not support KMSAN.
  103. */
  104. #define __no_sanitize_memory
  105. #define __no_kmsan_checks
  106. /*
  107. * Turn individual warnings and errors on and off locally, depending
  108. * on version.
  109. */
  110. #define __diag_GCC(version, severity, s) \
  111. __diag_GCC_ ## version(__diag_GCC_ ## severity s)
  112. /* Severity used in pragma directives */
  113. #define __diag_GCC_ignore ignored
  114. #define __diag_GCC_warn warning
  115. #define __diag_GCC_error error
  116. #define __diag_str1(s) #s
  117. #define __diag_str(s) __diag_str1(s)
  118. #define __diag(s) _Pragma(__diag_str(GCC diagnostic s))
  119. #if GCC_VERSION >= 80000
  120. #define __diag_GCC_8(s) __diag(s)
  121. #else
  122. #define __diag_GCC_8(s)
  123. #endif
  124. #define __diag_ignore_all(option, comment) \
  125. __diag_GCC(8, ignore, option)
  126. /*
  127. * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size"
  128. * attribute) do not work, and must be disabled.
  129. */
  130. #if GCC_VERSION < 90100
  131. #undef __alloc_size__
  132. #endif