nameif: add matching by phy_address=NUM; extend help text
[oweals/busybox.git] / include / platform.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright 2006, Bernhard Reutner-Fischer
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 #ifndef BB_PLATFORM_H
8 #define BB_PLATFORM_H 1
9
10 /* Assume all these functions exist by default.  Platforms where it is not
11  * true will #undef them below.
12  */
13 #define HAVE_CLEARENV 1
14 #define HAVE_FDATASYNC 1
15 #define HAVE_FDPRINTF 1
16 #define HAVE_MEMRCHR 1
17 #define HAVE_MKDTEMP 1
18 #define HAVE_PTSNAME_R 1
19 #define HAVE_SETBIT 1
20 #define HAVE_SIGHANDLER_T 1
21 #define HAVE_STPCPY 1
22 #define HAVE_STRCASESTR 1
23 #define HAVE_STRCHRNUL 1
24 #define HAVE_STRSEP 1
25 #define HAVE_STRSIGNAL 1
26 #define HAVE_VASPRINTF 1
27
28 /* Convenience macros to test the version of gcc. */
29 #undef __GNUC_PREREQ
30 #if defined __GNUC__ && defined __GNUC_MINOR__
31 # define __GNUC_PREREQ(maj, min) \
32                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
33 #else
34 # define __GNUC_PREREQ(maj, min) 0
35 #endif
36
37 /* __restrict is known in EGCS 1.2 and above. */
38 #if !__GNUC_PREREQ(2,92)
39 # ifndef __restrict
40 #  define __restrict
41 # endif
42 #endif
43
44 /* Define macros for some gcc attributes.  This permits us to use the
45    macros freely, and know that they will come into play for the
46    version of gcc in which they are supported.  */
47
48 #if !__GNUC_PREREQ(2,7)
49 # ifndef __attribute__
50 #  define __attribute__(x)
51 # endif
52 #endif
53
54 #undef inline
55 #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
56 /* it's a keyword */
57 #elif __GNUC_PREREQ(2,7)
58 # define inline __inline__
59 #else
60 # define inline
61 #endif
62
63 #ifndef __const
64 # define __const const
65 #endif
66
67 #define UNUSED_PARAM __attribute__ ((__unused__))
68 #define NORETURN __attribute__ ((__noreturn__))
69 /* "The malloc attribute is used to tell the compiler that a function
70  * may be treated as if any non-NULL pointer it returns cannot alias
71  * any other pointer valid when the function returns. This will often
72  * improve optimization. Standard functions with this property include
73  * malloc and calloc. realloc-like functions have this property as long
74  * as the old pointer is never referred to (including comparing it
75  * to the new pointer) after the function returns a non-NULL value."
76  */
77 #define RETURNS_MALLOC __attribute__ ((malloc))
78 #define PACKED __attribute__ ((__packed__))
79 #define ALIGNED(m) __attribute__ ((__aligned__(m)))
80
81 /* __NO_INLINE__: some gcc's do not honor inlining! :( */
82 #if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
83 # define ALWAYS_INLINE __attribute__ ((always_inline)) inline
84 /* I've seen a toolchain where I needed __noinline__ instead of noinline */
85 # define NOINLINE      __attribute__((__noinline__))
86 # if !ENABLE_WERROR
87 #  define DEPRECATED __attribute__ ((__deprecated__))
88 #  define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
89 # else
90 #  define DEPRECATED
91 #  define UNUSED_PARAM_RESULT
92 # endif
93 #else
94 # define ALWAYS_INLINE inline
95 # define NOINLINE
96 # define DEPRECATED
97 # define UNUSED_PARAM_RESULT
98 #endif
99
100 /* -fwhole-program makes all symbols local. The attribute externally_visible
101    forces a symbol global.  */
102 #if __GNUC_PREREQ(4,1)
103 # define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
104 //__attribute__ ((__externally_visible__))
105 #else
106 # define EXTERNALLY_VISIBLE
107 #endif
108
109 /* At 4.4 gcc become much more anal about this, need to use "aliased" types */
110 #if __GNUC_PREREQ(4,4)
111 # define FIX_ALIASING __attribute__((__may_alias__))
112 #else
113 # define FIX_ALIASING
114 #endif
115
116 /* We use __extension__ in some places to suppress -pedantic warnings
117    about GCC extensions.  This feature didn't work properly before
118    gcc 2.8.  */
119 #if !__GNUC_PREREQ(2,8)
120 # ifndef __extension__
121 #  define __extension__
122 # endif
123 #endif
124
125 /* gcc-2.95 had no va_copy but only __va_copy. */
126 #if !__GNUC_PREREQ(3,0)
127 # include <stdarg.h>
128 # if !defined va_copy && defined __va_copy
129 #  define va_copy(d,s) __va_copy((d),(s))
130 # endif
131 #endif
132
133 /* FAST_FUNC is a qualifier which (possibly) makes function call faster
134  * and/or smaller by using modified ABI. It is usually only needed
135  * on non-static, busybox internal functions. Recent versions of gcc
136  * optimize statics automatically. FAST_FUNC on static is required
137  * only if you need to match a function pointer's type */
138 #if __GNUC_PREREQ(3,0) && defined(i386) /* || defined(__x86_64__)? */
139 /* stdcall makes callee to pop arguments from stack, not caller */
140 # define FAST_FUNC __attribute__((regparm(3),stdcall))
141 /* #elif ... - add your favorite arch today! */
142 #else
143 # define FAST_FUNC
144 #endif
145
146 /* Make all declarations hidden (-fvisibility flag only affects definitions) */
147 /* (don't include system headers after this until corresponding pop!) */
148 #if __GNUC_PREREQ(4,1)
149 # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
150 # define POP_SAVED_FUNCTION_VISIBILITY              _Pragma("GCC visibility pop")
151 #else
152 # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
153 # define POP_SAVED_FUNCTION_VISIBILITY
154 #endif
155
156 /* ---- Endian Detection ------------------------------------ */
157
158 #include <limits.h>
159 #if defined(__digital__) && defined(__unix__)
160 # include <sex.h>
161 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
162    || defined(__APPLE__)
163 # include <sys/resource.h>  /* rlimit */
164 # include <machine/endian.h>
165 # define bswap_64 __bswap64
166 # define bswap_32 __bswap32
167 # define bswap_16 __bswap16
168 #else
169 # include <byteswap.h>
170 # include <endian.h>
171 #endif
172
173 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
174 # define BB_BIG_ENDIAN 1
175 # define BB_LITTLE_ENDIAN 0
176 #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
177 # define BB_BIG_ENDIAN 0
178 # define BB_LITTLE_ENDIAN 1
179 #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
180 # define BB_BIG_ENDIAN 1
181 # define BB_LITTLE_ENDIAN 0
182 #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
183 # define BB_BIG_ENDIAN 0
184 # define BB_LITTLE_ENDIAN 1
185 #elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
186 # define BB_BIG_ENDIAN 1
187 # define BB_LITTLE_ENDIAN 0
188 #elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
189 # define BB_BIG_ENDIAN 0
190 # define BB_LITTLE_ENDIAN 1
191 #elif defined(__386__)
192 # define BB_BIG_ENDIAN 0
193 # define BB_LITTLE_ENDIAN 1
194 #else
195 # error "Can't determine endianness"
196 #endif
197
198 #if ULONG_MAX > 0xffffffff
199 # define bb_bswap_64(x) bswap_64(x)
200 #endif
201
202 /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
203 #if BB_BIG_ENDIAN
204 # define SWAP_BE16(x) (x)
205 # define SWAP_BE32(x) (x)
206 # define SWAP_BE64(x) (x)
207 # define SWAP_LE16(x) bswap_16(x)
208 # define SWAP_LE32(x) bswap_32(x)
209 # define SWAP_LE64(x) bb_bswap_64(x)
210 # define IF_BIG_ENDIAN(...) __VA_ARGS__
211 # define IF_LITTLE_ENDIAN(...)
212 #else
213 # define SWAP_BE16(x) bswap_16(x)
214 # define SWAP_BE32(x) bswap_32(x)
215 # define SWAP_BE64(x) bb_bswap_64(x)
216 # define SWAP_LE16(x) (x)
217 # define SWAP_LE32(x) (x)
218 # define SWAP_LE64(x) (x)
219 # define IF_BIG_ENDIAN(...)
220 # define IF_LITTLE_ENDIAN(...) __VA_ARGS__
221 #endif
222
223 /* ---- Unaligned access ------------------------------------ */
224
225 #include <stdint.h>
226 typedef int      bb__aliased_int      FIX_ALIASING;
227 typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
228 typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
229
230 /* NB: unaligned parameter should be a pointer, aligned one -
231  * a lvalue. This makes it more likely to not swap them by mistake
232  */
233 #if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
234 # define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
235 # define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
236 # define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
237 # define move_to_unaligned16(u16p, v)   (*(bb__aliased_uint16_t*)(u16p) = (v))
238 # define move_to_unaligned32(u32p, v)   (*(bb__aliased_uint32_t*)(u32p) = (v))
239 /* #elif ... - add your favorite arch today! */
240 #else
241 /* performs reasonably well (gcc usually inlines memcpy here) */
242 # define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
243 # define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
244 # define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
245 # define move_to_unaligned16(u16p, v) do { \
246         uint16_t __t = (v); \
247         memcpy((u16p), &__t, 4); \
248 } while (0)
249 # define move_to_unaligned32(u32p, v) do { \
250         uint32_t __t = (v); \
251         memcpy((u32p), &__t, 4); \
252 } while (0)
253 #endif
254
255 /* ---- Compiler dependent settings ------------------------- */
256
257 #if (defined __digital__ && defined __unix__) \
258  || defined __APPLE__ \
259  || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
260 # undef HAVE_CLEARENV
261 # undef HAVE_FDATASYNC
262 # undef HAVE_MNTENT_H
263 # undef HAVE_PTSNAME_R
264 # undef HAVE_SYS_STATFS_H
265 # undef HAVE_SIGHANDLER_T
266 # undef HAVE_XTABS
267 # undef HAVE_FDPRINTF
268 #else
269 # define HAVE_MNTENT_H 1
270 # define HAVE_SYS_STATFS_H 1
271 # define HAVE_XTABS 1
272 #endif
273
274 /*----- Kernel versioning ------------------------------------*/
275
276 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
277
278 /* ---- Miscellaneous --------------------------------------- */
279
280 #if defined __GLIBC__ || defined __UCLIBC__ \
281  || defined __dietlibc__ || defined _NEWLIB_VERSION
282 # include <features.h>
283 #endif
284
285 /* Size-saving "small" ints (arch-dependent) */
286 #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
287 /* add other arches which benefit from this... */
288 typedef signed char smallint;
289 typedef unsigned char smalluint;
290 #else
291 /* for arches where byte accesses generate larger code: */
292 typedef int smallint;
293 typedef unsigned smalluint;
294 #endif
295
296 /* ISO C Standard:  7.16  Boolean type and values  <stdbool.h> */
297 #if (defined __digital__ && defined __unix__)
298 /* old system without (proper) C99 support */
299 # define bool smalluint
300 #else
301 /* modern system, so use it */
302 # include <stdbool.h>
303 #endif
304
305 /* Try to defeat gcc's alignment of "char message[]"-like data */
306 #if 1 /* if needed: !defined(arch1) && !defined(arch2) */
307 # define ALIGN1 __attribute__((aligned(1)))
308 # define ALIGN2 __attribute__((aligned(2)))
309 # define ALIGN4 __attribute__((aligned(4)))
310 #else
311 /* Arches which MUST have 2 or 4 byte alignment for everything are here */
312 # define ALIGN1
313 # define ALIGN2
314 # define ALIGN4
315 #endif
316
317
318 /* uclibc does not implement daemon() for no-mmu systems.
319  * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
320  * For earlier versions there is no reliable way to check if we are building
321  * for a mmu-less system.
322  */
323 #if ENABLE_NOMMU || \
324     (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
325     __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__)
326 # define BB_MMU 0
327 # define USE_FOR_NOMMU(...) __VA_ARGS__
328 # define USE_FOR_MMU(...)
329 #else
330 # define BB_MMU 1
331 # define USE_FOR_NOMMU(...)
332 # define USE_FOR_MMU(...) __VA_ARGS__
333 #endif
334
335 /* Don't use lchown with glibc older than 2.1.x */
336 #if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1
337 # define lchown chown
338 #endif
339
340 #if defined(__digital__) && defined(__unix__)
341
342 # include <standards.h>
343 # include <inttypes.h>
344 # define PRIu32 "u"
345 /* use legacy setpgrp(pid_t,pid_t) for now.  move to platform.c */
346 # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
347 # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
348 #  define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
349 # endif
350 # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
351 #  define ADJ_FREQUENCY MOD_FREQUENCY
352 # endif
353 # if !defined ADJ_TIMECONST && defined MOD_TIMECONST
354 #  define ADJ_TIMECONST MOD_TIMECONST
355 # endif
356 # if !defined ADJ_TICK && defined MOD_CLKB
357 #  define ADJ_TICK MOD_CLKB
358 # endif
359
360 # undef HAVE_STPCPY
361
362 #else
363
364 # define bb_setpgrp() setpgrp()
365
366 #endif
367
368 #include <unistd.h>
369 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || defined(__GLIBC__)
370 # define fdprintf dprintf
371 #endif
372
373 #if defined(__dietlibc__)
374 # undef HAVE_STRCHRNUL
375 #endif
376
377 #if defined(__WATCOMC__)
378 # undef HAVE_FDPRINTF
379 # undef HAVE_MEMRCHR
380 # undef HAVE_MKDTEMP
381 # undef HAVE_SETBIT
382 # undef HAVE_STPCPY
383 # undef HAVE_STRCASESTR
384 # undef HAVE_STRCHRNUL
385 # undef HAVE_STRSEP
386 # undef HAVE_STRSIGNAL
387 # undef HAVE_VASPRINTF
388 #endif
389
390 #if defined(__FreeBSD__)
391 # undef HAVE_STRCHRNUL
392 #endif
393
394 /*
395  * Now, define prototypes for all the functions defined in platform.c
396  * These must come after all the HAVE_* macros are defined (or not)
397  */
398
399 #ifndef HAVE_FDPRINTF
400 extern int fdprintf(int fd, const char *format, ...);
401 #endif
402
403 #ifndef HAVE_MEMRCHR
404 extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
405 #endif
406
407 #ifndef HAVE_MKDTEMP
408 extern char *mkdtemp(char *template) FAST_FUNC;
409 #endif
410
411 #ifndef HAVE_SETBIT
412 # define setbit(a, b)  ((a)[(b) >> 3] |= 1 << ((b) & 7))
413 # define clrbit(a, b)  ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
414 #endif
415
416 #ifndef HAVE_SIGHANDLER_T
417 typedef void (*sighandler_t)(int);
418 #endif
419
420 #ifndef HAVE_STPCPY
421 extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
422 #endif
423
424 #ifndef HAVE_STRCASESTR
425 extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
426 #endif
427
428 #ifndef HAVE_STRCHRNUL
429 extern char *strchrnul(const char *s, int c) FAST_FUNC;
430 #endif
431
432 #ifndef HAVE_STRSEP
433 extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
434 #endif
435
436 #ifndef HAVE_STRSIGNAL
437 /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
438 # define strsignal(sig) get_signame(sig)
439 #endif
440
441 #ifndef HAVE_VASPRINTF
442 extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
443 #endif
444
445 #endif