1 /* vi: set sw=4 ts=4: */
3 * Copyright 2006, Bernhard Reutner-Fischer
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 #define BB_PLATFORM_H 1
10 /* Assume all these functions exist by default. Platforms where it is not
11 * true will #undef them below.
13 #define HAVE_CLEARENV 1
14 #define HAVE_FDATASYNC 1
15 #define HAVE_DPRINTF 1
16 #define HAVE_MEMRCHR 1
17 #define HAVE_MKDTEMP 1
18 #define HAVE_PTSNAME_R 1
20 #define HAVE_SIGHANDLER_T 1
22 #define HAVE_STRCASESTR 1
23 #define HAVE_STRCHRNUL 1
25 #define HAVE_STRSIGNAL 1
26 #define HAVE_VASPRINTF 1
28 /* Convenience macros to test the version of gcc. */
30 #if defined __GNUC__ && defined __GNUC_MINOR__
31 # define __GNUC_PREREQ(maj, min) \
32 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
34 # define __GNUC_PREREQ(maj, min) 0
37 /* __restrict is known in EGCS 1.2 and above. */
38 #if !__GNUC_PREREQ(2,92)
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. */
48 #if !__GNUC_PREREQ(2,7)
49 # ifndef __attribute__
50 # define __attribute__(x)
55 #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
57 #elif __GNUC_PREREQ(2,7)
58 # define inline __inline__
64 # define __const const
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."
77 #define RETURNS_MALLOC __attribute__ ((malloc))
78 #define PACKED __attribute__ ((__packed__))
79 #define ALIGNED(m) __attribute__ ((__aligned__(m)))
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__))
87 # define DEPRECATED __attribute__ ((__deprecated__))
88 # define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
91 # define UNUSED_PARAM_RESULT
94 # define ALWAYS_INLINE inline
97 # define UNUSED_PARAM_RESULT
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__))
106 # define EXTERNALLY_VISIBLE
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__))
113 # define FIX_ALIASING
116 /* We use __extension__ in some places to suppress -pedantic warnings
117 about GCC extensions. This feature didn't work properly before
119 #if !__GNUC_PREREQ(2,8)
120 # ifndef __extension__
121 # define __extension__
125 /* gcc-2.95 had no va_copy but only __va_copy. */
126 #if !__GNUC_PREREQ(3,0)
128 # if !defined va_copy && defined __va_copy
129 # define va_copy(d,s) __va_copy((d),(s))
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! */
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")
152 # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
153 # define POP_SAVED_FUNCTION_VISIBILITY
156 /* ---- Endian Detection ------------------------------------ */
159 #if defined(__digital__) && defined(__unix__)
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
169 # include <byteswap.h>
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
195 # error "Can't determine endianness"
198 #if ULONG_MAX > 0xffffffff
199 # define bb_bswap_64(x) bswap_64(x)
202 /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
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(...)
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__
223 /* ---- Unaligned access ------------------------------------ */
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;
230 /* NB: unaligned parameter should be a pointer, aligned one -
231 * a lvalue. This makes it more likely to not swap them by mistake
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! */
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); \
249 # define move_to_unaligned32(u32p, v) do { \
250 uint32_t __t = (v); \
251 memcpy((u32p), &__t, 4); \
255 /* ---- Compiler dependent settings ------------------------- */
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
269 # define HAVE_MNTENT_H 1
270 # define HAVE_SYS_STATFS_H 1
271 # define HAVE_XTABS 1
274 /*----- Kernel versioning ------------------------------------*/
276 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
278 /* ---- Miscellaneous --------------------------------------- */
280 #if defined __GLIBC__ || defined __UCLIBC__ \
281 || defined __dietlibc__ || defined _NEWLIB_VERSION
282 # include <features.h>
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;
291 /* for arches where byte accesses generate larger code: */
292 typedef int smallint;
293 typedef unsigned smalluint;
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
301 /* modern system, so use it */
302 # include <stdbool.h>
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)))
311 /* Arches which MUST have 2 or 4 byte alignment for everything are here */
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.
323 #if ENABLE_NOMMU || \
324 (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
325 __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__)
327 # define USE_FOR_NOMMU(...) __VA_ARGS__
328 # define USE_FOR_MMU(...)
331 # define USE_FOR_NOMMU(...)
332 # define USE_FOR_MMU(...) __VA_ARGS__
335 #if defined(__digital__) && defined(__unix__)
337 # include <standards.h>
338 # include <inttypes.h>
340 /* use legacy setpgrp(pid_t,pid_t) for now. move to platform.c */
341 # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
342 # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
343 # define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
345 # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
346 # define ADJ_FREQUENCY MOD_FREQUENCY
348 # if !defined ADJ_TIMECONST && defined MOD_TIMECONST
349 # define ADJ_TIMECONST MOD_TIMECONST
351 # if !defined ADJ_TICK && defined MOD_CLKB
352 # define ADJ_TICK MOD_CLKB
359 # define bb_setpgrp() setpgrp()
364 #define fdprintf dprintf
366 #if defined(__dietlibc__)
367 # undef HAVE_STRCHRNUL
370 #if defined(__WATCOMC__)
376 # undef HAVE_STRCASESTR
377 # undef HAVE_STRCHRNUL
379 # undef HAVE_STRSIGNAL
380 # undef HAVE_VASPRINTF
383 #if defined(__FreeBSD__)
384 # undef HAVE_STRCHRNUL
388 * Now, define prototypes for all the functions defined in platform.c
389 * These must come after all the HAVE_* macros are defined (or not)
393 extern int dprintf(int fd, const char *format, ...);
397 extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
401 extern char *mkdtemp(char *template) FAST_FUNC;
405 # define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
406 # define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
409 #ifndef HAVE_SIGHANDLER_T
410 typedef void (*sighandler_t)(int);
414 extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
417 #ifndef HAVE_STRCASESTR
418 extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
421 #ifndef HAVE_STRCHRNUL
422 extern char *strchrnul(const char *s, int c) FAST_FUNC;
426 extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
429 #ifndef HAVE_STRSIGNAL
430 /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
431 # define strsignal(sig) get_signame(sig)
434 #ifndef HAVE_VASPRINTF
435 extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;