1 /* libc/sys/linux/sys/cdefs.h - Helper macros for K&R vs. ANSI C compat. */ 2 3 /* Written 2000 by Werner Almesberger */ 4 5 /*- 6 * SPDX-License-Identifier: BSD-3-Clause 7 * 8 * Copyright (c) 1991, 1993 9 * The Regents of the University of California. All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * Berkeley Software Design, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)cdefs.h 8.8 (Berkeley) 1/9/95 39 * $FreeBSD$ 40 */ 41 42 #ifndef _SYS_CDEFS_H_ 43 #define _SYS_CDEFS_H_ 44 45 #include <sys/config.h> 46 #include <machine/_default_types.h> 47 48 #define __PMT(args) args 49 #define __DOTS , ... 50 #define __THROW 51 52 #ifdef __GNUC__ 53 # define __ASMNAME(cname) __XSTRING (__USER_LABEL_PREFIX__) cname 54 # define _ASMNAME(cname) __asm__(__ASMNAME(cname)) 55 #else 56 # define _ASMNAME(cname) 57 #endif 58 59 #define __ptr_t void * 60 #define __long_double_t long double 61 62 #define __attribute_malloc__ 63 #define __attribute_pure__ 64 #define __attribute_format_strfmon__(a,b) 65 #define __flexarr [0] 66 67 #ifndef __BOUNDED_POINTERS__ 68 # define __bounded /* nothing */ 69 # define __unbounded /* nothing */ 70 # define __ptrvalue /* nothing */ 71 #endif 72 73 /* 74 * Testing against Clang-specific extensions. 75 */ 76 #ifndef __has_attribute 77 #define __has_attribute(x) 0 78 #endif 79 #ifndef __has_extension 80 #define __has_extension __has_feature 81 #endif 82 #ifndef __has_feature 83 #define __has_feature(x) 0 84 #endif 85 #ifndef __has_include 86 #define __has_include(x) 0 87 #endif 88 #ifndef __has_builtin 89 #define __has_builtin(x) 0 90 #endif 91 92 #if defined(__cplusplus) 93 #define __BEGIN_DECLS extern "C" { 94 #define __END_DECLS } 95 #else 96 #define __BEGIN_DECLS 97 #define __END_DECLS 98 #endif 99 100 /* ISO C++. */ 101 102 #ifdef __cplusplus 103 #if !(defined(_BEGIN_STD_C) && defined(_END_STD_C)) 104 #ifdef _HAVE_STD_CXX 105 #define _BEGIN_STD_C namespace std { extern "C" { 106 #define _END_STD_C } } 107 #else 108 #define _BEGIN_STD_C extern "C" { 109 #define _END_STD_C } 110 #endif 111 #if __GNUC_PREREQ (3, 3) 112 #define _NOTHROW __attribute__ ((__nothrow__)) 113 #else 114 #define _NOTHROW throw() 115 #endif 116 #endif 117 #else 118 #define _BEGIN_STD_C 119 #define _END_STD_C 120 #if __GNUC_PREREQ (3, 3) 121 #define _NOTHROW __attribute__ ((__nothrow__)) 122 #else 123 #define _NOTHROW 124 #endif 125 #endif 126 127 /** 128 * Not all compilers offer __builtin_expect (e.g. CompCert does 129 * not have it). In that case, transparently replace all 130 * occurences of that builtin with just the condition: 131 */ 132 #ifndef _HAVE_BUILTIN_EXPECT 133 #define __builtin_expect(cond, exp) (cond) 134 #endif 135 136 /* 137 * This code has been put in place to help reduce the addition of 138 * compiler specific defines in FreeBSD code. It helps to aid in 139 * having a compiler-agnostic source tree. 140 */ 141 142 #if defined(__GNUC__) 143 144 #if __GNUC__ >= 3 145 #define __GNUCLIKE_ASM 3 146 #define __GNUCLIKE_MATH_BUILTIN_CONSTANTS 147 #else 148 #define __GNUCLIKE_ASM 2 149 #endif 150 #define __GNUCLIKE___TYPEOF 1 151 #define __GNUCLIKE___SECTION 1 152 153 #define __GNUCLIKE_CTOR_SECTION_HANDLING 1 154 155 #define __GNUCLIKE_BUILTIN_CONSTANT_P 1 156 157 #if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3) 158 #define __GNUCLIKE_BUILTIN_VARARGS 1 159 #define __GNUCLIKE_BUILTIN_STDARG 1 160 #define __GNUCLIKE_BUILTIN_VAALIST 1 161 #endif 162 163 #define __GNUC_VA_LIST_COMPATIBILITY 1 164 165 /* 166 * Compiler memory barriers, specific to gcc and clang. 167 */ 168 #define __compiler_membar() __asm __volatile(" " : : : "memory") 169 170 #define __GNUCLIKE_BUILTIN_NEXT_ARG 1 171 #define __GNUCLIKE_MATH_BUILTIN_RELOPS 172 173 #define __GNUCLIKE_BUILTIN_MEMCPY 1 174 175 /* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */ 176 #define __CC_SUPPORTS_INLINE 1 177 #define __CC_SUPPORTS___INLINE 1 178 #define __CC_SUPPORTS___INLINE__ 1 179 180 #define __CC_SUPPORTS___FUNC__ 1 181 #define __CC_SUPPORTS_WARNING 1 182 183 #define __CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */ 184 185 #define __CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1 186 187 #endif /* __GNUC__ */ 188 189 /* 190 * The __CONCAT macro is used to concatenate parts of symbol names, e.g. 191 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo. 192 * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI 193 * mode -- there must be no spaces between its arguments, and for nested 194 * __CONCAT's, all the __CONCAT's must be at the left. __CONCAT can also 195 * concatenate double-quoted strings produced by the __STRING macro, but 196 * this only works with ANSI C. 197 * 198 * __XSTRING is like __STRING, but it expands any macros in its argument 199 * first. It is only available with ANSI C. 200 */ 201 #if defined(__STDC__) || defined(__cplusplus) 202 #define __P(protos) protos /* full-blown ANSI C */ 203 #define __CONCAT1(x,y) x ## y 204 #define __CONCAT(x,y) __CONCAT1(x,y) 205 #define __STRING(x) #x /* stringify without expanding x */ 206 #define __XSTRING(x) __STRING(x) /* expand x, then stringify */ 207 208 #define __const const /* define reserved names to standard */ 209 #define __signed signed 210 #define __volatile volatile 211 #if defined(__cplusplus) 212 #define __inline inline /* convert to C++ keyword */ 213 #else 214 #if !(defined(__CC_SUPPORTS___INLINE)) 215 #define __inline /* delete GCC keyword */ 216 #endif /* ! __CC_SUPPORTS___INLINE */ 217 #endif /* !__cplusplus */ 218 219 #else /* !(__STDC__ || __cplusplus) */ 220 #define __P(protos) () /* traditional C preprocessor */ 221 #define __CONCAT(x,y) x/**/y 222 #define __STRING(x) "x" 223 224 #if !defined(__CC_SUPPORTS___INLINE) 225 #define __const /* delete pseudo-ANSI C keywords */ 226 #define __inline 227 #define __signed 228 #define __volatile 229 /* 230 * In non-ANSI C environments, new programs will want ANSI-only C keywords 231 * deleted from the program and old programs will want them left alone. 232 * When using a compiler other than gcc, programs using the ANSI C keywords 233 * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS. 234 * When using "gcc -traditional", we assume that this is the intent; if 235 * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone. 236 */ 237 #ifndef NO_ANSI_KEYWORDS 238 #define const /* delete ANSI C keywords */ 239 #define inline 240 #define signed 241 #define volatile 242 #endif /* !NO_ANSI_KEYWORDS */ 243 #endif /* !__CC_SUPPORTS___INLINE */ 244 #endif /* !(__STDC__ || __cplusplus) */ 245 246 /* 247 * Compiler-dependent macros to help declare dead (non-returning) and 248 * pure (no side effects) functions, and unused variables. They are 249 * null except for versions of gcc that are known to support the features 250 * properly (old versions of gcc-2 supported the dead and pure features 251 * in a different (wrong) way). If we do not provide an implementation 252 * for a given compiler, let the compile fail if it is told to use 253 * a feature that we cannot live without. 254 */ 255 #define __weak_symbol __attribute__((__weak__)) 256 #if !__GNUC_PREREQ__(2, 5) 257 #define __dead2 258 #define __pure2 259 #define __unused 260 #endif 261 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7 262 #define __dead2 __attribute__((__noreturn__)) 263 #define __pure2 __attribute__((__const__)) 264 #define __unused 265 /* XXX Find out what to do for __packed, __aligned and __section */ 266 #endif 267 #if __GNUC_PREREQ__(2, 7) 268 #define __dead2 __attribute__((__noreturn__)) 269 #define __pure2 __attribute__((__const__)) 270 #define __unused __attribute__((__unused__)) 271 #define __used __attribute__((__used__)) 272 #define __packed __attribute__((__packed__)) 273 #define __aligned(x) __attribute__((__aligned__(x))) 274 #define __section(x) __attribute__((__section__(x))) 275 #endif 276 277 #ifdef _HAVE_ALLOC_SIZE 278 #define __alloc_size(x) __attribute__((__alloc_size__(x))) 279 #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x))) 280 #else 281 #define __alloc_size(x) 282 #define __alloc_size2(n, x) 283 #endif 284 #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__) 285 #define __alloc_align(x) __attribute__((__alloc_align__(x))) 286 #else 287 #define __alloc_align(x) 288 #endif 289 290 #if !__GNUC_PREREQ__(2, 95) 291 #define __alignof(x) __offsetof(struct { char __a; x __b; }, __b) 292 #endif 293 294 #if __GNUC_PREREQ__(4,5) || defined(__clang__) 295 #define __picolibc_deprecated(m) __attribute__((__deprecated__(m))) 296 #else 297 #define __picolibc_deprecated(m) _ATTRIBUTE(__deprecated__) 298 #endif 299 300 /* 301 * Keywords added in C11. 302 */ 303 304 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L 305 306 #if !__has_extension(c_alignas) 307 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \ 308 __has_extension(cxx_alignas) 309 #define _Alignas(x) alignas(x) 310 #else 311 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */ 312 #define _Alignas(x) __aligned(x) 313 #endif 314 #endif 315 316 #if defined(__cplusplus) && __cplusplus >= 201103L 317 #define _Alignof(x) alignof(x) 318 #else 319 #define _Alignof(x) __alignof(x) 320 #endif 321 322 #if !defined(__cplusplus) && !__has_extension(c_atomic) && \ 323 !__has_extension(cxx_atomic) && !__GNUC_PREREQ__(4, 7) 324 /* 325 * No native support for _Atomic(). Place object in structure to prevent 326 * most forms of direct non-atomic access. 327 */ 328 #define _Atomic(T) struct { T volatile __val; } 329 #endif 330 331 #if defined(__cplusplus) && __cplusplus >= 201103L 332 #define _Noreturn [[noreturn]] 333 #else 334 #define _Noreturn __dead2 335 #endif 336 337 #if !__has_extension(c_static_assert) 338 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \ 339 __has_extension(cxx_static_assert) 340 #define _Static_assert(x, y) static_assert(x, y) 341 #elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus) 342 /* Nothing, gcc 4.6 and higher has _Static_assert built-in */ 343 #elif defined(__COUNTER__) 344 #define _Static_assert(x, y) __Static_assert(x, __COUNTER__) 345 #define __Static_assert(x, y) ___Static_assert(x, y) 346 #define ___Static_assert(x, y) typedef char __assert_ ## y[(x) ? 1 : -1] \ 347 __unused 348 #else 349 #define _Static_assert(x, y) struct __hack 350 #endif 351 #endif 352 353 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */ 354 355 /* 356 * Emulation of C11 _Generic(). Unlike the previously defined C11 357 * keywords, it is not possible to implement this using exactly the same 358 * syntax. Therefore implement something similar under the name 359 * __generic(). Unlike _Generic(), this macro can only distinguish 360 * between a single type, so it requires nested invocations to 361 * distinguish multiple cases. 362 * 363 * Note that the comma operator is used to force expr to decay in 364 * order to match _Generic(). 365 */ 366 367 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ 368 __has_extension(c_generic_selections) 369 #define __generic(expr, t, yes, no) \ 370 _Generic(expr, t: yes, default: no) 371 #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus) 372 #define __generic(expr, t, yes, no) \ 373 __builtin_choose_expr( \ 374 __builtin_types_compatible_p(__typeof((0, (expr))), t), yes, no) 375 #endif 376 377 /* 378 * C99 Static array indices in function parameter declarations. Syntax such as: 379 * void bar(int myArray[static 10]); 380 * is allowed in C99 but not in C++. Define __min_size appropriately so 381 * headers using it can be compiled in either language. Use like this: 382 * void bar(int myArray[__min_size(10)]); 383 */ 384 #if !defined(__cplusplus) && \ 385 (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \ 386 (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901)) 387 #define __min_size(x) static (x) 388 #else 389 #define __min_size(x) (x) 390 #endif 391 392 #if __GNUC_PREREQ__(2, 96) 393 # if __GNUC_PREREQ__(11, 0) 394 # define __malloc_like_with_free(_f,_a) __attribute__((__malloc__, __malloc__(_f,_a))) 395 # else 396 # define __malloc_like_with_free(_f,_a) __attribute__((__malloc__)) 397 # endif 398 # define __pure __attribute__((__pure__)) 399 #else 400 #define __malloc_like_with_free(free,free_arg) 401 #define __pure 402 #endif 403 404 #define __malloc_like __malloc_like_with_free(free, 1) 405 406 #ifndef __always_inline 407 #if __GNUC_PREREQ__(3, 1) 408 #define __always_inline __inline__ __attribute__((__always_inline__)) 409 #else 410 #define __always_inline 411 #endif 412 #endif 413 414 #ifndef __noinline 415 #if __GNUC_PREREQ__(3, 1) 416 #define __noinline __attribute__ ((__noinline__)) 417 #else 418 #define __noinline 419 #endif 420 #endif 421 422 #if defined(_HAVE_ATTRIBUTE_ALWAYS_INLINE) && defined(_HAVE_ATTRIBUTE_GNU_INLINE) 423 /* 424 * When this macro is defined, use it to declare inline versions of extern functions. 425 */ 426 #define __declare_extern_inline(type) extern __inline type __attribute((gnu_inline, always_inline)) 427 #endif 428 429 #if defined(__clang__) && defined(__nonnull) 430 /* Clang has a builtin macro __nonnull for the _Nonnull qualifier */ 431 #undef __nonnull 432 #endif 433 #if __GNUC_PREREQ__(3, 3) 434 #define __nonnull(x) __attribute__((__nonnull__ x)) 435 #define __nonnull_all __attribute__((__nonnull__)) 436 #else 437 #define __nonnull(x) 438 #define __nonnull_all 439 #endif 440 441 #if __GNUC_PREREQ__(3, 4) 442 #define __fastcall __attribute__((__fastcall__)) 443 #define __result_use_check __attribute__((__warn_unused_result__)) 444 #else 445 #define __fastcall 446 #define __result_use_check 447 #endif 448 449 #if __GNUC_PREREQ__(4, 1) 450 #define __returns_twice __attribute__((__returns_twice__)) 451 #else 452 #define __returns_twice 453 #endif 454 455 #if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable) 456 #define __unreachable() __builtin_unreachable() 457 #else 458 #define __unreachable() ((void)0) 459 #endif 460 461 /* XXX: should use `#if __STDC_VERSION__ < 199901'. */ 462 #if !__GNUC_PREREQ__(2, 7) && !defined(__COMPCERT__) 463 #define __func__ NULL 464 #endif 465 466 /* 467 * We use `__restrict' as a way to define the `restrict' type qualifier 468 * without disturbing older software that is unaware of C99 keywords. 469 * GCC also provides `__restrict' as an extension to support C99-style 470 * restricted pointers in other language modes. 471 */ 472 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 473 #define __restrict restrict 474 #elif !__GNUC_PREREQ__(2, 95) 475 #define __restrict 476 #endif 477 478 /* 479 * Additionally, we allow to use `__restrict_arr' for declaring arrays as 480 * non-overlapping per C99. That's supported since gcc 3.1, but it's not 481 * allowed in C++. 482 */ 483 #if defined(__cplusplus) || !__GNUC_PREREQ__(3, 1) 484 #define __restrict_arr 485 #elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L 486 #define __restrict_arr restrict 487 #else 488 #define __restrict_arr 489 #endif 490 491 /* 492 * GNU C version 2.96 adds explicit branch prediction so that 493 * the CPU back-end can hint the processor and also so that 494 * code blocks can be reordered such that the predicted path 495 * sees a more linear flow, thus improving cache behavior, etc. 496 * 497 * The following two macros provide us with a way to utilize this 498 * compiler feature. Use __predict_true() if you expect the expression 499 * to evaluate to true, and __predict_false() if you expect the 500 * expression to evaluate to false. 501 * 502 * A few notes about usage: 503 * 504 * * Generally, __predict_false() error condition checks (unless 505 * you have some _strong_ reason to do otherwise, in which case 506 * document it), and/or __predict_true() `no-error' condition 507 * checks, assuming you want to optimize for the no-error case. 508 * 509 * * Other than that, if you don't know the likelihood of a test 510 * succeeding from empirical or other `hard' evidence, don't 511 * make predictions. 512 * 513 * * These are meant to be used in places that are run `a lot'. 514 * It is wasteful to make predictions in code that is run 515 * seldomly (e.g. at subsystem initialization time) as the 516 * basic block reordering that this affects can often generate 517 * larger code. 518 */ 519 #if __GNUC_PREREQ__(2, 96) 520 #define __predict_true(exp) __builtin_expect((exp), 1) 521 #define __predict_false(exp) __builtin_expect((exp), 0) 522 #else 523 #define __predict_true(exp) (exp) 524 #define __predict_false(exp) (exp) 525 #endif 526 527 #if __GNUC_PREREQ__(4, 0) 528 #define __null_sentinel __attribute__((__sentinel__)) 529 #define __exported __attribute__((__visibility__("default"))) 530 /* Only default visibility is supported on PE/COFF targets. */ 531 #define __hidden __attribute__((__visibility__("hidden"))) 532 #else 533 #define __null_sentinel 534 #define __exported 535 #define __hidden 536 #endif 537 538 #define __offsetof(type, field) offsetof(type, field) 539 #define __rangeof(type, start, end) \ 540 (__offsetof(type, end) - __offsetof(type, start)) 541 542 /* 543 * Given the pointer x to the member m of the struct s, return 544 * a pointer to the containing structure. When using GCC, we first 545 * assign pointer x to a local variable, to check that its type is 546 * compatible with member m. 547 */ 548 #if __GNUC_PREREQ__(3, 1) 549 #define __containerof(x, s, m) ({ \ 550 const volatile __typeof(((s *)0)->m) *__x = (x); \ 551 __DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\ 552 }) 553 #else 554 #define __containerof(x, s, m) \ 555 __DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m)) 556 #endif 557 558 /* 559 * Compiler-dependent macros to declare that functions take printf-like 560 * or scanf-like arguments. They are null except for versions of gcc 561 * that are known to support the features properly (old versions of gcc-2 562 * didn't permit keeping the keywords out of the application namespace). 563 */ 564 #if !__GNUC_PREREQ__(2, 7) 565 #define __printflike(fmtarg, firstvararg) 566 #define __scanflike(fmtarg, firstvararg) 567 #define __format_arg(fmtarg) 568 #define __strfmonlike(fmtarg, firstvararg) 569 #define __strftimelike(fmtarg, firstvararg) 570 #else 571 #define __printflike(fmtarg, firstvararg) \ 572 __attribute__((__format__ (__printf__, fmtarg, firstvararg))) 573 #define __scanflike(fmtarg, firstvararg) \ 574 __attribute__((__format__ (__scanf__, fmtarg, firstvararg))) 575 #define __format_arg(fmtarg) __attribute__((__format_arg__ (fmtarg))) 576 #define __strfmonlike(fmtarg, firstvararg) \ 577 __attribute__((__format__ (__strfmon__, fmtarg, firstvararg))) 578 #define __strftimelike(fmtarg, firstvararg) \ 579 __attribute__((__format__ (__strftime__, fmtarg, firstvararg))) 580 #endif 581 582 /* Compiler-dependent macros that rely on FreeBSD-specific extensions. */ 583 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \ 584 defined(__GNUC__) 585 #define __printf0like(fmtarg, firstvararg) \ 586 __attribute__((__format__ (__printf0__, fmtarg, firstvararg))) 587 #else 588 #define __printf0like(fmtarg, firstvararg) 589 #endif 590 591 #if defined(_HAVE_ALIAS_ATTRIBUTE) 592 #define __strong_reference(sym,aliassym) \ 593 extern __typeof (sym) aliassym __attribute__ ((__alias__ (__STRING(sym)))) 594 #endif 595 596 #if defined(_HAVE_WEAK_ATTRIBUTE) && defined(_HAVE_ALIAS_ATTRIBUTE) 597 #define __weak_reference(sym,aliassym) \ 598 extern __typeof (sym) aliassym __attribute__ ((__weak__, __alias__ (__STRING(sym)))) 599 #endif 600 601 /* 602 Taken from glibc: 603 Add the compiler optimization to inhibit loop transformation to library 604 calls. This is used to avoid recursive calls in memset and memmove 605 default implementations. 606 */ 607 #if defined(_HAVE_CC_INHIBIT_LOOP_TO_LIBCALL) 608 # define __inhibit_loop_to_libcall \ 609 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns"))) 610 #elif defined(_HAVE_NO_BUILTIN_ATTRIBUTE) 611 # define __inhibit_loop_to_libcall \ 612 __attribute__((no_builtin)) 613 #else 614 # define __inhibit_loop_to_libcall 615 #endif 616 617 /* 618 * Tell optimizer to not add new builtin calls when optimizing a 619 * function body. 620 */ 621 #if defined(_HAVE_NO_BUILTIN_ATTRIBUTE) 622 # define __inhibit_new_builtin_calls \ 623 __attribute__((no_builtin)) 624 #else 625 #define __inhibit_new_builtin_calls 626 #endif 627 628 #if defined(__GNUC__) 629 #ifndef __weak_reference 630 #ifdef __ELF__ 631 #ifdef __STDC__ 632 #define __weak_reference(sym,alias) \ 633 __asm__(".weak " #alias); \ 634 __asm__(".equ " #alias ", " #sym) 635 #else 636 #define __weak_reference(sym,alias) \ 637 __asm__(".weak alias"); \ 638 __asm__(".equ alias, sym") 639 #endif 640 #elif __clang__ 641 #ifdef __MACH__ 642 /* Macos prefixes all C symbols with an underscore, this needs to be done manually in asm */ 643 644 /* So far I have not been able to create on Macos an exported symbol that itself 645 * is weak but aliases a strong symbol. A workaround is to make the original 646 * symbol weak and the alias symbol will automatically become weak too. */ 647 /* Hint: use `nm -m obj.o` to check the symbols weak/strong on Mac */ 648 #define __weak_reference(sym,alias) \ 649 __asm__(".weak_definition _" #sym); \ 650 __asm__(".globl _" #alias); \ 651 __asm__(".set _" #alias ", _" #sym) 652 #elif defined(__STDC__) 653 #define __weak_reference(sym,alias) \ 654 __asm__(".weak_reference " #alias); \ 655 __asm__(".globl " #alias); \ 656 __asm__(".set " #alias ", " #sym) 657 #else 658 #define __weak_reference(sym,alias) \ 659 __asm__(".weak_reference alias");\ 660 __asm__(".set alias, sym") 661 #endif 662 #else /* !__ELF__ && !__clang__ */ 663 #ifdef __STDC__ 664 #define __weak_reference(sym,alias) \ 665 __asm__(".stabs \"_" #alias "\",11,0,0,0"); \ 666 __asm__(".stabs \"_" #sym "\",1,0,0,0") 667 #else 668 #define __weak_reference(sym,alias) \ 669 __asm__(".stabs \"_/**/alias\",11,0,0,0"); \ 670 __asm__(".stabs \"_/**/sym\",1,0,0,0") 671 #endif 672 #endif 673 #endif 674 675 #endif /* __GNUC__ */ 676 677 #ifndef __DECONST 678 #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var)) 679 #endif 680 681 #ifndef __DEVOLATILE 682 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var)) 683 #endif 684 685 #ifndef __DEQUALIFY 686 #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var)) 687 #endif 688 689 /* 690 * Nullability qualifiers: currently only supported by Clang. 691 */ 692 #if !(defined(__clang__) && __has_feature(nullability)) 693 #define _Nonnull 694 #define _Nullable 695 #define _Null_unspecified 696 #define __NULLABILITY_PRAGMA_PUSH 697 #define __NULLABILITY_PRAGMA_POP 698 #else 699 #define __NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push") \ 700 _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"") 701 #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop") 702 #endif 703 704 /* 705 * Type Safety Checking 706 * 707 * Clang provides additional attributes to enable checking type safety 708 * properties that cannot be enforced by the C type system. 709 */ 710 711 #if __has_attribute(__argument_with_type_tag__) && \ 712 __has_attribute(__type_tag_for_datatype__) 713 #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx) \ 714 __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx))) 715 #define __datatype_type_tag(kind, type) \ 716 __attribute__((__type_tag_for_datatype__(kind, type))) 717 #else 718 #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx) 719 #define __datatype_type_tag(kind, type) 720 #endif 721 722 /* 723 * Lock annotations. 724 * 725 * Clang provides support for doing basic thread-safety tests at 726 * compile-time, by marking which locks will/should be held when 727 * entering/leaving a functions. 728 * 729 * Furthermore, it is also possible to annotate variables and structure 730 * members to enforce that they are only accessed when certain locks are 731 * held. 732 */ 733 734 #if __has_extension(c_thread_safety_attributes) 735 #define __lock_annotate(x) __attribute__((x)) 736 #else 737 #define __lock_annotate(x) 738 #endif 739 740 /* Structure implements a lock. */ 741 /* FIXME: Use __lockable__, etc. to avoid colliding with user namespace macros, 742 * once clang is fixed: https://bugs.llvm.org/show_bug.cgi?id=34319 */ 743 #define __lockable __lock_annotate(lockable) 744 745 /* Function acquires an exclusive or shared lock. */ 746 #define __locks_exclusive(...) \ 747 __lock_annotate(exclusive_lock_function(__VA_ARGS__)) 748 #define __locks_shared(...) \ 749 __lock_annotate(shared_lock_function(__VA_ARGS__)) 750 751 /* Function attempts to acquire an exclusive or shared lock. */ 752 #define __trylocks_exclusive(...) \ 753 __lock_annotate(exclusive_trylock_function(__VA_ARGS__)) 754 #define __trylocks_shared(...) \ 755 __lock_annotate(shared_trylock_function(__VA_ARGS__)) 756 757 /* Function releases a lock. */ 758 #define __unlocks(...) __lock_annotate(unlock_function(__VA_ARGS__)) 759 760 /* Function asserts that an exclusive or shared lock is held. */ 761 #define __asserts_exclusive(...) \ 762 __lock_annotate(assert_exclusive_lock(__VA_ARGS__)) 763 #define __asserts_shared(...) \ 764 __lock_annotate(assert_shared_lock(__VA_ARGS__)) 765 766 /* Function requires that an exclusive or shared lock is or is not held. */ 767 #define __requires_exclusive(...) \ 768 __lock_annotate(exclusive_locks_required(__VA_ARGS__)) 769 #define __requires_shared(...) \ 770 __lock_annotate(shared_locks_required(__VA_ARGS__)) 771 #define __requires_unlocked(...) \ 772 __lock_annotate(locks_excluded(__VA_ARGS__)) 773 774 /* Function should not be analyzed. */ 775 #define __no_lock_analysis __lock_annotate(no_thread_safety_analysis) 776 777 /* 778 * Function or variable should not be sanitized, e.g., by AddressSanitizer. 779 * GCC has the nosanitize attribute, but as a function attribute only, and 780 * warns on use as a variable attribute. 781 */ 782 #if __has_attribute(no_sanitize) && defined(__clang__) 783 #ifdef _KERNEL 784 #define __nosanitizeaddress __attribute__((no_sanitize("kernel-address"))) 785 #define __nosanitizememory __attribute__((no_sanitize("kernel-memory"))) 786 #else 787 #define __nosanitizeaddress __attribute__((no_sanitize("address"))) 788 #define __nosanitizememory __attribute__((no_sanitize("memory"))) 789 #endif 790 #define __nosanitizethread __attribute__((no_sanitize("thread"))) 791 #else 792 #define __nosanitizeaddress 793 #define __nosanitizememory 794 #define __nosanitizethread 795 #endif 796 797 /* 798 * fall-through case statement annotations 799 */ 800 #if __cplusplus >= 201703L || __STDC_VERSION__ > 201710L 801 /* Standard C++17/C23 attribute */ 802 #define __PICOLIBC_FALLTHROUGH [[fallthrough]] 803 #elif __has_attribute(fallthrough) 804 /* Non-standard but supported by at least gcc and clang */ 805 #define __PICOLIBC_FALLTHROUGH __attribute__((fallthrough)) 806 #else 807 #define __PICOLIBC_FALLTHROUGH do { } while(0) 808 #endif 809 810 /* Guard variables and structure members by lock. */ 811 #define __guarded_by(x) __lock_annotate(guarded_by(x)) 812 #define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x)) 813 814 /* Alignment builtins for better type checking and improved code generation. */ 815 /* Provide fallback versions for other compilers (GCC/Clang < 10): */ 816 #if !__has_builtin(__builtin_is_aligned) 817 #define __builtin_is_aligned(x, align) \ 818 (((__uintptr_t)x & ((align) - 1)) == 0) 819 #endif 820 #if !__has_builtin(__builtin_align_up) 821 #define __builtin_align_up(x, align) \ 822 ((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1)))) 823 #endif 824 #if !__has_builtin(__builtin_align_down) 825 #define __builtin_align_down(x, align) \ 826 ((__typeof__(x))((x)&(~((align)-1)))) 827 #endif 828 829 #define __align_up(x, y) __builtin_align_up(x, y) 830 #define __align_down(x, y) __builtin_align_down(x, y) 831 #define __is_aligned(x, y) __builtin_is_aligned(x, y) 832 833 #ifndef _LONG_DOUBLE 834 #define _LONG_DOUBLE long double 835 #endif 836 837 /* Support gcc's __attribute__ facility. */ 838 839 #ifdef __GNUC__ 840 #define _ATTRIBUTE(attrs) __attribute__ (attrs) 841 #else 842 #define _ATTRIBUTE(attrs) 843 #endif 844 845 /* The traditional meaning of 'extern inline' for GCC is not 846 to emit the function body unless the address is explicitly 847 taken. However this behaviour is changing to match the C99 848 standard, which uses 'extern inline' to indicate that the 849 function body *must* be emitted. Likewise, a function declared 850 without either 'extern' or 'static' defaults to extern linkage 851 (C99 6.2.2p5), and the compiler may choose whether to use the 852 inline version or call the extern linkage version (6.7.4p6). 853 If we are using GCC, but do not have the new behaviour, we need 854 to use extern inline; if we are using a new GCC with the 855 C99-compatible behaviour, or a non-GCC compiler (which we will 856 have to hope is C99, since there is no other way to achieve the 857 effect of omitting the function if it isn't referenced) we use 858 'static inline', which c99 defines to mean more-or-less the same 859 as the Gnu C 'extern inline'. */ 860 #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__) 861 /* We're using GCC, but without the new C99-compatible behaviour. */ 862 #define _ELIDABLE_INLINE extern __inline__ _ATTRIBUTE ((__always_inline__)) 863 #else 864 /* We're using GCC in C99 mode, or an unknown compiler which 865 we just have to hope obeys the C99 semantics of inline. */ 866 #define _ELIDABLE_INLINE static __inline__ 867 #endif 868 869 #if __GNUC_PREREQ (3, 1) 870 #define _NOINLINE __attribute__ ((__noinline__)) 871 #define _NOINLINE_STATIC _NOINLINE static 872 #else 873 /* On non-GNU compilers and GCC prior to version 3.1 the compiler can't be 874 trusted not to inline if it is static. */ 875 #define _NOINLINE 876 #define _NOINLINE_STATIC 877 #endif 878 879 #endif /* !_SYS_CDEFS_H_ */ 880