fortify-headers.h (2745B)
1 /* 2 * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org> 3 * Copyright (C) 2022 q66 <q66@chimera-linux.org> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef _FORTIFY_HEADERS_H 18 #define _FORTIFY_HEADERS_H 19 20 #ifdef __clang__ 21 22 #if _FORTIFY_SOURCE > 2 && defined __has_attribute && __has_attribute(pass_dynamic_object_size) 23 #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n))) 24 #else 25 /* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */ 26 #define _FORTIFY_POSN(n) const __attribute__((pass_object_size__(n))) 27 #endif 28 29 /* we can't use extern inline with overloads without making them external */ 30 #ifdef __cplusplus 31 #define _FORTIFY_INLINE __inline__ \ 32 __attribute__((__always_inline__,__artificial__,__overloadable__)) 33 #else 34 #define _FORTIFY_INLINE static __inline__ \ 35 __attribute__((__always_inline__,__artificial__,__overloadable__)) 36 #endif 37 38 #else /* !__clang__ */ 39 40 #define _FORTIFY_POSN(n) 41 #define _FORTIFY_INLINE extern __inline__ \ 42 __attribute__((__always_inline__,__gnu_inline__,__artificial__)) 43 44 #endif /* __clang__ */ 45 46 #define _FORTIFY_POS0 _FORTIFY_POSN(0) 47 #define _FORTIFY_POS1 _FORTIFY_POSN(1) 48 #define _FORTIFY_POS2 _FORTIFY_POSN(2) 49 50 #define _FORTIFY_STR(s) #s 51 #define _FORTIFY_ORIG(p,fn) __typeof__(fn) __orig_##fn __asm__(_FORTIFY_STR(p) #fn) 52 #define _FORTIFY_FNB(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn) 53 #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE 54 55 56 #if _FORTIFY_SOURCE > 2 && defined __has_builtin && __has_builtin (__builtin_dynamic_object_size) 57 #define __bos(ptr, type) __builtin_dynamic_object_size (ptr, type) 58 #else 59 #define __bos(ptr, type) __builtin_object_size (ptr, type) 60 #endif 61 62 #if defined __has_attribute 63 64 #if __has_attribute (access) 65 #define __access(...) __attribute__ ((access (__VA_ARGS__))) 66 #else 67 #define __access(...) 68 #endif 69 70 #if __has_attribute (format) 71 #define __format(...) __attribute__ ((format (__VA_ARGS__))) 72 #else 73 #define __format(...) 74 #endif 75 76 #if __has_attribute (__diagnose_if) 77 #define __warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) 78 #else 79 #define __warning_if(cond, msg) 80 #endif 81 82 #endif 83 84 #endif