1
0

cdefs.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Copyright (C) 1992-2026 Free Software Foundation, Inc.
  17. Copyright The GNU Toolchain Authors.
  18. This file is part of the GNU C Library.
  19. The GNU C Library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #ifndef _SYS_CDEFS_H
  31. #define _SYS_CDEFS_H 1
  32. /* We are almost always included from features.h. */
  33. #ifndef _FEATURES_H
  34. # include <features.h>
  35. #endif
  36. /* The GNU libc does not support any K&R compilers or the traditional mode
  37. of ISO C compilers anymore. Check for some of the combinations not
  38. supported anymore. */
  39. #if defined __GNUC__ && !defined __STDC__ && !defined __cplusplus
  40. # error "You need a ISO C or C++ conforming compiler to use the glibc headers"
  41. #endif
  42. /* Some user header file might have defined this before. */
  43. #undef __P
  44. #undef __PMT
  45. /* Compilers that lack __has_attribute may object to
  46. #if defined __has_attribute && __has_attribute (...)
  47. even though they do not need to evaluate the right-hand side of the &&.
  48. Similarly for __has_builtin, etc. */
  49. #if (defined __has_attribute \
  50. && (!defined __clang_minor__ \
  51. || 3 < __clang_major__ + (5 <= __clang_minor__)))
  52. # define __glibc_has_attribute(attr) __has_attribute (attr)
  53. #else
  54. # define __glibc_has_attribute(attr) 0
  55. #endif
  56. #ifdef __has_builtin
  57. # define __glibc_has_builtin(name) __has_builtin (name)
  58. #else
  59. # define __glibc_has_builtin(name) 0
  60. #endif
  61. #ifdef __has_extension
  62. # define __glibc_has_extension(ext) __has_extension (ext)
  63. #else
  64. # define __glibc_has_extension(ext) 0
  65. #endif
  66. #if defined __GNUC__ || defined __clang__
  67. /* All functions, except those with callbacks or those that
  68. synchronize memory, are leaf functions. */
  69. # if __GNUC_PREREQ (4, 6) && !defined _LIBC
  70. # define __LEAF , __leaf__
  71. # define __LEAF_ATTR __attribute__ ((__leaf__))
  72. # else
  73. # define __LEAF
  74. # define __LEAF_ATTR
  75. # endif
  76. /* GCC can always grok prototypes. For C++ programs we add throw()
  77. to help it optimize the function calls. But this only works with
  78. gcc 2.8.x and egcs. For gcc 3.4 and up we even mark C functions
  79. as non-throwing using a function attribute since programs can use
  80. the -fexceptions options for C code as well. */
  81. # if !defined __cplusplus \
  82. && (__GNUC_PREREQ (3, 4) || __glibc_has_attribute (__nothrow__))
  83. # define __THROW __attribute__ ((__nothrow__ __LEAF))
  84. # define __THROWNL __attribute__ ((__nothrow__))
  85. # define __NTH(fct) __attribute__ ((__nothrow__ __LEAF)) fct
  86. # define __NTHNL(fct) __attribute__ ((__nothrow__)) fct
  87. # else
  88. # if defined __cplusplus && (__GNUC_PREREQ (2,8) || __clang_major__ >= 4)
  89. # if __cplusplus >= 201103L
  90. # define __THROW noexcept (true)
  91. # else
  92. # define __THROW throw ()
  93. # endif
  94. # define __THROWNL __THROW
  95. # define __NTH(fct) __LEAF_ATTR fct __THROW
  96. # define __NTHNL(fct) fct __THROW
  97. # else
  98. # define __THROW
  99. # define __THROWNL
  100. # define __NTH(fct) fct
  101. # define __NTHNL(fct) fct
  102. # endif
  103. # endif
  104. # if __GNUC_PREREQ (4, 3) || __glibc_has_attribute (__cold__)
  105. # define __COLD __attribute__ ((__cold__))
  106. # else
  107. # define __COLD
  108. # endif
  109. #else /* Not GCC or clang. */
  110. # if (defined __cplusplus \
  111. || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
  112. # define __inline inline
  113. # else
  114. # define __inline /* No inline functions. */
  115. # endif
  116. # define __THROW
  117. # define __THROWNL
  118. # define __NTH(fct) fct
  119. # define __COLD
  120. #endif /* GCC || clang. */
  121. /* These two macros are not used in glibc anymore. They are kept here
  122. only because some other projects expect the macros to be defined. */
  123. #define __P(args) args
  124. #define __PMT(args) args
  125. /* For these things, GCC behaves the ANSI way normally,
  126. and the non-ANSI way under -traditional. */
  127. #define __CONCAT(x,y) x ## y
  128. #define __STRING(x) #x
  129. /* This is not a typedef so `const __ptr_t' does the right thing. */
  130. #define __ptr_t void *
  131. /* C++ needs to know that types and declarations are C, not C++. */
  132. #ifdef __cplusplus
  133. # define __BEGIN_DECLS extern "C" {
  134. # define __END_DECLS }
  135. #else
  136. # define __BEGIN_DECLS
  137. # define __END_DECLS
  138. #endif
  139. /* The overloadable attribute was added on clang 2.6. */
  140. #if defined __clang_major__ \
  141. && (__clang_major__ + (__clang_minor__ >= 6) > 2)
  142. # define __attribute_overloadable__ __attribute__((__overloadable__))
  143. #else
  144. # define __attribute_overloadable__
  145. #endif
  146. /* Fortify support. */
  147. #define __bos(ptr) __builtin_object_size (ptr, __USE_FORTIFY_LEVEL > 1)
  148. #define __bos0(ptr) __builtin_object_size (ptr, 0)
  149. /* Use __builtin_dynamic_object_size at _FORTIFY_SOURCE=3 when available. */
  150. #if __USE_FORTIFY_LEVEL == 3 && (__glibc_clang_prereq (9, 0) \
  151. || __GNUC_PREREQ (12, 0))
  152. # define __glibc_objsize0(__o) __builtin_dynamic_object_size (__o, 0)
  153. # define __glibc_objsize(__o) __builtin_dynamic_object_size (__o, 1)
  154. #else
  155. # define __glibc_objsize0(__o) __bos0 (__o)
  156. # define __glibc_objsize(__o) __bos (__o)
  157. #endif
  158. #if __USE_FORTIFY_LEVEL > 0
  159. /* Compile time conditions to choose between the regular, _chk and _chk_warn
  160. variants. These conditions should get evaluated to constant and optimized
  161. away. */
  162. #define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
  163. #define __glibc_unsigned_or_positive(__l) \
  164. ((__typeof (__l)) 0 < (__typeof (__l)) -1 \
  165. || (__builtin_constant_p (__l) && (__l) > 0))
  166. /* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
  167. condition can be folded to a constant and if it is true, or unknown (-1) */
  168. #define __glibc_safe_or_unknown_len(__l, __s, __osz) \
  169. ((__builtin_constant_p (__osz) && (__osz) == (__SIZE_TYPE__) -1) \
  170. || (__glibc_unsigned_or_positive (__l) \
  171. && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), \
  172. (__s), (__osz))) \
  173. && __glibc_safe_len_cond ((__SIZE_TYPE__) (__l), (__s), (__osz))))
  174. /* Conversely, we know at compile time that the length is unsafe if the
  175. __L * __S <= __OBJSZ condition can be folded to a constant and if it is
  176. false. */
  177. #define __glibc_unsafe_len(__l, __s, __osz) \
  178. (__glibc_unsigned_or_positive (__l) \
  179. && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), \
  180. __s, __osz)) \
  181. && !__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
  182. /* To correctly instrument the fortify wrapper clang requires the
  183. pass_object_size attribute, and the attribute has the restriction that the
  184. argument needs to be 'const'. Furthermore, to make it usable with C
  185. interfaces, clang provides the overload attribute, which provides a C++
  186. like function overload support. The overloaded fortify wrapper with the
  187. pass_object_size attribute has precedence over the default symbol.
  188. Also, clang does not support __va_arg_pack, so variadic functions are
  189. expanded to issue va_arg implementations. The error function must not have
  190. bodies (address takes are expanded to nonfortified calls), and with
  191. __fortify_function compiler might still create a body with the C++
  192. mangling name (due to the overload attribute). In this case, the function
  193. is defined with __fortify_function_error_function macro instead.
  194. The argument size check is also done with a clang-only attribute,
  195. __attribute__ ((__diagnose_if__ (...))), different than gcc which calls
  196. symbol_chk_warn alias with uses __warnattr attribute.
  197. The pass_object_size was added on clang 4.0, __diagnose_if__ on 5.0,
  198. and pass_dynamic_object_size on 9.0. */
  199. #if defined __clang_major__ && __clang_major__ >= 5
  200. # define __fortify_use_clang 1
  201. # define __fortify_function_error_function static __attribute__((__unused__))
  202. # define __fortify_clang_pass_object_size_n(n) \
  203. __attribute__ ((__pass_object_size__ (n)))
  204. # define __fortify_clang_pass_object_size0 \
  205. __fortify_clang_pass_object_size_n (0)
  206. # define __fortify_clang_pass_object_size \
  207. __fortify_clang_pass_object_size_n (__USE_FORTIFY_LEVEL > 1)
  208. # if __clang_major__ >= 9
  209. # define __fortify_clang_pass_dynamic_object_size_n(n) \
  210. __attribute__ ((__pass_dynamic_object_size__ (n)))
  211. # define __fortify_clang_pass_dynamic_object_size0 \
  212. __fortify_clang_pass_dynamic_object_size_n (0)
  213. # define __fortify_clang_pass_dynamic_object_size \
  214. __fortify_clang_pass_dynamic_object_size_n (1)
  215. # else
  216. # define __fortify_clang_pass_dynamic_object_size_n(n)
  217. # define __fortify_clang_pass_dynamic_object_size0
  218. # define __fortify_clang_pass_dynamic_object_size
  219. # endif
  220. # define __fortify_clang_bos_static_lt_impl(bos_val, n, s) \
  221. ((bos_val) != -1ULL && (n) > (bos_val) / (s))
  222. # define __fortify_clang_bos_static_lt2(__n, __e, __s) \
  223. __fortify_clang_bos_static_lt_impl (__bos (__e), __n, __s)
  224. # define __fortify_clang_bos_static_lt(__n, __e) \
  225. __fortify_clang_bos_static_lt2 (__n, __e, 1)
  226. # define __fortify_clang_bos0_static_lt2(__n, __e, __s) \
  227. __fortify_clang_bos_static_lt_impl (__bos0 (__e), __n, __s)
  228. # define __fortify_clang_bos0_static_lt(__n, __e) \
  229. __fortify_clang_bos0_static_lt2 (__n, __e, 1)
  230. # define __fortify_clang_bosn_args(bos_fn, n, buf, div, complaint) \
  231. (__fortify_clang_bos_static_lt_impl (bos_fn (buf), n, div)), (complaint), \
  232. "warning"
  233. # define __fortify_clang_warning(__c, __msg) \
  234. __attribute__ ((__diagnose_if__ ((__c), (__msg), "warning")))
  235. # define __fortify_clang_error(__c, __msg) \
  236. __attribute__ ((__diagnose_if__ ((__c), (__msg), "error")))
  237. # define __fortify_clang_warning_only_if_bos0_lt(n, buf, complaint) \
  238. __attribute__ ((__diagnose_if__ \
  239. (__fortify_clang_bosn_args (__bos0, n, buf, 1, complaint))))
  240. # define __fortify_clang_warning_only_if_bos0_lt2(n, buf, div, complaint) \
  241. __attribute__ ((__diagnose_if__ \
  242. (__fortify_clang_bosn_args (__bos0, n, buf, div, complaint))))
  243. # define __fortify_clang_warning_only_if_bos_lt(n, buf, complaint) \
  244. __attribute__ ((__diagnose_if__ \
  245. (__fortify_clang_bosn_args (__bos, n, buf, 1, complaint))))
  246. # define __fortify_clang_warning_only_if_bos_lt2(n, buf, div, complaint) \
  247. __attribute__ ((__diagnose_if__ \
  248. (__fortify_clang_bosn_args (__bos, n, buf, div, complaint))))
  249. # define __fortify_clang_prefer_this_overload \
  250. __attribute__ ((enable_if (1, "")))
  251. # define __fortify_clang_unavailable(__msg) \
  252. __attribute__ ((unavailable(__msg)))
  253. # if __USE_FORTIFY_LEVEL == 3
  254. # define __fortify_clang_overload_arg(__type, __attr, __name) \
  255. __type __attr const __fortify_clang_pass_dynamic_object_size __name
  256. # define __fortify_clang_overload_arg0(__type, __attr, __name) \
  257. __type __attr const __fortify_clang_pass_dynamic_object_size0 __name
  258. # else
  259. # define __fortify_clang_overload_arg(__type, __attr, __name) \
  260. __type __attr const __fortify_clang_pass_object_size __name
  261. # define __fortify_clang_overload_arg0(__type, __attr, __name) \
  262. __type __attr const __fortify_clang_pass_object_size0 __name
  263. # endif
  264. # define __fortify_clang_mul_may_overflow(size, n) \
  265. ((size | n) >= (((size_t)1) << (8 * sizeof (size_t) / 2)))
  266. # define __fortify_clang_size_too_small(__bos, __dest, __len) \
  267. (__bos (__dest) != (size_t) -1 && __bos (__dest) < __len)
  268. # define __fortify_clang_warn_if_src_too_large(__dest, __src) \
  269. __fortify_clang_warning (__fortify_clang_size_too_small (__glibc_objsize, \
  270. __dest, \
  271. __builtin_strlen (__src) + 1), \
  272. "destination buffer will always be overflown by source")
  273. # define __fortify_clang_warn_if_dest_too_small(__dest, __len) \
  274. __fortify_clang_warning (__fortify_clang_size_too_small (__glibc_objsize, \
  275. __dest, \
  276. __len), \
  277. "function called with bigger length than the destination buffer")
  278. # define __fortify_clang_warn_if_dest_too_small0(__dest, __len) \
  279. __fortify_clang_warning (__fortify_clang_size_too_small (__glibc_objsize0, \
  280. __dest, \
  281. __len), \
  282. "function called with bigger length than the destination buffer")
  283. #else
  284. # define __fortify_use_clang 0
  285. # define __fortify_clang_warning(__c, __msg)
  286. # define __fortify_clang_warning_only_if_bos0_lt(__n, __buf, __complaint)
  287. # define __fortify_clang_warning_only_if_bos0_lt2(__n, __buf, __div, complaint)
  288. # define __fortify_clang_warning_only_if_bos_lt(__n, __buf, __complaint)
  289. # define __fortify_clang_warning_only_if_bos_lt2(__n, __buf, div, __complaint)
  290. # define __fortify_clang_overload_arg(__type, __attr, __name) \
  291. __type __attr __name
  292. # define __fortify_clang_overload_arg0(__type, __attr, __name) \
  293. __fortify_clang_overload_arg (__type, __attr, __name)
  294. # define __fortify_clang_warn_if_src_too_large(__dest, __src)
  295. # define __fortify_clang_warn_if_dest_too_small(__dest, __len)
  296. # define __fortify_clang_warn_if_dest_too_small0(__dest, __len)
  297. #endif
  298. /* Fortify function f. __f_alias, __f_chk and __f_chk_warn must be
  299. declared. */
  300. #if !__fortify_use_clang
  301. # define __glibc_fortify(f, __l, __s, __osz, ...) \
  302. (__glibc_safe_or_unknown_len (__l, __s, __osz) \
  303. ? __ ## f ## _alias (__VA_ARGS__) \
  304. : (__glibc_unsafe_len (__l, __s, __osz) \
  305. ? __ ## f ## _chk_warn (__VA_ARGS__, __osz) \
  306. : __ ## f ## _chk (__VA_ARGS__, __osz)))
  307. #else
  308. # define __glibc_fortify(f, __l, __s, __osz, ...) \
  309. (__osz == (__SIZE_TYPE__) -1) \
  310. ? __ ## f ## _alias (__VA_ARGS__) \
  311. : __ ## f ## _chk (__VA_ARGS__, __osz)
  312. #endif
  313. /* Fortify function f, where object size argument passed to f is the number of
  314. elements and not total size. */
  315. #if !__fortify_use_clang
  316. # define __glibc_fortify_n(f, __l, __s, __osz, ...) \
  317. (__glibc_safe_or_unknown_len (__l, __s, __osz) \
  318. ? __ ## f ## _alias (__VA_ARGS__) \
  319. : (__glibc_unsafe_len (__l, __s, __osz) \
  320. ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s)) \
  321. : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))
  322. # else
  323. # define __glibc_fortify_n(f, __l, __s, __osz, ...) \
  324. (__osz == (__SIZE_TYPE__) -1) \
  325. ? __ ## f ## _alias (__VA_ARGS__) \
  326. : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))
  327. #endif
  328. #endif /* __USE_FORTIFY_LEVEL > 0 */
  329. #if __GNUC_PREREQ (4,3)
  330. # define __warnattr(msg) __attribute__((__warning__ (msg)))
  331. # define __errordecl(name, msg) \
  332. extern void name (void) __attribute__((__error__ (msg)))
  333. #else
  334. # define __warnattr(msg)
  335. # define __errordecl(name, msg) extern void name (void)
  336. #endif
  337. /* Support for flexible arrays.
  338. Headers that should use flexible arrays only if they're "real"
  339. (e.g. only if they won't affect sizeof()) should test
  340. #if __glibc_c99_flexarr_available. */
  341. #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L && !defined __HP_cc
  342. # define __flexarr []
  343. # define __glibc_c99_flexarr_available 1
  344. #elif __GNUC_PREREQ (2,97) || defined __clang__
  345. /* GCC 2.97 and clang support C99 flexible array members as an extension,
  346. even when in C89 mode or compiling C++ (any version). */
  347. # define __flexarr []
  348. # define __glibc_c99_flexarr_available 1
  349. #elif defined __GNUC__
  350. /* Pre-2.97 GCC did not support C99 flexible arrays but did have
  351. an equivalent extension with slightly different notation. */
  352. # define __flexarr [0]
  353. # define __glibc_c99_flexarr_available 1
  354. #else
  355. /* Some other non-C99 compiler. Approximate with [1]. */
  356. # define __flexarr [1]
  357. # define __glibc_c99_flexarr_available 0
  358. #endif
  359. /* __asm__ ("xyz") is used throughout the headers to rename functions
  360. at the assembly language level. This is wrapped by the __REDIRECT
  361. macro, in order to support compilers that can do this some other
  362. way. When compilers don't support asm-names at all, we have to do
  363. preprocessor tricks instead (which don't have exactly the right
  364. semantics, but it's the best we can do).
  365. Example:
  366. int __REDIRECT(setpgrp, (__pid_t pid, __pid_t pgrp), setpgid); */
  367. #if (defined __GNUC__ && __GNUC__ >= 2) || (__clang_major__ >= 4)
  368. # define __REDIRECT(name, proto, alias) name proto __asm__ (__ASMNAME (#alias))
  369. # ifdef __cplusplus
  370. # define __REDIRECT_NTH(name, proto, alias) \
  371. name proto __THROW __asm__ (__ASMNAME (#alias))
  372. # define __REDIRECT_NTHNL(name, proto, alias) \
  373. name proto __THROWNL __asm__ (__ASMNAME (#alias))
  374. # else
  375. # define __REDIRECT_NTH(name, proto, alias) \
  376. name proto __asm__ (__ASMNAME (#alias)) __THROW
  377. # define __REDIRECT_NTHNL(name, proto, alias) \
  378. name proto __asm__ (__ASMNAME (#alias)) __THROWNL
  379. # endif
  380. # define __ASMNAME(cname) __ASMNAME2 (__USER_LABEL_PREFIX__, cname)
  381. # define __ASMNAME2(prefix, cname) __STRING (prefix) cname
  382. #ifndef __REDIRECT_FORTIFY
  383. #define __REDIRECT_FORTIFY __REDIRECT
  384. #endif
  385. #ifndef __REDIRECT_FORTIFY_NTH
  386. #define __REDIRECT_FORTIFY_NTH __REDIRECT_NTH
  387. #endif
  388. /*
  389. #elif __SOME_OTHER_COMPILER__
  390. # define __REDIRECT(name, proto, alias) name proto; \
  391. _Pragma("let " #name " = " #alias)
  392. */
  393. #endif
  394. /* GCC, clang, and compatible compilers have various useful declarations
  395. that can be made with the '__attribute__' syntax. All of the ways we use
  396. this do fine if they are omitted for compilers that don't understand it. */
  397. #if !(defined __GNUC__ || defined __clang__ || defined __TINYC__)
  398. # define __attribute__(xyz) /* Ignore */
  399. #endif
  400. /* At some point during the gcc 2.96 development the `malloc' attribute
  401. for functions was introduced. We don't want to use it unconditionally
  402. (although this would be possible) since it generates warnings. */
  403. #if __GNUC_PREREQ (2,96) || __glibc_has_attribute (__malloc__)
  404. # define __attribute_malloc__ __attribute__ ((__malloc__))
  405. #else
  406. # define __attribute_malloc__ /* Ignore */
  407. #endif
  408. /* Tell the compiler which arguments to an allocation function
  409. indicate the size of the allocation. */
  410. #if __GNUC_PREREQ (4, 3)
  411. # define __attribute_alloc_size__(params) \
  412. __attribute__ ((__alloc_size__ params))
  413. #else
  414. # define __attribute_alloc_size__(params) /* Ignore. */
  415. #endif
  416. /* Tell the compiler which argument to an allocation function
  417. indicates the alignment of the allocation. */
  418. #if __GNUC_PREREQ (4, 9) || __glibc_has_attribute (__alloc_align__)
  419. # define __attribute_alloc_align__(param) \
  420. __attribute__ ((__alloc_align__ param))
  421. #else
  422. # define __attribute_alloc_align__(param) /* Ignore. */
  423. #endif
  424. /* At some point during the gcc 2.96 development the `pure' attribute
  425. for functions was introduced. We don't want to use it unconditionally
  426. (although this would be possible) since it generates warnings. */
  427. #if __GNUC_PREREQ (2,96) || __glibc_has_attribute (__pure__)
  428. # define __attribute_pure__ __attribute__ ((__pure__))
  429. #else
  430. # define __attribute_pure__ /* Ignore */
  431. #endif
  432. /* This declaration tells the compiler that the value is constant. */
  433. #if __GNUC_PREREQ (2,5) || __glibc_has_attribute (__const__)
  434. # define __attribute_const__ __attribute__ ((__const__))
  435. #else
  436. # define __attribute_const__ /* Ignore */
  437. #endif
  438. #if __GNUC_PREREQ (2,7) || __glibc_has_attribute (__unused__)
  439. # define __attribute_maybe_unused__ __attribute__ ((__unused__))
  440. #else
  441. # define __attribute_maybe_unused__ /* Ignore */
  442. #endif
  443. /* At some point during the gcc 3.1 development the `used' attribute
  444. for functions was introduced. We don't want to use it unconditionally
  445. (although this would be possible) since it generates warnings. */
  446. #if __GNUC_PREREQ (3,1) || __glibc_has_attribute (__used__)
  447. # define __attribute_used__ __attribute__ ((__used__))
  448. # define __attribute_noinline__ __attribute__ ((__noinline__))
  449. #else
  450. # define __attribute_used__ __attribute__ ((__unused__))
  451. # define __attribute_noinline__ /* Ignore */
  452. #endif
  453. /* Since version 3.2, gcc allows marking deprecated functions. */
  454. #if __GNUC_PREREQ (3,2) || __glibc_has_attribute (__deprecated__)
  455. # define __attribute_deprecated__ __attribute__ ((__deprecated__))
  456. #else
  457. # define __attribute_deprecated__ /* Ignore */
  458. #endif
  459. /* Since version 4.5, gcc also allows one to specify the message printed
  460. when a deprecated function is used. clang claims to be gcc 4.2, but
  461. may also support this feature. */
  462. #if __GNUC_PREREQ (4,5) \
  463. || __glibc_has_extension (__attribute_deprecated_with_message__)
  464. # define __attribute_deprecated_msg__(msg) \
  465. __attribute__ ((__deprecated__ (msg)))
  466. #else
  467. # define __attribute_deprecated_msg__(msg) __attribute_deprecated__
  468. #endif
  469. /* At some point during the gcc 2.8 development the `format_arg' attribute
  470. for functions was introduced. We don't want to use it unconditionally
  471. (although this would be possible) since it generates warnings.
  472. If several `format_arg' attributes are given for the same function, in
  473. gcc-3.0 and older, all but the last one are ignored. In newer gccs,
  474. all designated arguments are considered. */
  475. #if __GNUC_PREREQ (2,8) || __glibc_has_attribute (__format_arg__)
  476. # define __attribute_format_arg__(x) __attribute__ ((__format_arg__ (x)))
  477. #else
  478. # define __attribute_format_arg__(x) /* Ignore */
  479. #endif
  480. /* At some point during the gcc 2.97 development the `strfmon' format
  481. attribute for functions was introduced. We don't want to use it
  482. unconditionally (although this would be possible) since it
  483. generates warnings. */
  484. #if __GNUC_PREREQ (2,97) || __glibc_has_attribute (__format__)
  485. # define __attribute_format_strfmon__(a,b) \
  486. __attribute__ ((__format__ (__strfmon__, a, b)))
  487. #else
  488. # define __attribute_format_strfmon__(a,b) /* Ignore */
  489. #endif
  490. /* The nonnull function attribute marks pointer parameters that
  491. must not be NULL. This has the name __nonnull in glibc,
  492. and __attribute_nonnull__ in files shared with Gnulib to avoid
  493. collision with a different __nonnull in DragonFlyBSD 5.9. */
  494. #ifndef __attribute_nonnull__
  495. # if __GNUC_PREREQ (3,3) || __glibc_has_attribute (__nonnull__)
  496. # define __attribute_nonnull__(params) __attribute__ ((__nonnull__ params))
  497. # else
  498. # define __attribute_nonnull__(params)
  499. # endif
  500. #endif
  501. #ifndef __nonnull
  502. # define __nonnull(params) __attribute_nonnull__ (params)
  503. #endif
  504. /* The returns_nonnull function attribute marks the return type of the function
  505. as always being non-null. */
  506. #ifndef __returns_nonnull
  507. # if __GNUC_PREREQ (4, 9) || __glibc_has_attribute (__returns_nonnull__)
  508. # define __returns_nonnull __attribute__ ((__returns_nonnull__))
  509. # else
  510. # define __returns_nonnull
  511. # endif
  512. #endif
  513. /* If fortification mode, we warn about unused results of certain
  514. function calls which can lead to problems. */
  515. #if __GNUC_PREREQ (3,4) || __glibc_has_attribute (__warn_unused_result__)
  516. # define __attribute_warn_unused_result__ \
  517. __attribute__ ((__warn_unused_result__))
  518. # if defined __USE_FORTIFY_LEVEL && __USE_FORTIFY_LEVEL > 0
  519. # define __wur __attribute_warn_unused_result__
  520. # endif
  521. #else
  522. # define __attribute_warn_unused_result__ /* empty */
  523. #endif
  524. #ifndef __wur
  525. # define __wur /* Ignore */
  526. #endif
  527. /* Forces a function to be always inlined. */
  528. #if __GNUC_PREREQ (3,2) || __glibc_has_attribute (__always_inline__)
  529. /* The Linux kernel defines __always_inline in stddef.h (283d7573), and
  530. it conflicts with this definition. Therefore undefine it first to
  531. allow either header to be included first. */
  532. # undef __always_inline
  533. # define __always_inline __inline __attribute__ ((__always_inline__))
  534. #else
  535. # undef __always_inline
  536. # define __always_inline __inline
  537. #endif
  538. /* Associate error messages with the source location of the call site rather
  539. than with the source location inside the function. */
  540. #if __GNUC_PREREQ (4,3) || __glibc_has_attribute (__artificial__)
  541. # define __attribute_artificial__ __attribute__ ((__artificial__))
  542. #else
  543. # define __attribute_artificial__ /* Ignore */
  544. #endif
  545. /* GCC 4.3 and above with -std=c99 or -std=gnu99 implements ISO C99 inline
  546. semantics, unless -fgnu89-inline is used. Using __GNUC_STDC_INLINE__ or
  547. __GNUC_GNU_INLINE__ is not a good enough check for gcc because gcc versions
  548. older than 4.3 may define these macros and still not guarantee GNU inlining
  549. semantics.
  550. clang++ identifies itself as gcc-4.2, but has support for GNU inlining
  551. semantics, that can be checked for by using the __GNUC_STDC_INLINE__ and
  552. __GNUC_GNU_INLINE__ macro definitions. */
  553. #if (!defined __cplusplus || __GNUC_PREREQ (4,3) \
  554. || (defined __clang__ && (defined __GNUC_STDC_INLINE__ \
  555. || defined __GNUC_GNU_INLINE__)))
  556. # if defined __GNUC_STDC_INLINE__ || defined __cplusplus
  557. # define __extern_inline extern __inline __attribute__ ((__gnu_inline__))
  558. # define __extern_always_inline \
  559. extern __always_inline __attribute__ ((__gnu_inline__))
  560. # else
  561. # define __extern_inline extern __inline
  562. # define __extern_always_inline extern __always_inline
  563. # endif
  564. #endif
  565. #ifdef __extern_always_inline
  566. # define __fortify_function __extern_always_inline __attribute_artificial__
  567. #endif
  568. /* GCC 4.3 and above allow passing all anonymous arguments of an
  569. __extern_always_inline function to some other vararg function. */
  570. #if __GNUC_PREREQ (4,3)
  571. # define __va_arg_pack() __builtin_va_arg_pack ()
  572. # define __va_arg_pack_len() __builtin_va_arg_pack_len ()
  573. #endif
  574. /* It is possible to compile containing GCC extensions even if GCC is
  575. run in pedantic mode if the uses are carefully marked using the
  576. `__extension__' keyword. But this is not generally available before
  577. version 2.8. */
  578. #if !(__GNUC_PREREQ (2,8) || defined __clang__)
  579. # define __extension__ /* Ignore */
  580. #endif
  581. /* __restrict is known in EGCS 1.2 and above, and in clang.
  582. It works also in C++ mode (outside of arrays), but only when spelled
  583. as '__restrict', not 'restrict'. */
  584. #if !(__GNUC_PREREQ (2,92) || __clang_major__ >= 3)
  585. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  586. # define __restrict restrict
  587. # else
  588. # define __restrict /* Ignore */
  589. # endif
  590. #endif
  591. /* ISO C99 also allows to declare arrays as non-overlapping. The syntax is
  592. array_name[restrict]
  593. GCC 3.1 and clang support this.
  594. This syntax is not usable in C++ mode. */
  595. #if (__GNUC_PREREQ (3,1) || __clang_major__ >= 3) && !defined __cplusplus
  596. # define __restrict_arr __restrict
  597. #else
  598. # ifdef __GNUC__
  599. # define __restrict_arr /* Not supported in old GCC. */
  600. # else
  601. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  602. # define __restrict_arr restrict
  603. # else
  604. /* Some other non-C99 compiler. */
  605. # define __restrict_arr /* Not supported. */
  606. # endif
  607. # endif
  608. #endif
  609. #if (__GNUC__ >= 3) || __glibc_has_builtin (__builtin_expect)
  610. # define __glibc_unlikely(cond) __builtin_expect ((cond), 0)
  611. # define __glibc_likely(cond) __builtin_expect ((cond), 1)
  612. #else
  613. # define __glibc_unlikely(cond) (cond)
  614. # define __glibc_likely(cond) (cond)
  615. #endif
  616. #if (!defined _Noreturn \
  617. && (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112 \
  618. && !(__GNUC_PREREQ (4,7) \
  619. || (3 < __clang_major__ + (5 <= __clang_minor__))))
  620. # if __GNUC_PREREQ (2,8)
  621. # define _Noreturn __attribute__ ((__noreturn__))
  622. # else
  623. # define _Noreturn
  624. # endif
  625. #endif
  626. #if __GNUC_PREREQ (8, 0)
  627. /* Describes a char array whose address can safely be passed as the first
  628. argument to strncpy and strncat, as the char array is not necessarily
  629. a NUL-terminated string. */
  630. # define __attribute_nonstring__ __attribute__ ((__nonstring__))
  631. #else
  632. # define __attribute_nonstring__
  633. #endif
  634. /* Undefine (also defined in libc-symbols.h). */
  635. #undef __attribute_copy__
  636. #if __GNUC_PREREQ (9, 0)
  637. /* Copies attributes from the declaration or type referenced by
  638. the argument. */
  639. # define __attribute_copy__(arg) __attribute__ ((__copy__ (arg)))
  640. #else
  641. # define __attribute_copy__(arg)
  642. #endif
  643. #if (!defined _Static_assert && !defined __cplusplus \
  644. && (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112 \
  645. && (!(__GNUC_PREREQ (4, 6) || __clang_major__ >= 4) \
  646. || defined __STRICT_ANSI__))
  647. # define _Static_assert(expr, diagnostic) \
  648. extern int (*__Static_assert_function (void)) \
  649. [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
  650. #endif
  651. /* Gnulib avoids including these, as they don't work on non-glibc or
  652. older glibc platforms. */
  653. #ifndef __GNULIB_CDEFS
  654. # include <bits/wordsize.h>
  655. # include <bits/long-double.h>
  656. #endif
  657. #if __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  658. # ifdef __REDIRECT
  659. /* Alias name defined automatically. */
  660. # define __LDBL_REDIR(name, proto) ... unused__ldbl_redir
  661. # define __LDBL_REDIR_DECL(name) \
  662. extern __typeof (name) name __asm (__ASMNAME ("__" #name "ieee128"));
  663. # define __REDIRECT_LDBL(name, proto, alias) \
  664. name proto __asm (__ASMNAME ("__" #alias "ieee128"))
  665. /* Alias name defined automatically, with leading underscores. */
  666. # define __LDBL_REDIR2_DECL(name) \
  667. extern __typeof (__##name) __##name \
  668. __asm (__ASMNAME ("__" #name "ieee128"));
  669. /* Alias name defined manually. */
  670. # define __LDBL_REDIR1(name, proto, alias) ... unused__ldbl_redir1
  671. # define __LDBL_REDIR1_DECL(name, alias) \
  672. extern __typeof (name) name __asm (__ASMNAME (#alias));
  673. # define __LDBL_REDIR1_NTH(name, proto, alias) \
  674. __REDIRECT_NTH (name, proto, alias)
  675. # define __REDIRECT_NTH_LDBL(name, proto, alias) \
  676. __LDBL_REDIR1_NTH (name, proto, __##alias##ieee128)
  677. /* Unused. */
  678. # define __LDBL_REDIR_NTH(name, proto) ... unused__ldbl_redir_nth
  679. # else
  680. _Static_assert (0, "IEEE 128-bits long double requires redirection on this platform");
  681. # endif
  682. #elif defined __LONG_DOUBLE_MATH_OPTIONAL && defined __NO_LONG_DOUBLE_MATH
  683. # define __LDBL_COMPAT 1
  684. # ifdef __REDIRECT
  685. # define __LDBL_REDIR1(name, proto, alias) __REDIRECT (name, proto, alias)
  686. # define __LDBL_REDIR(name, proto) \
  687. __LDBL_REDIR1 (name, proto, __nldbl_##name)
  688. # define __LDBL_REDIR1_NTH(name, proto, alias) __REDIRECT_NTH (name, proto, alias)
  689. # define __LDBL_REDIR_NTH(name, proto) \
  690. __LDBL_REDIR1_NTH (name, proto, __nldbl_##name)
  691. # define __LDBL_REDIR2_DECL(name) \
  692. extern __typeof (__##name) __##name __asm (__ASMNAME ("__nldbl___" #name));
  693. # define __LDBL_REDIR1_DECL(name, alias) \
  694. extern __typeof (name) name __asm (__ASMNAME (#alias));
  695. # define __LDBL_REDIR_DECL(name) \
  696. extern __typeof (name) name __asm (__ASMNAME ("__nldbl_" #name));
  697. # define __REDIRECT_LDBL(name, proto, alias) \
  698. __LDBL_REDIR1 (name, proto, __nldbl_##alias)
  699. # define __REDIRECT_NTH_LDBL(name, proto, alias) \
  700. __LDBL_REDIR1_NTH (name, proto, __nldbl_##alias)
  701. # endif
  702. #endif
  703. #if (!defined __LDBL_COMPAT && __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 0) \
  704. || !defined __REDIRECT
  705. # define __LDBL_REDIR1(name, proto, alias) name proto
  706. # define __LDBL_REDIR(name, proto) name proto
  707. # define __LDBL_REDIR1_NTH(name, proto, alias) name proto __THROW
  708. # define __LDBL_REDIR_NTH(name, proto) name proto __THROW
  709. # define __LDBL_REDIR2_DECL(name)
  710. # define __LDBL_REDIR_DECL(name)
  711. # ifdef __REDIRECT
  712. # define __REDIRECT_LDBL(name, proto, alias) __REDIRECT (name, proto, alias)
  713. # define __REDIRECT_NTH_LDBL(name, proto, alias) \
  714. __REDIRECT_NTH (name, proto, alias)
  715. # endif
  716. #endif
  717. /* __glibc_macro_warning (MESSAGE) issues warning MESSAGE. This is
  718. intended for use in preprocessor macros.
  719. Note: MESSAGE must be a _single_ string; concatenation of string
  720. literals is not supported. */
  721. #if __GNUC_PREREQ (4,8) || __glibc_clang_prereq (3,5)
  722. # define __glibc_macro_warning1(message) _Pragma (#message)
  723. # define __glibc_macro_warning(message) \
  724. __glibc_macro_warning1 (GCC warning message)
  725. #else
  726. # define __glibc_macro_warning(msg)
  727. #endif
  728. /* Generic selection (ISO C11) is a C-only feature, available in GCC
  729. since version 4.9. Previous versions do not provide generic
  730. selection, even though they might set __STDC_VERSION__ to 201112L,
  731. when in -std=c11 mode. Thus, we must check for !defined __GNUC__
  732. when testing __STDC_VERSION__ for generic selection support.
  733. On the other hand, Clang also defines __GNUC__, so a clang-specific
  734. check is required to enable the use of generic selection. */
  735. #if !defined __cplusplus \
  736. && (__GNUC_PREREQ (4, 9) \
  737. || __glibc_has_extension (c_generic_selections) \
  738. || (!defined __GNUC__ && defined __STDC_VERSION__ \
  739. && __STDC_VERSION__ >= 201112L))
  740. # define __HAVE_GENERIC_SELECTION 1
  741. #else
  742. # define __HAVE_GENERIC_SELECTION 0
  743. #endif
  744. #if __HAVE_GENERIC_SELECTION
  745. /* If PTR is a pointer to const, return CALL cast to type CTYPE,
  746. otherwise return CALL. Pointers to types with non-const qualifiers
  747. are not valid. This should not be defined for C++, as macros are
  748. not an appropriate way of implementing such qualifier-generic
  749. operations for C++. */
  750. # define __glibc_const_generic(PTR, CTYPE, CALL) \
  751. _Generic (0 ? (PTR) : (void *) 1, \
  752. const void *: (CTYPE) (CALL), \
  753. default: CALL)
  754. #endif
  755. #if __GNUC_PREREQ (10, 0)
  756. /* Designates a 1-based positional argument ref-index of pointer type
  757. that can be used to access size-index elements of the pointed-to
  758. array according to access mode, or at least one element when
  759. size-index is not provided:
  760. access (access-mode, <ref-index> [, <size-index>]) */
  761. # define __attr_access(x) __attribute__ ((__access__ x))
  762. /* For _FORTIFY_SOURCE == 3 we use __builtin_dynamic_object_size, which may
  763. use the access attribute to get object sizes from function definition
  764. arguments, so we can't use them on functions we fortify. Drop the access
  765. attribute for such functions. */
  766. # if __USE_FORTIFY_LEVEL == 3
  767. # define __fortified_attr_access(a, o, s)
  768. # else
  769. # define __fortified_attr_access(a, o, s) __attr_access ((a, o, s))
  770. # endif
  771. # if __GNUC_PREREQ (11, 0)
  772. # define __attr_access_none(argno) __attribute__ ((__access__ (__none__, argno)))
  773. # else
  774. # define __attr_access_none(argno)
  775. # endif
  776. #else
  777. # define __fortified_attr_access(a, o, s)
  778. # define __attr_access(x)
  779. # define __attr_access_none(argno)
  780. #endif
  781. #if __GNUC_PREREQ (11, 0)
  782. /* Designates dealloc as a function to call to deallocate objects
  783. allocated by the declared function. */
  784. # define __attr_dealloc(dealloc, argno) \
  785. __attribute__ ((__malloc__ (dealloc, argno)))
  786. # define __attr_dealloc_free __attr_dealloc (__builtin_free, 1)
  787. #else
  788. # define __attr_dealloc(dealloc, argno)
  789. # define __attr_dealloc_free
  790. #endif
  791. /* Specify that a function such as setjmp or vfork may return
  792. twice. */
  793. #if __GNUC_PREREQ (4, 1)
  794. # define __attribute_returns_twice__ __attribute__ ((__returns_twice__))
  795. #else
  796. # define __attribute_returns_twice__ /* Ignore. */
  797. #endif
  798. /* Mark struct types as aliasable. Restricted to compilers that
  799. support forward declarations of structs in the presence of the
  800. attribute. */
  801. #if __GNUC_PREREQ (7, 1) || defined __clang__
  802. # define __attribute_struct_may_alias__ __attribute__ ((__may_alias__))
  803. #else
  804. # define __attribute_struct_may_alias__
  805. #endif
  806. #endif /* sys/cdefs.h */