xref: /freebsd/tools/build/cross-build/include/common/sys/cdefs.h (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2018-2020 Alex Richardson <arichardson@FreeBSD.org>
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory (Department of Computer Science and
8  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9  * DARPA SSITH research programme.
10  *
11  * This software was developed by SRI International and the University of
12  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
13  * ("CTSRD"), as part of the DARPA CRASH research programme.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 #pragma once
37 /* musl libc does not provide a sys/cdefs.h header */
38 #if __has_include_next(<sys/cdefs.h>)
39 #include_next <sys/cdefs.h>
40 #else
41 /* No sys/cdefs.h header exists so we have to provide some basic macros */
42 #ifdef __cplusplus
43 #define __BEGIN_DECLS extern "C" {
44 #define __END_DECLS }
45 #else
46 #define __BEGIN_DECLS
47 #define __END_DECLS
48 #endif
49 
50 #endif
51 
52 #ifndef __FBSDID
53 #define __FBSDID(id)
54 #endif
55 
56 #ifndef __IDSTRING
57 #define __IDSTRING(name, string)
58 #endif
59 
60 #ifndef __pure
61 #define __pure __attribute__((__pure__))
62 #endif
63 #ifndef __packed
64 #define __packed __attribute__((__packed__))
65 #endif
66 #ifndef __dead2
67 #define __dead2 __attribute__((__noreturn__))
68 #endif
69 #ifndef __pure2
70 #define __pure2 __attribute__((__const__))
71 #endif
72 #ifndef __used
73 #define __used __attribute__((__used__))
74 #endif
75 #ifndef __aligned
76 #define __aligned(x) __attribute__((__aligned__(x)))
77 #endif
78 #ifndef __section
79 #define __section(x) __attribute__((__section__(x)))
80 #endif
81 
82 #ifndef __alloc_size
83 #define __alloc_size(...) __attribute__((__alloc_size__(__VA_ARGS__)))
84 #endif
85 #ifndef __alloc_size2
86 #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x)))
87 #endif
88 #ifndef __alloc_align
89 #define __alloc_align(x) __attribute__((__alloc_align__(x)))
90 #endif
91 #ifndef __result_use_check
92 #define __result_use_check __attribute__((__warn_unused_result__))
93 #endif
94 #ifndef __printflike
95 #define __printflike(fmtarg, firstvararg) \
96 	__attribute__((__format__(__printf__, fmtarg, firstvararg)))
97 #endif
98 #ifndef __printf0like
99 #define __printf0like(fmtarg, firstvararg) \
100 	__attribute__((__format__(__printf0__, fmtarg, firstvararg)))
101 #endif
102 
103 #ifndef __predict_true
104 #define __predict_true(exp) __builtin_expect((exp), 1)
105 #endif
106 #ifndef __predict_false
107 #define __predict_false(exp) __builtin_expect((exp), 0)
108 #endif
109 
110 #ifndef __weak_symbol
111 #define __weak_symbol __attribute__((__weak__))
112 #endif
113 #ifndef __weak_reference
114 #ifdef __ELF__
115 #define __weak_reference(sym, alias) \
116 	__asm__(".weak " #alias);    \
117 	__asm__(".equ " #alias ", " #sym)
118 #else
119 #define __weak_reference(sym, alias) \
120 	static int alias() __attribute__((weakref(#sym)));
121 #endif
122 #endif
123 
124 #ifndef __WEAK
125 #ifdef __ELF__
126 #define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
127 #endif
128 #endif
129 
130 /* Some files built as part of the bootstrap libegacy use these macros, but
131  * since we aren't actually building libc.so, we can defined them to be empty */
132 #ifndef __sym_compat
133 #define __sym_compat(sym, impl, verid) /* not needed for bootstrapping */
134 #endif
135 #ifndef __sym_default
136 #define __sym_default(sym, impl, verid) /* not needed for bootstrapping */
137 #endif
138 #ifndef __sym_default
139 #define __warn_references(sym, msg) /* not needed for bootstrapping */
140 #endif
141 
142 #ifndef __malloc_like
143 #define __malloc_like __attribute__((__malloc__))
144 #endif
145 
146 #ifndef __min_size
147 #if !defined(__cplusplus)
148 #define __min_size(x) static(x)
149 #else
150 #define __min_size(x) (x)
151 #endif
152 #endif
153 
154 #ifndef __unused
155 #define __unused __attribute__((unused))
156 #endif
157 #define __format_arg(fmtarg) __attribute__((__format_arg__(fmtarg)))
158 
159 #ifndef __exported
160 #define __exported __attribute__((__visibility__("default")))
161 #endif
162 #ifndef __hidden
163 #define __hidden __attribute__((__visibility__("hidden")))
164 #endif
165 
166 #ifndef __unreachable
167 #define __unreachable() __builtin_unreachable()
168 #endif
169 
170 #ifndef __clang__
171 /* GCC doesn't like the printf0 format specifier. Clang treats it the same as
172  * printf so add the compatibility macro here. */
173 #define __printf0__ __printf__
174 #endif
175 
176 /* On MacOS __CONCAT is defined as x ## y, which won't expand macros */
177 #undef __CONCAT
178 #define __CONCAT1(x, y) x##y
179 #define __CONCAT(x, y) __CONCAT1(x, y)
180 
181 #ifndef __STRING
182 #define __STRING(x) #x /* stringify without expanding x */
183 #endif
184 #ifndef __XSTRING
185 #define __XSTRING(x) __STRING(x) /* expand x, then stringify */
186 #endif
187 
188 #ifndef __has_feature
189 #define __has_feature(...) 0
190 #endif
191 
192 #ifndef __has_builtin
193 #define __has_builtin(...) 0
194 #endif
195 
196 /*
197  * Nullability qualifiers: currently only supported by Clang.
198  */
199 #if !(defined(__clang__) && __has_feature(nullability))
200 #define _Nonnull
201 #define _Nullable
202 #define _Null_unspecified
203 #define __NULLABILITY_PRAGMA_PUSH
204 #define __NULLABILITY_PRAGMA_POP
205 #else
206 #define __NULLABILITY_PRAGMA_PUSH        \
207 	_Pragma("clang diagnostic push") \
208 	    _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
209 #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
210 #endif
211 
212 #ifndef __offsetof
213 #define __offsetof(type, field) __builtin_offsetof(type, field)
214 #endif
215 
216 #define __rangeof(type, start, end) \
217 	(__offsetof(type, end) - __offsetof(type, start))
218 
219 #ifndef __containerof
220 #define __containerof(x, s, m)                                           \
221 	({                                                               \
222 		const volatile __typeof(((s *)0)->m) *__x = (x);         \
223 		__DEQUALIFY(                                             \
224 		    s *, (const volatile char *)__x - __offsetof(s, m)); \
225 	})
226 #endif
227 
228 #ifndef __RCSID
229 #define __RCSID(x)
230 #endif
231 #ifndef __FBSDID
232 #define __FBSDID(x)
233 #endif
234 #ifndef __RCSID
235 #define __RCSID(x)
236 #endif
237 #ifndef __RCSID_SOURCE
238 #define __RCSID_SOURCE(x)
239 #endif
240 #ifndef __SCCSID
241 #define __SCCSID(x)
242 #endif
243 #ifndef __COPYRIGHT
244 #define __COPYRIGHT(x)
245 #endif
246 #ifndef __DECONST
247 #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
248 #endif
249 
250 #ifndef __DEVOLATILE
251 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
252 #endif
253 
254 #ifndef __DEQUALIFY
255 #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
256 #endif
257 
258 #ifndef __nosanitizeaddress
259 #if __has_attribute(no_sanitize) && defined(__clang__)
260 #define __nosanitizeaddress	__attribute__((no_sanitize("address")))
261 #else
262 #define __nosanitizeaddress
263 #endif
264 #endif
265 
266 /* Expose all declarations when using FreeBSD headers */
267 #define	__POSIX_VISIBLE		200809
268 #define	__XSI_VISIBLE		700
269 #define	__BSD_VISIBLE		1
270 #define	__ISO_C_VISIBLE		2011
271 #define	__EXT1_VISIBLE		1
272 
273 /* Alignment builtins for better type checking and improved code generation. */
274 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
275 #if !__has_builtin(__builtin_is_aligned)
276 #define __builtin_is_aligned(x, align)	\
277 	(((__uintptr_t)x & ((align) - 1)) == 0)
278 #endif
279 #if !__has_builtin(__builtin_align_up)
280 #define __builtin_align_up(x, align)	\
281 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
282 #endif
283 #if !__has_builtin(__builtin_align_down)
284 #define __builtin_align_down(x, align)	\
285 	((__typeof__(x))((x)&(~((align)-1))))
286 #endif
287 
288 #define __align_up(x, y) __builtin_align_up(x, y)
289 #define __align_down(x, y) __builtin_align_down(x, y)
290 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
291